Last active
September 17, 2019 15:36
-
-
Save ederst/c0ab443ef3bafa928c508a06a171a213 to your computer and use it in GitHub Desktop.
Phaser 3 "Space" example without drag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = { | |
type: Phaser.AUTO, | |
width: 800, | |
height: 600, | |
parent: 'phaser-example', | |
physics: { | |
default: 'arcade', | |
arcade: { | |
debug: true | |
} | |
}, | |
scene: { | |
preload: preload, | |
create: create, | |
update: update | |
}, | |
pixelArt: false, | |
roundPixels: false | |
}; | |
var bg; | |
var stars; | |
var ship; | |
var bullets; | |
var lastFired = 0; | |
var cursors; | |
var fire; | |
var game = new Phaser.Game(config); | |
function preload () | |
{ | |
this.load.image('background', 'assets/tests/space/nebula.jpg'); | |
this.load.image('stars', 'assets/tests/space/stars.png'); | |
this.load.atlas('space', 'assets/tests/space/space.png', 'assets/tests/space/space.json'); | |
} | |
function create () | |
{ | |
var Bullet = new Phaser.Class({ | |
Extends: Phaser.Physics.Arcade.Image, | |
initialize: | |
function Bullet (scene) | |
{ | |
//Phaser.Physics.Arcade.Image.call(this, scene, 0, 0, 'space', 'blaster'); | |
Phaser.Physics.Arcade.Image.call(this, scene, 0, 0, 'space', 'blaster'); | |
this.setBlendMode(1); | |
this.setDepth(1); | |
this.speed = 1000; | |
//this.lifespan = 1000; | |
//this._temp = new Phaser.Math.Vector2(); | |
}, | |
fire: function (ship, target, laserMount) | |
{ | |
this.lifespan = 1000; | |
//this.setSize(10, 10, true); | |
this.setDisplaySize(20, 20, true); | |
this.setActive(true); | |
this.setVisible(true); | |
this.direction = Math.atan( (target.x-this.x) / (target.y-this.y)); | |
this.rotation = ship.rotation; | |
this.setPosition(laserMount.x, laserMount.y); // Initial position | |
/*if (target.y >= this.y) { | |
this.xSpeed = this.speed*Math.sin(this.direction); | |
this.ySpeed = this.speed*Math.cos(this.direction); | |
} else { | |
this.xSpeed = -this.speed*Math.sin(this.direction); | |
this.ySpeed = -this.speed*Math.cos(this.direction); | |
}*/ | |
// this.setRotation(ship.rotation); | |
//this.setAngle(ship.body.rotation); | |
//this.setPosition(ship.x, ship.y); | |
//this.body.reset(ship.x, ship.y); | |
//ship.body.advancePosition(10, this._temp); | |
// this.setPosition(this._temp.x, this._temp.y); | |
// this.body.reset(this._temp.x, this._temp.y); | |
// if ship is rotating we need to add it here | |
// var a = ship.body.angularVelocity; | |
// if (ship.body.speed !== 0) | |
// { | |
// var angle = Math.atan2(ship.body.velocity.y, ship.body.velocity.x); | |
// } | |
// else | |
// { | |
//var angle = Phaser.Math.DegToRad(ship.body.rotation); | |
// } | |
//this.body.world.velocityFromRotation(this.rotation, this.speed + ship.body.speed, this.body.velocity); | |
this.scene.physics.velocityFromRotation(this.rotation, this.speed, this.body.velocity); | |
this.body.velocity.x *= 2; | |
this.body.velocity.y *= 2; | |
//this.body.velocity.x = | |
}, | |
update: function (time, delta) | |
{ | |
this.lifespan -= delta; | |
if (this.lifespan <= 0) | |
{ | |
this.setActive(false); | |
this.setVisible(false); | |
this.body.stop(); | |
} | |
} | |
}); | |
// Prepare some spritesheets and animations | |
this.textures.addSpriteSheetFromAtlas('mine-sheet', { atlas: 'space', frame: 'mine', frameWidth: 64 }); | |
this.textures.addSpriteSheetFromAtlas('asteroid1-sheet', { atlas: 'space', frame: 'asteroid1', frameWidth: 96 }); | |
this.textures.addSpriteSheetFromAtlas('asteroid2-sheet', { atlas: 'space', frame: 'asteroid2', frameWidth: 96 }); | |
this.textures.addSpriteSheetFromAtlas('asteroid3-sheet', { atlas: 'space', frame: 'asteroid3', frameWidth: 96 }); | |
this.textures.addSpriteSheetFromAtlas('asteroid4-sheet', { atlas: 'space', frame: 'asteroid4', frameWidth: 64 }); | |
this.anims.create({ key: 'mine-anim', frames: this.anims.generateFrameNumbers('mine-sheet', { start: 0, end: 15 }), frameRate: 20, repeat: -1 }); | |
this.anims.create({ key: 'asteroid1-anim', frames: this.anims.generateFrameNumbers('asteroid1-sheet', { start: 0, end: 24 }), frameRate: 20, repeat: -1 }); | |
this.anims.create({ key: 'asteroid2-anim', frames: this.anims.generateFrameNumbers('asteroid2-sheet', { start: 0, end: 24 }), frameRate: 20, repeat: -1 }); | |
this.anims.create({ key: 'asteroid3-anim', frames: this.anims.generateFrameNumbers('asteroid3-sheet', { start: 0, end: 24 }), frameRate: 20, repeat: -1 }); | |
this.anims.create({ key: 'asteroid4-anim', frames: this.anims.generateFrameNumbers('asteroid4-sheet', { start: 0, end: 24 }), frameRate: 20, repeat: -1 }); | |
// World size is 8000 x 6000 | |
bg = this.add.tileSprite(400, 300, 800, 600, 'background').setScrollFactor(0); | |
// Add our planets, etc | |
this.add.image(512, 680, 'space', 'blue-planet').setOrigin(0).setScrollFactor(0.6); | |
this.add.image(2833, 1246, 'space', 'brown-planet').setOrigin(0).setScrollFactor(0.6); | |
this.add.image(3875, 531, 'space', 'sun').setOrigin(0).setScrollFactor(0.6); | |
var galaxy = this.add.image(5345 + 1024, 327 + 1024, 'space', 'galaxy').setBlendMode(1).setScrollFactor(0.6); | |
this.add.image(908, 3922, 'space', 'gas-giant').setOrigin(0).setScrollFactor(0.6); | |
this.add.image(3140, 2974, 'space', 'brown-planet').setOrigin(0).setScrollFactor(0.6).setScale(0.8).setTint(0x882d2d); | |
this.add.image(6052, 4280, 'space', 'purple-planet').setOrigin(0).setScrollFactor(0.6); | |
for (var i = 0; i < 8; i++) | |
{ | |
this.add.image(Phaser.Math.Between(0, 8000), Phaser.Math.Between(0, 6000), 'space', 'eyes').setBlendMode(1).setScrollFactor(0.8); | |
} | |
stars = this.add.tileSprite(400, 300, 800, 600, 'stars').setScrollFactor(0); | |
var particles = this.add.particles('space'); | |
var emitter = particles.createEmitter({ | |
frame: 'blue', | |
speed: 100, | |
lifespan: { | |
onEmit: function (particle, key, t, value) | |
{ | |
return Phaser.Math.Percent(ship.body.speed, 0, 300) * 2000; | |
} | |
}, | |
alpha: { | |
onEmit: function (particle, key, t, value) | |
{ | |
return Phaser.Math.Percent(ship.body.speed, 0, 300); | |
} | |
}, | |
angle: { | |
onEmit: function (particle, key, t, value) | |
{ | |
var v = Phaser.Math.Between(-10, 10); | |
return (ship.angle - 180) + v; | |
} | |
}, | |
scale: { start: 0.3, end: 0 }, | |
blendMode: 'ADD' | |
}); | |
bullets = this.physics.add.group({ | |
classType: Bullet, | |
maxSize: 30, | |
runChildUpdate: true | |
}); | |
ship = this.physics.add.image(4000, 3000, 'space', 'ship').setDepth(2); | |
ship | |
.setOrigin(0.5, 0.5) | |
.setDisplaySize(30, 30) | |
.setDrag(0) | |
.setAngularDrag(0) | |
.setMaxVelocity(1000); | |
reticle = this.physics.add.sprite(ship.x, ship.y + 50, 'target'); | |
reticle.setOrigin(0.5, 0.5).setDisplaySize(10, 10); | |
laserMount = this.physics.add.sprite(ship.x + 25 * Math.cos(ship.rotation), ship.y + 25 * Math.sin(ship.rotation), 'laserMount'); | |
laserMount.setOrigin(0.5, 0.5).setDisplaySize(5, 5); | |
emitter.startFollow(ship); | |
this.cameras.main.startFollow(ship); | |
//this.cameras.main.zoom = 0.5 | |
//cursors = this.input.keyboard.createCursorKeys(); | |
keyBackThrust = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S); | |
keyFwdThrust = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W); | |
keyLeft = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A); | |
keyRight = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D); | |
fire = false; | |
//this.input.mouse.add | |
// Pointer lock will only work after mousedown | |
game.canvas.addEventListener('mousedown', function () { | |
game.input.mouse.requestPointerLock(); | |
}); | |
this.input.on('pointermove', function (pointer) { | |
if (this.input.mouse.locked) | |
{ | |
reticle.x += pointer.movementX; | |
reticle.y += pointer.movementY; | |
} | |
}, this); | |
// Fires bullets if mousebuttons down | |
this.input.on('pointerdown', function (pointer) { | |
fire = true; | |
}, this); | |
this.input.on('pointerup', function (pointer) { | |
fire = false; | |
}, this); | |
this.add.sprite(4300, 3000).play('asteroid1-anim'); | |
this.tweens.add({ | |
targets: galaxy, | |
angle: 360, | |
duration: 100000, | |
ease: 'Linear', | |
loop: -1 | |
}); | |
text = this.add.text(32, 32).setScrollFactor(0).setFontSize(10).setColor('#ffffff'); | |
} | |
function constrainReticle(reticle) | |
{ | |
var distX = reticle.x-ship.x; // X distance between player & reticle | |
var distY = reticle.y-ship.y; // Y distance between player & reticle | |
// Ensures reticle cannot be moved offscreen (player follow) | |
if (distX > 200) | |
reticle.x = ship.x+200; | |
else if (distX < -200) | |
reticle.x = ship.x-200; | |
if (distY > 200) | |
reticle.y = ship.y+200; | |
else if (distY < -200) | |
reticle.y = ship.y-200; | |
} | |
function update (time, delta) | |
{ | |
// Rotates player to face towards reticle | |
ship.rotation = Phaser.Math.Angle.Between(ship.x, ship.y, reticle.x, reticle.y); | |
var noCursorDown = true; | |
var downVec = Phaser.Math.Vector2.ZERO; | |
var upVec = Phaser.Math.Vector2.ZERO; | |
var leftVec = Phaser.Math.Vector2.ZERO; | |
var rightVec = Phaser.Math.Vector2.ZERO; | |
if (keyLeft.isDown) { | |
noCursorDown = false; | |
leftVec = this.physics.velocityFromAngle(ship.angle - 90, 200); | |
//ship.body.acceleration = leftVec; | |
} | |
if (keyRight.isDown) { | |
noCursorDown = false; | |
rightVec = this.physics.velocityFromAngle(ship.angle + 90, 200); | |
//ship.body.acceleration = rightVec; | |
} | |
if (keyBackThrust.isDown) { | |
noCursorDown = false; | |
downVec = this.physics.velocityFromAngle(ship.angle - 180, 100); | |
//ship.body.acceleration = downVec; | |
} | |
if (keyFwdThrust.isDown) { | |
noCursorDown = false; | |
upVec = this.physics.velocityFromAngle(ship.angle, 600); | |
//ship.body.acceleration = upVec; | |
} | |
var addVec = new Phaser.Math.Vector2( | |
downVec.x + upVec.x + leftVec.x + rightVec.x, | |
downVec.y + upVec.y + leftVec.y + rightVec.y | |
); | |
if (noCursorDown) { | |
ship.setAcceleration(0); | |
} else { | |
ship.body.acceleration = addVec; | |
} | |
reticle.body.velocity.x = ship.body.velocity.x; | |
reticle.body.velocity.y = ship.body.velocity.y; | |
// Constrain position of constrainReticle | |
//constrainReticle(reticle); | |
laserMount.setPosition(ship.x + 25 * Math.cos(ship.rotation), ship.y + 25 * Math.sin(ship.rotation)); | |
laserMount.body.velocity.x = ship.body.velocity.x; | |
laserMount.body.velocity.y = ship.body.velocity.y; | |
if (fire && time > lastFired) | |
{ | |
var bullet = bullets.get(); | |
if (bullet) | |
{ | |
bullet.fire(ship, reticle, laserMount); | |
lastFired = time + 100; | |
} | |
} | |
bg.tilePositionX += ship.body.deltaX() * 0.5; | |
bg.tilePositionY += ship.body.deltaY() * 0.5; | |
stars.tilePositionX += ship.body.deltaX() * 2; | |
stars.tilePositionY += ship.body.deltaY() * 2; | |
text.setText([ | |
"up: (" + upVec.x + ", " + upVec.y + "), down: (" + downVec.x + ", " + downVec.y + ")", | |
"left: (" + leftVec.x + ", " + leftVec.y + "), right: (" + rightVec.x + ", " + rightVec.y + ")", | |
"res: (" + addVec.x + ", " + addVec.y + ")", | |
"vel: (" + ship.body.velocity.x + ", " + ship.body.velocity.y + "), pos: (" + ship.x + ", " + ship.y + ")", | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment