| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-09-15 09:52:54 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-09-15 09:52:54 +0200 | 
| commit | 9a2d8f37dea8e14afa57affb135def13954df547 (patch) | |
| tree | ee7daa101ffdb76bc4a5932c3698b6a89613df78 /client | |
Satisfied with Elm so far, let's go with it.
Diffstat (limited to 'client')
| -rw-r--r-- | client/Makefile | 9 | ||||
| -rw-r--r-- | client/elm/battlemap/Makefile | 11 | ||||
| -rw-r--r-- | client/elm/battlemap/elm-package.json | 15 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Battlemap.elm | 47 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Battlemap/Direction.elm | 8 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Battlemap/Html.elm | 84 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Battlemap/Location.elm | 18 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Battlemap/Navigator.elm | 50 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Battlemap/Tile.elm | 41 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Main.elm | 13 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Model.elm | 7 | ||||
| -rw-r--r-- | client/elm/battlemap/src/Update.elm | 11 | ||||
| -rw-r--r-- | client/elm/battlemap/src/View.elm | 35 | 
13 files changed, 349 insertions, 0 deletions
| diff --git a/client/Makefile b/client/Makefile new file mode 100644 index 0000000..5746f57 --- /dev/null +++ b/client/Makefile @@ -0,0 +1,9 @@ +TARGETS = battlemap + +all: $(TARGETS) $(addsuffix .html,$(TARGETS)) + +$(TARGETS): +	$(MAKE) -C elm/$@ index.html + +%.html: elm/%/index.html +	cp $< $@ diff --git a/client/elm/battlemap/Makefile b/client/elm/battlemap/Makefile new file mode 100644 index 0000000..54b52d8 --- /dev/null +++ b/client/elm/battlemap/Makefile @@ -0,0 +1,11 @@ +ELM_CC = elm-make +SRC_DIR = src + +MAIN_MODULE = $(SRC_DIR)/Main.elm +SUB_MODULES = $(shell find $(SRC_DIR) -type f | grep "elm$$") + +index.html: $(MAIN_MODULE) $(SUB_MODULES) +	$(ELM_CC) $(MAIN_MODULE) + +clean: +	rm -f index.html diff --git a/client/elm/battlemap/elm-package.json b/client/elm/battlemap/elm-package.json new file mode 100644 index 0000000..bcb6f4a --- /dev/null +++ b/client/elm/battlemap/elm-package.json @@ -0,0 +1,15 @@ +{ +    "version": "1.0.0", +    "summary": "helpful summary of your project, less than 80 characters", +    "repository": "https://github.com/user/project.git", +    "license": "BSD3", +    "source-directories": [ +        "src" +    ], +    "exposed-modules": [], +    "dependencies": { +        "elm-lang/core": "5.1.1 <= v < 6.0.0", +        "elm-lang/html": "2.0.0 <= v < 3.0.0" +    }, +    "elm-version": "0.18.0 <= v < 0.19.0" +} 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)] +         ) +      ] +   ) | 


