diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3bd52a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +elm-stuff +elm.js diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..b1c7df8 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Copyright (c) 2015 Christopher Hendrix + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/elm-package.json b/elm-package.json new file mode 100644 index 0000000..5151e66 --- /dev/null +++ b/elm-package.json @@ -0,0 +1,14 @@ +{ + "version": "0.0.1", + "summary": "Rogue game", + "repository": "https://github.com/chendrix/elm-rogue.git", + "license": "MIT", + "source-directories": [ + "src" + ], + "exposed-modules": [], + "dependencies": { + "elm-lang/core": "2.0.1 <= v < 3.0.0" + }, + "elm-version": "0.15.0 <= v < 0.16.0" +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..ad2d633 --- /dev/null +++ b/index.html @@ -0,0 +1,17 @@ + + + + + + Elm Rogue + + + + + + + + + \ No newline at end of file diff --git a/src/Rogue.elm b/src/Rogue.elm new file mode 100644 index 0000000..a9d4912 --- /dev/null +++ b/src/Rogue.elm @@ -0,0 +1,139 @@ +module Rogue where + +import Color exposing (..) +import Graphics.Collage exposing (..) +import Graphics.Element exposing (..) +import Keyboard +import Text +import Window +import List exposing (..) +import Time exposing (..) +import String exposing (join) + + +-- MODEL + +type alias Location = (Int, Int) + +type alias GameMap = + { board : Board + , start : Location + } + +type alias Board = List (List Cell) + +type Player = Player + +type alias Game = + { gameMap : GameMap + , player : Player + , location : Location + } + +type Cell = Open Location + +isAt : Location -> Cell -> Bool +isAt queried (Open current) = queried == current + +newBoard : Int -> Board +newBoard i = + map (\row -> + map + (\col -> Open (row, col) + ) [0..i]) [0..i] + +gameMap : Int -> GameMap +gameMap i = + { board = newBoard i + , start = (0, 0) + } + +defaultGame : Game +defaultGame = + let + g = gameMap 10 + in + { gameMap = g + , player = Player + , location = g.start + } + +type alias Dir = + { x : Int + , y : Int + } + +type alias Input = + { dir : Dir + } + +numRows : Board -> Int +numRows b = length b + +numCols : Board -> Int +numCols = numRows + +within : Board -> Location -> Bool +within board (row, col) = + row >= 0 && row < numRows board && col >= 0 && col < numCols board + +translate : Location -> Dir -> Location +translate (row,col) dir = + (row - dir.y, col + dir.x) + +-- UPDATE + +update : Input -> Game -> Game +update {dir} ({gameMap,player,location} as game) = + let + (row, col) = location + newLocation = translate location dir + in + if within gameMap.board newLocation + then + { game | location <- newLocation + } + else + game + +-- VIEW + +view : Game -> Element +view g = leftAligned (Text.monospace (Text.fromString (toString g))) + +toString : Game -> String +toString {gameMap,player,location} = + let + rowifier = + (\row -> + map + (\cell -> + if isAt location cell + then "@" + else "." + ) + row + |> join "" + ) + in + map rowifier gameMap.board + |> join "\n" + +-- SIGNALS + +main = + Signal.map view gameState + + +gameState : Signal Game +gameState = + Signal.foldp update defaultGame input + + +delta = + Signal.map inSeconds (fps 35) + + +input : Signal Input +input = + Signal.map Input Keyboard.wasd