|
|
@@ -1,186 +1,184 @@
|
|
|
-
|
|
|
"atomic component";
|
|
|
|
|
|
var inspectorFields = {
|
|
|
|
|
|
- jumpSound: ["Sound"]
|
|
|
+ jumpSound: ["Sound"]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
exports.component = function(self) {
|
|
|
|
|
|
- var node = self.node;
|
|
|
- var camera = self.node.scene.getMainCamera();
|
|
|
- var cameraNode = camera.node;
|
|
|
- var input = Atomic.input;
|
|
|
- var soundSource = node.getComponent("SoundSource");
|
|
|
-
|
|
|
- var MAX_VELOCITY = 3;
|
|
|
- var anim = "";
|
|
|
- var flipped = false;
|
|
|
- var contactCount = 0;
|
|
|
- var jumpDelta = 0;
|
|
|
- var control = false;
|
|
|
-
|
|
|
- var lastAnimDelta = 0;
|
|
|
-
|
|
|
- var spawnPosition = node.position2D;
|
|
|
-
|
|
|
- var sprite;
|
|
|
- var animationSet;
|
|
|
- var jumpSound;
|
|
|
+ var node = self.node;
|
|
|
+ var camera = self.node.scene.getMainCamera();
|
|
|
+ var cameraNode = camera.node;
|
|
|
+ var input = Atomic.input;
|
|
|
+ var soundSource = node.getComponent("SoundSource");
|
|
|
|
|
|
- // physics
|
|
|
- var body;
|
|
|
- var circle;
|
|
|
+ var MAX_VELOCITY = 3;
|
|
|
+ var anim = "";
|
|
|
+ var flipped = false;
|
|
|
+ var contactCount = 0;
|
|
|
+ var jumpDelta = 0;
|
|
|
+ var control = false;
|
|
|
|
|
|
- self.start = function() {
|
|
|
+ var lastAnimDelta = 0;
|
|
|
|
|
|
- sprite = node.getComponent("AnimatedSprite2D");
|
|
|
- body = node.getComponent("RigidBody2D");
|
|
|
- circle = node.getComponent("CollisionCircle2D");
|
|
|
+ var spawnPosition = node.position2D;
|
|
|
|
|
|
- setAnimation("Idle");
|
|
|
+ var sprite;
|
|
|
+ var animationSet;
|
|
|
+ var jumpSound;
|
|
|
|
|
|
- self.subscribeToEvent("PhysicsBeginContact2D", function(event) {
|
|
|
- if (event.bodyB == body)
|
|
|
- contactCount++;
|
|
|
- });
|
|
|
+ // physics
|
|
|
+ var body;
|
|
|
+ var circle;
|
|
|
|
|
|
- self.subscribeToEvent("PhysicsEndContact2D", function(event) {
|
|
|
- if (event.bodyB == body)
|
|
|
- contactCount--;
|
|
|
- });
|
|
|
+ self.start = function() {
|
|
|
|
|
|
- }
|
|
|
+ sprite = node.getComponent("AnimatedSprite2D");
|
|
|
+ body = node.getComponent("RigidBody2D");
|
|
|
+ circle = node.getComponent("CollisionCircle2D");
|
|
|
|
|
|
- self.update = function(timeStep) {
|
|
|
+ setAnimation("Idle");
|
|
|
+
|
|
|
+ self.subscribeToEvent("PhysicsPostStep2D", function(event) {
|
|
|
+
|
|
|
+ cameraNode.position = node.position;
|
|
|
+
|
|
|
+ });
|
|
|
|
|
|
- handleInput(timeStep);
|
|
|
- handleAnimation(timeStep);
|
|
|
+ self.subscribeToEvent("PhysicsBeginContact2D", function(event) {
|
|
|
+ if (event.bodyB == body)
|
|
|
+ contactCount++;
|
|
|
+ });
|
|
|
|
|
|
- if (node.position[1] < 14) {
|
|
|
- //TODO: FIX, I have to set scale to 0 and the back to 1 to force
|
|
|
- // setposition catching dirty
|
|
|
- node.scale2D = [0, 0];
|
|
|
- node.position2D = spawnPosition;
|
|
|
- node.scale2D = [1, 1];
|
|
|
+ self.subscribeToEvent("PhysicsEndContact2D", function(event) {
|
|
|
+ if (event.bodyB == body)
|
|
|
+ contactCount--;
|
|
|
+ });
|
|
|
|
|
|
}
|
|
|
|
|
|
- cameraNode.position = node.position;
|
|
|
+ self.update = function(timeStep) {
|
|
|
|
|
|
- }
|
|
|
+ handleInput(timeStep);
|
|
|
+ handleAnimation(timeStep);
|
|
|
|
|
|
- self.postUpdate = function() {
|
|
|
+ if (node.position[1] < 14) {
|
|
|
+ //TODO: FIX, I have to set scale to 0 and the back to 1 to force
|
|
|
+ // setposition catching dirty
|
|
|
+ node.scale2D = [0, 0];
|
|
|
+ node.position2D = spawnPosition;
|
|
|
+ node.scale2D = [1, 1];
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function setAnimation(animName) {
|
|
|
+ function setAnimation(animName) {
|
|
|
|
|
|
- if (anim == animName || lastAnimDelta > 0)
|
|
|
- return;
|
|
|
+ if (anim == animName || lastAnimDelta > 0)
|
|
|
+ return;
|
|
|
|
|
|
- lastAnimDelta = .25;
|
|
|
+ lastAnimDelta = .25;
|
|
|
|
|
|
- sprite.setAnimation(animName);
|
|
|
- anim = animName;
|
|
|
+ sprite.setAnimation(animName);
|
|
|
+ anim = animName;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function handleAnimation(timeStep) {
|
|
|
+ function handleAnimation(timeStep) {
|
|
|
|
|
|
- lastAnimDelta -= timeStep;
|
|
|
+ lastAnimDelta -= timeStep;
|
|
|
|
|
|
- var vel = body.linearVelocity;
|
|
|
+ var vel = body.linearVelocity;
|
|
|
|
|
|
- if (contactCount) {
|
|
|
+ if (contactCount) {
|
|
|
|
|
|
- if (vel[0] < -0 && control) {
|
|
|
- if (!flipped) {
|
|
|
- sprite.flipX = true;
|
|
|
- flipped = true;
|
|
|
+ if (vel[0] < -0 && control) {
|
|
|
+ if (!flipped) {
|
|
|
+ sprite.flipX = true;
|
|
|
+ flipped = true;
|
|
|
|
|
|
- }
|
|
|
- setAnimation("Run");
|
|
|
- } else if (vel[0] > 0 && control) {
|
|
|
+ }
|
|
|
+ setAnimation("Run");
|
|
|
+ } else if (vel[0] > 0 && control) {
|
|
|
|
|
|
- if (flipped) {
|
|
|
- sprite.flipX = false;
|
|
|
- flipped = false;
|
|
|
+ if (flipped) {
|
|
|
+ sprite.flipX = false;
|
|
|
+ flipped = false;
|
|
|
|
|
|
- }
|
|
|
- setAnimation("Run");
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ setAnimation("Run");
|
|
|
+ } else {
|
|
|
|
|
|
- setAnimation("Idle");
|
|
|
- }
|
|
|
- } else {
|
|
|
+ setAnimation("Idle");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
|
|
|
- if (vel[1] > 1.0) {
|
|
|
- setAnimation("Jump");
|
|
|
- } else if (vel[1] < -1.0) {
|
|
|
- setAnimation("Land");
|
|
|
- }
|
|
|
+ if (vel[1] > 1.0) {
|
|
|
+ setAnimation("Jump");
|
|
|
+ } else if (vel[1] < -1.0) {
|
|
|
+ setAnimation("Land");
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function handleInput(timeStep) {
|
|
|
+ function handleInput(timeStep) {
|
|
|
|
|
|
- var vel = body.linearVelocity;
|
|
|
- var pos = node.position2D;
|
|
|
+ var vel = body.linearVelocity;
|
|
|
+ var pos = node.position2D;
|
|
|
|
|
|
- jumpDelta -= timeStep;
|
|
|
+ jumpDelta -= timeStep;
|
|
|
|
|
|
- if (Math.abs(vel[0]) > MAX_VELOCITY) {
|
|
|
- vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
|
|
|
- body.setLinearVelocity(vel);
|
|
|
- }
|
|
|
+ if (Math.abs(vel[0]) > MAX_VELOCITY) {
|
|
|
+ vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
|
|
|
+ body.setLinearVelocity(vel);
|
|
|
+ }
|
|
|
|
|
|
- var left = input.getKeyDown(Atomic.KEY_LEFT) || input.getKeyDown(Atomic.KEY_A);
|
|
|
- var right = input.getKeyDown(Atomic.KEY_RIGHT) || input.getKeyDown(Atomic.KEY_D);
|
|
|
+ var left = input.getKeyDown(Atomic.KEY_LEFT) || input.getKeyDown(Atomic.KEY_A);
|
|
|
+ var right = input.getKeyDown(Atomic.KEY_RIGHT) || input.getKeyDown(Atomic.KEY_D);
|
|
|
|
|
|
- var jump = input.getKeyDown(Atomic.KEY_UP) || input.getKeyDown(Atomic.KEY_SPACE) || input.getKeyDown(Atomic.KEY_W);
|
|
|
+ var jump = input.getKeyDown(Atomic.KEY_UP) || input.getKeyDown(Atomic.KEY_SPACE) || input.getKeyDown(Atomic.KEY_W);
|
|
|
|
|
|
- control = false;
|
|
|
+ control = false;
|
|
|
|
|
|
- if (left || right)
|
|
|
- control = true;
|
|
|
+ if (left || right)
|
|
|
+ control = true;
|
|
|
|
|
|
- if (left && vel[0] > -MAX_VELOCITY) {
|
|
|
- body.applyLinearImpulse([-2, 0], pos, true);
|
|
|
- } else if (right && vel[0] < MAX_VELOCITY) {
|
|
|
- body.applyLinearImpulse([2, 0], pos, true);
|
|
|
- }
|
|
|
+ if (left && vel[0] > -MAX_VELOCITY) {
|
|
|
+ body.applyLinearImpulse([-2, 0], pos, true);
|
|
|
+ } else if (right && vel[0] < MAX_VELOCITY) {
|
|
|
+ body.applyLinearImpulse([2, 0], pos, true);
|
|
|
+ }
|
|
|
|
|
|
- if (!left && !right) {
|
|
|
- vel[0] *= 0.9;
|
|
|
- body.linearVelocity = vel;
|
|
|
- circle.friction = 1000.0;
|
|
|
- } else {
|
|
|
- circle.friction = .2;
|
|
|
- }
|
|
|
+ if (!left && !right) {
|
|
|
+ vel[0] *= 0.9;
|
|
|
+ body.linearVelocity = vel;
|
|
|
+ circle.friction = 1000.0;
|
|
|
+ } else {
|
|
|
+ circle.friction = .2;
|
|
|
+ }
|
|
|
|
|
|
- if (!contactCount)
|
|
|
- circle.friction = 0.0;
|
|
|
+ if (!contactCount)
|
|
|
+ circle.friction = 0.0;
|
|
|
|
|
|
- if (jump && jumpDelta <= 0 && contactCount) {
|
|
|
+ if (jump && jumpDelta <= 0 && contactCount) {
|
|
|
|
|
|
- jumpDelta = .25;
|
|
|
- if (self.jumpSound) {
|
|
|
- soundSource.gain = 0.45;
|
|
|
- soundSource.play(self.jumpSound);
|
|
|
- }
|
|
|
+ jumpDelta = .25;
|
|
|
+ if (self.jumpSound) {
|
|
|
+ soundSource.gain = 0.45;
|
|
|
+ soundSource.play(self.jumpSound);
|
|
|
+ }
|
|
|
|
|
|
- vel[1] = 0;
|
|
|
- body.linearVelocity = vel;
|
|
|
- body.applyLinearImpulse([0, 6], pos, true);
|
|
|
- }
|
|
|
+ vel[1] = 0;
|
|
|
+ body.linearVelocity = vel;
|
|
|
+ body.applyLinearImpulse([0, 6], pos, true);
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
-}
|
|
|
+}
|