Skip to content

Commit 2e45eec

Browse files
committed
Initial commit
0 parents  commit 2e45eec

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js

README.markdown

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# hrdlr js
2+
3+
Turn up your volume.
4+
5+
``` sh
6+
coffee --compile *.coffee
7+
open index.html
8+
```
9+
10+
By Odin Dutton and Jason Weathered.

hrdlr.coffee

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
pre = document.querySelector('pre')
2+
frame = null
3+
sounds = {
4+
jump: new Audio('sounds/jump.m4a')
5+
}
6+
7+
getRandomInt = (min, max) ->
8+
return Math.floor(Math.random() * (max - min + 1) + min)
9+
10+
class Sprite
11+
constructor: (@config) ->
12+
@frames = @config.frames
13+
@states = @config.states
14+
@changeState(Object.keys(@states)[0])
15+
16+
changeState: (state) ->
17+
@currentState = state
18+
@frameIndex = -1
19+
20+
tick: ->
21+
@frameIndex++
22+
if @frameIndex >= @states[@currentState].length
23+
@frameIndex = 0
24+
frameName = @states[@currentState][@frameIndex]
25+
@currentFrame = @frames[frameName]
26+
27+
class Items
28+
constructor: (@initial, @step_min, @step_max) ->
29+
@items = [@initial]
30+
@deleted = []
31+
32+
get: (x1, x2) ->
33+
# populate till the end of the range
34+
while @items[@items.length-1] < x2
35+
@items.push @items[@items.length-1] + getRandomInt(@step_min, @step_max)
36+
# remove old entries
37+
if @items.size > 1000
38+
@items = @items.slice(@items.length-1000)
39+
# return items in requested range (without deleted items)
40+
items = []
41+
for x in @items
42+
if x >= x1 && x <= x2 && @deleted.indexOf(x) < 0
43+
items.push x
44+
items
45+
46+
delete: (items) ->
47+
@deleted.push items...
48+
49+
playerSprite = new Sprite
50+
frames:
51+
run1:
52+
'''
53+
o
54+
<|-
55+
/ >
56+
'''
57+
run2:
58+
'''
59+
o
60+
/|~
61+
--\\
62+
'''
63+
jump:
64+
'''
65+
o/
66+
/|
67+
---
68+
'''
69+
states:
70+
running: [
71+
'run1',
72+
'run1',
73+
'run2',
74+
'run2',
75+
]
76+
jumping: [
77+
'jump',
78+
]
79+
80+
class Player
81+
constructor: (@sprite) ->
82+
@jumpPos = null
83+
@posX = 0
84+
@posY = 2
85+
86+
tick: ->
87+
@posX++
88+
if @jumpPos?
89+
if @jumpPos < 4
90+
@jumpPos++
91+
else
92+
@jumpPos = null
93+
@posY = 2
94+
@sprite.changeState 'running'
95+
96+
jump: ->
97+
return if @jumpPos?
98+
@jumpPos = 0
99+
@posY = 1
100+
@sprite.changeState 'jumping'
101+
sounds.jump.play()
102+
103+
player = new Player(playerSprite)
104+
105+
document.addEventListener 'keypress', (event) ->
106+
if event.keyCode == 32
107+
player.jump()
108+
109+
document.addEventListener 'touchstart', ->
110+
player.jump()
111+
112+
drawSprite = (spriteFrame, x, y) ->
113+
spriteFrameLines = spriteFrame.split('\n')
114+
for i in [0..spriteFrameLines.length-1]
115+
frame[i+y] = frame[i+y].slice(0, x) + spriteFrameLines[i] + frame[i+y].slice(x+spriteFrameLines[i].length)
116+
117+
clearFrame = ->
118+
frame = [
119+
new Array(81).join('-'),
120+
new Array(81).join(' '),
121+
new Array(81).join(' '),
122+
new Array(81).join(' '),
123+
new Array(81).join(' '),
124+
new Array(81).join('-')
125+
]
126+
127+
hurdles = new Items(50, 10, 20)
128+
129+
viewportX = -3
130+
131+
tick = ->
132+
clearFrame()
133+
134+
player.tick()
135+
playerSprite.tick()
136+
137+
for hurdle_x in hurdles.get(0+player.posX+viewportX, 80+player.posX+viewportX)
138+
drawSprite('#', hurdle_x-player.posX-viewportX, 4)
139+
140+
drawSprite(playerSprite.currentFrame, -viewportX, player.posY)
141+
142+
pre.innerText = frame.join('\n')
143+
144+
setTimeout(tick, 100)
145+
146+
tick()

index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
* { margin: 0; padding: 0; }
6+
body { background: #111; color: #eee; }
7+
</style>
8+
</head>
9+
<body>
10+
<pre>Loading...</pre>
11+
<script src=hrdlr.js type=text/javascript></script>
12+
</body>
13+
</html>

sounds/jump.m4a

2.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)