Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chendrix committed Jun 18, 2015
0 parents commit a2ddfa6
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elm-stuff
elm.js
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 14 additions & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<html>

<head>
<meta charset="UTF-8">
<title>Elm Rogue</title>
<script type="text/javascript" src="elm.js"></script>
</head>

<body>
</body>

<script type="text/javascript">
var rogue = Elm.fullscreen(Elm.Rogue);
</script>

</html>
139 changes: 139 additions & 0 deletions src/Rogue.elm
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a2ddfa6

Please sign in to comment.