-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame2.coffee
More file actions
185 lines (155 loc) · 4.52 KB
/
game2.coffee
File metadata and controls
185 lines (155 loc) · 4.52 KB
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
random_color = ->
"#" + Math.floor(Math.random() * 16777215).toString(16)
Game = ->
window.Q = Quintus(development: true).include("Sprites, Scenes, 2D, Input, UI").setup(maximize: true)
Q.component 'autoFlip',
added: ->
@entity.on("step", this, "flipStep")
flipStep: ->
p = @entity.p
if (Q.inputs['left'])
p.flip = 'x'
if (Q.inputs['right'])
p.flip = false
Q.Sprite.extend "Player",
init: (p) ->
@_super p,
asset: Game.Assets.monster_faces[0]
hitPoints: 10
damage: 5
x: BLOCK_SIZE
y: BLOCK_SIZE
face_count: FACE_DELAY
current_face: 0
faces: Game.Assets.monster_faces
angle: -5
points: [[-50, -50], [40, -50], [40, 40], [-50, 40]]
salto: 0
score: 0
speed_wakkel: false
@update_face()
@add "2d, platformerControls, autoFlip"
window.addEventListener 'deviceorientation', (eventData) =>
# gamma is the left-to-right tilt in degrees, where right is positive
tilt_lr = eventData.gamma
@p.vx = tilt_lr
step: ->
@p.face_count -= 1
if @p.speed_wakkel || @p.face_count < 0 && @p.salto == 0
@p.angle *= -1
if @p.salto > 0
@p.angle = @p.salto
@p.salto -= 5
if @p.face_count < 0
@p.face_count = FACE_DELAY
@next_face()
next_face: ->
@p.current_face += 1
@p.current_face = 0 if @p.current_face >= @p.faces.length
@update_face()
update_face: ->
@p.asset = @p.faces[@p.current_face]
salto: ->
@p.salto += 360 if @p.salto < 360
@p.vy = -600
inc_score: ->
@p.score += 1
Game.score_label.p.label = '' + @p.score
collect_gem: ->
@inc_score()
@p.angle = -7
@p.speed_wakkel = true
undo_wakkel = =>
@p.angle = 5
@p.speed_wakkel = false
setTimeout undo_wakkel, 1000
Q.Sprite.extend "Ruby",
init: (p) ->
console.log Game.Assets.rubyred
@_super p,
asset: Game.Assets.rubyred
@add "2d"
@on "hit", this, "collision"
collision: (coll) ->
if coll.obj.isA 'Player'
coll.obj.collect_gem()
@destroy()
Q.Sprite.extend "Block",
init: (p) ->
@_super p,
color: random_color()
w: BLOCK_SIZE
h: BLOCK_SIZE
# gravity: 0
#@add '2d'
draw: (ctx) ->
ctx.fillStyle = @p.color
ctx.fillRect -@p.cx, -@p.cy, @p.w, @p.h
Q.scene "start", (stage) ->
player = new Q.Player({stage: stage})
stage.insert player
stage.add('viewport').follow(player,{ x: true, y: true })
y = 0
Game.score_label = stage.insert(new Q.UI.Text(
x: Q.width - 250
y: 100
scale: 3
label: '0'
))
build_world = ->
while y < Level.length
row = Level[y]
x = 0
while x < row.length
char = row.charAt(x)
if char is "x"
block = new Q.Block(
x: (x + 0.5) * BLOCK_SIZE
y: (y + 0.5) * BLOCK_SIZE
)
stage.insert block
if char is "." && Math.random() < 0.2
ruby = new Q.Ruby(
x: (x + 0.5) * BLOCK_SIZE
y: (y + 0.5) * BLOCK_SIZE
)
stage.insert ruby
x++
y++
build_world()
Q.input.on 'fire', stage, (e) ->
player.salto()
Q.load Game.Assets.all, ->
Q.stageScene "start"
Q.input.keyboardControls()
Level = [
"...... .",
"xxxxxx ....x",
". ...... xxxxx",
" xxxxxx ",
"...... . x..... ",
"xx xxx xxxxxxxxxxxxxxxxxxx ",
".. . ....x....... ",
"xx ..x.. xxxxx.......xxxx ",
". xx.xx xxxxxxx xxx ",
"........... xxxx xxx ",
"xxxxxx xxxxxx xxx ",
"x.............x xxx ",
"x xx xx x ",
"x xx x ",
"x.............x ",
"xxxxxxxxxxxxxxx ",
]
FACE_DELAY = 8
BLOCK_SIZE = 100
Game.Assets =
monster1: "monster1.png"
monster2: "monster2.png"
monster3: "monster3.png"
rubyred: "rubyred.png"
rubygreen: "rubygreen.png"
rubyblue: "rubyblue.png"
Game.Assets.monster_faces = [Game.Assets.monster1, Game.Assets.monster2]
Game.Assets.rubies = [Game.Assets.rubyred, Game.Assets.rubygreen, Game.Assets.rubyblue]
Game.Assets.all = Game.Assets.monster_faces.concat(Game.Assets.rubies)
window.addEventListener "load", Game