Skip to content

Commit 6620dff

Browse files
committed
update game left-or-right
1 parent aac8903 commit 6620dff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+24981
-0
lines changed

Left-or-right/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Left or right? That is the question!
2+
============
3+
4+
In busy season, the correct scheduling of trains is not easy, you must prevent them from collision.
5+
Each train has its own destination which marked by the solid circle with same color.
6+
Click branches marked by green rectangles to control the track of trains.
7+
8+
Getting Started
9+
------------
10+
Click [here](http://cnnzp.github.com/left-or-right) to play.
11+
Or clone https://github.com/cnnzp/game-off-2012.git and open index.html to play.
12+
13+
Make sure your browser support html5 canvas.
14+
We recommend high version chrome, safari and opera. Firefox 16.0 is ok, but version 17.0 has some font problems.
15+
IE is not supported.
16+
17+
Screenshot
18+
------------
19+
Oh! Collision!
20+
21+
![Oh!Collision](http://github.com/cnnzp/game-off-2012/raw/master/screenshot/1.png)
22+
23+
Yeah! Here we are!
24+
25+
![Yeah!Here we are!](http://github.com/cnnzp/game-off-2012/raw/master/screenshot/2.png)
26+
27+
Used open source projects:
28+
------------
29+
* Colorbox: a html5 game engine which will be officially opened soon.
30+
* Cocos2d-html: just geometry.js and distributing tool.
31+
32+
License:
33+
------------
34+
"Left or Right?" game is licensed under the terms of the MIT license.

Left-or-right/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var bearcat = require('bearcat');
2+
3+
var contextPath = require.resolve('./context.json');
4+
5+
global.bearcat = bearcat;
6+
bearcat.createApp([contextPath]);
7+
bearcat.start(function() {
8+
var car = bearcat.getBean('car');
9+
car.run();
10+
});

Left-or-right/app/collide.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
var Collide = function() {
2+
this.$id = "collide";
3+
this.$colorBox = null;
4+
this.$init = "init";
5+
this.debug = null;
6+
this.geo = null;
7+
this.trains = [];
8+
this.props = [];
9+
};
10+
11+
Collide.prototype.init = function() {
12+
this.debug = this.$colorBox.debug;
13+
this.geo = this.$colorBox.geo;
14+
}
15+
16+
Collide.prototype.addTrain = function(train) {
17+
this.debug.assert(this.trains.indexOf(train) == -1, "add same train");
18+
19+
this.trains.push(train);
20+
};
21+
22+
Collide.prototype.rmTrain = function(train) {
23+
this.debug.assert(train == undefined || this.trains.indexOf(train) != -1, "remove non exist train");
24+
25+
if (undefined == train)
26+
this.trains.length = 0;
27+
28+
var idx = this.trains.indexOf(train);
29+
this.trains.splice(idx, 1);
30+
};
31+
32+
Collide.prototype.addProp = function(prop) {
33+
this.debug.assert(this.props.indexOf(prop) == -1, "add same prop");
34+
35+
this.props.push(prop);
36+
};
37+
38+
Collide.prototype.rmProp = function(prop) {
39+
this.debug.assert(this.props.indexOf(prop) != -1, "remove non exist prop");
40+
41+
if (undefined == prop)
42+
this.props.lenght = 0;
43+
44+
var idx = this.props.indexOf(prop);
45+
this.props.splice(idx, 1);
46+
};
47+
48+
Collide.prototype.isTrainCollide = function(t1, t2, painter) {
49+
var geo = this.geo;
50+
var bboxes1 = painter.exec("bbox", t1.exec("model"));
51+
var bboxes2 = painter.exec("bbox", t2.exec("model"));
52+
53+
return bboxes1.some(function(bbox1) {
54+
return bboxes2.some(function(bbox2) {
55+
return geo.rectOverlapsRect(bbox1, bbox2);
56+
});
57+
});
58+
};
59+
60+
Collide.prototype.isTrainEatProp = function(train, prop, painter) {
61+
var geo = this.geo;
62+
var trainbbox = painter.exec("bbox", train.exec("model"));
63+
var propbbox = painter.exec("bbox", prop.exec("model"));
64+
65+
return trainbbox.some(function(tb) {
66+
return geo.rectOverlapsRect(tb, propbbox);
67+
});
68+
};
69+
70+
Collide.prototype.resolve = function() {
71+
var collide = this;
72+
var d = this.$colorBox.director;
73+
74+
var painter = d.director().exec("defaultPainter");
75+
76+
//用来统计哪些train碰撞了train
77+
var diedTrains = [];
78+
diedTrains.length = this.trains.length;
79+
80+
//collide train with train
81+
for (var i = 0; i < this.trains.length - 1; i++) {
82+
var train1 = this.trains[i];
83+
84+
for (var j = i + 1; j < this.trains.length; j++) {
85+
var train2 = this.trains[j];
86+
87+
if (this.isTrainCollide(train1, train2, painter)) {
88+
diedTrains[i] = true;
89+
diedTrains[j] = true;
90+
}
91+
}
92+
}
93+
94+
var liveTrains = [],
95+
dieds = [];
96+
97+
diedTrains.forEach(function(bDied, idx) {
98+
if (bDied) {
99+
//collide.trains[idx].exec("die");
100+
dieds.push(collide.trains[idx]);
101+
} else
102+
liveTrains.push(collide.trains[idx]);
103+
});
104+
dieds.forEach(function(die) {
105+
die.exec("die");
106+
});
107+
108+
//记录每个train吃了哪些prop。不在找的过程中通知主要是考虑到prop或者train的通知函数可能同步从collide中删除自身
109+
var self = this;
110+
var eatedProps = [];
111+
liveTrains.forEach(function(train, idx) {
112+
collide.props.forEach(function(prop) {
113+
if (self.isTrainEatProp(train, prop)) {
114+
if (eatedProps[idx] == undefined)
115+
eatedProps[idx] = [prop];
116+
else
117+
eatedProps[idx].push(prop);
118+
}
119+
});
120+
});
121+
122+
liveTrains.forEach(function(train, idx) {
123+
if (eatedProps[idx]) {
124+
eatedProps[idx].forEach(function(prop) {
125+
train.exec("eatProp", prop);
126+
prop.exec("eated", train);
127+
});
128+
}
129+
});
130+
};
131+
132+
// exports.collide = new Collide();
133+
134+
bearcat.module(Collide, typeof module !== 'undefined' ? module : {});

Left-or-right/app/colorBox.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
if (typeof __resources__ === "undefined") {
2+
var __resources__ = {}
3+
}
4+
5+
__resources__["/colorBoxAdaptor.js"] = {
6+
meta: {
7+
mimetype: "application/javascript"
8+
},
9+
data: function(exports, require, module, __filename, __dirname) {
10+
var ps = require("ps");
11+
var node = require("node");
12+
var pipe = require("pipe");
13+
var level = require("level");
14+
var model = require("model");
15+
var debug = require("debug");
16+
var geo = require("geometry");
17+
var helper = require("helper");
18+
// var effect = require("effect");
19+
var animate = require("animate");
20+
var painter = require("painter");
21+
var particle = require("particle")
22+
var director = require("director");
23+
// var openlevel = require("openlevel");
24+
var Button = require("gui/button").Button;
25+
var leveltransition = require("leveltransition");
26+
27+
var ColorBox = function() {
28+
this.$id = "colorBox";
29+
this.ps = ps;
30+
this.geo = geo;
31+
this.node = node;
32+
this.pipe = pipe;
33+
this.level = level;
34+
this.model = model;
35+
this.debug = debug;
36+
this.helper = helper;
37+
// this.effect = effect;
38+
this.Button = Button;
39+
this.animate = animate;
40+
this.painter = painter;
41+
this.director = director;
42+
this.particle = particle;
43+
// this.openlevel = openlevel;
44+
this.leveltransition = leveltransition;
45+
}
46+
47+
console.log('register colorBox colorBoxAdaptor.....')
48+
bearcat.module(ColorBox, typeof module !== 'undefined' ? module : {});
49+
}
50+
}

0 commit comments

Comments
 (0)