117 lines
4.2 KiB
TypeScript
117 lines
4.2 KiB
TypeScript
module SpaceTac.View {
|
|
"use strict";
|
|
|
|
// Ship sprite in the arena (BattleView)
|
|
export class ArenaShip extends Phaser.Group {
|
|
// Link to displayed ship
|
|
ship: Game.Ship;
|
|
|
|
// Ship sprite
|
|
sprite: Phaser.Button;
|
|
|
|
// Hover effect
|
|
hover: Phaser.Image;
|
|
|
|
// Playing effect
|
|
playing: Phaser.Image;
|
|
|
|
// Create a ship sprite usable in the Arena
|
|
constructor(battleview: BattleView, ship: Game.Ship) {
|
|
this.ship = ship;
|
|
|
|
super(battleview.game);
|
|
|
|
// Add ship sprite
|
|
this.sprite = new Phaser.Button(battleview.game, 0, 0, "ship-scout-sprite");
|
|
this.sprite.scale.set(0.1, 0.1);
|
|
this.sprite.rotation = ship.arena_angle;
|
|
this.sprite.anchor.set(0.5, 0.5);
|
|
this.addChild(this.sprite);
|
|
|
|
// Add hover effect
|
|
this.hover = new Phaser.Image(battleview.game, 0, 0, "battle-arena-shipspritehover", 0);
|
|
this.hover.anchor.set(0.5, 0.5);
|
|
this.hover.visible = false;
|
|
this.addChild(this.hover);
|
|
|
|
// Add playing effect
|
|
this.playing = new Phaser.Image(battleview.game, 0, 0, "battle-arena-shipspriteplaying", 0);
|
|
this.playing.anchor.set(0.5, 0.5);
|
|
this.playing.visible = false;
|
|
this.addChild(this.playing);
|
|
|
|
// Handle input on ship sprite
|
|
this.sprite.input.useHandCursor = true;
|
|
this.sprite.onInputOver.add(() => {
|
|
battleview.cursorOnShip(ship);
|
|
});
|
|
this.sprite.onInputOut.add(() => {
|
|
battleview.cursorOffShip(ship);
|
|
});
|
|
this.sprite.onInputUp.add(() => {
|
|
battleview.cursorClicked();
|
|
});
|
|
|
|
// Set location
|
|
this.position.set(ship.arena_x, ship.arena_y);
|
|
}
|
|
|
|
// Set the hovered state on this ship
|
|
// This will toggle the hover effect
|
|
setHovered(hovered: boolean) {
|
|
Animation.setVisibility(this.game, this.hover, hovered, 200);
|
|
}
|
|
|
|
// Set the playing state on this ship
|
|
// This will toggle the "playing" indicator
|
|
setPlaying(playing: boolean) {
|
|
Animation.setVisibility(this.game, this.playing, playing, 500);
|
|
}
|
|
|
|
// Move the sprite to a location
|
|
moveTo(x: number, y: number, facing_angle: number, animate: boolean = true) {
|
|
if (animate) {
|
|
var tween_group = this.game.tweens.create(this);
|
|
var tween_sprite = this.game.tweens.create(this.sprite);
|
|
tween_group.to({x: x, y: y});
|
|
tween_group.start();
|
|
Tools.rotationTween(tween_sprite, facing_angle);
|
|
tween_sprite.start();
|
|
} else {
|
|
this.x = x;
|
|
this.y = y;
|
|
this.sprite.rotation = facing_angle;
|
|
}
|
|
}
|
|
|
|
// Briefly display the damage done to the ship
|
|
displayDamage(hull: number, shield: number) {
|
|
if (hull > 0) {
|
|
var hull_text = new Phaser.Text(this.game, -20, -20, Math.round(hull).toString(),
|
|
{font: "bold 16px Arial", align: "center", fill: "#ffbbbb"});
|
|
hull_text.anchor.set(0.5, 0.5);
|
|
this.addChild(hull_text);
|
|
this.animateDamageText(hull_text);
|
|
}
|
|
if (shield > 0) {
|
|
var shield_text = new Phaser.Text(this.game, 20, -20, Math.round(shield).toString(),
|
|
{font: "bold 16px Arial", align: "center", fill: "#bbbbff"});
|
|
shield_text.anchor.set(0.5, 0.5);
|
|
this.addChild(shield_text);
|
|
this.animateDamageText(shield_text);
|
|
}
|
|
}
|
|
|
|
private animateDamageText(text: Phaser.Text) {
|
|
text.alpha = 0;
|
|
var tween = this.game.tweens.create(text);
|
|
tween.to({alpha: 1}, 100, Phaser.Easing.Circular.In, false, 500);
|
|
tween.to({y: -50, alpha: 0}, 800, Phaser.Easing.Circular.In, false, 200);
|
|
tween.onComplete.addOnce(() => {
|
|
text.destroy();
|
|
});
|
|
tween.start();
|
|
}
|
|
}
|
|
}
|