-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameModel.elm
126 lines (106 loc) · 3.58 KB
/
GameModel.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module GameModel where
import Char (KeyCode)
import Grid
import Generator
import Generator.Standard
type State = { player : Player
, enemies : [Enemy]
, level : Grid.Grid Tile
, explored : Grid.Grid Visibility
, log : [String]
, generator : Random
}
type Player = { location : Location
, avatar : Element
, name : String
, health : Int
, energy : Int
, hunger : Int
, stealth : Int
, armor : Int
, protection : Int
, coordination : Int
, power : Int
, initiative : Int
, placed : Bool
}
type Enemy = { location : Location
, avatar : Element
, name : String
, health : Int
, stealth : Int
, armor : Int
, protection : Int
, coordination : Int
, power : Int
, initiative : Int
, placed : Bool
}
type Location = Grid.Coordinate
data Tile = Floor
| Wall
| Door
| Acid
data Visibility = Visible
| Unexplored
| Explored
data Input = Up | Down | Left | Right | Nop
type Random = Generator.Generator Generator.Standard.Standard
player : Element -> String -> Random -> (Player, Random)
player elem name gen =
let (initiative, gen') = Generator.int32Range (1, 100) gen
in (Player (Grid.Coordinate 2 2) elem name 10 10 10 20 1 50 100 2 initiative False, gen')
enemy : Element -> String -> Random -> (Enemy, Random)
enemy elem name gen =
let (initiative, gen') = Generator.int32Range (1, 100) gen
in (Enemy (Grid.Coordinate 14 4) elem name 10 20 1 50 100 2 initiative False, gen')
location : Int -> Int -> Location
location = Grid.Coordinate
handle : KeyCode -> Input
handle key =
case key of
37 -> Left
38 -> Up
39 -> Right
40 -> Down
_ -> Nop
validLocation : Location -> State -> Bool
validLocation location state = Grid.inGrid location state.level
pathable : Location -> State -> Bool
pathable location state =
let level = state.level
tile = Grid.get location level
in case tile of
Nothing -> False
Just Floor -> True
Just _ -> False
getRandomPathable : State -> (Location, State)
getRandomPathable state =
let (x, gen') = Generator.int32Range (1, state.level.size.width) state.generator
(y, gen'') = Generator.int32Range (1, state.level.size.height) gen'
locn = location x y
state' = {state | generator <- gen''}
in case pathable locn state' of
True -> (locn, state')
False -> getRandomPathable state'
placeEnemy : Enemy -> State -> State
placeEnemy a state =
let (loc, state') = getRandomPathable state
in {state'| enemies <- {a| location <- loc, placed <- True} :: tail state'.enemies}
placePlayer : State -> State
placePlayer state =
let (loc, state') = getRandomPathable state
player' = state'.player
in {state'| player <- {player'| location <- loc, placed <- True}}
showTile : Tile -> Element
showTile tile =
let c = case tile of
Floor -> " "
Wall -> "#"
Door -> "+"
Acid -> "~"
in centered << monospace << toText <| c
visible : State -> [Location]
visible state = Grid.neighborhood2 state.player.location
visibility : State -> Location -> Visibility
visibility state location = Grid.getOrElse Unexplored location state.explored