UNPKG

lr-core

Version:
139 lines (127 loc) 3.41 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Binding = exports.FlutterPoint = exports.CollisionPoint = exports.Point = undefined; var _immo = require('../../immo'); var _immo2 = _interopRequireDefault(_immo); var _v = require('../../v2'); var _v2 = _interopRequireDefault(_v); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } let ZERO_VEC = { x: 0, y: 0 }; // @setupImmo class Point extends _immo2.default { __props__() { return { id: null, friction: 0, airFriction: 0, collidable: false, steppable: true }; } __state__() { return { pos: { x: 0, y: 0 }, prevPos: { x: 0, y: 0 }, vel: { x: 0, y: 0 } }; } constructor({ id, x, y, friction = 0, airFriction = 0 }, { position = ZERO_VEC, velocity = ZERO_VEC } = {}) { super({ props: { id, friction, airFriction }, state: { pos: (0, _v2.default)({ x, y }).add(position), vel: (0, _v2.default)(velocity), prevPos: (0, _v2.default)({ x, y }).add(position).sub(velocity) } }); } getNextPos(vel) { return (0, _v2.default)(this.pos).add(vel); } step({ gravity }) { let vel = (0, _v2.default)(this.pos).sub(this.prevPos).mul(1 - this.airFriction).add(gravity); return this.updateState({ pos: this.getNextPos(vel), prevPos: this.pos, vel: vel }); } setPosition(pos) { return this.updateState({ pos }); } } exports.Point = Point; (0, _immo.setupImmo)(Point); class CollisionPoint extends Point { __props__() { return { collidable: true }; } } exports.CollisionPoint = CollisionPoint; (0, _immo.setupImmo)(CollisionPoint); // based on the canonical glsl rand // returns a psuedorandom number between 0 and 1 const V = { x: 12.9898, y: 78.233 }; const K = 43758.5453; function rand(seed) { return Math.sin(_v2.default.dot(seed, V)) * K % 1; } const INTENSITY = 2; const SPEED_THRESHOLD = 40; // as this gets smaller, the scarf intensifies faster while speed increases class FlutterPoint extends Point { static getFlutter(vel, seed) { let speed = Math.pow(_v2.default.lenSq(vel), 0.25); let randMag = rand(vel); let randAng = rand(seed); randMag *= INTENSITY * speed * -Math.expm1(-speed / SPEED_THRESHOLD); randAng *= 2 * Math.PI; return { x: randMag * Math.cos(randAng), y: randMag * Math.sin(randAng) }; } getNextPos(vel) { return (0, _v2.default)(this.pos).add(vel).add(FlutterPoint.getFlutter(vel, this.pos)); } } exports.FlutterPoint = FlutterPoint; class Binding extends _immo2.default { __props__() { return { id: null, collidable: false, steppable: true }; } __state__() { return { framesSinceUnbind: -1 }; } constructor({ id }) { super({ props: { id } }); } isBinded() { return this.framesSinceUnbind === -1; } setBind(bind) { if (bind && !this.isBinded()) { return this.updateState({ framesSinceUnbind: -1 }); } else if (!bind && this.isBinded()) { return this.updateState({ framesSinceUnbind: 0 }); } return this; } step() { if (this.isBinded()) { return this; } return this.updateState({ framesSinceUnbind: this.framesSinceUnbind + 1 }); } } exports.Binding = Binding; (0, _immo.setupImmo)(Binding);