-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.coffee
467 lines (386 loc) · 12 KB
/
script.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
---
---
Array::pop = (index=@length-1) -> @splice(index, 1)[0]
Array::random = -> @[Math.floor(@length * Math.random())]
$::css_center = (max_width) -> @css('left', (max_width - @width()) / 2)
$::clear_pos = -> @css('left', '') and @css('top', '')
stage_height = 300
stage_width = 1000
tweet = (text) ->
url = '//neizod.github.io/kick'
link = $('<a>').addClass('twitter-share-button')
.html('tweet')
.attr('href', 'https://twitter.com/share?ref_src=twsrc%5Etfw')
.attr('data-text', text)
.attr('data-url', url)
.attr('data-related', 'neizod')
.attr('data-show-count', 'false')
$('#tweet').html(link)
twttr?.widgets.load()
class ShootingWord
constructor: (@full) ->
@remain = @full
@speed = 110
if player.lvl < 10
@speed += player.lvl * (25 - player.lvl) / 2
else
@speed += player.lvl * 4 + 36
@repr = $('<div>').addClass('absolute').addClass('shooting-word')
@update()
$('#placeholder').html(@repr)
@top = (stage_height - @repr.height()) * Math.random()
@left = stage_width - @repr.width()
@repr.css('top', @top).css('left', @left)
$('#placeholder').empty()
done: ->
@full.slice(0, @full.length - @remain.length)
shot: (c) ->
if c == @remain[0]
@remain = @remain.slice(1)
@update()
move: ->
@left -= @speed / fps.rate()
@repr.css('left', @left)
update: ->
@repr.html([ $('<u>').html(@done().toUpperCase()),
$('<b>').html(@remain.toUpperCase()) ])
reset: ->
@remain = @full
@update()
fps = new class
constructor: ->
@frames = 60
@actual = 0
@time = 0
@date = new Date()
loop: ->
if @actual != @frames
@actual += 1
now = new Date()
@time += (now - @date - @time) / @actual
@date = now
rate: ->
1000 / @time
show: ->
"#{@rate().toFixed(1)} fps"
boss = new class
constructor: ->
@status = 'normal'
reset: ->
@constructor()
$('#boss').attr('class', 'absolute')
.addClass(@status)
sleep: ->
$('#boss').removeClass(@status)
.addClass('sleep')
awake: ->
$('#boss').removeClass('sleep')
.addClass(@status)
update: (lvl=player.lvl) ->
$('#boss').removeClass(@status)
if lvl > 7
@status = 'tear'
else
@status = [ 'love', 'normal', 'ennui', 'curious',
'exclaim', 'angry', 'wtf', 'wth', ][lvl]
$('#boss').addClass(@status)
player = new class
constructor: ->
@lvl = 1
@lives = 3
@score = 0
@word = null
@pair = null
@longest = []
reset: ->
@word?.reset()
@word = null
@pair?.reset()
@pair = null
shoot: (c) ->
@word?.shot(c) and @pair?.shot(c)
if @word?.remain == ''
pool.remove(@word)
if @word.full == inventory.name?.full
@score += inventory.scoring()
inventory.clear()
else if not @pair?
@score += 1
inventory.add(@word)
else
inventory.remove(@word)
if @lvl < Math.floor(Math.log(Math.E + @score))
@lvl += 1
@lives += 1
boss.update()
pool.autofill()
@word = null
@pair = null
show_lives: ->
if @lives > 3
"lives: ♥× #{@lives}"
else if @lives > 0
"lives: #{('♥' for [0...Math.max(@lives, 0)]).join('')}"
else
'lives: ☠'
show_lvl: ->
"level: #{@lvl}"
show_score: ->
"score: #{@score}"
make_summary: ->
@make_tweet()
$('#summary').html("<p>game over, your score is <u>#{@score}</u>.</p>")
.append('<p>click below to tweet your score!</p>')
.show()
make_tweet: ->
unless @longest.length
return tweet('i got kicked by @neizod.')
url_preserve_length = 23
score = " @neizod and score #{@score}!"
quota = 140 - url_preserve_length - score.length
sentence = 'i'
for word in @longest
if sentence.length + 1 + word.full.length >= quota
break
sentence += ' ' + word.full
tweet(sentence + score)
class WordKeeper
get: (str) ->
for word, i in @words
comparator = if str.length == 1 then word.remain[0] else word.full
if str == comparator
return @words[i]
remove: (word) ->
if (index = @index(word))?
@words.pop(index)
index: (find) ->
for word, i in @words
if word.full == find.full
return i
pool = new class extends WordKeeper
constructor: ->
@words = []
actions: null
add: ([email protected]()) ->
@words.push(new ShootingWord(word))
autofill: ->
for [0...Math.max([email protected], 0)]
@add()
loop: ->
for word in @words
word.move()
attack: ->
(@words.filter (word) -> word.left <= 0).length
clean: ->
if player.word?.left <= 0
player.word = null
player.pair?.reset()
@words = @words.filter (word) -> word.left > 0
easter_egg: (force=false) ->
if animate.id == 1 or force
@words = []
@add('kick')
inventory = new class extends WordKeeper
constructor: ->
@words = []
@name = null
add: (word) ->
word.reset()
@words.push(word)
if not @name?
@name = new ShootingWord('@neizod')
remove: (word) ->
super
if not @words.length
@name = null
get_name: (c) ->
if c == @name?.remain[0]
return @name
scoring: ->
score = 0
for word, i in @words
score += word.full.length * Math.max(player.lvl-i, 1)
score
clear: ->
if @words.length >= player.longest.length
player.longest = @words
@name = null
@words = []
show: ->
sentence = @words.concat(if @name? then [@name] else [])
(word.repr.html() for word in sentence).join(' ')
animate = new class
constructor: ->
@id = null
start: ->
fps.constructor()
pool.autofill()
@id = setInterval(@loop, 7)
@toggle_tools()
pause: ->
@id = clearInterval(@id)
@toggle_tools()
stop: ->
@pause()
reset: ->
for reinit_object in [player, inventory, pool]
reinit_object.constructor()
alive: ->
$('#playground').removeClass('dead').addClass('alive')
dead: ->
$('#playground').removeClass('alive').addClass('dead')
loop: =>
fps.loop()
pool.loop()
$('#score').html(player.show_score())
$('#lvl').html(player.show_lvl())
$('#keep').html(inventory.show()).css_center(stage_width)
$('#fps').html(fps.show())
$('#playground').html(word.repr for word in pool.words)
if (damage = pool.attack())
player.lives -= damage
pool.clean()
pool.autofill()
$('#lives').html(player.show_lives())
if player.lives <= 0
if player.score == 0
boss.update(0)
player.make_summary()
@stop()
@reset()
@toggle_tools()
@dead()
$('#start').html('play again!')
$('#menu').show().css_center(stage_width)
toggle_tools: ->
if animate.id
$('#pause').show()
$('#resume').hide()
$('#erase').prop('disabled', false)
else
$('#pause').hide()
$('#resume').show()
$('#erase').prop('disabled', true)
tutorial = new class
constructor: ->
@step = 0
@all_steps = 4
reset: ->
while @step
@nextstep()
@sample[0]()
sample:
0: ->
animate.reset()
$('#score').html(player.show_score())
$('#lvl').html(player.show_lvl())
$('#lives').html(player.show_lives())
1: ->
pool.easter_egg(true)
player.word = pool.words[0]
player.word.repr.clear_pos().attr('id', 'sample-1')
$('#playground').html(player.word.repr)
2: ->
player.word.remain = player.word.full.slice(2)
player.word.update()
player.word.repr.clear_pos().attr('id', 'sample-2')
$('#playground').html(player.word.repr)
3: ->
player.word.remain = ''
player.shoot()
player.word = inventory.name
player.word.remain = player.word.full.slice(4)
player.word.update()
$('#playground').empty()
$('#keep').html(inventory.show()).css_center(stage_width)
4: ->
player.word.remain = ''
player.shoot()
$('#keep').empty()
$('#score').html(player.show_score())
$('#lvl').html(player.show_lvl())
nextstep: ->
if @step
$("#step-#{@step}").hide()
else
$('#howto').hide()
$('#start').hide()
$('.social').hide()
$('#nextstep').show()
$('#tutorial').show()
animate.alive()
@step += 1
@step %= @all_steps + 1
if @step
$("#step-#{@step}").show()
if @step == @all_steps
$('#start').show()
$('#howto').show()
$('.social').show()
$('#nextstep').hide()
else
$('#howto').show()
$('#nextstep').hide()
$('#tutorial').hide()
@sample[@step]()
$(document).keydown (event) ->
hotkeys =
nextstep: [13, 27] # enter, esc
howto: [27] # esc
erase: [8, 46] # backspace, delete
pause: [27] # esc
start: [13] # enter
resume: [13, 27] # enter, esc
for id, keys of hotkeys
button = $("##{id}")
clickable_button = button.is(':visible') and not button.is(':disabled')
if clickable_button and event.keyCode in keys
return button.click() and false
$(document).keypress (event) ->
return if not animate.id?
c = String.fromCharCode(event.charCode)
if not player.word?
player.word = pool.get(c)
if player.word?
if player.word.full == player.word.remain
player.pair = inventory.get(player.word.full)
else
player.word = inventory.get_name(c)
player.shoot(c)
$(document).ready ->
animate.toggle_tools()
for pre_hidden in ['#nextstep', '#tutorial', '#summary']
$(pre_hidden).hide()
animate.alive()
boss.reset()
$('#menu').show().css_center(stage_width)
$.get 'words.txt', (data) ->
pool.actions = data.split('\n').filter (word) -> word.length
$('#start').click ->
tutorial.reset()
boss.reset()
$('#summary').hide()
$('#start, #resume').click ->
unless pool.actions
return $('#keep').html('-- file not ready, please try again. --')
.css_center(stage_width)
animate.start()
boss.awake()
pool.easter_egg()
$('#menu').hide()
animate.alive()
$('#howto').click ->
tutorial.reset()
boss.reset()
$('#summary').hide()
$('#howto, #nextstep').click ->
tutorial.nextstep()
$('#menu').show().css_center(stage_width)
$('#erase').click ->
player.reset()
$('#pause').click ->
animate.pause()
boss.sleep()
$('#playground').empty()
$('#keep').html('-- game pause --').css_center(stage_width)
tweet("let's play kick @neizod!")