1846c3e421350df747f929a7dc2dbf4aa5530606
5 return b2Vec2
.call(this, a
[0] || a
.x
|| 0.0, a
[1] || a
.y
|| 0.0);
11 yf
.ipairs(function (k
, v
) { Box2D
[k
] = Box2D
[v
]; }, {
12 DYNAMIC_BODY
: 'b2_dynamicBody'
15 function camelizeCase (s
) {
16 return s
.replace(/_([a-z])/g, function (m
) {
17 return m
[1].toUpperCase();
21 function wrapEmscriptenType (T
) {
23 var keys
= Object
.keys(P
);
24 var members
= keys
.filter(function (k
) { return k
.startsWith("get_"); });
25 var accessors
= keys
.filter(function (k
) { return k
.startsWith("Get"); });
26 members
.forEach(function (k
) {
27 var name
= camelizeCase(k
.slice(4));
28 if (!name
|| name
in P
) return;
29 Object
.defineProperty(P
, name
, {
30 get: P
[k
], set: P
['s' + k
.slice(1)]
34 accessors
.forEach(function (k
) {
35 var name
= k
.slice(3);
36 name
= name
[0].toLowerCase() + name
.slice(1);
37 if (!name
|| name
in P
) return;
38 Object
.defineProperty(P
, name
, {
39 get: P
[k
], set: P
['S' + k
.slice(1)]
45 yf
.each(wrapEmscriptenType
, [
46 Box2D
.b2Vec2
, Box2D
.b2BodyDef
, Box2D
.b2Body
,
47 Box2D
.b2JointDef
, Box2D
.b2RevoluteJointDef
,
48 Box2D
.b2Joint
, Box2D
.b2DistanceJoint
, Box2D
.b2RevoluteJoint
,
51 yT
.defineProperties(Box2D
.b2Vec2
.prototype, {
59 var BodyC
= yT(yuu
.C
, {
62 constructor: function (body
, size
) {
64 this._matrix
= mat4
.create();
67 position
: { alias
: 'body.position' },
68 angle
: { alias
: 'body.angle' },
69 linearVelocity
: { alias
: 'body.linearVelocity' },
70 ApplyForce
: { proxy
: 'body.ApplyForce' },
72 matrix
: { get: function () {
73 var mat
= this._matrix
;
75 var pos
= this.body
.position
;
76 mat4
.translate(mat
, mat
, [pos
.x
, pos
.y
, 0]);
77 mat4
.rotateZ(mat
, mat
, this.body
.angle
);
82 var PlayerController
= yT(yuu
.C
, {
83 constructor: function (body
, left
, right
, leftJoint
, rightJoint
) {
87 this.leftJoint
= leftJoint
;
88 this.rightJoint
= rightJoint
;
89 this.dleftLeft
= this.dleftRight
=
90 this.drightLeft
= this.drightRight
= 0;
94 dleftLeft
: yuu
.propcmd(this, 'dleftLeft'),
95 dleftRight
: yuu
.propcmd(this, 'dleftRight'),
96 drightLeft
: yuu
.propcmd(this, 'drightLeft'),
97 drightRight
: yuu
.propcmd(this, 'drightRight'),
98 up
: yuu
.propcmd(this, 'up'),
99 free
: yuu
.propcmd(this, 'free'),
103 _updatePivots: function () {
105 this.leftJoint
.motorSpeed
=
106 (this.dleftRight
- this.dleftLeft
) * PIVOT_SPEED
;
107 this.rightJoint
.motorSpeed
=
108 (this.drightRight
- this.drightLeft
) * PIVOT_SPEED
;
111 _updateTransforms: function () {
115 this._updatePivots();
117 /* var DRAG_FREE = 0.01;
120 var CORRECTION = 1;*/
122 var leftAngle
= this.leftJoint
.GetJointAngle();
123 var rightAngle
= this.rightJoint
.GetJointAngle();
124 var thrust
= +!this.free
* +this.up
* THRUST
;
125 var leftThrust
= new b2Vec2(
126 Math
.sin(leftAngle
) * thrust
, Math
.cos(leftAngle
) * thrust
);
127 var rightThrust
= new b2Vec2(
128 Math
.sin(rightAngle
) * thrust
, Math
.cos(rightAngle
) * thrust
);
129 this.body
.body
.ApplyForceToCenter(leftThrust
);
130 this.body
.body
.ApplyForceToCenter(rightThrust
);
132 var cleft
= Math
.cos(leftAngle
);
133 var cright
= Math
.cos(rightAngle
);
134 var sleft
= Math
.sin(leftAngle
);
135 var sright
= Math
.sin(rightAngle
);
137 var ax
= thrust
* (cleft
- cright
);
138 var ay
= thrust
* (sright
+ sleft
);
141 var v = this.body.linearVelocity;
142 var drag = this.up ? DRAG_OPEN : this.free ? DRAG_FREE : DRAG_LOCK;
143 ax += drag * Math.max(cleft, cright) * v.x * v.x * -Math.sign(v.x);
144 ay += drag * (sleft + sright) * v.y * v.y * -Math.sign(v.y);
146 if (!this.up || this.free)
147 ax += CORRECTION * (cleft - cright) * v.y * v.y * Math.sign(v.y);
150 this.body
.ApplyForce(new b2Vec2(ax
, ay
), this.body
.position
);
152 this._updateTransforms();
158 function bodyFromAABB (world
, position
, aabb
, density
, center
) {
159 var dfn
= new Box2D
.b2BodyDef();
160 var shape
= new Box2D
.b2PolygonShape();
162 shape
.SetAsBox(aabb
.hw
, aabb
.hh
, new b2Vec2(center
), 0);
164 shape
.SetAsBox(aabb
.hw
, aabb
.hh
);
165 if (density
!== undefined)
166 dfn
.type
= Box2D
.DYNAMIC_BODY
;
167 dfn
.position
= new b2Vec2(position
[0], position
[1]);
168 var body
= world
.CreateBody(dfn
);
169 body
.CreateFixture(shape
, density
|| 0.0001);
173 function bodyFromLine (world
, p0
, p1
) {
174 var dfn
= new Box2D
.b2BodyDef();
175 var shape
= new Box2D
.b2EdgeShape();
176 shape
.Set(new b2Vec2(p0
), new b2Vec2(p1
));
177 var body
= world
.CreateBody(dfn
);
178 body
.CreateFixture(shape
, 0);
182 function pinJoint (world
, bodyA
, bodyB
, anchor
) {
183 var dfn
= new Box2D
.b2RevoluteJointDef();
184 dfn
.Initialize(bodyA
, bodyB
, new b2Vec2(anchor
));
185 dfn
.maxMotorTorque
= 100.0;
186 dfn
.motorSpeed
= 0.1;
187 dfn
.enableMotor
= true;
188 dfn
.enableLimit
= true;
189 return Box2D
.castObject(world
.CreateJoint(dfn
), Box2D
.b2RevoluteJoint
);
192 var GameScene
= yT(yuu
.Scene
, {
193 constructor: function () {
194 yuu
.Scene
.call(this);
198 zoom
* -1.3333333333/2, zoom
* -0.2, zoom
* 1.3333333333, zoom
* 1);
200 var world
= new Box2D
.b2World(new b2Vec2(0, -5));
202 var body
, left
, right
;
203 this.player
= new yuu
.E(
204 body
= new BodyC(bodyFromAABB(
205 world
, [0, 5], new yuu
.AABB(0.89, 1.0), 1.0)),
206 new yuu
.QuadC('@player')
207 .setSize([0.89, 1.0])
210 var leftWing
= new yuu
.E(
211 left
= new BodyC(bodyFromAABB(
212 world
, [-0.275, 5.15], new yuu
.AABB(0.45, 0.22), 0,
214 new yuu
.QuadC('@left')
215 .setAnchorAtPosition("right")
217 .setSize([0.45, 0.22]));
218 var leftJoint
= pinJoint(world
, left
.body
, body
.body
, [0.1, 5.15]);
219 var rightWing
= new yuu
.E(right
= new BodyC(
220 bodyFromAABB(world
, [0.50, 5.15], new yuu
.AABB(0.45, 0.22), 0)),
221 new yuu
.QuadC('@right')
223 .setSize([0.45, 0.22]));
224 var rightJoint
= pinJoint(world
, right
.body
, body
.body
, [0.1, 5.15]);
225 this.player
.addChildren(leftWing
, rightWing
);
226 this.entity0
.addChild(this.player
);
227 leftJoint
.SetLimits(0, Math
.PI
/ 2);
228 rightJoint
.SetLimits(-Math
.PI
/ 2, 0);
230 var ground
= new yuu
.E(
231 new BodyC(bodyFromLine(world
, [-100, 0], [100, 0])),
233 .setAnchorAtPosition('top')
235 .setColor([0, 0.5, 0, 1]));
236 this.entity0
.addChild(ground
);
239 this.controller
= new PlayerController(body
, left
, right
,
240 leftJoint
, rightJoint
));
241 Object
.assign(this.commands
, this.controller
.commands
);
243 this.entity0
.attach(new yuu
.Ticker(function () {
244 world
.Step(1/60, 8, 8);
248 this.ready
= yuu
.ready([
249 new yuu
.Material('@player'),
250 new yuu
.Material('@left'),
251 new yuu
.Material('@right')]);
255 var audio0
= new Audio();
256 audio0
.src
= audio0
.canPlayType('audio/ogg')
257 ? "data/sound/starting-line.ogg"
258 : "data/sound/starting-line.mp3";
259 audio0
.autoplay
= true;
261 document
.body
.appendChild(audio0
);
262 var source
= yuu
.audio
.createMediaElementSource(audio0
);
263 source
.connect(yuu
.audio
.music
);
279 yuu
.director
.start();
283 storage
= ystorage
.getStorage();
284 yuu
.audio
.storage
= storage
;
285 var game
= new GameScene();
286 yuu
.director
.pushScene(game
);
290 window
.addEventListener("load", function() {
291 yuu
.registerInitHook(load
);
292 yuu
.init({ backgroundColor
: [0, 0, 0, 1], antialias
: false })