Skip to content

Commit

Permalink
Fix all linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Poupin committed Jul 18, 2018
1 parent 815814c commit 00309ae
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"trailingComma": "es5",
"trailingComma": "all",
"singleQuote": true,
"bracketSpacing": true,
"tabWidth": 2,
Expand Down
4 changes: 2 additions & 2 deletions src/constants/map-content-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export const mapContentKeys = {
NPCS: 'npcs',
TREANTS: 'treants',
ZONES: 'zones',
}
}
},
};
4 changes: 2 additions & 2 deletions src/constants/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const maps = {
second: {
file: 'second-map.json',
key: 'second',
}
}
},
};
8 changes: 4 additions & 4 deletions src/game-objects/Arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Player } from './Player';
const ARROW_SPEED = 150;

export class Arrow {
scene: Phaser.Scene;
player: Player;
gameObject: Phaser.Physics.Arcade.Sprite;
public gameObject: Phaser.Physics.Arcade.Sprite;
private scene: Phaser.Scene;
private player: Player;

constructor(scene: Phaser.Scene, player, direction) {
this.scene = scene;
Expand All @@ -24,7 +24,7 @@ export class Arrow {
player.gameObject.x,
player.gameObject.y,
spriteName,
0
0,
);

switch (direction) {
Expand Down
16 changes: 8 additions & 8 deletions src/game-objects/Npc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { AbstractScene } from '../scenes/AbstractScene';
const TEXT_VERTICAL_SHIFT = 10;

export class Npc {
scene: AbstractScene;
gameObject: Phaser.Physics.Arcade.Sprite;
textGameObject: Phaser.GameObjects.Text;
public gameObject: Phaser.Physics.Arcade.Sprite;
private scene: AbstractScene;
private textGameObject: Phaser.GameObjects.Text;

constructor(scene: AbstractScene, x: number, y: number, text: string) {
this.scene = scene;
Expand All @@ -14,22 +14,22 @@ export class Npc {
this.textGameObject.setWordWrapWidth(150);
this.textGameObject.setPosition(
this.gameObject.x + (this.gameObject.width - this.textGameObject.width) / 2,
this.gameObject.y - this.textGameObject.height - TEXT_VERTICAL_SHIFT
this.gameObject.y - this.textGameObject.height - TEXT_VERTICAL_SHIFT,
);
this.textGameObject.setAlpha(0);
this.gameObject.setImmovable(true);
}

talk = () => {
public talk = () => {
this.textGameObject.setAlpha(1);
this.scene.time.addEvent({
delay: 3000,
callback: this.hideText,
callbackScope: this,
});
};
}

hideText = () => {
private hideText = () => {
this.textGameObject.setAlpha(0);
};
}
}
138 changes: 69 additions & 69 deletions src/game-objects/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { Arrow } from './Arrow';
import { AbstractScene } from '../scenes/AbstractScene';
import { registry as REGISTRY_KEYS } from '../constants/registry';

const HIT_DELAY = 500; //0.5s
const HIT_DELAY = 500;
const PLAYER_SPEED = 100;
const PLAYER_SHOOTING_TIME = 300;
const DISTANCE_BETWEEN_HEARTS = 15;
const PLAYER_RELOAD = 500;
const MAX_HP = 3;

export class Player {
scene: AbstractScene;
maxHp: number;
gameObject: Phaser.Physics.Arcade.Sprite;
orientation: 'up' | 'down' | 'left' | 'right';
lastTimeHit: number;
isLoading: boolean;
isShooting: boolean;
tomb: Phaser.GameObjects.Sprite;
hearts: Array<Phaser.GameObjects.Sprite>;
public gameObject: Phaser.Physics.Arcade.Sprite;
private scene: AbstractScene;
private maxHp: number;
private orientation: 'up' | 'down' | 'left' | 'right';
private lastTimeHit: number;
private isLoading: boolean;
private isShooting: boolean;
private tomb: Phaser.GameObjects.Sprite;
private hearts: Phaser.GameObjects.Sprite[];

constructor(scene: AbstractScene, x: number, y: number) {
this.scene = scene;
Expand All @@ -43,20 +43,60 @@ export class Player {
this.initHearts();
}

getHp(): number {
public update(keyPressed) {
if (!this.gameObject.active) {
return;
}
this.gameObject.setVelocity(0);
this.handleMovement(keyPressed);

if (keyPressed.space) {
this.punch();
}

const noKeyPressed = Object.values(keyPressed).filter(x => x).length === 0;
if (noKeyPressed && !this.isLoading) {
this.beIdle();
}

this.handleShootKey(keyPressed);
}

public canGetHit() {
return new Date().getTime() - this.lastTimeHit > HIT_DELAY;
}

public loseHp() {
this.addHp(-1);
this.updateHearts();

this.lastTimeHit = new Date().getTime();

if (this.getHp() > 0) {
return;
}

// Player dies
if (!this.tomb) {
this.tomb = this.scene.add.sprite(this.gameObject.x, this.gameObject.y, 'tomb').setScale(0.1);
}
this.gameObject.destroy();
}

private getHp(): number {
return this.scene.registry.get(REGISTRY_KEYS.PLAYER.HP);
}

setHp(newHp: number) {
private setHp(newHp: number) {
this.scene.registry.set(REGISTRY_KEYS.PLAYER.HP, newHp);
}

addHp(hpToAdd: number) {
private addHp(hpToAdd: number) {
const hp = this.scene.registry.get(REGISTRY_KEYS.PLAYER.HP);
this.setHp(hp + hpToAdd);
}

initHearts() {
private initHearts() {
Array(MAX_HP)
.fill(0)
.map((_, i) => {
Expand All @@ -76,15 +116,15 @@ export class Player {
});
}

updateHearts() {
private updateHearts() {
this.hearts.map((heart, index) => {
if (index >= this.getHp()) {
heart.setAlpha(0);
}
});
}

reload() {
private reload() {
this.isLoading = true;
this.scene.time.addEvent({
delay: PLAYER_RELOAD,
Expand All @@ -93,11 +133,11 @@ export class Player {
});
}

readyToFire() {
private readyToFire() {
this.isLoading = false;
}

go(direction, shouldAnimate = true) {
private go(direction, shouldAnimate = true) {
switch (direction) {
case 'left':
this.gameObject.setVelocityX(-PLAYER_SPEED);
Expand All @@ -124,7 +164,7 @@ export class Player {
this.gameObject.play(direction, true);
}

handleHorizontalMovement(keyPressed) {
private handleHorizontalMovement(keyPressed) {
const isUpDownPressed = keyPressed.up || keyPressed.down;

if (keyPressed.left) {
Expand All @@ -138,23 +178,23 @@ export class Player {
}
}

handleVerticalMovement(keyPressed) {
private handleVerticalMovement(keyPressed) {
if (keyPressed.up) {
this.go('up');
} else if (keyPressed.down) {
this.go('down');
}
}

handleMovement(keyPressed) {
private handleMovement(keyPressed) {
if (this.isShooting) {
return;
}
this.handleHorizontalMovement(keyPressed);
this.handleVerticalMovement(keyPressed);
}

punch() {
private punch() {
const animSwitch = {
down: { flip: false, anim: 'attack-down' },
up: { flip: false, anim: 'attack-up' },
Expand All @@ -166,7 +206,7 @@ export class Player {
this.gameObject.play(animSwitch[this.orientation].anim, true);
}

beIdle() {
private beIdle() {
const animSwitch = {
down: { flip: false, anim: 'idle-down' },
up: { flip: false, anim: 'idle-up' },
Expand All @@ -177,11 +217,11 @@ export class Player {
this.gameObject.play(animSwitch[this.orientation].anim, true);
}

endShoot = () => {
private endShoot = () => {
this.isShooting = false;
}
}

shoot() {
private shoot() {
this.isShooting = true;
this.scene.time.addEvent({
delay: PLAYER_SHOOTING_TIME,
Expand All @@ -202,7 +242,7 @@ export class Player {
return arrow;
}

handleShootKey(keyPressed) {
private handleShootKey(keyPressed) {
if (keyPressed.shift) {
if (this.isLoading) {
return;
Expand All @@ -216,49 +256,9 @@ export class Player {
this.scene.physics.add.collider(
arrowGameObject,
treant.gameObject,
treant.treantLoseHp(arrowGameObject)
)
treant.treantLoseHp(arrowGameObject),
),
);
}
}

update(keyPressed) {
if (!this.gameObject.active) {
return;
}
this.gameObject.setVelocity(0);
this.handleMovement(keyPressed);

if (keyPressed.space) {
this.punch();
}

const noKeyPressed = Object.values(keyPressed).filter(x => x).length === 0;
if (noKeyPressed && !this.isLoading) {
this.beIdle();
}

this.handleShootKey(keyPressed);
}

loseHp() {
this.addHp(-1);
this.updateHearts();

this.lastTimeHit = new Date().getTime();

if (this.getHp() > 0) {
return;
}

// Player dies
if (!this.tomb) {
this.tomb = this.scene.add.sprite(this.gameObject.x, this.gameObject.y, 'tomb').setScale(0.1);
}
this.gameObject.destroy();
}

canGetHit() {
return new Date().getTime() - this.lastTimeHit > HIT_DELAY;
}
}
Loading

0 comments on commit 00309ae

Please sign in to comment.