-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobs.fnl
187 lines (182 loc) · 7.68 KB
/
mobs.fnl
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(local utils (require "utils"))
(local tile-size 16)
(local tile-set (love.graphics.newImage "assets/images/mobs.png"))
(local tile-set-width (: tile-set :getWidth))
(local tile-set-height (: tile-set :getHeight))
(local animations-count 2)
(local animation-duration 2)
(fn setup-mob [sprite-index title max-hp attack defense]
{:quads
[(love.graphics.newQuad
(* sprite-index tile-size) 0
tile-size tile-size
tile-set-width tile-set-height)
(love.graphics.newQuad
(* sprite-index tile-size) tile-size
tile-size tile-size
tile-set-width tile-set-height)]
:title title
:max-hp max-hp
:attack attack
:defense defense})
(local demon (setup-mob 0 "demon" 16 "1d4^+1" "1d4"))
(local spider (setup-mob 1 "giant spider" 8 "2d2^+2" "1d1"))
(local slime (setup-mob 2 "vile slime" 6 "1d1" "1d4^+1"))
(local zombie (setup-mob 3 "zombie" 10 "1d2" "1d2"))
(local mob-classes [demon spider slime zombie])
(fn setup-mobs [dungeon player-position combat items update-status-message]
(let [sprite-batch (love.graphics.newSpriteBatch tile-set)
mobs [] ;; [int][int] -> mob class + HP
]
(var current-time 0)
(var current-stance 1)
(fn generate-mob [class-choices]
(local class (lume.weightedchoice class-choices))
(let [class (lume.weightedchoice class-choices)
max-hp class.max-hp
hp
(math.max
(math.random max-hp)
(math.random max-hp)
(math.random max-hp))]
[class hp]))
(fn build-sprite-batch []
(: sprite-batch :clear)
(for [x 0 (dungeon.width)]
(let [col-pos (* x tile-size)
col (. mobs x)]
(when col
(for [y 0 (dungeon.height)]
(let [mob (. col y)]
(when mob
(let [mob-class (. mob 1)]
(: sprite-batch :add
(. mob-class.quads current-stance)
col-pos (* y tile-size))))))))))
(fn mob-at [x y]
(let [col (. mobs x)]
(if col (. col y) nil)))
(fn set-mob-at [x y mob rebuild]
(when (not (. mobs x))
(tset mobs x []))
(tset (. mobs x) y mob)
(when rebuild
(build-sprite-batch)))
(let [[player-x player-y] (player-position)]
(lume.each
(dungeon.dead-ends)
(fn [dead-end]
(let [[x y] dead-end]
(when (and (not (= x player-x)) (not (= y player-y)) (not (mob-at x y)))
(set-mob-at
x y
(generate-mob {spider 0.95 slime 0.05}))))))
(lume.each
(dungeon.rooms)
(fn [room]
(let [[x y] room]
(when (and (not (= x player-x)) (not (= y player-y)) (not (mob-at x y)))
(set-mob-at
x y
(generate-mob {demon 0.20 zombie 0.80})))))))
(build-sprite-batch)
(fn describe [x y]
(var result "")
(for [dir 1 4]
(let [[cell-x cell-y] (utils.advance x y dir)]
(let [mob (mob-at cell-x cell-y)]
(when mob
(let [[mob-class hp] mob
hp-description (utils.describe-hp hp mob-class.max-hp)]
(set result
(..
result
(if (= result "") "" " ")
"There is "
hp-description
(if (= hp-description "") "" " ")
mob-class.title
" to the "
(utils.direction-description dir)
".")))))))
result)
(fn remove-mob [x y rebuild]
(tset (. mobs x) y nil)
(when rebuild
(build-sprite-batch)))
(fn simulate []
(let [[player-x player-y] (player-position)]
(fn move-mob [x y new-x new-y]
(if (and (dungeon.traversable? new-x new-y)
(not (mob-at new-x new-y))
(not (and (= player-x new-x) (= player-y new-y))))
(let [mob (mob-at x y)]
(tset (. mobs x) y nil)
(set-mob-at new-x new-y mob)
true)
false))
(for [x 0 (dungeon.width)]
(let [col (. mobs x)]
(when col
(for [y 0 (dungeon.height)]
(let [mob (. col y)]
(when mob
(if (> (lume.distance x y player-x player-y) 10)
(let [dir (math.random 2)
dx (if (= dir 1) (math.random -1 1) 0)
dy (if (= dir 2) (math.random -1 1) 0)
new-x (+ x dx)
new-y (+ y dy)]
(when (or (not (= dx 0)) (not (= dy 0)))
(move-mob x y new-x new-y)))
(do
((. (combat) :maybe-attack-player) x y)
(var moved false)
(if (> player-x x)
(set moved (move-mob x y (+ x 1) y))
(< player-x x)
(set moved (move-mob x y (- x 1) y)))
(if (and (> player-y y)
(not moved))
(move-mob x y x (+ y 1))
(and (< player-y y)
(not moved))
(move-mob x y x (- y 1)))))))))))))
(fn update-state [set-mode]
(var mob-count 0)
(for [x 0 (dungeon.width)]
(let [col (. mobs x)]
(when col
(for [y 0 (dungeon.height)]
(let [mob (. col y)]
(when mob
(let [mob-hp (. mob 2)]
(if (<= mob-hp 0)
(do
(update-status-message
(.. "The " (. (. mob 1) :title) " dies."))
(items.monster-drop x y)
(remove-mob x y))
(set mob-count (+ mob-count 1)))))))))))
(fn update [dt]
(set current-time (+ current-time dt))
(when (>= current-time animation-duration)
(set current-time (- current-time animation-duration)))
(let [new-stance
(+ 1 (math.floor (* (/ current-time animation-duration)
animations-count)))]
(when (not (= new-stance current-stance))
(set current-stance new-stance)
(build-sprite-batch))))
(fn update-world [set-mode]
(update-state set-mode)
(simulate)
(build-sprite-batch))
{:draw (fn [] (love.graphics.draw sprite-batch))
:update update
:update-world update-world
:describe describe
:mob-at mob-at
:set-mob-at set-mob-at
:remove-mob remove-mob
}))