summaryrefslogtreecommitdiff
blob: 7b5a9a9f27c7b73a7a6c71050d95dd69b3570661 (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
module Update.CharacterTurn.ToggleTarget exposing (apply_to, apply_to_ref)

-- Battle Map ------------------------------------------------------------------
import BattleMap.Struct.Location

-- Local Module ----------------------------------------------------------------
import Struct.Battle
import Struct.Character
import Struct.CharacterTurn
import Struct.Event
import Struct.Model
import Struct.Navigator
import Struct.UI

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

            _ ->
               False
      )
   )

toggle_attack_character : (
      Struct.Model.Type ->
      Int ->
      Struct.Model.Type
   )
toggle_attack_character model target_char_id =
   {model |
      char_turn =
         (Struct.CharacterTurn.toggle_target_index
            target_char_id
            model.char_turn
         ),
      ui =
         (Struct.UI.reset_displayed_nav
            (Struct.UI.reset_displayed_tab
               (Struct.UI.set_previous_action Nothing model.ui)
            )
         )
   }

undo_attack_character : (
      Struct.Model.Type ->
      Int ->
      Struct.Model.Type
   )
undo_attack_character model target_char_id =
   {model |
      char_turn =
         (Struct.CharacterTurn.remove_target_index
            target_char_id
            model.char_turn
         ),
      ui =
         (Struct.UI.reset_displayed_nav
            (Struct.UI.reset_displayed_tab
               (Struct.UI.set_previous_action Nothing model.ui)
            )
         )
   }

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
apply_to : (
      Struct.Character.Type ->
      Struct.Model.Type ->
      (Struct.Model.Type, (Cmd Struct.Event.Type))
   )
apply_to target model =
   (
      (
         let target_ix = (Struct.Character.get_index target) in
            if (can_target_character model target)
            then (toggle_attack_character model target_ix)
            else (undo_attack_character model target_ix)
      ),
      Cmd.none
   )

apply_to_ref : (
      Int ->
      Struct.Model.Type ->
      (Struct.Model.Type, (Cmd Struct.Event.Type))
   )
apply_to_ref target_ix model =
   case (Struct.Battle.get_character target_ix model.battle) of
      Nothing -> (model, Cmd.none)
      (Just char) -> (apply_to char model)