From 9a2d8f37dea8e14afa57affb135def13954df547 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Fri, 15 Sep 2017 09:52:54 +0200 Subject: Satisfied with Elm so far, let's go with it. --- client/elm/battlemap/src/Battlemap.elm | 47 +++++++++++++ client/elm/battlemap/src/Battlemap/Direction.elm | 8 +++ client/elm/battlemap/src/Battlemap/Html.elm | 84 ++++++++++++++++++++++++ client/elm/battlemap/src/Battlemap/Location.elm | 18 +++++ client/elm/battlemap/src/Battlemap/Navigator.elm | 50 ++++++++++++++ client/elm/battlemap/src/Battlemap/Tile.elm | 41 ++++++++++++ client/elm/battlemap/src/Main.elm | 13 ++++ client/elm/battlemap/src/Model.elm | 7 ++ client/elm/battlemap/src/Update.elm | 11 ++++ client/elm/battlemap/src/View.elm | 35 ++++++++++ 10 files changed, 314 insertions(+) create mode 100644 client/elm/battlemap/src/Battlemap.elm create mode 100644 client/elm/battlemap/src/Battlemap/Direction.elm create mode 100644 client/elm/battlemap/src/Battlemap/Html.elm create mode 100644 client/elm/battlemap/src/Battlemap/Location.elm create mode 100644 client/elm/battlemap/src/Battlemap/Navigator.elm create mode 100644 client/elm/battlemap/src/Battlemap/Tile.elm create mode 100644 client/elm/battlemap/src/Main.elm create mode 100644 client/elm/battlemap/src/Model.elm create mode 100644 client/elm/battlemap/src/Update.elm create mode 100644 client/elm/battlemap/src/View.elm (limited to 'client/elm/battlemap/src') diff --git a/client/elm/battlemap/src/Battlemap.elm b/client/elm/battlemap/src/Battlemap.elm new file mode 100644 index 0000000..dbf797a --- /dev/null +++ b/client/elm/battlemap/src/Battlemap.elm @@ -0,0 +1,47 @@ +module Battlemap exposing (Battlemap, random, apply_to_tile) + +import Array exposing (Array, set, get) + +import Battlemap.Tile exposing (Tile, generate) +import Battlemap.Direction exposing (..) +import Battlemap.Location exposing (..) + +type alias Battlemap = + { + width : Int, + height : Int, + content : (Array Tile) + } + +random : Battlemap +random = + { + width = 6, + height = 6, + content = (generate 6 6) + } + +location_to_index : Battlemap -> Location -> Int +location_to_index bmap loc = + ((loc.y * bmap.width) + loc.x) + +apply_to_tile : Battlemap -> Location -> (Tile -> Tile) -> (Maybe Battlemap) +apply_to_tile bmap loc fun = + let + index = (location_to_index bmap loc) + at_index = (get index bmap.content) + in + case at_index of + Nothing -> + Nothing + (Just tile) -> + (Just + {bmap | + content = + (set + index + (fun tile) + bmap.content + ) + } + ) diff --git a/client/elm/battlemap/src/Battlemap/Direction.elm b/client/elm/battlemap/src/Battlemap/Direction.elm new file mode 100644 index 0000000..b943c2d --- /dev/null +++ b/client/elm/battlemap/src/Battlemap/Direction.elm @@ -0,0 +1,8 @@ +module Battlemap.Direction exposing (..) + +type Direction = + None + | Left + | Right + | Up + | Down diff --git a/client/elm/battlemap/src/Battlemap/Html.elm b/client/elm/battlemap/src/Battlemap/Html.elm new file mode 100644 index 0000000..d14e075 --- /dev/null +++ b/client/elm/battlemap/src/Battlemap/Html.elm @@ -0,0 +1,84 @@ +module Battlemap.Html exposing (view) + +import Html exposing (Html, text, table, tr, td) +-- import List as Lt exposing (map) +import Array as Ay exposing (foldr) + +import Update exposing (Msg) +import Model exposing (Model) + +import Battlemap exposing (Battlemap, random) +import Battlemap.Tile exposing (Tile) + +view_battlemap_cell : Tile -> (Html Msg) +view_battlemap_cell t = + (td + [] + [ (text (toString t.floor_level)) ] + ) + +type alias GridBuilder = + { + row : (List (Html Msg)), + columns : (List (Html Msg)), + row_size : Int, + bmap : Battlemap + } + +foldr_to_html : Tile -> GridBuilder -> GridBuilder +foldr_to_html t bg = + if (bg.row_size == bg.bmap.width) + then + {bg | + row = [(view_battlemap_cell t)], + row_size = 1, + columns = + ( + (tr [] bg.row) :: bg.columns + ) + } + else + {bg | + row = ((view_battlemap_cell t) :: bg.row), + row_size = (bg.row_size + 1) + } + +grid_builder_to_html : GridBuilder -> (List (Html Msg)) +grid_builder_to_html gb = + if (gb.row_size == 0) + then + gb.columns + else + (grid_builder_to_html + {gb | + row = [], + row_size = 0, + columns = + ( + (tr [] gb.row) :: gb.columns + ) + } + ) + +view_battlemap : Battlemap -> (Html Msg) +view_battlemap battlemap = + (table + [] + (grid_builder_to_html + (Ay.foldr + (foldr_to_html) + { + row = [], + columns = [], + row_size = 0, + bmap = battlemap + } + battlemap.content + ) + ) + ) + + +view : Model -> (Html Msg) +view m = + (view_battlemap random) diff --git a/client/elm/battlemap/src/Battlemap/Location.elm b/client/elm/battlemap/src/Battlemap/Location.elm new file mode 100644 index 0000000..ffe3f0d --- /dev/null +++ b/client/elm/battlemap/src/Battlemap/Location.elm @@ -0,0 +1,18 @@ +module Battlemap.Location exposing (..) + +import Battlemap.Direction exposing (..) + +type alias Location = + { + x : Int, + y : Int + } + +neighbor : Location -> Direction -> Location +neighbor loc dir = + case dir of + Right -> {loc | x = (loc.x + 1)} + Left -> {loc | x = (loc.x - 1)} + Up -> {loc | y = (loc.y - 1)} + Down -> {loc | y = (loc.y + 1)} + None -> loc diff --git a/client/elm/battlemap/src/Battlemap/Navigator.elm b/client/elm/battlemap/src/Battlemap/Navigator.elm new file mode 100644 index 0000000..ffd52cc --- /dev/null +++ b/client/elm/battlemap/src/Battlemap/Navigator.elm @@ -0,0 +1,50 @@ +module Battlemap.Navigator exposing (Navigator, new_navigator, go) + +import Set exposing (Set, member, empty) + +import Battlemap exposing (Battlemap, has_location) +import Battlemap.Location exposing (..) +import Battlemap.Direction exposing (..) +import Battlemap.Tile exposing (set_tile_direction) + +type alias Navigator = + { + current_location : Location, + visited_locations : (Set Location) + } + +new_navigator : Location -> Navigator +new_navigator start = + { + current_location = start, + visited_locations = empty + } + +go : Navigator -> Direction -> (Battlemap, Navigator) +go battlemap nav dir = + let + next_location = (neighbor nav.current_location dir) + in + if + ( + (has_location battlemap next_location) + && (current_location != next_location) + && (not (member next_location nav.visited_locations)) + ) + then + ( + (set_tile_direction + nav.current_location + dir + ), + { + current_location = next_location, + visited_locations = + (insert + nav.current_location + nav.visited_locations + ) + } + ) + else + nav diff --git a/client/elm/battlemap/src/Battlemap/Tile.elm b/client/elm/battlemap/src/Battlemap/Tile.elm new file mode 100644 index 0000000..e8f2493 --- /dev/null +++ b/client/elm/battlemap/src/Battlemap/Tile.elm @@ -0,0 +1,41 @@ +module Battlemap.Tile exposing (Tile, generate, set_direction) + +import Battlemap.Direction exposing (..) + +import List exposing (map) +import Array exposing (Array, fromList) + +type alias Tile = + { + floor_level : Int, + nav_level : Direction +-- char_level : Int, +-- mod_level : Int + } + +set_direction : Tile -> Direction -> Tile +set_direction t d = + {t | nav_level = d} + +from_int : Int -> Tile +from_int i = + { + floor_level = i, + nav_level = None + } + +generate : Int -> Int -> (Array Tile) +generate width height = + (fromList + (map + (from_int) + [ + 1, 1, 1, 2, 2, 2, + 1, 0, 0, 0, 0, 2, + 1, 0, 1, 2, 0, 2, + 3, 0, 3, 4, 0, 4, + 3, 0, 0, 0, 0, 4, + 3, 3, 3, 4, 4, 4 + ] + ) + ) diff --git a/client/elm/battlemap/src/Main.elm b/client/elm/battlemap/src/Main.elm new file mode 100644 index 0000000..d7fb8e2 --- /dev/null +++ b/client/elm/battlemap/src/Main.elm @@ -0,0 +1,13 @@ +import Html exposing (Html) +import View exposing (view) +import Model exposing (model) +import Update exposing (update) + +main = + (Html.beginnerProgram + { + model = model, + view = view, + update = update + } + ) diff --git a/client/elm/battlemap/src/Model.elm b/client/elm/battlemap/src/Model.elm new file mode 100644 index 0000000..024fc4d --- /dev/null +++ b/client/elm/battlemap/src/Model.elm @@ -0,0 +1,7 @@ +module Model exposing (Model, model) + +-- MODEL +type alias Model = Int + +model : Model +model = 0 diff --git a/client/elm/battlemap/src/Update.elm b/client/elm/battlemap/src/Update.elm new file mode 100644 index 0000000..d03239a --- /dev/null +++ b/client/elm/battlemap/src/Update.elm @@ -0,0 +1,11 @@ +module Update exposing (Msg(Increment, Decrement), update) + +import Model exposing (Model, model) + +type Msg = Increment | Decrement + +update : Msg -> Model -> Model +update msg model = + case msg of + Increment -> (model + 1) + Decrement -> (model - 1) diff --git a/client/elm/battlemap/src/View.elm b/client/elm/battlemap/src/View.elm new file mode 100644 index 0000000..f4774c7 --- /dev/null +++ b/client/elm/battlemap/src/View.elm @@ -0,0 +1,35 @@ +module View exposing (view) + +import Html exposing (Html, button, div, text, table, tr, td) +import Html.Events exposing (onClick) + +import Update exposing (Msg(Increment, Decrement)) +import Model exposing (Model) + +import Battlemap.Html as Batmap exposing (view) + +-- VIEW + +view : Model -> (Html Msg) +view model = + (div + [] + [ + (button + [ (onClick Decrement) ] + [ (text "-") ] + ), + (div + [] + [ (text (toString model)) ] + ), + (button + [ (onClick Increment) ] + [ (text "+") ] + ), + (div + [] + [(Batmap.view model)] + ) + ] + ) -- cgit v1.2.3-70-g09d2