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 | |
parent | 0b9096ed0c66db403c244a4720bac60326a40394 (diff) |
Characters can actually move.
-rw-r--r-- | client/elm/battlemap/src/Battlemap.elm | 19 | ||||
-rw-r--r-- | client/elm/battlemap/src/Character.elm | 5 | ||||
-rw-r--r-- | client/elm/battlemap/src/Model.elm | 9 | ||||
-rw-r--r-- | client/elm/battlemap/src/Update.elm | 123 | ||||
-rw-r--r-- | client/elm/battlemap/src/View.elm | 4 |
5 files changed, 114 insertions, 46 deletions
diff --git a/client/elm/battlemap/src/Battlemap.elm b/client/elm/battlemap/src/Battlemap.elm index d6c0017..ef24c80 100644 --- a/client/elm/battlemap/src/Battlemap.elm +++ b/client/elm/battlemap/src/Battlemap.elm @@ -3,6 +3,7 @@ module Battlemap exposing Battlemap, random, apply_to_tile, + apply_to_tile_unsafe, has_location, apply_to_all_tiles ) @@ -67,3 +68,21 @@ apply_to_tile bmap loc fun = ) } ) + +apply_to_tile_unsafe : Battlemap -> Location -> (Tile -> Tile) -> Battlemap +apply_to_tile_unsafe bmap loc fun = + let + index = (location_to_index bmap loc) + at_index = (get index bmap.content) + in + case at_index of + Nothing -> bmap + (Just tile) -> + {bmap | + content = + (set + index + (fun tile) + bmap.content + ) + } diff --git a/client/elm/battlemap/src/Character.elm b/client/elm/battlemap/src/Character.elm index 3082550..4804cd0 100644 --- a/client/elm/battlemap/src/Character.elm +++ b/client/elm/battlemap/src/Character.elm @@ -1,13 +1,14 @@ module Character exposing (Character, CharacterRef, to_comparable) +import Battlemap.Location exposing (Location) + type alias Character = { id : String, name : String, icon : String, portrait : String, - x : Int, - y : Int + location : Location } type alias CharacterRef = String diff --git a/client/elm/battlemap/src/Model.elm b/client/elm/battlemap/src/Model.elm index 61d827b..a019c45 100644 --- a/client/elm/battlemap/src/Model.elm +++ b/client/elm/battlemap/src/Model.elm @@ -30,8 +30,7 @@ model = name = "Char2", icon = "Icon2", portrait = "Portrait2", - x = 1, - y = 4 + location = {x = 1, y = 4} } (insert "1" @@ -40,8 +39,7 @@ model = name = "Char1", icon = "Icon1", portrait = "Portrait1", - x = 4, - y = 1 + location = {x = 4, y = 1} } (insert "0" @@ -50,8 +48,7 @@ model = name = "Char0", icon = "Icon0", portrait = "Portrait0", - x = 0, - y = 0 + location = {x = 0, y = 0} } empty ) 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 diff --git a/client/elm/battlemap/src/View.elm b/client/elm/battlemap/src/View.elm index a76a0a1..d0cb8c8 100644 --- a/client/elm/battlemap/src/View.elm +++ b/client/elm/battlemap/src/View.elm @@ -32,6 +32,10 @@ view model = [ (onClick (DirectionRequest Right)) ] [ (text "Right") ] ), + (button + [ (onClick EndTurn) ] + [ (text "Apply") ] + ), (div [] [(Batmap.view model)] |