summaryrefslogtreecommitdiff
blob: 2abddb5a138d9744caf8b0b50a342e0aaf33a04a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module Update exposing (update, Type(..))

import Model

import Battlemap
import Battlemap.Direction
import Battlemap.Navigator

import Dict

import Character

type Type =
   DirectionRequest Battlemap.Direction.Type
   | SelectCharacter Character.Ref
   | EndTurn

handle_direction_request : (
      Model.Type ->
      Battlemap.Direction.Type ->
      Model.Type
   )
handle_direction_request model dir =
   (case (model.selection, model.navigator) of
      (Nothing, _) -> model
      (_ , Nothing) -> model
      ((Just char_id), (Just nav)) ->
         let
            (new_bmap, new_nav) =
               (Battlemap.Navigator.go
                  model.battlemap
                  nav
                  dir
                  (Dict.values model.characters)
               )
         in
            {model |
               battlemap = new_bmap,
               navigator = (Just new_nav)
            }
   )

handle_select_character : Model.Type -> Character.Ref -> Model.Type
handle_select_character model char_id =
   {model |
      selection = (Just char_id),
      battlemap =
         (Battlemap.apply_to_all_tiles
            model.battlemap
            (Battlemap.Navigator.reset_navigation)
         ),
      navigator =
         (case (Dict.get char_id model.characters) of
            Nothing -> Nothing
            (Just char) ->
               (Just
                  (Battlemap.Navigator.new_navigator
                     char.location
                     char.movement_points
                  )
               )
         )
   }

handle_end_turn : Model.Type -> Model.Type
handle_end_turn model =
   case (model.navigator, model.selection) of
      (_, Nothing) -> model
      (Nothing, _) -> model
      ((Just nav), (Just char_id)) ->
         (case (Dict.get char_id model.characters) of
            Nothing -> model
            (Just char) ->
               {model |
                  navigator =
                     (Just
                        (Battlemap.Navigator.new_navigator
                           nav.current_location
                           char.movement_points
                        )
                     ),
                  battlemap =
                     (Battlemap.apply_to_all_tiles
                        (Battlemap.apply_to_tile_unsafe
                           (Battlemap.apply_to_tile_unsafe
                              model.battlemap
                              char.location
                              (\t -> {t | char_level = Nothing})
                           )
                           nav.current_location
                           (\t -> {t | char_level = (Just char_id)})
                        )
                        (Battlemap.Navigator.reset_navigation)
                     ),
                  characters =
                     (Dict.update
                        char_id
                        (\mc ->
                           case mc of
                              Nothing -> Nothing
                              (Just c) ->
                                 (Just {c | location = nav.current_location})
                        )
                        model.characters
                     )
               }
         )

update : Type -> Model.Type -> Model.Type
update msg model =
   case msg of
      (DirectionRequest d) ->
         (handle_direction_request model d)
      (SelectCharacter char_id) ->
         (handle_select_character model char_id)
      EndTurn ->
         (handle_end_turn model)
      --_ -> model