| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-09-18 19:12:54 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-09-18 19:12:54 +0200 | 
| commit | 36344e727e45b6a1d39f372a6a39ab973e023bdf (patch) | |
| tree | 740a524f94b2ce846dcd7a920848139c5caa4e38 /client/elm/battlemap/src/Update.elm | |
| parent | 0b9096ed0c66db403c244a4720bac60326a40394 (diff) | |
Characters can actually move.
Diffstat (limited to 'client/elm/battlemap/src/Update.elm')
| -rw-r--r-- | client/elm/battlemap/src/Update.elm | 123 | 
1 files changed, 85 insertions, 38 deletions
| diff --git a/client/elm/battlemap/src/Update.elm b/client/elm/battlemap/src/Update.elm index 719d259..2c86c4a 100644 --- a/client/elm/battlemap/src/Update.elm +++ b/client/elm/battlemap/src/Update.elm @@ -1,58 +1,105 @@ -module Update exposing (..) +module Update exposing (update, Msg(..))  import Model exposing (Model, model) -import Battlemap exposing (apply_to_all_tiles) +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) +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) -> -         (case model.selection of -            Nothing -> -               model -            (Just char_id) -> -               (case model.navigator of -                  Nothing -> model -                  (Just nav) -> -                     let -                        (new_bmap, new_nav) = -                           (Nr.go -                              model.battlemap -                              nav -                              d -                           ) -                     in -                        {model | -                           battlemap = new_bmap, -                           navigator = (Just new_nav) -                        } -               ) -         ) +         (handle_direction_request model d)        (SelectCharacter 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 {x = char.x, y = char.y})) -               ) -         } +         (handle_select_character model char_id) +      EndTurn -> +         (handle_end_turn model)        --_ -> model | 


