summaryrefslogtreecommitdiff
blob: 2c86c4a633c081914eeed528a8405250226faca9 (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
module Update exposing (update, Msg(..))

import Model exposing (Model, model)

import Battlemap exposing (apply_to_all_tiles, apply_to_tile_unsafe)
import Battlemap.Direction exposing (Direction)

import Battlemap.Navigator as Nr exposing (go, reset_navigation)

import Dict as Dt exposing (get, update)

import Character exposing (CharacterRef)


type Msg =
   DirectionRequest Direction
   | SelectCharacter CharacterRef
   | EndTurn

handle_direction_request : Model -> Direction -> Model
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) =
               (Nr.go
                  model.battlemap
                  nav
                  dir
               )
         in
            {model |
               battlemap = new_bmap,
               navigator = (Just new_nav)
            }
   )

handle_select_character : Model -> CharacterRef -> Model
handle_select_character model char_id =
   {model |
      selection = (Just char_id),
      battlemap =
         (apply_to_all_tiles
            model.battlemap
            (reset_navigation)
         ),
      navigator =
         (case (Dt.get char_id model.characters) of
            Nothing -> Nothing
            (Just char) ->
               (Just (Nr.new_navigator char.location))
         )
   }

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

update : Msg -> Model -> Model
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