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/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 ++++++++++++ 5 files changed, 201 insertions(+) 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 (limited to 'client/elm/battlemap/src/Battlemap') 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 + ] + ) + ) -- cgit v1.2.3-70-g09d2