ecff045199e696e668da38e7b431df441ce53f0a
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 linearVelocity
: { alias
: 'body.linearVelocity' },
69 ApplyForce
: { proxy
: 'body.ApplyForce' },
71 matrix
: { get: function () {
72 var mat
= this._matrix
;
74 var pos
= this.body
.position
;
75 mat4
.translate(mat
, mat
, [pos
.x
, pos
.y
, 0]);
76 mat4
.rotateZ(mat
, mat
, this.body
.angle
);
81 var PlayerController
= yT(yuu
.C
, {
82 constructor: function (body
, left
, right
, leftJoint
, rightJoint
) {
86 this.leftJoint
= leftJoint
;
87 this.rightJoint
= rightJoint
;
88 this.dleftLeft
= this.dleftRight
=
89 this.drightLeft
= this.drightRight
= 0;
95 dleftLeft
: yuu
.propcmd(this, 'dleftLeft'),
96 dleftRight
: yuu
.propcmd(this, 'dleftRight'),
97 drightLeft
: yuu
.propcmd(this, 'drightLeft'),
98 drightRight
: yuu
.propcmd(this, 'drightRight'),
99 up
: yuu
.propcmd(this, 'up'),
100 free
: yuu
.propcmd(this, 'free'),
104 _updatePivots: function () {
105 var PIVOT_SPEED
= 0.05;
106 var leftSpeed
= (this.dleftRight
- this.dleftLeft
) * PIVOT_SPEED
;
107 var rightSpeed
= (this.drightLeft
- this.drightRight
) * PIVOT_SPEED
;
108 this.leftPivot
= yf
.clamp(this.leftPivot
+ leftSpeed
, 0, 1);
109 this.rightPivot
= yf
.clamp(this.rightPivot
+ rightSpeed
, 0, 1);
112 _updateTransforms: function () {
114 var leftTarget
= this.leftPivot
* Math
.PI
/ 2;
115 var rightTarget
= -this.rightPivot
* Math
.PI
/ 2;
116 var leftError
= this.leftJoint
.jointAngle
- leftTarget
;
117 var rightError
= this.rightJoint
.jointAngle
- rightTarget
;
118 this.leftJoint
.motorSpeed
= -gain
* leftError
;
119 this.rightJoint
.motorSpeed
= -gain
* rightError
;
123 this._updatePivots();
125 var DRAG_FREE
= 0.01;
130 var leftAngle
= (1 - this.leftPivot
) * Math
.PI
/ 2;
131 var rightAngle
= (1 - this.rightPivot
) * Math
.PI
/ 2;
133 var cleft
= Math
.cos(leftAngle
);
134 var cright
= Math
.cos(rightAngle
);
135 var sleft
= Math
.sin(leftAngle
);
136 var sright
= Math
.sin(rightAngle
);
138 var thrust
= +!this.free
* +this.up
* THRUST
;
139 var ax
= thrust
* (cleft
- cright
);
140 var ay
= thrust
* (sright
+ sleft
);
142 var v
= this.body
.linearVelocity
;
143 var drag
= this.up
? DRAG_OPEN
: this.free
? DRAG_FREE
: DRAG_LOCK
;
144 ax
+= drag
* Math
.max(cleft
, cright
) * v
.x
* v
.x
* -Math
.sign(v
.x
);
145 ay
+= drag
* (sleft
+ sright
) * v
.y
* v
.y
* -Math
.sign(v
.y
);
147 if (!this.up
|| this.free
)
148 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
) {
159 var bd
= new Box2D
.b2BodyDef();
160 var shape
= new Box2D
.b2PolygonShape();
161 shape
.SetAsBox(aabb
.hw
, aabb
.hh
);
163 bd
.type
= Box2D
.DYNAMIC_BODY
;
164 bd
.position
= new b2Vec2(position
[0], position
[1]);
165 var body
= world
.CreateBody(bd
);
166 body
.CreateFixture(shape
, density
|| 0);
170 function bodyFromLine (world
, p0
, p1
) {
171 var bd
= new Box2D
.b2BodyDef();
172 var shape
= new Box2D
.b2EdgeShape();
173 shape
.Set(new b2Vec2(p0
), new b2Vec2(p1
));
174 var body
= world
.CreateBody(bd
);
175 body
.CreateFixture(shape
, 0);
179 function pinJoint (world
, bodyA
, bodyB
, anchor
) {
180 var dfn
= new Box2D
.b2RevoluteJointDef();
181 dfn
.Initialize(bodyA
, bodyB
, new b2Vec2(anchor
));
182 dfn
.maxMotorTorque
= 10.0;
183 dfn
.motorSpeed
= 0.0;
184 dfn
.enableMotor
= true;
185 return Box2D
.castObject(world
.CreateJoint(dfn
), Box2D
.b2RevoluteJoint
);
188 var GameScene
= yT(yuu
.Scene
, {
189 constructor: function () {
190 yuu
.Scene
.call(this);
194 zoom
* -1.3333333333/2, zoom
* -0.2, zoom
* 1.3333333333, zoom
* 1);
196 var world
= new Box2D
.b2World(new b2Vec2(0, -5));
198 var body
, left
, right
;
199 this.player
= new yuu
.E(
200 body
= new BodyC(bodyFromAABB(
201 world
, [0, 5], new yuu
.AABB(0.89, 1.0), 1.0)),
202 new yuu
.QuadC('@player')
203 .setSize([0.89, 1.0])
206 var leftWing
= new yuu
.E(left
= new BodyC(
207 bodyFromAABB(world
, [-0.50, 5.15], new yuu
.AABB(0.45, 0.22), 1.0)),
208 new yuu
.QuadC('@left')
210 .setSize([0.45, 0.22]));
211 var leftJoint
= pinJoint(world
, left
.body
, body
.body
, [-0.1, 5.15]);
212 var rightWing
= new yuu
.E(right
= new BodyC(
213 bodyFromAABB(world
, [0.50, 5.15], new yuu
.AABB(0.45, 0.22), 1.0)),
214 new yuu
.QuadC('@right')
216 .setSize([0.45, 0.22]));
217 var rightJoint
= pinJoint(world
, right
.body
, body
.body
, [0.1, 5.15]);
218 this.player
.addChildren(leftWing
, rightWing
);
219 this.entity0
.addChild(this.player
);
221 var ground
= new yuu
.E(
222 new BodyC(bodyFromLine(world
, [-100, 0], [100, 0])),
224 .setAnchorAtPosition('top')
226 .setColor([0, 0.5, 0, 1]));
227 this.entity0
.addChild(ground
);
230 this.controller
= new PlayerController(body
, left
, right
,
231 leftJoint
, rightJoint
));
232 Object
.assign(this.commands
, this.controller
.commands
);
234 this.entity0
.attach(new yuu
.Ticker(function () {
235 world
.Step(1/60, 8, 8);
239 this.ready
= yuu
.ready([
240 new yuu
.Material('@player'),
241 new yuu
.Material('@left'),
242 new yuu
.Material('@right')]);
246 var audio0
= new Audio();
247 audio0
.src
= audio0
.canPlayType('audio/ogg')
248 ? "data/sound/starting-line.ogg"
249 : "data/sound/starting-line.mp3";
250 audio0
.autoplay
= true;
252 document
.body
.appendChild(audio0
);
253 var source
= yuu
.audio
.createMediaElementSource(audio0
);
254 source
.connect(yuu
.audio
.music
);
270 yuu
.director
.start();
274 storage
= ystorage
.getStorage();
275 yuu
.audio
.storage
= storage
;
276 var game
= new GameScene();
277 yuu
.director
.pushScene(game
);
281 window
.addEventListener("load", function() {
282 yuu
.registerInitHook(load
);
283 yuu
.init({ backgroundColor
: [0, 0, 0, 1], antialias
: false })