Calculate positions using sizes of the entities involved

This removes the magic numbers
This commit is contained in:
Liam Westby 2018-05-18 06:54:27 -05:00
parent a39f8f52b7
commit 80cb7a4364
2 changed files with 46 additions and 36 deletions

View File

@ -17,36 +17,37 @@ export class Player {
public update(x: number, y: number, direction: string) {
//console.log(`[Player-${this.name}] x=${x} y=${y} direction=${direction}`);
let playerMoved: boolean = (this.x !== x) || (this.y !== y) || (this.direction !== direction);
let playerMoved: boolean = this.x !== x || this.y !== y || this.direction !== direction;
this.x = x;
this.y = y;
this.direction = direction;
if (!this.tooltip)
if (!this.tooltip) {
this.tooltip = new PlayerTooltip(this);
}
if (!this.image) {
this.image = Assets.PLAYER_BITMAP.clone();
this.image.regX = this.image.getBounds().width / 2;
this.image.regY = this.image.getBounds().height / 2;
this.image.scaleX = 2.0;
this.image.scaleY = 2.0;
}
if (direction === 'UP') {
this.image.rotation = 0;
this.image.x = x - 10;
this.image.y = y - 10;
} else if (direction === 'RIGHT') {
this.image.rotation = 90;
this.image.x = x + 40;
this.image.y = y - 10;
} else if (direction === 'DOWN') {
this.image.rotation = 180;
this.image.x = x + 40;
this.image.y = y + 40;
} else {
this.image.rotation = 270;
this.image.x = x - 10;
this.image.y = y + 40;
}
this.image.x = x;
this.image.y = y;
this.tooltip.update(direction);
return playerMoved;
}
@ -61,8 +62,8 @@ export class Player {
this.explosionImage = Assets.PLAYER_DEAD_BITMAP.clone();
this.explosionImage.scaleX = 1.2;
this.explosionImage.scaleY = 1.2;
this.explosionImage.x = this.x - 16;
this.explosionImage.y = this.y - 16;
this.explosionImage.x = this.x - this.explosionImage.getBounds().width / 2;
this.explosionImage.y = this.y - this.explosionImage.getBounds().height / 2;
if (!Player.audioLoaded) {
Player.audioLoaded = true;
Assets.BAM.load();
@ -73,17 +74,26 @@ export class Player {
}
public addTo(stage: Stage) {
if (this.tooltip)
if (this.tooltip) {
stage.addChild(this.tooltip.tooltipShape);
}
stage.addChild(this.image);
if (this.explosionImage)
if (this.explosionImage) {
stage.addChild(this.explosionImage);
}
}
public removeFrom(stage: Stage) {
if (this.tooltip)
if (this.tooltip) {
stage.removeChild(this.tooltip.tooltipShape);
if (this.explosionImage)
stage.removeChild(this.explosionImage);
}
stage.removeChild(this.image);
if (this.explosionImage) {
stage.removeChild(this.explosionImage);
}
}
}

View File

@ -140,15 +140,15 @@ export class GameComponent implements OnInit, OnDestroy {
const playerEntity = this.players.get(player.id);
if (player.status === 'Alive') {
noneAlive = false;
if (playerEntity.update(player.x * Constants.BOX_SIZE, player.y * Constants.BOX_SIZE, player.direction))
if (playerEntity.update(player.x * Constants.BOX_SIZE + (player.width / 2) * Constants.BOX_SIZE, player.y * Constants.BOX_SIZE + (player.height / 2) * Constants.BOX_SIZE, player.direction))
playersMoved = true;
// Stamp down player on trails canvas so it can be erased properly when obstacles roll over it
this.trailsContext.shadowBlur = 20;
this.trailsContext.shadowColor = player.color;
this.trailsContext.fillStyle = player.color;
this.trailsContext.fillRect(Constants.BOX_SIZE * player.x + player.width / 2 * Constants.BOX_SIZE - Constants.BOX_SIZE / 2,
Constants.BOX_SIZE * player.y + player.height / 2 * Constants.BOX_SIZE - Constants.BOX_SIZE / 2,
this.trailsContext.fillRect(Constants.BOX_SIZE * player.x + (player.width / 2) * Constants.BOX_SIZE - Constants.BOX_SIZE / 2,
Constants.BOX_SIZE * player.y + (player.height / 2) * Constants.BOX_SIZE - Constants.BOX_SIZE / 2,
Constants.BOX_SIZE, Constants.BOX_SIZE);
} else if (!player.alive) {
// Ensure tooltip is hidden in case player dies before it fades out