-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
279 lines (230 loc) · 8.03 KB
/
core.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
277
278
279
//import * as planck from "./planck";
var Vec2 = planck.Vec2;
var preloadCount = 0;
var preloadTotal = 9;
var bedImg = new Image();
var couchImg = new Image();
var chairImg = new Image();
var armchairImg = new Image();
var tableImg = new Image();
var deskImg = new Image();
var lampImg = new Image();
var standImg = new Image();
var windowImg = new Image();
var stage;
var world;
var toUpdate = [];
var walls;
var friction;
var mouseJoint;
var mouseGround;
var scale = 50;
var offset = 20;
var active = true;
var started = false;
function startGame()
{
stage = new createjs.Stage("gameCanvas");
var text = new createjs.Text("Loading...");
text.x = stage.canvas.width/2; text.y = stage.canvas.height/2;
text.textAlign = "center"; text.textBaseline = "middle";
stage.addChild(text);
stage.update();
preloadAssets();
}
function preloadAssets()
{
bedImg.onload = preloadUpdate;
bedImg.src = "bed.png";
couchImg.onload = preloadUpdate;
couchImg.src = "couch.png";
chairImg.onload = preloadUpdate;
chairImg.src = "chair.png";
armchairImg.onload = preloadUpdate;
armchairImg.src = "armchair.png";
deskImg.onload = preloadUpdate;
deskImg.src = "desk.png";
tableImg.onload = preloadUpdate;
tableImg.src = "table.png";
lampImg.onload = preloadUpdate;
lampImg.src = "lamp.png";
standImg.onload = preloadUpdate;
standImg.src = "stand.png";
windowImg.onload = preloadUpdate;
windowImg.src = "window.png";
//createjs.Sound.addEventListener("fileload", preloadUpdate);
//createjs.Sound.registerSound("media/receive.wav", "jump", 4);
}
function preloadUpdate()
{
preloadCount++;
if(preloadCount === preloadTotal)
launchGame();
}
function launchGame()
{
stage.removeChildAt(0); //loading text
world = planck.World();
walls = world.createBody({
type: 'static',
position: Vec2(.5, .5),
});
var wallFD = {
density: 1.0,
restitution: 0.3,
};
walls.createFixture(planck.Edge(Vec2(0, 0), Vec2(9, 0)), wallFD);
walls.createFixture(planck.Edge(Vec2(0, 0), Vec2(0, 9)), wallFD);
walls.createFixture(planck.Edge(Vec2(0, 9), Vec2(9, 9)), wallFD);
walls.createFixture(planck.Edge(Vec2(9, 0), Vec2(9, 9)), wallFD);
walls.createFixture(planck.Edge(Vec2(0, 4.7), Vec2(4.5, 4.7)), wallFD);
walls.createFixture(planck.Edge(Vec2(4.5, 4.7), Vec2(4.5, 4.9)), wallFD);
walls.createFixture(planck.Edge(Vec2(4.5, 4.9), Vec2(0, 4.9)), wallFD);
walls.createFixture(planck.Edge(Vec2(9, 6.7), Vec2(6, 6.7)), wallFD);
walls.createFixture(planck.Edge(Vec2(6, 6.7), Vec2(6, 7.7)), wallFD);
walls.createFixture(planck.Edge(Vec2(6, 7.7), Vec2(6.1, 7.7)), wallFD);
walls.createFixture(planck.Edge(Vec2(6.1, 7.7), Vec2(6.1, 6.9)), wallFD);
walls.createFixture(planck.Edge(Vec2(6.1, 6.9), Vec2(9, 6.9)), wallFD);
drawWalls();
//draw windows
var w1 = new createjs.Bitmap(windowImg);
var w2 = new createjs.Bitmap(windowImg);
w1.x = 300; w1.y = 3;
w2.x = 200; w2.y = 3;
w2.scaleX = -1;
stage.addChild(w1);
stage.addChild(w2);
mouseGround = world.createBody();
var bed = new Furniture(new createjs.Bitmap(bedImg), 95, 120, -90);
toUpdate.push(bed);
let couch1 = new Furniture(new createjs.Bitmap(couchImg), 300, 50);
toUpdate.push(couch1);
let couch2 = new Furniture(new createjs.Bitmap(couchImg), 400, 170, 90);
toUpdate.push(couch2);
toUpdate.push(new Furniture(new createjs.Bitmap(chairImg), 330, 320, 180, true));
toUpdate.push(new Furniture(new createjs.Bitmap(chairImg), 380, 320, 180, true));
toUpdate.push(new Furniture(new createjs.Bitmap(chairImg), 430, 320, 180, true));
toUpdate.push(new Furniture(new createjs.Bitmap(tableImg), 310, 160, 90));
let desk = new Furniture(new createjs.Bitmap(deskImg), 150, 400);
toUpdate.push(desk);
toUpdate.push(new Furniture(new createjs.Bitmap(armchairImg), 140, 310));
toUpdate.push(new Furniture(new createjs.Bitmap(standImg), 25, 25, -90));
toUpdate.push(new Furniture(new createjs.Bitmap(standImg), 25, 215, -90));
let lamp1 = new Furniture(new createjs.Bitmap(lampImg), 410, 50, 60, true);
toUpdate.push(lamp1);
let lamp2 = new Furniture(new createjs.Bitmap(lampImg), 40, 410, 120, true);
toUpdate.push(lamp2);
let lamp3 = new Furniture(new createjs.Bitmap(lampImg), 345, 380, 20, true);
toUpdate.push(lamp3);
//add constraints
nbConstraints = 4;
var constraint1 = new DirConstraint("north", bed, "bednorth", "bnstatus");
var constraint2 = new ProxiConstraint(bed, 2, 2.7, [lamp1, lamp2, lamp3], "lamprox", "lpstatus");
var constraint3 = new ProxiConstraint(desk, 1, 2.1, [couch1, couch2], "couprox", "cpstatus");
var constraint4 = new ProxiConstraint(desk, 1, 3.1, [w1, w2], "wiprox", "wistatus");
//add required stuff to access pos
w1.box = {getPosition : function(){return Vec2((w1.x + 60)/scale, -1);}};
w2.box = {getPosition : function(){return Vec2((w2.x - 60)/scale, -1);}};
toUpdate.push(constraint1);
toUpdate.push(constraint2);
toUpdate.push(constraint3);
toUpdate.push(constraint4);
stage.on("stagemousemove", function(evt) {
if(!started)
started = true;
if(mouseJoint)
mouseJoint.setTarget({x: evt.stageX / scale, y: evt.stageY / scale});
});
createjs.Ticker.framerate = 30;
createjs.Ticker.addEventListener("tick", update);
}
var counter = 0;
function update(event)
{
if(!active)
return;
if(started) {
counter += event.delta / 1000;
document.getElementById("timer").innerHTML = counter.toFixed(1) + "s";
}
if(document.getElementById("objectives").innerHTML.split("✔").length - 1 === nbConstraints) { //lol
active = false;
var wintxt = new createjs.Text("YAY!\r\nWIN!", "230px Arial", "#ff0180");
var score = new createjs.Text("you did it in " + counter.toFixed(1) + "s", "50px Arial", "#ff0180");
score.y = 430; score.x = 50;
stage.addChild(wintxt);
stage.addChild(score);
stage.update();
}
world.step(1 / 30);
for(var f of toUpdate)
{
f.update();
}
stage.update();
}
function drawWalls() {
//around
var wsh = new createjs.Shape();
wsh.graphics.setStrokeStyle(2);
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(.5 * scale, .5 * scale);
wsh.graphics.lineTo(.5 * scale, 9.5 * scale);
wsh.graphics.lineTo(9.5 * scale, 9.5 * scale);
wsh.graphics.lineTo(9.5 * scale, .5 * scale);
wsh.graphics.lineTo(.5 * scale, .5 * scale);
wsh.graphics.endStroke();
//cloison gauche
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(0 * scale, 5.2 * scale);
wsh.graphics.lineTo(5 * scale, 5.2 * scale);
wsh.graphics.lineTo(4.95 * scale, 5.4 * scale);
wsh.graphics.lineTo(0.5 * scale, 5.4 * scale);
wsh.graphics.lineTo(0 * scale, 5.23 * scale);
wsh.graphics.endStroke();
//cloison droite
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(10 * scale, 7.37 * scale);
wsh.graphics.lineTo(9.5 * scale, 7.2 * scale);
wsh.graphics.lineTo(6.5 * scale, 7.2 * scale);
wsh.graphics.lineTo(6.5 * scale, 8.2 * scale);
wsh.graphics.lineTo(6.6 * scale, 8.2 * scale);
wsh.graphics.lineTo(6.6 * scale, 7.4 * scale);
wsh.graphics.lineTo(10 * scale, 7.4 * scale);
wsh.graphics.endStroke();
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(6.5 * scale, 7.2 * scale);
wsh.graphics.lineTo(6.6 * scale, 7.4 * scale);
wsh.graphics.endStroke();
//draw 3D walls
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(0 * scale, 0 * scale);
wsh.graphics.lineTo(.5 * scale, .5 * scale);
wsh.graphics.endStroke();
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(0 * scale, 10 * scale);
wsh.graphics.lineTo(.5 * scale, 9.5 * scale);
wsh.graphics.endStroke();
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(10 * scale, 10 * scale);
wsh.graphics.lineTo(9.5 * scale, 9.5 * scale);
wsh.graphics.endStroke();
wsh.graphics.beginStroke("black");
wsh.graphics.moveTo(10 * scale, 0 * scale);
wsh.graphics.lineTo(9.5 * scale, .5 * scale);
wsh.graphics.endStroke();
stage.addChild(wsh);
//erase bits that overlap
var fix = new createjs.Shape();
fix.graphics.beginStroke("white");
fix.graphics.setStrokeStyle(2);
fix.graphics.moveTo(25, 261);
fix.graphics.lineTo(25, 269);
fix.graphics.endStroke();
fix.graphics.beginStroke("white");
fix.graphics.setStrokeStyle(2);
fix.graphics.moveTo(475, 361);
fix.graphics.lineTo(475, 369);
fix.graphics.endStroke();
stage.addChild(fix);
}