aboutsummaryrefslogtreecommitdiff
blob: 3f0dbd47672967b349aa832bdf29e33da29fad62 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
module Update.SelectCharacter exposing (apply_to)

-- Elm -------------------------------------------------------------------------
import Dict

-- Battlemap -------------------------------------------------------------------
import Struct.Battlemap
import Struct.Character
import Struct.CharacterTurn
import Struct.Error
import Struct.Event
import Struct.Location
import Struct.Model
import Struct.Navigator
import Struct.Statistics
import Struct.UI
import Struct.Weapon
import Struct.WeaponSet

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
attack_character : (
      Struct.Model.Type ->
      Struct.Character.Ref ->
      Struct.Character.Type ->
      Struct.Model.Type
   )
attack_character model target_char_id target_char =
   {model |
      char_turn =
         (Struct.CharacterTurn.set_target
            (Just target_char_id)
            model.char_turn
         ),
      ui =
         (Struct.UI.set_previous_action model.ui Nothing)
   }

ctrl_or_focus_character : (
      Struct.Model.Type ->
      Struct.Character.Ref ->
      Struct.Character.Type ->
      Struct.Model.Type
   )
ctrl_or_focus_character model target_char_id target_char =
   if (Struct.Character.is_enabled target_char)
   then
      let
         weapon =
            (Struct.WeaponSet.get_active_weapon
               (Struct.Character.get_weapons target_char)
            )
      in
         {model |
            char_turn =
               (Struct.CharacterTurn.set_navigator
                  (Struct.Navigator.new
                     (Struct.Character.get_location target_char)
                     (Struct.Statistics.get_movement_points
                        (Struct.Character.get_statistics target_char)
                     )
                     (Struct.Weapon.get_attack_range weapon)
                     (Struct.Weapon.get_defense_range weapon)
                     (Struct.Battlemap.get_movement_cost_function
                        model.battlemap
                        (Struct.Character.get_location target_char)
                        (Dict.values model.characters)
                     )
                  )
                  (Struct.CharacterTurn.set_active_character
                     target_char
                     model.char_turn
                  )
               ),
            ui = (Struct.UI.set_previous_action model.ui Nothing)
         }
   else
      {model |
         ui =
            (Struct.UI.set_previous_action
               model.ui
               (Just (Struct.UI.SelectedCharacter target_char_id))
            )
      }

can_target_character : (
      Struct.Model.Type ->
      Struct.Character.Type ->
      Bool
   )
can_target_character model target =
   (
      (Struct.CharacterTurn.can_select_target model.char_turn)
      && (Struct.Character.is_alive target)
      &&
      (
         case
            (Struct.CharacterTurn.try_getting_navigator
               model.char_turn
            )
         of
            (Just nav) ->
               case
                  (Struct.Navigator.try_getting_path_to
                     nav
                     (Struct.Location.get_ref
                        (Struct.Character.get_location target)
                     )
                  )
               of
                  (Just _) -> True
                  _ -> False

            _ ->
               False
      )
   )

double_clicked_character : (
      Struct.Model.Type ->
      Struct.Character.Ref ->
      (Struct.Model.Type, (Cmd Struct.Event.Type))
   )
double_clicked_character model target_char_id =
   case (Dict.get target_char_id model.characters) of
      (Just target_char) ->
         case
            (Struct.CharacterTurn.try_getting_active_character
               model.char_turn
            )
         of
            (Just _) ->
               if (can_target_character model target_char)
               then
                  (
                     (attack_character
                        model
                        target_char_id
                        target_char
                     ),
                     Cmd.none
                  )
               else
                  (
                     (Struct.Model.invalidate
                        model
                        (Struct.Error.new
                           Struct.Error.IllegalAction
                           "Has not yet moved, target is out of range, or dead."
                        )
                     ),
                     Cmd.none
                  )

            _ ->
               (
                  (ctrl_or_focus_character model target_char_id target_char),
                  Cmd.none
               )

      Nothing ->
         (
            (Struct.Model.invalidate
               model
               (Struct.Error.new
                  Struct.Error.Programming
                  "SelectCharacter: Unknown char selected."
               )
            ),
            Cmd.none
         )

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
apply_to : (
      Struct.Model.Type ->
      Struct.Character.Ref ->
      (Struct.Model.Type, (Cmd Struct.Event.Type))
   )
apply_to model target_char_id =
   if
   (
      (Struct.UI.get_previous_action model.ui)
      ==
      (Just (Struct.UI.SelectedCharacter target_char_id))
   )
   then
      (double_clicked_character model target_char_id)
   else
      (
         {model |
            ui =
               (Struct.UI.set_previous_action
                  (Struct.UI.set_displayed_tab
                     model.ui
                     Struct.UI.StatusTab
                  )
                  (Just (Struct.UI.SelectedCharacter target_char_id))
               )
         },
         Cmd.none
      )