Phaser updown test
phaser tests
by luckylooke
HTML
<script src="http://examples.phaser.io/_site/js/phaser.js"></script>
<script>
function Vector(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
Vector.prototype = {
negative: function() {
return new Vector(-this.x, -this.y, -this.z);
},
add: function(v) {
if (v instanceof Vector) return new Vector(this.x + v.x, this.y + v.y, this.z + v.z);
else return new Vector(this.x + v, this.y + v, this.z + v);
},
subtract: function(v) {
if (v instanceof Vector) return new Vector(this.x - v.x, this.y - v.y, this.z - v.z);
else return new Vector(this.x - v, this.y - v, this.z - v);
},
multiply: function(v) {
if (v instanceof Vector) return new Vector(this.x * v.x, this.y * v.y, this.z * v.z);
else return new Vector(this.x * v, this.y * v, this.z * v);
},
divide: function(v) {
if (v instanceof Vector) return new Vector(this.x / v.x, this.y / v.y, this.z / v.z);
else return new Vector(this.x / v, this.y / v, this.z / v);
},
equals: function(v) {
return this.x == v.x && this.y == v.y && this.z == v.z;
},
dot: function(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
},
cross: function(v) {
return new Vector(
this.y * v.z - this.z * v.y,
this.z * v.x - this.x * v.z,
this.x * v.y - this.y * v.x);
},
length: function() {
return Math.sqrt(this.dot(this));
},
unit: function() {
return this.divide(this.length());
},
min: function() {
return Math.min(Math.min(this.x, this.y), this.z);
},
max: function() {
return Math.max(Math.max(this.x, this.y), this.z);
},
toAngles: function() {
return {
...
JavaScript
var state = {
preload: function () {
game.load.image('background', 'img/starfield.jpg');
utils.draw('fighter', player.fighter.draw, 30, 30);
},
create: function () {
this.worldgroup = this.game.add.group();
var bg = game.add.tileSprite(game.width / 2, game.height * 3 / 4, 1000, 1000, 'background');
game.physics.enable(bg, Phaser.Physics.ARCADE);
bg.fixedToCamera = true;
bg.anchor.setTo(0.5, 0.5);
// bg.angle = 90;
this.bg = bg;
// this.worldgroup.add(bg);
var fighter = player.fighter.instance;
fighter = game.add.sprite(game.width / 2, game.height * 3 / 4, game.cache.getBitmapData('fighter'));
game.physics.enable(fighter, Phaser.Physics.ARCADE);
fighter.anchor.setTo(0.5, 0.5);
fighter.z = 500;
fighter.body.drag.set(0);
fighter.body.maxVelocity.setTo(200, 200);
fighter.dx = 0;
fighter.dy = 0;
fighter.angle = -90;
this.fighter = fighter;
fighter.thrustPower = 0;
fighter.thrustRotation = 0;
fighter.moveSpeed = 0;
fighter.maxSpeed = 0;
fighter.moveRotation = 0;
fighter.prev = new Vector();
fighter.thrust = new Vector();
fighter.eff = new Vector();
fighter.bringToTop();
this.cursors = game.input.keyboard.createCursorKeys();
var updateManeuver = function () {
var cursors = this.cursors,
effectivity, equality;
effectivity = (-1 / Math.pow((fighter.prev.length() / fighter.maxSpeed) * 5 + 1, 2)) + 1.03;
equality = 1 - (Math.PI - Math.abs(Math.abs(bg.rotation - Math.atan2(fighter.prev.x, fighter.prev.y)) - Math.PI)) / (2 * Math.PI);
fighter.prev = fighter.prev.subtract(fighter.prev.unit().multiply(fighter.thrust.length() * effectivity * equality)).add(fighter.thrust);
};
// Create maneuver Timer
this.timer =...