-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.coffee
415 lines (358 loc) · 10 KB
/
game.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"use strict"
canvas = document.getElementById 'c'
context = canvas.getContext '2d'
[canvas.width, canvas.height] = [320,320]
fps = 0
last_frame_count = 0
frames = 0
map = null
hero = null
#- constants
for direction in ['NORTH','SOUTH','EAST','WEST', 'NORTH_EAST', 'NORTH_WEST', 'SOUTH_EAST','SOUTH_WEST']
@["#{direction}"] = {NORTH: 1, SOUTH: 2, EAST: 4, WEST: 8, NORTH_EAST: 5, NORTH_WEST: 9, SOUTH_EAST: 6, SOUTH_WEST: 10}[direction]
@["DIR_#{direction}"] = new ->
@[direction] = true
@.dirname = window[direction]
@.movement = false
@.marked_for_deletion = false
@.delete = ->
@marked_for_deletion = true
@
#- helper methods
rgba = (r,g,b,a) ->
colors = for color in [r,g,b]
~~(color)
colors.push(a)
"rgba(#{colors.join(',')})"
rgb = (r,g,b) -> rgba(r,g,b,1)
pixel_coords_to_tile_coords = (x,y) ->
[(x >> 5),(y >> 5)]
clear = ->
context.fillStyle = rgb(0xd0,0xe7,0xf9)
context.beginPath()
context.rect 0,
0,
canvas.width,
canvas.height
context.closePath()
context.fill()
update_fps= ->
fps = frames - last_frame_count
document.getElementById('fps').innerText= [fps, 60].join('/') + ' FPS'
last_frame_count = frames
setTimeout(update_fps, 1000)
get_dir = (loc1,loc2) ->
return NORTH_EAST if loc1.x < loc2.x and loc1.y > loc2.y
return NORTH_WEST if loc1.x > loc2.x and loc1.y > loc2.y
return SOUTH_WEST if loc1.x > loc2.x and loc1.y < loc2.y
return SOUTH_EAST if loc1.x < loc2.x and loc1.y < loc2.y
return EAST if loc1.x < loc2.x
return WEST if loc1.x > loc2.x
return SOUTH if loc1.y > loc2.y
return NORTH
class Highlighter
@.current_hightlight = null
@.highlight = (target) ->
context.strokeStyle = rgb(0xff,0xff,0x00)
context.beginPath()
context.strokeRect(target.x,target.y, target.width, target.height)
context.closePath()
context.fill()
@current_hightlight = target
@.refresh = ->
@highlight(@current_hightlight) if @current_hightlight? and (frames%4==0)
Object.prototype.reverse_merge= (other_hash) ->
for attrib of other_hash
@[attrib] = other_hash[attrib] if !@[attrib]?
@
#- game loop
game_loop= ->
frames++
clear()
for x in map
for turf in x
turf.draw()
hero.draw()
hero.check_moving()
Highlighter.refresh()
setTimeout(game_loop, 1000/60)
#- Graphics
window.Icon = class Icon
#- set up public attributes
failed: false
success: false
width: null
height: null
states: {}
frame_rate: 3
current_frame: 0
current_state: 0
target: null
constructor: (target,options={}) ->
@target = target if target?
@.reverse_merge(options)
#- set up a private attributes
image = new Image()
image.addEventListener('error', @on_fail, false)
image.addEventListener('load', @on_success, false)
#- privileged methods
@.set_source = (new_src) ->
image.src = new_src
@.set_source(options.src) if options.src?
#- using the failure alerts and such, we can show a place holder for broken images, etc.
@.image_data = ->
if @success? then image else null
do =>
i = 0
for dir in [NORTH,SOUTH, EAST, WEST]
@states[dir] = {x_off: i * (@frame_rate * @width) , y_off: 0}
i++
#- return the final object
@
on_fail: ->
console.log 'image failed'
@failed = true
delete @.success
on_success: ->
@success = true
delete @.failed
x_offset: ->
frame = frames
offset = 0
#- what do you mean we don't have a state for that direction!
try
offset = @states[@target.dir].x_off
catch exception
flat_dir = @target.dir & ~(NORTH|SOUTH)
offset = @states[flat_dir].x_off
(@current_frame = (@current_frame + 1) % @frame_rate) if @target.moving and @target.ready_to_animate()
offset += (@current_frame * @width)
offset
y_offset: ->
0
#- models
class Turf
constructor: (options={})->
options.reverse_merge @attributes if @attributes?
option_defaults =
image_src: 'images/turf.png'
x: 0
y: 0
width: 32
height: 32
frame: 0
frames: 0
animated: false
frame_rate: 0
density: 0
options.reverse_merge(option_defaults)
@.reverse_merge(options)
@image = new Image()
@image.src = @image_src
@image.width = @width
@image.height = @height
draw: ->
context.drawImage @image,
0,
0,
@image.width,
@image.height,
@x,
@y,
@image.width,
@image.height
@animate() if @animated and @ready_to_animate()
animate: ->
ready_to_animate: ->
!!((frames%@frame_rate)==0)
bumped: (bumper) ->
class Wall extends Turf
@prototype.attributes =
image_src: 'images/wall.png'
density: true
bumped: (bumper) ->
console.log 'You ran into a wall'
class Water extends Turf
animate: ->
@frame = (@frame+1) % @frames
@image.src = @image_srcs[@frame]
@prototype.attributes =
frames: 2
frame_rate: 20
animated: true
image_src: 'images/water0.png'
density: true
image_srcs: {0: 'images/water0.png', 1: 'images/water1.png'}
class Hero
@prototype.attributes=
x: 0
y: 0
width: 20
height: 32
image_src: 'images/goku.png'
image_width: 20
image_height: 32
frames:
1:
x_off: 20
t_off: 10
dir: SOUTH
moving: false
icon: null
constructor: (options={})->
@.reverse_merge @attributes
@.reverse_merge options
@icon = new Icon @,{width: @image_width, height: @image_height, src: @image_src}
@move_to(@x,@y)
ready_to_animate: ->
((frames % 4) == 0)
heading: (dir) ->
!!(@dir & dir)
draw: ->
context.drawImage @icon.image_data(),
@icon.x_offset(),
@icon.y_offset(),
@icon.width,
@icon.height,
@x,
@y,
@icon.width,
@icon.height
move_to: (x,y,options={}) ->
success = true
option_defaults =
animate: true
ignore_density: false
options.reverse_merge(option_defaults)
unless options.ignore_density
#- normalize the coordinates, so any lay offer is negated
[offx , offy] = [x, y]
offx += ~~(@width) if @heading(EAST)
offy += ~~(@height) if @heading(SOUTH)
[tile_x ,tile_y] = pixel_coords_to_tile_coords(offx - (offx&31),offy + (offy&31))
tile = map[tile_x][tile_y]
if tile? and @colliding_with(tile)?
Highlighter.highlight(tile)
tile.bumped(@)
success = false
[@x,@y] = [x,y] if success
#- if colliding_with?(obj)
colliding_with: (turf,options={}) ->
options.reverse_merge {x: @x, y: @y}
left1 = options.x
left2= turf.x
right1 = options.x + @width
right2 = turf.x + turf.width
top1 = options.y
top2 = turf.y
bottom1 = options.y + @height
bottom2 = turf.y + turf.height
return null if !turf.density
return null if bottom1 < top2
return null if top1 > bottom2
return null if right1 < left2
return null if left1 > right2
true
check_moving: ->
if @moving
moving_states = {}
moving_states[NORTH] = {x: 0, y: -1}
moving_states[SOUTH] = {x: 0, y: 1}
moving_states[EAST] = {x: 1, y: 0}
moving_states[WEST] = {x: -1, y: 0}
xo = yo = 0
for dir in Object.keys(moving_states)
if !!(@dir & dir)
xo += moving_states[dir].x
yo += moving_states[dir].y
@move_to @x + xo, @y + yo
start_moving: (dir) ->
@dir = switch (dir | @dir)
when 3,12 then dir
else (if @moving then (dir | @dir) else dir)
@moving = true
stop_moving: (dir)->
@moving = switch @dir
when NORTH_EAST,NORTH_WEST,SOUTH_EAST,SOUTH_WEST then true
else false
@dir = switch (pending_dir = @dir ^ dir)
when 0, (NORTH|SOUTH),(EAST|WEST) then dir
else pending_dir
#- map
map_data = [0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,
0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,
0, 0 ,2 ,2 ,2 ,2 ,2 ,0 ,0 ,0,
0, 0 ,2 ,1 ,1 ,1 ,2 ,0 ,0 ,0,
0, 0 ,2 ,1 ,1 ,1 ,2 ,0 ,0 ,0,
0, 0 ,2 ,2 ,0 ,2 ,2 ,0 ,0 ,0,
0, 0 ,0 ,2 ,0 ,2 ,0 ,0 ,0 ,0,
0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,
0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,
0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0]
tile_mappings =
0: Turf
1: Water
2: Wall
map = for x in [0..9]
for y in [0..9]
index = y * 10 + x
struct = map_data[index]
options = {
x: x << 5,
y: y << 5
}
new tile_mappings[struct](options)
#- keyboard
KeyBoard = do ->
parse_key: (code) ->
String.fromCharCode(code)
parse_event: (key,event) ->
[KeyBoard.parse_key(key.key_code()), event].join('_')
for key in [0..255]
try
button = KeyBoard.parse_key(key)
KeyBoard["#{button}_pressed"] = ->
KeyBoard["#{button}_released"] = ->
catch exception
console.log 'unabled to define helper method for ', key, KeyBoard.parse_key(key)
((kb_event) ->
kb_event.key_code= ->
@keyCode || @charCode
kb_event.pressed = ->
event = KeyBoard.parse_event @, 'pressed'
KeyBoard[event].call()
kb_event.released = ->
event = KeyBoard.parse_event @, 'released'
KeyBoard[event].call()
)(KeyboardEvent.prototype)
document.addEventListener('keydown', (key) ->
key.pressed()
, false)
document.addEventListener('keyup', (key) ->
key.released()
, false)
KeyBoard.A_pressed = ->
hero.start_moving WEST
KeyBoard.A_released = ->
hero.stop_moving WEST
KeyBoard.D_pressed = ->
hero.start_moving EAST
KeyBoard.D_released = ->
hero.stop_moving EAST
KeyBoard.W_pressed = ->
hero.start_moving NORTH
KeyBoard.W_released = ->
hero.stop_moving NORTH
KeyBoard.S_pressed = ->
hero.start_moving SOUTH
KeyBoard.S_released = ->
hero.stop_moving SOUTH
KeyBoard.L_released = ->
alert 'you pressed L'
window.KeyBoard = KeyBoard
window.addEventListener('load', ->
hero = new Hero()
game_loop()
update_fps()
window.hero = hero
, false)