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 accessor (T
, p
) {
16 if (p
[0].toLowerCase() === p
[0])
17 return { get: T
.prototype['get_' + p
],
18 set: T
.prototype['set_' + p
] };
20 return { get: T
.prototype['Get' + p
],
21 set: T
.prototype['Set' + p
] };
24 yT
.defineProperties(Box2D
.b2Vec2
.prototype, {
25 x
: accessor(Box2D
.b2Vec2
, 'x'),
26 y
: accessor(Box2D
.b2Vec2
, 'y'),
31 yT
.defineProperties(Box2D
.b2BodyDef
.prototype, {
32 type
: accessor(Box2D
.b2BodyDef
, 'type'),
33 position
: accessor(Box2D
.b2BodyDef
, 'position'),
36 yT
.defineProperties(Box2D
.b2Body
.prototype, {
37 position
: accessor(Box2D
.b2Body
, 'Position'),
38 linearVelocity
: accessor(Box2D
.b2Body
, 'LinearVelocity'),
39 angle
: accessor(Box2D
.b2Body
, 'Angle'),
45 var BodyC
= yT(yuu
.C
, {
48 constructor: function (body
, size
) {
50 this._matrix
= mat4
.create();
53 position
: { alias
: '_body.position' },
54 linearVelocity
: { alias
: '_body.linearVelocity' },
55 ApplyForce
: { proxy
: '_body.ApplyForce' },
57 matrix
: { get: function () {
58 var mat
= this._matrix
;
60 mat4
.rotateZ(mat
, mat
, this._body
.angle
);
61 var pos
= this._body
.position
;
62 mat4
.translate(mat
, mat
, [pos
.x
, pos
.y
, 0]);
67 var PlayerController
= yT(yuu
.C
, {
68 constructor: function (body
, left
, right
) {
72 this.dleftLeft
= this.dleftRight
=
73 this.drightLeft
= this.drightRight
= 0;
79 dleftLeft
: yuu
.propcmd(this, 'dleftLeft'),
80 dleftRight
: yuu
.propcmd(this, 'dleftRight'),
81 drightLeft
: yuu
.propcmd(this, 'drightLeft'),
82 drightRight
: yuu
.propcmd(this, 'drightRight'),
83 up
: yuu
.propcmd(this, 'up'),
84 free
: yuu
.propcmd(this, 'free'),
88 _updatePivots: function () {
89 var PIVOT_SPEED
= 0.05;
90 var leftSpeed
= (this.dleftRight
- this.dleftLeft
) * PIVOT_SPEED
;
91 var rightSpeed
= (this.drightLeft
- this.drightRight
) * PIVOT_SPEED
;
92 this.leftPivot
= yf
.clamp(this.leftPivot
+ leftSpeed
, 0, 1);
93 this.rightPivot
= yf
.clamp(this.rightPivot
+ rightSpeed
, 0, 1);
96 _updateTransforms: function () {
97 this.left
.yaw
= -this.leftPivot
* Math
.PI
/ 2;
98 this.right
.yaw
= this.rightPivot
* Math
.PI
/ 2;
102 this._updatePivots();
104 var DRAG_FREE
= 0.01;
109 var leftAngle
= (1 - this.leftPivot
) * Math
.PI
/ 2;
110 var rightAngle
= (1 - this.rightPivot
) * Math
.PI
/ 2;
112 var cleft
= Math
.cos(leftAngle
);
113 var cright
= Math
.cos(rightAngle
);
114 var sleft
= Math
.sin(leftAngle
);
115 var sright
= Math
.sin(rightAngle
);
117 var thrust
= +!this.free
* +this.up
* THRUST
;
118 var ax
= thrust
* (cleft
- cright
);
119 var ay
= thrust
* (sright
+ sleft
);
121 var v
= this.body
.linearVelocity
;
122 var drag
= this.up
? DRAG_OPEN
: this.free
? DRAG_FREE
: DRAG_LOCK
;
123 ax
+= drag
* Math
.max(cleft
, cright
) * v
.x
* v
.x
* -Math
.sign(v
.x
);
124 ay
+= drag
* (sleft
+ sright
) * v
.y
* v
.y
* -Math
.sign(v
.y
);
126 if (!this.up
|| this.free
)
127 ax
+= CORRECTION
* (cleft
- cright
) * v
.y
* v
.y
* Math
.sign(v
.y
);
129 this.body
.ApplyForce(new b2Vec2(ax
, ay
), this.body
.position
);
131 this._updateTransforms();
137 function bodyFromAABB (world
, position
, aabb
, density
) {
138 var bd
= new Box2D
.b2BodyDef();
139 var shape
= new Box2D
.b2PolygonShape();
140 shape
.SetAsBox(aabb
.w
/ 2, aabb
.h
/ 2);
142 bd
.type
= Box2D
.DYNAMIC_BODY
;
143 bd
.position
= new b2Vec2(position
[0], position
[1]);
144 var body
= world
.CreateBody(bd
);
145 body
.CreateFixture(shape
, density
|| 0);
149 function bodyFromLine (world
, p0
, p1
) {
150 var bd
= new Box2D
.b2BodyDef();
151 var shape
= new Box2D
.b2EdgeShape();
152 shape
.Set(new b2Vec2(p0
), new b2Vec2(p1
));
153 var body
= world
.CreateBody(bd
);
154 body
.CreateFixture(shape
, 0);
158 var GameScene
= yT(yuu
.Scene
, {
159 constructor: function () {
160 yuu
.Scene
.call(this);
164 zoom
* -1.3333333333/2, zoom
* -0.2, zoom
* 1.3333333333, zoom
* 1);
166 var world
= new Box2D
.b2World(new b2Vec2(0, -5));
168 var body
, left
, right
;
169 this.player
= new yuu
.E(
170 body
= new BodyC(bodyFromAABB(
171 world
, [0, 5], new yuu
.AABB(0.89, 1.0), 1.0)),
172 new yuu
.QuadC('@player')
173 .setSize([0.89, 1.0])
176 var leftWing
= new yuu
.E(left
= new yuu
.Transform()
177 .setPosition([-0.25, 0.15, 0]),
178 new yuu
.QuadC('@left')
181 .setSize([0.45, 0.22])
182 .setPosition([0, 0]));
183 var rightWing
= new yuu
.E(right
= new yuu
.Transform()
184 .setPosition([0.25, 0.15, 0]),
185 new yuu
.QuadC('@right')
188 .setSize([0.45, 0.22])
189 .setPosition([0, 0]));
190 this.player
.addChildren(leftWing
, rightWing
);
191 this.entity0
.addChild(this.player
);
193 var ground
= new yuu
.E(
194 new BodyC(bodyFromLine(world
, [-100, 0], [100, 0])),
196 .setAnchorAtPosition('top')
198 .setColor([0, 0.5, 0, 1]));
199 this.entity0
.addChild(ground
);
202 this.controller
= new PlayerController(body
, left
, right
));
203 Object
.assign(this.commands
, this.controller
.commands
);
205 this.entity0
.attach(new yuu
.Ticker(function () {
206 world
.Step(1/60, 4, 4);
210 this.ready
= yuu
.ready([
211 new yuu
.Material('@player'),
212 new yuu
.Material('@left'),
213 new yuu
.Material('@right')]);
217 var audio0
= new Audio();
218 audio0
.src
= audio0
.canPlayType('audio/ogg')
219 ? "data/sound/starting-line.ogg"
220 : "data/sound/starting-line.mp3";
221 audio0
.autoplay
= true;
223 document
.body
.appendChild(audio0
);
224 var source
= yuu
.audio
.createMediaElementSource(audio0
);
225 source
.connect(yuu
.audio
.music
);
241 yuu
.director
.start();
245 storage
= ystorage
.getStorage();
246 yuu
.audio
.storage
= storage
;
247 var game
= new GameScene();
248 yuu
.director
.pushScene(game
);
252 window
.addEventListener("load", function() {
253 yuu
.registerInitHook(load
);
254 yuu
.init({ backgroundColor
: [0, 0, 0, 1], antialias
: false })