-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.js
212 lines (179 loc) · 5.83 KB
/
box.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
/**
* @file box.js
* @brief Implement Box type entities also Clouds because
* And also VictoryBox
* @requires entity.js
*/
function Box(position, velocity, acceleration, canvas)
{
Entity.call(this, position, velocity, acceleration, canvas, "");
this.index = Math.round(1 + Math.random());
this.frame = canvas.LoadTexture("data/box/box"+this.index+".gif");
this.damagedFrame = canvas.LoadTexture("data/box/box"+this.index+"_damaged.gif");
this.name = "Box";
this.ignoreCollisions["Roof"] = true;
this.health = 2+Math.random()*4;
this.fallbackColour = "#A52A2AFF";
}
Box.prototype = Object.create(Entity.prototype);
Box.prototype.constructor = Box;
Box.prototype.Step = function(game)
{
if (this.position[1] > 0.9)
this.acceleration = [0.5*game.gravity[0], 0.5*game.gravity[1]];
else
this.acceleration = game.gravity;
if (this.health < 1)
this.frame = this.damagedFrame;
Entity.prototype.Step.call(this, game);
if (Math.abs(this.position[0]) > 1.2)
{
this.Die();
return;
}
if (Math.abs(this.velocity[0]) < 1e-2)
this.velocity[0] = 0;
else if (Math.abs(this.velocity[0]) <= 0.5
&& Math.abs(this.acceleration[0]) < 1e-12)
this.velocity[0] /= 4;
}
/**
* Box Collision
*/
Box.prototype.HandleCollision = function(other, instigator, game)
{
if (instigator)
{
if (Math.abs(this.velocity[0]) > 0)
this.velocity[0] /= 2;
return Entity.prototype.HandleCollision.call(this,other, instigator,game);
}
if (other.Top() - this.Bottom() < 0.05*other.Height())
this.position[1] += 0.07*other.Height();
if (other.GetName() == "Box" && other.Top() > this.Top())
{
// Ensure boxes always take 2 hits minimum to open
this.health -= Math.min(0.99, Math.abs(Math.random()*other.velocity[1]));
this.frame = this.damagedFrame; // It's always damaged, change frame immediately i guess?
if (this.health <= 0)
this.Die();
}
if (other.GetName() == "Wall")
{
return true;
}
this.acceleration = game.gravity;
return Entity.prototype.HandleCollision.call(this,other,instigator);
}
Box.prototype.Die = function(reason, other, game) {
if (game && other && other === game.player) {
game.SpawnEntity(Carrot, this)
other.velocity[1] = -0.3*other.velocity[1]; // Bounce!
}
return Entity.prototype.Die.call(this, reason, other, game);
}
function Cloud(position, canvas)
{
var vx = position[0] < -1 ? 0.5*Math.random() : -0.5*Math.random();
Entity.call(this, position, [vx,0], [0,0], canvas, "");
this.frame = canvas.LoadTexture("data/cloud/cloud1.png");
this.name = "Cloud";
this.ignoreCollisions = Cloud.prototype.ignoreCollisions;
this.ignoreCollisions["Roof"] = true;
this.ignoreCollisions["Wall"] = true;
this.fallbackColour = "#AAAAAA99";
this.bounds = {min: [-64/canvas.width, -33/canvas.width], max:[64/canvas.width, -32/canvas.width]};
console.debug("Created cloud");
}
Cloud.prototype = Object.create(Entity.prototype);
Cloud.prototype.constructor = Cloud;
Cloud.prototype.ignoreCollisions = [];
Cloud.prototype.Step = function(game)
{
Entity.prototype.Step.call(this, game);
if (Math.abs(this.position[0]) > 1.2)
{
this.Die(this.GetName(), this, game);
return;
}
if (Math.abs(this.velocity[0]) <= 0)
{
this.Die(this.GetName(), this, game);
}
}
Cloud.prototype.HandleCollision = function(other, instigator, game)
{
Cloud.prototype.ignoreCollisions[other.GetName()] = true;
// This seems really horrible but it is simpler than manually adding it to all objects?
other.CollisionActions["Cloud"] = Cloud.prototype.CloudCollisionHandler;
// pass
return false;
}
/**
* Collision handler for a cloud
*/
Cloud.prototype.CloudCollisionHandler = function(other, instigator, game)
{
if (instigator && this.Bottom() <= other.Top())
{
this.position[0] += this.velocity[0]*this.delta/2000;
this.position[1] += this.velocity[1]*this.delta/2000;
return false;
}
// Wait, why can clouds jump again?
this.canJump = 1;
return true;
}
function VictoryBox(position, velocity, acceleration, canvas) {
Box.call(this, position, velocity, acceleration, canvas);
this.name = "Portal";
console.debug(canvas)
const baseImage = "data/box/box_victory"
const baseExtension = ".gif"
const frameIndexes = [... Array(5).keys()]
this.frames = frameIndexes.map(index => canvas.LoadTexture(baseImage+String(index+1)+baseExtension))
console.debug('frames are ', this.frames)
this.portalEffect = null;
this.description = "NEXT\nLEVEL";
this.helpfulHint = "(STOMP)\n↓↓↓";
}
VictoryBox.prototype = Object.create(Box.prototype);
VictoryBox.prototype.constructor = VictoryBox;
VictoryBox.prototype.HandleCollision = function(other, instigator, game) {
if (["Floor", "Wall", "Ceiling"].indexOf(other.GetName()) >= 0) {
return true;
}
if (other !== game.player) {
if (["Hat", "Box"].indexOf(other.GetName()) >= 0) {
other.Die("portal", this, game);
}
return true;
}
if (other.MovingTowards(this) && other.Above(this) && other.velocity[1] < -1 && other.IsStomping(this)) {
console.debug(`Victory! ${other.velocity[1]}`);
if (!this.portalEffect) {
this.portalEffect = new SFXEntity(this, 2000/game.stepRate, ["data/hats/hat1_reversed.png"], game.canvas, [0, 0]);
this.portalEffect2 = new SFXEntity(other, 2000/game.stepRate, ["data/sfx/shield1.png"], game.canvas, [0, 0]);
this.portalEffect.onDeath(() => {
this.Die("victory", other, game);
this.portalEffect = null;
game.NextLevel();
})
game.AddEntity(this.portalEffect);
game.AddEntity(this.portalEffect2);
}
other.Hide();
// Hack to prevent enemies from killing the player
other.position[0] = 100;
other.position[1] = 100;
this.Hide();
return true;
} else {
if (this.description != this.helpfulHint) {
const oldDescription = this.description;
this.description = this.helpfulHint;
game.AddTimeout("PortalHint", () => this.description = oldDescription, 1000);
}
}
return Box.prototype.HandleCollision.call(this, other, instigator, game);
}