-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroguelike.coffee
287 lines (243 loc) · 6.69 KB
/
roguelike.coffee
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
mapString = """
# # # # # # # # # # # # # # # # # # # # # # # # #
. . . . . . . . . . . . . . . . . . . . . . . . #
# # # # # # # # # # # . # # # # # # # # # # # # #
. . . . . . . . . . # . # . . . . . . . . . . . .
. . . . . . . . . . # . # . . . . . . . . . . . .
. . . . . . . . . . # . # . . . . . . . . . . . .
. . . . . . . . . # # . # # . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
"""
tiles =
'#':
name: 'brick'
passable: false
fg: '#888'
bg: '#444'
action: ->
console.log 'Your way is blocked by a brick wall.'
'.':
name: 'ground'
passable: true
fg: '#888'
undefined:
passable: false
action: -> console.log 'Your way is blocked by the void.'
class Map
@fromString: (map) ->
new this((col for col in row.split(' ') for row in map.split('\n')))
constructor: (@map) ->
@cellSize = 20
@canvas = @getCanvas()
@context = @canvas.getContext('2d')
getCanvas: ->
currentCanvas = document.getElementsByTagName('canvas')
if currentCanvas.length
return currentCanvas[0]
else
canvas = document.createElement('canvas')
canvas.height = 500
canvas.width = 500
document.body.appendChild canvas
return canvas
draw: ->
# paint background
@context.fillStyle = '#333'
@context.fillRect(0, 0, @canvas.height, @canvas.width)
@context.fillStyle = '#ffefef'
for row, y in @map
for cell, x in row
tile = tiles[cell]
@drawTile(x, y, cell, tile.fg, tile.bg)
for entity in entities
x = entity.location.x
y = entity.location.y
if entity.marker
@context.fillText(entity.marker, x * @cellSize, ++y * @cellSize)
drawTile: (x, y, text, fg = 'white', bg = 'black') ->
@context.font = '25px Menlo'
@context.fillStyle = bg
@context.fillRect(x * @cellSize, y * @cellSize, @cellSize, @cellSize)
@context.fillStyle = fg
@context.fillText(text, x * @cellSize, ++y * @cellSize, @cellSize, @cellSize)
# reset context
@context.fillStyle = 'white'
clear: ->
@context.clearRect(0, 0, @canvas.width, @canvas.height)
checkCollision: (x, y, executeActions = false) ->
entity = _.where(entities, {location: {x: x, y: y}})?[0]
cell = tiles[@map[y]?[x]]
if executeActions
entity?.action?() if entity?.marker isnt null
cell?.action?()
return not (cell?.passable is false or entity?.passable is false)
inRadius: (center, radius, point) ->
# Is point in radius of center?
for y in [center.y - radius .. center.y + radius]
for x in [center.x - radius .. center.x + radius]
if point.x == x and point.y == y
return true
# An entity is an item, actor, or an event
class Entity
constructor: (options) ->
defaults =
location: { x: null, y: null }
marker: null
name: ''
passable: true
_.extend(defaults, options)
this[key] = value for key, value of defaults
entities.push(this)
class Item extends Entity
class Event extends Entity
constructor: (options) ->
super options
@marker = ' '
action: ->
console.log @name
@marker = null
class Actor extends Entity
move: (x, y) ->
if map.checkCollision(x, y)
@location.x = x
@location.y = y
# AI Behaviors
randomMovement: ->
x = this.location.x + _.random(-1, 1)
y = this.location.y + _.random(-1, 1)
if map.checkCollision(x, y)
this.location.x = x
this.location.y = y
moveTowardsDude: ->
if dude.location.x > @location.x
@location.x++
else if dude.location.x < @location.x
@location.x--
if dude.location.y < @location.y
@location.y--
else if dude.location.y > @location.y
@location.y++
attack: (location) ->
entity = dude
if entity and entity.health > 0
console.log "#{ @name } attacks #{ entity.name } for #{ @damage } points."
entity.health -= @damage
class GiantSpider extends Actor
constructor: (options) ->
super options
@marker = 'm'
@name = 'giant spider'
@passable = false
@health = 30
@damage = 3
@agro = 5
@attackRange = 1
action: ->
if @health > 0
dude.attack(this)
else
console.log "You killed the #{ @name }."
@passable = true
@marker = '%'
behavior: ->
return if @health <= 0
if map.inRadius(@location, @attackRange, dude.location)
@attack(dude.location)
else if map.inRadius(@location, @agro, dude.location)
@moveTowardsDude(this)
else
@randomMovement(this)
entities = []
new Item
location:
x: 11
y: 3
marker: '¶'
name: 'pilcrow of might'
type: 'weapon'
rating: 30
action: ->
console.log "You wield the #{ @name }"
dude.equip(this)
@marker = null
new Event
location:
x: 11
y: 7
name: "You walk into an open courtyard. In the middle creeps a giant spider!"
for i in [0..10]
new GiantSpider
location:
x: _.random(0, 20)
y: _.random(8, 20)
map = Map.fromString(mapString)
dude = new Actor
name: 'dude'
health: 100
armor:
name: 'clothes'
rating: 0
weapon:
name: 'fists'
rating: 5
location:
x: 1
y: 1
move: (key) ->
LEFT = 37
UP = 38
RIGHT = 39
DOWN = 40
x = @location.x
y = @location.y
switch key
when LEFT then x--
when RIGHT then x++
when UP then y--
when DOWN then y++
if map.checkCollision(x, y, true)
@location.x = x
@location.y = y
equip: (item) ->
if item.type is 'armor'
@armor.name = item.name
@armor.rating = item.rating
console.log "Your armor rating is now #{ @armor.rating }."
else if item.type is 'weapon'
@weapon.name = item.name
@weapon.rating = item.rating
console.log "Your weapon rating is now #{ @weapon.rating }."
attack: (entity) ->
damage = @weapon.rating
entity.health -= damage
console.log("You attack the #{ entity.name } for #{ damage } points")
draw: ->
map.context.fillText('@', @location.x * map.cellSize, (@location.y + 1) * map.cellSize)
step = ->
map.clear()
map.draw()
dude.draw()
robots = _.filter(entities, 'behavior')
robot.behavior() for robot in robots
$(document).on 'keydown', (e) ->
dude.move(e.which)
step()
# initialize everything
step()