-
Notifications
You must be signed in to change notification settings - Fork 1
/
go.js
276 lines (240 loc) · 8.28 KB
/
go.js
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
$(function() {
var width = 800,
height = 600,
scrollThreshold = 300,
accelerationRate = 0.4,
decelerationRate = 0.1,
collisionTolerance = 0.01,
oscillatorAccelerationRate = 0.1,
oscillatorMaximumRate = 2,
oscillatorMaximumDisplacement = 100,
maximumXRate = 4,
jumpSpeed = 10,
gravityRate = 0.5,
playerInitialPosition = {x: 400, y: 380},
r = Raphael("map", width, height);
$(r).focus();
var target = r.rect(1390, 420, 200, 20, 0);
var oscillator = r.rect(1190, 220, 200, 20, 0);
var terrain = [
r.rect(50, 400, 700, 100, 0),
r.rect(50, 320, 200, 100, 0),
r.rect(550, 320, 200, 100, 0),
r.rect(350, 220, 200, 20, 0),
r.rect(50, 120, 200, 20, 0),
r.rect(790, 420, 200, 20, 0),
r.rect(990, 320, 200, 20, 0),
oscillator,
r.rect(1290, 200, 200, 20, 0),
target
];
var terrainSet = r.set();
var completeSet = r.set();
$.each(terrain, function(index,item) {terrainSet.push(item); completeSet.push(item);});
terrainSet.push(terrain);
terrainSet.attr({fill: "#0a0", 'stroke-width': 0});
target.attr({'target': true, 'fill': '#00f'});
var xMomentum = 0;
var yMomentum = 0;
var environmentalMomentum = 0;
var totalXMomentum = 0;
// var background = r.rect(0, 0, width, height).attr({fill: "90-#222-#000"}).toBack();
var background = r.circle(400, 300, width/1.55).attr({fill: "r#222-#000"}).toBack();
var player = r.rect(playerInitialPosition['x'], playerInitialPosition['y'], 20, 10)
.attr({
'stroke-width': 0,
'fill': "#f00",
'opacity': 1
})
.toFront();
completeSet.push(player);
var keyFunctionMap = {
'37': function (amount) { alterXMomentum(-1 * amount) },
'38': jump,
'39': function (amount) { alterXMomentum(amount) }
// '40': function (amount) { alterYMomentum(amount) }
};
var keyStatus = {
'37': false,
'38': false,
'39': false
// '40': false
};
// Set up key bindings
$(window).keydown(function(e) {
if (e.keyCode in keyStatus)
keyStatus[e.keyCode] = true;
});
$(window).keyup(function(e) {
if (e.keyCode in keyStatus)
keyStatus[e.keyCode] = false;
});
function loop() {
moveOscillator();
computePlayerMomentumChanges();
player.translate(xMomentum + environmentalMomentum, yMomentum);
checkDeath();
checkDeceleration();
doGravity();
checkScroll();
checkCollisions();
}
var loopInterval = setInterval(loop, 20);
function computePlayerMomentumChanges() {
$.each(keyStatus, function(key, value) {
if (value == true) {
keyFunctionMap[key](accelerationRate);
}
else if (key == '38') {
preventJump = false;
}
});
totalXMomentum = xMomentum + environmentalMomentum;
}
function alterXMomentum(amount) {
xMomentum = xMomentum + amount;
if (xMomentum > maximumXRate)
xMomentum = maximumXRate;
if (xMomentum < -maximumXRate)
xMomentum = -maximumXRate;
}
function alterYMomentum(amount) {
yMomentum = yMomentum + amount;
}
function checkDeceleration() {
if (keyStatus['37'] || keyStatus['39']) {
return;
}
else {
if (xMomentum <= -decelerationRate)
xMomentum += decelerationRate;
else if (xMomentum >= decelerationRate)
xMomentum -= decelerationRate;
else
xMomentum = 0;
}
}
function doGravity() {
yMomentum += gravityRate;
}
var inTheAir = true;
var preventJump = false;
function jump() {
if (!inTheAir && !preventJump) {
yMomentum = -jumpSpeed;
preventJump = true;
}
}
function checkCollisions() {
inTheAir = true;
$.each(terrain, function(index, thing) {
if (rectangleIntersectsRectangle(player, thing)) {
if (thing.attr('target')) {
displayWin();
}
if (yMomentum > 0
&& player.attr('y') + player.attr('height') - yMomentum < thing.attr('y')
&& player.attr('y') + player.attr('height') >= thing.attr('y')) {
// Player landed on it
yMomentum = 0;
player.attr('y', thing.getBBox().y - player.getBBox().height);
inTheAir = false;
}
else if (yMomentum < 0
&& player.attr('y') - yMomentum > thing.attr('y') + thing.attr('height')
&& player.attr('y') <= thing.attr('y') + thing.attr('height')) {
// Player hit it from the bottom
yMomentum = 0;
player.attr('y', thing.getBBox().y + thing.getBBox().height);
}
if (totalXMomentum > 0
&& player.attr('x') + player.attr('width') - totalXMomentum <= thing.attr('x')
&& player.attr('x') + player.attr('width') >= thing.attr('x')) {
totalXMomentum = 0;
player.attr('x', thing.attr('x') - player.attr('width'));
}
else if (totalXMomentum < 0
&& player.attr('x') - totalXMomentum >= thing.attr('x') + thing.attr('width')
&& player.attr('x') <= thing.attr('x') + thing.attr('width')) {
totalXMomentum = 0;
player.attr('x', thing.attr('x') + thing.attr('width'));
}
}
});
}
function checkScroll() {
if (
(player.attr('x') < scrollThreshold && xMomentum < 0) ||
(width - (player.attr('x') + player.attr('width')) < scrollThreshold && xMomentum > 0)
) {
completeSet.translate(-xMomentum, 0);
}
}
var oscillatorMomentum = 0;
var oscillatorDisplacement = 0;
var oscillatorDirection = 1;
function moveOscillator() {
if (oscillatorDisplacement <= 0) {
oscillatorDirection = 1;
}
else if (oscillatorDisplacement >= oscillatorMaximumDisplacement) {
oscillatorDirection = -1;
}
oscillatorMomentum += oscillatorDirection * oscillatorAccelerationRate;
if (Math.abs(oscillatorMomentum) > oscillatorMaximumRate)
oscillatorMomentum = oscillatorDirection * oscillatorMaximumRate;
oscillator.translate(oscillatorMomentum, 0);
oscillatorDisplacement += oscillatorMomentum;
if (rectangleAdjacentToRectangle(player, oscillator))
environmentalMomentum = oscillatorMomentum;
else
environmentalMomentum = 0;
}
function checkDeath() {
if (player.attr('y') > height) {
// you're dead
clearInterval(loopInterval);
$.each(completeSet, function(index, item) { item.hide() });
r.text(404, 304, "GAME\nOVER").attr({'font-size': 180, fill: "#280000", 'stroke-width': 20, 'stroke': '#280000'});
r.text(400, 300, "GAME\nOVER").attr({'font-size': 180, fill: "red"});
}
}
function displayWin() {
clearInterval(loopInterval);
// $.each(completeSet, function(index, item) { item.hide() });
r.text(402, 152, "YOU\nWIN!").attr({'font-size': 100, 'fill': '#002800', 'stroke-width': 10, 'stroke': '#002800'});
r.text(400, 150, "YOU\nWIN!").attr({'font-size': 100, fill: 'green'});
}
//
// Collision detection
//
function rectangleIntersectsRectangle(r1, r2) {
var dim1 = r1.getBBox();
var dim2 = r2.getBBox();
if (dim1.x + dim1.width > dim2.x && dim1.x < dim2.x + dim2.width) {
if (dim1.y + dim1.height > dim2.y && dim1.y < dim2.y + dim2.height) {
return true;
}
}
}
function rectangleAdjacentToRectangle(r1, r2) {
var dim1 = r1.getBBox();
var dim2 = r2.getBBox();
if (dim1.x + dim1.width >= dim2.x && dim1.x <= dim2.x + dim2.width) {
if (dim1.y + dim1.height >= dim2.y && dim1.y <= dim2.y + dim2.height) {
return true;
}
}
}
function rectangleIntersectsCircle(theRectangle, theCircle) {
var distanceX = Math.abs(theCircle.attr('cx') - theRectangle.attr('x') - theRectangle.attr('width')/2);
var distanceY = Math.abs(theCircle.attr('cy') - theRectangle.attr('y') - theRectangle.attr('height')/2);
if (distanceX > (theRectangle.attr('width')/2 + theCircle.attr('r'))) { return false; }
if (distanceY > (theRectangle.attr('height')/2 + theCircle.attr('r'))) { return false; }
if (distanceX <= (theRectangle.attr('width')/2)) { return true; }
if (distanceY <= (theRectangle.attr('height')/2)) { return true; }
var cornerDistance_sq = Math.pow((distanceX - theRectangle.attr('width')/2), 2) +
Math.pow((distanceY - theRectangle.attr('height')/2), 2);
return (cornerDistance_sq <= Math.pow(theCircle.r, 2));
}
});