Last active
February 1, 2020 13:39
-
-
Save netcell/60097d0661ad2f74a258 to your computer and use it in GitHub Desktop.
Camera for Phaser
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 game = require('../common/game'); | |
class Camera extends Phaser.Group { | |
constructor({x = 0, y = 0}) { | |
super(game); | |
var {world, physics, camera} = game; | |
var {centerX, centerY, bounds} = world; | |
this.scale.setTo(1, 1); | |
this.position.setTo(x, y); | |
this.bounds = Phaser.Rectangle.clone(bounds); | |
this.stalker = game.add.sprite(0, 0, null); | |
this.stalker.position.setTo(centerX, centerY); | |
physics.arcade.enable(this.stalker); | |
camera.follow(this.stalker); | |
this.nullTarget = game.add.sprite(centerX, centerY, null); | |
this.target = this.nullTarget; | |
this.secondsToTarget = 0.5; | |
this.safeDistance = 3; | |
return this; | |
} | |
removeTarget(){ | |
this.target = this.nullTarget; | |
} | |
follow(target){ | |
this.target = target.world; | |
} | |
get speed(){ | |
var distance = this.stalker.position.distance(this.target); | |
if (distance < this.safeDistance) return 0; | |
else return distance/this.secondsToTarget; | |
} | |
update(){ | |
super.update(); | |
if (this.target) { | |
var {stalker, target, speed} = this; | |
var position = stalker.position; | |
var arcade = game.physics.arcade; | |
if (!speed) position.copyFrom(target); | |
else arcade.moveToObject(stalker, target, speed); | |
} | |
} | |
zoomTo(scale, duration){ | |
var bounds = this.bounds; | |
var cameraBounds = game.camera.bounds; | |
var postionScale = (1 - scale) / 2; | |
var x = bounds.width * postionScale, | |
y = bounds.height * postionScale, | |
width = bounds.width * scale, | |
height = bounds.height * scale; | |
if (!duration) { | |
cameraBounds.x = x; | |
cameraBounds.y = y; | |
cameraBounds.width = width; | |
cameraBounds.height = height; | |
this.scale.setTo(scale); | |
} else { | |
game.add.tween(cameraBounds) | |
.to({x, y, width, height}, duration).start(); | |
return game.add.tween(this.scale) | |
.to({x: scale, y: scale}, duration).start(); | |
} | |
} | |
}; | |
module.exports = Camera; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have an ES5 version of this available? My game is not currently in ES6, but i would love to use this.