Skip to content

Instantly share code, notes, and snippets.

@drhayes
Created January 12, 2017 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drhayes/897c4622986e96cc2cb66446e8469fe2 to your computer and use it in GitHub Desktop.
Save drhayes/897c4622986e96cc2cb66446e8469fe2 to your computer and use it in GitHub Desktop.
import forsaken from '../forsaken';
export const FACTORY = (game, mapEntity) => {
const { x, y, width, height, properties: { id, reversible = true } } = mapEntity;
return new StoneSmasher(game, id, x, y, width, height, reversible);
}
export const GROUP_NAME = 'obstacles';
const frame = i => `stoneSmasher${i}`;
const RISE_UP_FACTOR = 15.625;
export default class StoneSmasher extends Phaser.Sprite {
constructor(game, id, x, y, width, height, reversible) {
super(game, x, y);
this.id = id;
forsaken.savedState.track(this);
game.physics.arcade.enable(this);
this.fullWidth = width;
this.fullHeight = height;
// Terrible name! What this means is, "Does this start running again when triggered?"
// It defaults to true.
this.reversible = reversible;
this.body.setSize(width, 0)
this.body.height = 0;
this.body.immovable = true;
this.body.allowGravity = false;
const wall = game.add.tileSprite(0, 0, width, height, 'sprites', frame(0));
wall.width = width;
wall.height = 1;
this.addChild(wall);
this.wall = wall;
const leftSide = game.add.tileSprite(-16, 0, 16, height, 'sprites', frame(1));
this.addChild(leftSide);
this.leftSide = leftSide;
const rightSide = game.add.tileSprite(width, 0, 16, height, 'sprites', frame(2));
this.addChild(rightSide);
this.rightSide = rightSide;
const foot = game.add.tileSprite(0, height, width, 16, 'sprites', frame(3));
this.addChild(foot);
this.foot = foot;
const lowerLeft = game.add.image(0, height, 'sprites', frame(4));
this.addChild(lowerLeft);
this.lowerLeft = lowerLeft;
const lowerRight = game.add.image(width, height, 'sprites', frame(5));
this.addChild(lowerRight);
this.lowerRight = lowerRight;
this.innerSmashExtension = 0;
this.state.running = forsaken.savedState.getDefault(id, 'running', true);
if (this.state.running) {
this.smashDown();
}
}
smashDown = () => {
if (this.currentTween) {
this.currentTween.stop();
}
if (!this.state.running) {
return;
}
this.currentTween = game.add.tween(this)
.to({ smashExtension: 1}, this.smashExtension * 1500, Phaser.Easing.Bounce.Out, true);
this.currentTween.onComplete.addOnce(this.slideUp);
}
slideUp = () => {
if (this.currentTween) {
this.currentTween.stop();
}
this.currentTween = game.add.tween(this)
.to({ smashExtension: 0}, this.smashExtension * 1000, Phaser.Easing.Linear.None, true);
this.currentTween.onComplete.add(this.smashDown);
}
get smashExtension() {
return this.innerSmashExtension;
}
set smashExtension(newValue) {
this.innerSmashExtension = newValue;
const tweenedHeight = Math.round(this.fullHeight * newValue);
this.wall.height = tweenedHeight;
this.wall.tilePosition.y = tweenedHeight;
this.leftSide.height = tweenedHeight;
this.leftSide.tilePosition.y = tweenedHeight;
this.rightSide.height = tweenedHeight;
this.rightSide.tilePosition.y = tweenedHeight;
this.foot.y = tweenedHeight;
this.lowerLeft.y = tweenedHeight;
this.lowerRight.y = tweenedHeight;
this.body.setSize(this.fullWidth, tweenedHeight);
}
onPlayerCollide(player) {
if (this.state.running) {
player.damage(5);
}
}
// So yeah: call start to stop the smasher. Yay! Or pass true to restart it. Yay again!
start(restarted) {
this.state.running = !!restarted;
if (this.currentTween) {
this.currentTween.stop();
this.slideUp();
}
if (this.state.running) {
this.smashDown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment