-
Notifications
You must be signed in to change notification settings - Fork 2
/
gc.js
473 lines (424 loc) · 13.9 KB
/
gc.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
enchant();
//// var
var game;
var game
var T = {
NERD : 0,
PREP : 1,
OTHER : 2
};
var gomi; // gomibako
var jock; // jock man
var chars = []; // preps and nerds
var dist; // distination for preps
var score;
var born; // counter for born
var scorelabel;
var roomlabel;
var gameoverlabel;
var infolabel;
var baseBornCount = 40;
var scorePerOne = 10;
var minBornCount = 20;
var baseNerdizePos = 500;
var NerdizePerOne = 10;
var minNerdizePos = 250;
var CHARMAX = 24;
var CHARINIT = 10;
var G_FPS = 20;
var G_WIDTH = 420; // window size
var G_HEIGHT = 320;
var S_WIDTH = 350;
var S_HEIGHT = 320;
var P_WIDTH = 22;
var P_HEIGHT = 52;
//// class
function Chara(w,h,x,y){
Sprite.call(this,w,h)
this.x = x;
this.y = y;
this.rad = 0;
this.count = 0;
this.iswait = true;
this.type = T.OTHER;
}
Chara.prototype = Object.create(Sprite.prototype);
//Chara.prototype.constructor = Chara;
function Prep(x,y){
Chara.call(this,P_WIDTH,P_HEIGHT,x,y);
//this.image = game.assets["img/prep.png"];
this.image = game.assets["img/jock.png"];
this.count = Math.random() * game.fps;
this.drag = false;
this.type = T.PREP;
// each frame
this.addEventListener(enchant.Event.ENTER_FRAME, function(){
if(this.drag) {
return;
}
if(Math.random() * nerdizePos() < 1){
var tmpscene = this.parentNode;
this.parentNode.removeChild(this);
Nerd.call(this,this.x,this.y);
tmpscene.addChild(this);
return;
}
if(this.iswait) { //wait
this.count -= 1;
if(this.count < 0){ // init for move
this.iswait = false;
var toX = (Math.random()-0.5) * 120 + dist.x - P_WIDTH/2;
var toY = (Math.random()-0.5) * 120 + dist.y - P_HEIGHT/2;
this.rad = Math.atan2(toY-this.y, toX-this.x);
this.count = Math.sqrt((toX-this.x)*(toX-this.x) +
(toY-this.y)*(toY-this.y));
}
return;
} else { // move
this.moveBy(Math.cos(this.rad)*2, Math.sin(this.rad)*2);
this.count -= 2;
if(this.count < 0){ // init for wait
this.iswait = true;
this.count = Math.random() * game.fps;
}
return;
}
});
this.addEventListener(enchant.Event.TOUCH_START, function(e){
this.deltaX = e.x - this.x;
this.deltaY = e.y - this.y;
this.drag = true;
});
this.addEventListener(enchant.Event.TOUCH_MOVE, function(e){
this.x = e.x - this.deltaX;
this.y = e.y - this.deltaY;
});
this.addEventListener(enchant.Event.TOUCH_END, function(e){
if(this.intersect(gomi)){
this.parentNode.removeChild(this);
chars.splice(chars.indexOf(this),1);
game.pushScene(createGameoverScene("有用な人材を除去しました"));
}
// wait then move
this.iswait = false;
this.count = -1;
this.drag = false;
});
}
Prep.prototype = Object.create(Chara.prototype);
//Prep.prototype.constructor = Prep;
function Nerd(x,y){
Chara.call(this,P_WIDTH,P_HEIGHT,x,y);
this.image = this.image = game.assets["img/nerd.png"];
this.count = Math.random() * game.fps;
this.drag = false;
this.atedge = false;
this.horiz = false;
this.type = T.NERD;
// each frame
this.addEventListener(enchant.Event.ENTER_FRAME, function(){
if(this.drag) {
return;
}
if(this.iswait) { //wait
this.count -= 1;
if(this.count < 0){ // init for move
this.iswait = false;
if(!this.atedge){
var toX = (Math.random()-0.5) * 50 + jock.x;
var toY = (Math.random()-0.5) * 50 + jock.y;
this.rad = Math.atan2(this.y-toY, this.x-toX);
this.count = Math.sqrt((toX-this.x)*(toX-this.x) +
(toY-this.y)*(toY-this.y));
if(this.count < 20){
this.count += 20;
}
} else { // at edge
if(this.horiz){
if(Math.random() < 0.5)
this.rad = 0;
else
this.rad = Math.PI;
} else {
if(Math.random() < 0.5)
this.rad = Math.PI/2;
else
this.rad = -Math.PI/2;
}
this.count = Math.random()*40+40;
}
}
return;
} else { // move
this.moveBy(Math.cos(this.rad)*2, Math.sin(this.rad)*2);
if(!this.atedge){
if(this.x < 10 ||
this.x + this.width > S_WIDTH - 10){
this.atedge = true;
this.horiz = false;
this.count = 0;
}
if(this.y < 10 ||
this.y + this.height > S_HEIGHT - 30){
this.atedge = true;
this.horiz = true;
this.count = 0;
}
} else {
if(this.horiz){
if(this.x < 10){
this.x = 10;
this.rad = 0;
}
if(this.x + this.width > S_WIDTH - 10){
this.x = S_WIDTH - this.width - 10;
this.rad = Math.PI;
}
} else {
if(this.y < 10){
this.y = 10;
this.rad = -Math.PI/2;
}
if(this.y + this.height > S_HEIGHT - 30){
this.y = S_HEIGHT - this.height - 30;
this.rad = Math.PI/2;
}
}
}
this.count -= 2;
if(this.count < 0){ // init for wait
this.iswait = true;
this.count = (Math.random() + 1) * game.fps * 1;
}
return;
}
});
this.addEventListener(enchant.Event.TOUCH_START, function(e){
this.deltaX = e.x - this.x;
this.deltaY = e.y - this.y;
this.drag = true;
});
this.addEventListener(enchant.Event.TOUCH_MOVE, function(e){
this.x = e.x - this.deltaX;
this.y = e.y - this.deltaY;
});
this.addEventListener(enchant.Event.TOUCH_END, function(e){
if(this.intersect(gomi)){
this.parentNode.removeChild(this);
score += scorePerOne;
scorelabel.text = "score: " + score;
infolabel.text = infotext();
chars.splice(chars.indexOf(this),1);
}
// wait then move
if(this.x > S_WIDTH - this.width + 5){
this.iswait = false;
this.count = -1;
this.atedge = true;
this.horiz = false;
} else {
this.iswait = false;
this.count = -1;
this.atedge = false;
}
this.drag = false;
});
}
Nerd.prototype = Object.create(Chara.prototype);
//Nerd.prototype.constructor = Nerd;
function Jockman(){
Chara.call(this,P_WIDTH,P_HEIGHT,100,100);
//this.image = game.assets["img/jock.png"];
this.count = Math.random() * game.fps;
this.addEventListener(enchant.Event.ENTER_FRAME, function(){
if(this.iswait) { //wait
this.count -= 1;
if(this.count < 0){ // init for move
this.iswait = false;
var ps = chars.filter(function(elem){
return elem.type == T.PREP;
});
var toX = 0.0; var toY = 0.0;
for(var i=0; i<ps.length; i++){
toX += ps[i].x;
toY += ps[i].y;
}
toX /= ps.length; toY /= ps.length;
toX += (Math.random()-0.5) * 40;
toY += (Math.random()-0.5) * 40;
this.rad = Math.atan2(toY-this.y, toX-this.x);
this.count = Math.sqrt((toX-this.x)*(toX-this.x) +
(toY-this.y)*(toY-this.y));
}
return;
} else { // move
this.moveBy(Math.cos(this.rad)*2, Math.sin(this.rad)*2);
this.count -= 2;
if(this.count < 0){ // init for wait
this.iswait = true;
this.count = Math.random() * game.fps;
}
return;
}
});
}
Jockman.prototype = Object.create(Chara.prototype);
//Jockman.prototype.constructor = Jockman; //??
function Distination(){
Chara.call(this,10,10,S_WIDTH/2,S_HEIGHT/2);
this.count = Math.random() * game.fps;
this.addEventListener(enchant.Event.ENTER_FRAME, function(){
if(this.iswait) { //wait
this.count -= 1;
if(this.count < 0){ // init for move
this.iswait = false;
var toX = (Math.random()-0.5) * 100 + S_WIDTH/2;
var toY = (Math.random()-0.5) * 150 + S_HEIGHT/2;
this.rad = Math.atan2(toY-this.y, toX-this.x);
this.count = Math.sqrt((toX-this.x)*(toX-this.x) +
(toY-this.y)*(toY-this.y));
}
return;
} else { // move
this.moveBy(Math.cos(this.rad)*2, Math.sin(this.rad)*2);
this.count -= 2;
if(this.count < 0){ // init for wait
this.iswait = true;
this.count = Math.random() * game.fps;
}
return;
}
});
}
Distination.prototype = Object.create(Chara.prototype);
//Distination.prototype.constructor = Distination; //??
function Gomibako(){
Chara.call(this,60,55,G_WIDTH-70,G_HEIGHT-80);
this.image = game.assets["img/gomi.png"];
}
Gomibako.prototype = Object.create(Chara.prototype);
//Gomibako.prototype.constructor = Gomibako; //??
//// func
function bornCount(){
return Math.max(minBornCount, baseBornCount - score/scorePerOne);
}
function nerdizePos(){
return Math.max(minNerdizePos, baseNerdizePos - score);
}
function infotext(){
return createName() + " : " + createInfo();
}
//// scene
function createTitleScene(){
var scene = new Scene;
var title = new Label("Garbage Collection");
title.font = "30px cursive";
title.x = G_WIDTH/2 - 130;
title.y = 80;
scene.addChild(title);
var button = new Sprite(100,70);
button.image = game.assets["img/startbutton.png"];
button.x = G_WIDTH/2 - button.width/2;
button.y = 130 + title.height;
scene.addChild(button);
button.addEventListener(enchant.Event.TOUCH_START, function(){
game.popScene();
game.pushScene(createGameScene());
//game.replaceScene(createGameScene());
});
return scene;
};
function createGameScene(){
var scene = new Scene;
// char ini
jock = new Jockman();
scene.addChild(jock);
dist = new Distination();
scene.addChild(dist);
chars = [];
for(var i=0; i<CHARINIT; i++){
chars.push(new Prep(Math.random()*(S_WIDTH -P_WIDTH),
Math.random()*(S_HEIGHT-P_HEIGHT)));
scene.addChild(chars[chars.length-1]);
}
// score init
score = 0;
scorelabel = new Label("score: " + score);
scorelabel.x = G_WIDTH-80;
scorelabel.y = G_HEIGHT-17;
scene.addChild(scorelabel);
// label init
var social = new Label("社会資源");
social.x = G_WIDTH-70;
social.y = G_HEIGHT-130;
scene.addChild(social);
roomlabel = new Label(CHARMAX-chars.length);
roomlabel.x = G_WIDTH-30;
roomlabel.y = G_HEIGHT-110;
scene.addChild(roomlabel);
infolabel = new Label("");
infolabel.font = "13px cursive";
infolabel.x = 10;
infolabel.y = G_HEIGHT - 17;
scene.addChild(infolabel);
// frame
born = 60;
scene.addEventListener(enchant.Event.ENTER_FRAME, function(){
// label
roomlabel.text = CHARMAX-chars.length;
// new comer
if(born > 0) {
born -= 1;
} else {
if(chars.length >= CHARMAX){
game.pushScene(createGameoverScene("社会資源が枯渇しました"));
}
chars.push(new Prep(G_WIDTH-40, 10));
scene.addChild(chars[chars.length-1]);
born = bornCount();
}
});
return scene;
};
function createGameoverScene(str){
var scene = new Scene;
var logo = new Label("Game Over");
logo.font = "30px cursive";
logo.x = G_WIDTH/2 - 130;
logo.y = 80;
scene.addChild(logo);
var tex = new Label(str);
tex.font = "15px cursive";
tex.x = G_WIDTH/2 - 50;
tex.y = 120;
scene.addChild(tex);
var button = new Sprite(100,70);
button.image = game.assets["img/startbutton.png"];
button.x = 20;
button.y = G_HEIGHT - button.height - 20;
scene.addChild(button);
button.addEventListener(enchant.Event.TOUCH_START, function(){
game.popScene();
game.popScene();
game.pushScene(createGameScene());
//game.replaceScene(createGameScene());
});
var text = "#ガベこれ で" + score + "点を記録しました";
document.getElementById('tweetlink').href = "https://twitter.com/share?text=" + encodeURIComponent(text);
return scene;
}
//// main
window.onload = function(){
game = new Game(G_WIDTH,G_HEIGHT);
game.fps = G_FPS;
game.preload("img/prep.png","img/jock.png","img/nerd.png");
game.preload("img/gomi.png","img/startbutton.png");
game.onload = function(){
game.rootScene.backgroundColor = "#ffaaaa";
// gomibako
gomi = new Gomibako();
game.rootScene.addChild(gomi);
game.replaceScene(createTitleScene());
};
game.start();
};