Attach wings via physics engine (they're now... odd.)
[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 linearVelocity: { alias: 'body.linearVelocity' },
69 ApplyForce: { proxy: 'body.ApplyForce' },
70
71 matrix: { get: function () {
72 var mat = this._matrix;
73 mat4.identity(mat);
74 var pos = this.body.position;
75 mat4.translate(mat, mat, [pos.x, pos.y, 0]);
76 mat4.rotateZ(mat, mat, this.body.angle);
77 return mat;
78 } }
79 });
80
81 var PlayerController = yT(yuu.C, {
82 constructor: function (body, left, right, leftJoint, rightJoint) {
83 this.body = body;
84 this.left = left;
85 this.right = right;
86 this.leftJoint = leftJoint;
87 this.rightJoint = rightJoint;
88 this.dleftLeft = this.dleftRight =
89 this.drightLeft = this.drightRight = 0;
90 this.up = 0;
91 this.free = 0;
92 this.leftPivot = 0;
93 this.rightPivot = 0;
94 this.commands = {
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'),
101 };
102 },
103
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);
110 },
111
112 _updateTransforms: function () {
113 var gain = 1.0;
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;
120 },
121
122 tick: function () {
123 this._updatePivots();
124 var THRUST = 3.5;
125 var DRAG_FREE = 0.01;
126 var DRAG_OPEN = 0.5;
127 var DRAG_LOCK = 1;
128 var CORRECTION = 1;
129
130 var leftAngle = (1 - this.leftPivot) * Math.PI / 2;
131 var rightAngle = (1 - this.rightPivot) * Math.PI / 2;
132
133 var cleft = Math.cos(leftAngle);
134 var cright = Math.cos(rightAngle);
135 var sleft = Math.sin(leftAngle);
136 var sright = Math.sin(rightAngle);
137
138 var thrust = +!this.free * +this.up * THRUST;
139 var ax = thrust * (cleft - cright);
140 var ay = thrust * (sright + sleft);
141
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);
146
147 if (!this.up || this.free)
148 ax += CORRECTION * (cleft - cright) * v.y * v.y * Math.sign(v.y);
149
150 this.body.ApplyForce(new b2Vec2(ax, ay), this.body.position);
151
152 this._updateTransforms();
153 },
154
155 TAPS: ['tick'],
156 });
157
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);
162 if (density)
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);
167 return body;
168 }
169
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);
176 return body;
177 }
178
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);
186 }
187
188 var GameScene = yT(yuu.Scene, {
189 constructor: function () {
190 yuu.Scene.call(this);
191
192 var zoom = 10;
193 this.layer0.resize(
194 zoom * -1.3333333333/2, zoom * -0.2, zoom * 1.3333333333, zoom * 1);
195
196 var world = new Box2D.b2World(new b2Vec2(0, -5));
197
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])
204 );
205
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')
209 .setZ(-1)
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')
215 .setZ(-1)
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);
220
221 var ground = new yuu.E(
222 new BodyC(bodyFromLine(world, [-100, 0], [100, 0])),
223 new yuu.QuadC()
224 .setAnchorAtPosition('top')
225 .setSize([100, 1])
226 .setColor([0, 0.5, 0, 1]));
227 this.entity0.addChild(ground);
228
229 this.player.attach(
230 this.controller = new PlayerController(body, left, right,
231 leftJoint, rightJoint));
232 Object.assign(this.commands, this.controller.commands);
233
234 this.entity0.attach(new yuu.Ticker(function () {
235 world.Step(1/60, 8, 8);
236 return true;
237 }, 1));
238
239 this.ready = yuu.ready([
240 new yuu.Material('@player'),
241 new yuu.Material('@left'),
242 new yuu.Material('@right')]);
243 },
244
245 init: function () {
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;
251 audio0.loop = true;
252 document.body.appendChild(audio0);
253 var source = yuu.audio.createMediaElementSource(audio0);
254 source.connect(yuu.audio.music);
255 },
256
257 KEYBINDS: {
258 space: '+up',
259 up: '+up',
260 q: '+dleftLeft',
261 w: '+dleftRight',
262 o: '+drightLeft',
263 p: '+drightRight',
264 z: '+free',
265 x: '+up',
266 }
267 });
268
269 function start () {
270 yuu.director.start();
271 }
272
273 function load () {
274 storage = ystorage.getStorage();
275 yuu.audio.storage = storage;
276 var game = new GameScene();
277 yuu.director.pushScene(game);
278 return game.ready;
279 }
280
281 window.addEventListener("load", function() {
282 yuu.registerInitHook(load);
283 yuu.init({ backgroundColor: [0, 0, 0, 1], antialias: false })
284 .then(start);
285 });