var parsePath = function(svgPathEle) { var d = svgPathEle.getAttribute("d").split(" "); var startPstn = d[1].split(",").map(function(s) { return parseFloat(s) }); startPstn = {x:startPstn[0], y:startPstn[1]}; var controlPstn1 = d[3].split(",").map(function(s) { return parseFloat(s) }); controlPstn1 = {x:controlPstn1[0], y:controlPstn1[1]}; var controlPstn2 = d[4].split(",").map(function(s) { return parseFloat(s) }); controlPstn2 = {x:controlPstn2[0], y:controlPstn2[1]}; var endPstn = d[5].split(",").map(function(s) { return parseFloat(s) }); endPstn = {x:endPstn[0], y:endPstn[1]}; if (d[2] == "C") return {start:startPstn, control:[controlPstn1, controlPstn2], end:endPstn}; else return {start:startPstn, control:[geo.ccpAdd(controlPstn1, startPstn), geo.ccpAdd(controlPstn2, startPstn)], end:geo.ccpAdd(startPstn, endPstn)}; };
updateView: function () { var cam = this.get('currentCamera'), camPos = geo.ccpAdd(cam.get('position'), cam.get('offset')), camAnchor = cam.get('anchorPointInPixels'), newPos = geo.ccpAdd(geo.ccpNeg(camPos), camAnchor); this.set('position', newPos); },
reverse: function() { var c = this.config, bc = new geo.BezierConfig() bc.endPosition = geo.ccpNeg(c.endPosition) bc.controlPoint1 = geo.ccpAdd(c.controlPoint2, geo.ccpNeg(c.endPosition)) bc.controlPoint2 = geo.ccpAdd(c.controlPoint1, geo.ccpNeg(c.endPosition)) return new BezierBy({bezier: bc, duration: this.duration}) }
reverse: function() { var c = this.get('config'), bc = new geo.BezierConfig(); bc.endPosition = geo.ccpNeg(c.endPosition); bc.controlPoint1 = geo.ccpAdd(c.controlPoint2, geo.ccpNeg(c.endPosition)); bc.controlPoint2 = geo.ccpAdd(c.controlPoint1, geo.ccpNeg(c.endPosition)); return BezierBy.create({bezier: bc, duration: this.get('duration')}); }
update: function(t) { var c = this.config var xa = 0, xb = c.controlPoint1.x, xc = c.controlPoint2.x, xd = c.endPosition.x, ya = 0, yb = c.controlPoint1.y, yc = c.controlPoint2.y, yd = c.endPosition.y var x = bezierat(xa, xb, xc, xd, t) var y = bezierat(ya, yb, yc, yd, t) this.target.position = geo.ccpAdd(this.startPosition, geo.ccp(x, y)) },
update: function(t) { var c = this.get('config'); var xa = 0, xb = c.controlPoint1.x, xc = c.controlPoint2.x, xd = c.endPosition.x, ya = 0, yb = c.controlPoint1.y, yc = c.controlPoint2.y, yd = c.endPosition.y; var x = BezierBy.bezierat(xa, xb, xc, xd, t); var y = BezierBy.bezierat(ya, yb, yc, yd, t); this.target.set('position', geo.ccpAdd(this.get('startPosition'), geo.ccp(x, y))); },
function Movable() { Movable.superclass.constructor.call(this); this.mass = 0.1; } Movable.inherit(NodeContainer, { size: new geo.Size(1,1), get front() {if (this.angleChanged) this.resetFront(); return this._f;}, set front(v) {throw new Error('use resetFront instead of direct setting it');}, _f: ccp(0, 0), resetFront: function() { var half = this.size.width / 2; this._f.x = half * Math.cos(this.angle); this._f.y = half * Math.sin(this.angle); this.angleChanged = false; }, get rear() {return geo.ccpNeg(this.front);}, get frontPoint() { return geo.ccpAdd(this.location, this.front);}, get rearPoint() { return geo.ccpAdd(this.location, this.rear);}, get density() { return 1; } }); module.exports = Movable;
mouseDragged: function (event) { var node = this.getChild({tag: kTagTileMap}) var currentPos = node.position node.position = geo.ccpAdd(currentPos, new geo.Point(event.deltaX, event.deltaY)) return true }
update: function(dt) { // Reset acceleration on local player. if(this.isLocal) { this.acceleration = ccp(0,0); } // Apply forward force.. if(this.keys.UP && this.fuel > 0) { var rotInRadians = (-this.rotation + 90) * (Math.PI / 180.0); var force = ccp( Math.cos(rotInRadians) * this.moveSpeed * dt, Math.sin(rotInRadians) * this.moveSpeed * dt ); this.acceleration = geo.ccpAdd(this.acceleration, force); this.fuel--; } else { // Regenerate fuel. if(this.fuel < this.maxFuel) { this.fuel += this.fuelRegen; } } // Turn right or left. if(this.keys.RIGHT) { this.rotation += this.turnSpeed * dt; } if(this.keys.LEFT) { this.rotation -= this.turnSpeed * dt; } // Apply gravity. //this.velocity.y -= 7.5 * dt; // Apply acceleration to velocity. this.velocity = geo.ccpAdd(this.velocity, this.acceleration); // Simulate air drag. this.velocity.x *= this.airDrag; this.velocity.y *= this.airDrag; // Cap velocity at some maximum. var speed = Math.sqrt( this.velocity.x*this.velocity.x + this.velocity.y*this.velocity.y ); if(speed > this.maxSpeed) { this.velocity.x = (this.velocity.x / speed) * this.maxSpeed; this.velocity.y = (this.velocity.y / speed) * this.maxSpeed; } // Apply velocity to position. this.position = geo.ccpAdd(this.position, this.velocity); // Wrap-around of player position. var size = Director.sharedDirector.winSize; if(this.position.y > size.height) { this.position.y = 0; } if(this.position.y < 0) { this.position.y = size.height; } if(this.position.x > size.width) { this.position.x = 0; } if(this.position.x < 0) { this.position.x = size.width; } // Animate over time if pressing the UP key. if(this.keys.UP) { this.lastFrameChange += dt; if(this.lastFrameChange > 0.1) { this.animationFrame++; if(this.animationFrame > 2) { this.animationFrame = 1; } this.setFrame( this.animationFrame ); this.lastFrameChange = 0; } } else { this.setFrame(0); this.lastFrameChange = 0; } }