Use Font Standard instead of Font Awesome.
[featherfall2.git] / src / main.js
1 "use strict";
2
3 var b2Vec2 = yf.argcd(
4 function (a) {
5 return b2Vec2.call(this, a[0] || a.x || 0.0, a[1] || a.y || 0.0);
6 },
7 Box2D.b2Vec2
8 );
9
10
11 yf.ipairs(function (k, v) { Box2D[k] = Box2D[v]; }, {
12 DYNAMIC_BODY: 'b2_dynamicBody'
13 });
14
15 function camelizeCase (s) {
16 return s.replace(/_([a-z])/g, function (m) {
17 return m[1].toUpperCase();
18 });
19 }
20
21 function wrapEmscriptenType (T) {
22 var P = T.prototype;
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)]
31 });
32
33 });
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)]
40 });
41
42 });
43 }
44
45 yf.each(wrapEmscriptenType, [
46 Box2D.b2Vec2, Box2D.b2BodyDef, Box2D.b2Body,
47 Box2D.b2JointDef, Box2D.b2RevoluteJointDef,
48 Box2D.b2Joint, Box2D.b2DistanceJoint, Box2D.b2RevoluteJoint,
49 ]);
50
51 yT.defineProperties(Box2D.b2Vec2.prototype, {
52 0: { alias: 'x' },
53 1: { alias: 'y' },
54 });
55
56
57 var storage;
58
59 var BodyC = yT(yuu.C, {
60 SLOTS: ['transform'],
61
62 constructor: function (body, size) {
63 this.body = body;
64 this._matrix = mat4.create();
65 },
66
67 position: { alias: 'body.position' },
68 angle: { alias: 'body.angle' },
69 linearVelocity: { alias: 'body.linearVelocity' },
70 ApplyForce: { proxy: 'body.ApplyForce' },
71
72 matrix: { get: function () {
73 var mat = this._matrix;
74 mat4.identity(mat);
75 var pos = this.body.position;
76 mat4.translate(mat, mat, [pos.x, pos.y, 0]);
77 mat4.rotateZ(mat, mat, this.body.angle);
78 return mat;
79 } }
80 });
81
82 var PlayerController = yT(yuu.C, {
83 constructor: function (body, left, right, leftJoint, rightJoint) {
84 this.body = body;
85 this.left = left;
86 this.right = right;
87 this.leftJoint = leftJoint;
88 this.rightJoint = rightJoint;
89 this.dleftLeft = this.dleftRight =
90 this.drightLeft = this.drightRight = 0;
91 this.up = 0;
92 this.free = 0;
93 this.commands = {
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'),
100 };
101 },
102
103 _updatePivots: function () {
104 var PIVOT_SPEED = 2;
105 this.leftJoint.motorSpeed =
106 (this.dleftRight - this.dleftLeft) * PIVOT_SPEED;
107 this.rightJoint.motorSpeed =
108 (this.drightRight - this.drightLeft) * PIVOT_SPEED;
109 },
110
111 _updateTransforms: function () {
112 },
113
114 tick: function () {
115 this._updatePivots();
116 var THRUST = 3.5;
117 /* var DRAG_FREE = 0.01;
118 var DRAG_OPEN = 0.5;
119 var DRAG_LOCK = 1;
120 var CORRECTION = 1;*/
121
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);
131
132 /*
133 var v = this.body.linearVelocity;
134 var drag = this.up ? DRAG_OPEN : this.free ? DRAG_FREE : DRAG_LOCK;
135 ax += drag * Math.max(cleft, cright) * v.x * v.x * -Math.sign(v.x);
136 ay += drag * (sleft + sright) * v.y * v.y * -Math.sign(v.y);
137
138 if (!this.up || this.free)
139 ax += CORRECTION * (cleft - cright) * v.y * v.y * Math.sign(v.y);
140 */
141
142 this._updateTransforms();
143 },
144
145 TAPS: ['tick'],
146 });
147
148 function bodyFromAABB (world, position, aabb, density, center) {
149 var dfn = new Box2D.b2BodyDef();
150 var shape = new Box2D.b2PolygonShape();
151 if (center)
152 shape.SetAsBox(aabb.hw, aabb.hh, new b2Vec2(center), 0);
153 else
154 shape.SetAsBox(aabb.hw, aabb.hh);
155 if (density !== undefined)
156 dfn.type = Box2D.DYNAMIC_BODY;
157 dfn.position = new b2Vec2(position[0], position[1]);
158 var body = world.CreateBody(dfn);
159 body.CreateFixture(shape, density || 0.0001);
160 return body;
161 }
162
163 function bodyFromLine (world, p0, p1) {
164 var dfn = new Box2D.b2BodyDef();
165 var shape = new Box2D.b2EdgeShape();
166 shape.Set(new b2Vec2(p0), new b2Vec2(p1));
167 var body = world.CreateBody(dfn);
168 body.CreateFixture(shape, 0);
169 return body;
170 }
171
172 function pinJoint (world, bodyA, bodyB, anchor) {
173 var dfn = new Box2D.b2RevoluteJointDef();
174 dfn.Initialize(bodyA, bodyB, new b2Vec2(anchor));
175 dfn.maxMotorTorque = 100.0;
176 dfn.motorSpeed = 0.1;
177 dfn.enableMotor = true;
178 return Box2D.castObject(world.CreateJoint(dfn), Box2D.b2RevoluteJoint);
179 }
180
181 var GameScene = yT(yuu.Scene, {
182 constructor: function () {
183 yuu.Scene.call(this);
184
185 var zoom = 10;
186 this.layer0.resize(
187 zoom * -1.3333333333/2, zoom * -0.2, zoom * 1.3333333333, zoom * 1);
188
189 var world = new Box2D.b2World(new b2Vec2(0, -5));
190
191 var body, left, right;
192 this.player = new yuu.E(
193 body = new BodyC(bodyFromAABB(
194 world, [0, 5], new yuu.AABB(0.89, 1.0), 1.0)),
195 new yuu.QuadC('@player')
196 .setSize([0.89, 1.0])
197 );
198
199 var leftWing = new yuu.E(
200 left = new BodyC(bodyFromAABB(
201 world, [-0.275, 5.15], new yuu.AABB(0.45, 0.22), 0,
202 [-0.45/2, 0.0])),
203 new yuu.QuadC('@left')
204 .setAnchorAtPosition("right")
205 .setZ(-1)
206 .setSize([0.45, 0.22]));
207 var leftJoint = pinJoint(world, left.body, body.body, [0.1, 5.15]);
208 var rightWing = new yuu.E(right = new BodyC(
209 bodyFromAABB(world, [0.50, 5.15], new yuu.AABB(0.45, 0.22), 0)),
210 new yuu.QuadC('@right')
211 .setZ(-1)
212 .setSize([0.45, 0.22]));
213 var rightJoint = pinJoint(world, right.body, body.body, [0.1, 5.15]);
214 this.player.addChildren(leftWing, rightWing);
215 this.entity0.addChild(this.player);
216
217 var ground = new yuu.E(
218 new BodyC(bodyFromLine(world, [-100, 0], [100, 0])),
219 new yuu.QuadC()
220 .setAnchorAtPosition('top')
221 .setSize([100, 1])
222 .setColor([0, 0.5, 0, 1]));
223 this.entity0.addChild(ground);
224
225 this.player.attach(
226 this.controller = new PlayerController(body, left, right,
227 leftJoint, rightJoint));
228 Object.assign(this.commands, this.controller.commands);
229
230 this.entity0.attach(new yuu.Ticker(function () {
231 world.Step(1/60, 8, 8);
232 return true;
233 }, 1));
234
235 this.ready = yuu.ready([
236 new yuu.Material('@player'),
237 new yuu.Material('@left'),
238 new yuu.Material('@right')]);
239 },
240
241 init: function () {
242 var audio0 = new Audio();
243 audio0.src = audio0.canPlayType('audio/ogg')
244 ? "data/sound/starting-line.ogg"
245 : "data/sound/starting-line.mp3";
246 audio0.autoplay = true;
247 audio0.loop = true;
248 document.body.appendChild(audio0);
249 var source = yuu.audio.createMediaElementSource(audio0);
250 source.connect(yuu.audio.music);
251 },
252
253 KEYBINDS: {
254 space: '+up',
255 up: '+up',
256 q: '+dleftLeft',
257 w: '+dleftRight',
258 o: '+drightLeft',
259 p: '+drightRight',
260 z: '+free',
261 x: '+up',
262 }
263 });
264
265 function start () {
266 yuu.director.start();
267 }
268
269 function load () {
270 storage = ystorage.getStorage();
271 yuu.audio.storage = storage;
272 var game = new GameScene();
273 yuu.director.pushScene(game);
274 return game.ready;
275 }
276
277 window.addEventListener("load", function() {
278 yuu.registerInitHook(load);
279 yuu.init({ backgroundColor: [0, 0, 0, 1], antialias: false })
280 .then(start);
281 });