Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Nov 6, 2023
1 parent 7a67c68 commit ccada5b
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/game/scenes/screen/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Screen extends Scene implements IScreen {
}

public failure(text?: LangPhrase, format?: any[]) {
this.game.sound.play(ScreenAudio.ERROR);
this.sound.play(ScreenAudio.ERROR);
if (text) {
this.events.emit(ScreenEvents.NOTICE, { text, format });
}
Expand Down
6 changes: 3 additions & 3 deletions src/game/scenes/world/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ export class Builder extends Phaser.Events.EventEmitter implements IBuilder {
this.variant = variant;
}

this.scene.sound.play(BuildingAudio.SELECT);
this.scene.fx.playSound(BuildingAudio.SELECT);
}

public unsetBuildingVariant() {
if (this.variant === null) {
return;
}

this.scene.sound.play(BuildingAudio.UNSELECT);
this.scene.fx.playSound(BuildingAudio.UNSELECT);

this.clearBuildingVariant();

Expand Down Expand Up @@ -336,7 +336,7 @@ export class Builder extends Phaser.Events.EventEmitter implements IBuilder {
this.scene.player.takeResources(BuildingInstance.Cost);
this.scene.player.giveExperience(DIFFICULTY.BUILDING_BUILD_EXPERIENCE);

this.scene.sound.play(BuildingAudio.BUILD);
this.scene.fx.playSound(BuildingAudio.BUILD);

if (this.variant) {
if (this.scene.game.isDesktop()) {
Expand Down
15 changes: 14 additions & 1 deletion src/game/scenes/world/effects/fx-manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Analytics } from '~lib/analytics';
import { Environment } from '~lib/environment';
import { GameFlag, GameSettings } from '~type/game';
import { IWorld } from '~type/world';
import { EffectTexture, IParticlesParent, ParticlesTexture } from '~type/world/effects';
import { IFXManager } from '~type/world/effects/fx-manager';
import { IFXManager, SoundParams } from '~type/world/effects/fx-manager';
import { IBuilding } from '~type/world/entities/building';
import { INPC } from '~type/world/entities/npc';
import { IEnemy } from '~type/world/entities/npc/enemy';
Expand All @@ -20,6 +21,18 @@ export class FXManager implements IFXManager {
this.scene = scene;
}

public playSound(key: string | string[], params: SoundParams = {}) {
try {
const sound = typeof key === 'string' ? key : Phaser.Utils.Array.GetRandom(key);

if (!params.limit || this.scene.game.sound.getAll(sound).length < params.limit) {
this.scene.fx.playSound(sound, params);
}
} catch (error) {
Analytics.TrackWarn('Failed to play sound', error as TypeError);
}
}

public createDustEffect(parent: IPlayer) {
if (!this.isEffectsEnabled()) {
return null;
Expand Down
19 changes: 8 additions & 11 deletions src/game/scenes/world/entities/building/building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class Building extends Phaser.GameObjects.Image implements IBuilding, ITi

this.scene.player.giveExperience(experience);

this.scene.game.sound.play(BuildingAudio.UPGRADE);
this.scene.fx.playSound(BuildingAudio.UPGRADE);

if (Tutorial.IsInProgress(TutorialStep.UPGRADE_BUILDING)) {
Tutorial.Complete(TutorialStep.UPGRADE_BUILDING);
Expand Down Expand Up @@ -382,7 +382,7 @@ export class Building extends Phaser.GameObjects.Image implements IBuilding, ITi

this.scene.player.takeResources(cost);

this.scene.sound.play(BuildingAudio.REPAIR);
this.scene.fx.playSound(BuildingAudio.REPAIR);
}

private upgradeHealth() {
Expand Down Expand Up @@ -490,16 +490,13 @@ export class Building extends Phaser.GameObjects.Image implements IBuilding, ITi
private onDamage() {
this.updateTileCost();

const audio = Phaser.Utils.Array.GetRandom([
this.scene.fx.createDamageEffect(this);
this.scene.fx.playSound([
BuildingAudio.DAMAGE_1,
BuildingAudio.DAMAGE_2,
]);

if (this.scene.game.sound.getAll(audio).length === 0) {
this.scene.game.sound.play(audio);
}

this.scene.fx.createDamageEffect(this);
], {
limit: 1,
});

if (
this.scene.isModeActive(WorldMode.AUTO_REPAIR)
Expand Down Expand Up @@ -731,7 +728,7 @@ export class Building extends Phaser.GameObjects.Image implements IBuilding, ITi
}

public break() {
this.scene.sound.play(BuildingAudio.DEAD);
this.scene.fx.playSound(BuildingAudio.DEAD);
this.scene.fx.createSmokeEffect(this);

const group = this.scene.getEntitiesGroup(EntityType.BUILDING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ export class BuildingAmmunition extends Building implements IBuildingAmmunition
if (this.scene.isModeActive(WorldMode.AUTO_REPAIR)) {
this.buyAmmo(true);
} else {
if (this.scene.game.sound.getAll(BuildingAudio.OVER).length === 0) {
this.scene.game.sound.play(BuildingAudio.OVER);
}
this.scene.fx.playSound(BuildingAudio.OVER, {
limit: 1,
});

Tutorial.Start(TutorialStep.BUY_AMMO);
}
Expand Down Expand Up @@ -167,7 +167,7 @@ export class BuildingAmmunition extends Building implements IBuildingAmmunition
this.scene.getEntitiesGroup(EntityType.BUILDING)
.emit(BuildingEvents.BUY_AMMO, this);

this.scene.sound.play(BuildingAudio.RELOAD);
this.scene.fx.playSound(BuildingAudio.RELOAD);

Tutorial.Complete(TutorialStep.BUY_AMMO);
}
Expand Down
36 changes: 19 additions & 17 deletions src/game/scenes/world/entities/building/variants/tower/tower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,50 +203,54 @@ export class BuildingTower extends Building implements IBuildingTower {
this.removeAlertIcon();
this.needReload = false;

if (this.scene.game.sound.getAll(BuildingAudio.RELOAD).length === 0) {
this.scene.game.sound.play(BuildingAudio.RELOAD);
}
this.scene.fx.playSound(BuildingAudio.RELOAD, {
limit: 1,
});
}
} else if (!this.needReload) {
this.addAlertIcon();
this.needReload = true;

if (this.scene.game.sound.getAll(BuildingAudio.OVER).length === 0) {
this.scene.game.sound.play(BuildingAudio.OVER);
}
this.scene.fx.playSound(BuildingAudio.OVER, {
limit: 1,
});

Tutorial.Start(TutorialStep.RELOAD_TOWER);
}
}

public getTargets() {
const enemies = this.scene.getEntities<IEnemy>(EntityType.ENEMY).filter((enemy) => {
const towerPosition = this.getBottomEdgePosition();

return this.scene.getEntities<IEnemy>(EntityType.ENEMY).filter((enemy) => {
if (
enemy.alpha >= 1.0
&& !enemy.live.isDead()
&& (!this.shotDefaultParams?.freeze || !enemy.isFreezed(true))
) {
const position = enemy.getBottomEdgePosition();
const enemyPosition = enemy.getBottomEdgePosition();

return (
this.actionsAreaContains(position)
&& !this.scene.level.hasTilesBetweenPositions(position, this.getBottomEdgePosition())
this.actionsAreaContains(enemyPosition)
&& !this.scene.level.hasTilesBetweenPositions(enemyPosition, towerPosition)
);
}

return false;
});

const enemy = getClosestByIsometricDistance(enemies, this);

return enemy ? [enemy] : [];
}

public shoot(targets: IEnemy[]) {
if (!this.shot) {
return;
}

const target = getClosestByIsometricDistance(targets, this);

if (!target) {
return;
}

const params = this.getShotCurrentParams();

if (this.power > 1.0) {
Expand All @@ -258,9 +262,7 @@ export class BuildingTower extends Building implements IBuildingTower {
}
}

targets.forEach((target) => {
this.shot?.shoot(target, params);
});
this.shot?.shoot(target, params);
}

private getBooster() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ export class BuildingTowerElectro extends BuildingTower {
}
});

if (this.scene.game.sound.getAll(BuildingAudio.ELECTRO).length === 0) {
this.scene.game.sound.play(BuildingAudio.ELECTRO);
}
this.scene.fx.playSound(BuildingAudio.ELECTRO, {
limit: 1,
});

if (this.area) {
this.area.setActive(true);
Expand Down
2 changes: 1 addition & 1 deletion src/game/scenes/world/entities/crystal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Crystal extends Phaser.GameObjects.Image implements ICrystal, ITile
this.scene.getEntitiesGroup(EntityType.CRYSTAL)
.emit(CrystalEvents.PICKUP, this, resources);

this.scene.sound.play(CrystalAudio.PICKUP);
this.scene.fx.playSound(CrystalAudio.PICKUP);

this.destroy();
}
Expand Down
21 changes: 10 additions & 11 deletions src/game/scenes/world/entities/npc/variants/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,24 @@ export class Assistant extends NPC implements IAssistant {
this.instantShot = !this.instantShot;
}

private getTarget(): Nullable<IEnemy> {
private getTarget() {
const assistantPosition = this.getBottomEdgePosition();
const maxDistance = progressionQuadratic({
defaultValue: DIFFICULTY.ASSISTANT_ATTACK_DISTANCE,
scale: DIFFICULTY.ASSISTANT_ATTACK_DISTANCE_GROWTH,
level: this.owner.upgradeLevel[PlayerSkill.ATTACK_DISTANCE],
});

const enemies = this.scene.getEntities<IEnemy>(EntityType.ENEMY).filter((enemy) => {
if (enemy.alpha < 1.0 || enemy.live.isDead()) {
return false;
}
if (enemy.alpha >= 1.0 && !enemy.live.isDead()) {
const enemyPosition = enemy.getBottomEdgePosition();

const positionFrom = this.getBottomEdgePosition();
const positionTo = enemy.getBottomEdgePosition();
return (
getIsometricDistance(enemyPosition, assistantPosition) <= maxDistance
&& !this.scene.level.hasTilesBetweenPositions(enemyPosition, assistantPosition)
);
}

return (
getIsometricDistance(positionFrom, positionTo) <= maxDistance
&& !this.scene.level.hasTilesBetweenPositions(positionFrom, positionTo)
);
return false;
});

return getClosestByIsometricDistance(enemies, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class EnemyBoss extends Enemy {
}

public onDead() {
this.scene.sound.play(EnemyAudio.ROAR);
this.scene.fx.playSound(EnemyAudio.ROAR);
super.onDead();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export class EnemyExplosive extends Enemy {
},
});

this.scene.sound.play(EffectAudio.EXPLOSION);
this.scene.fx.createExplosionEffect(this);
this.scene.fx.playSound(EffectAudio.EXPLOSION);

let targets: IEnemyTarget[] = [this.scene.player];

Expand Down
23 changes: 10 additions & 13 deletions src/game/scenes/world/entities/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class Player extends Sprite implements IPlayer {
if (this.stamina === 0.0) {
this.updateMovementAnimation();
this.scene.sound.stopByKey(PlayerAudio.WALK);
this.scene.game.sound.play(PlayerAudio.WALK, {
this.scene.fx.playSound(PlayerAudio.WALK, {
loop: true,
rate: 1.4,
});
Expand Down Expand Up @@ -352,7 +352,7 @@ export class Player extends Sprite implements IPlayer {

this.takeResources(cost);

this.scene.sound.play(PlayerAudio.SUPERSKILL);
this.scene.fx.playSound(PlayerAudio.SUPERSKILL);

if (this.scene.game.isSettingEnabled(GameSettings.EFFECTS)) {
const position = this.getBottomEdgePosition();
Expand Down Expand Up @@ -439,7 +439,7 @@ export class Player extends Sprite implements IPlayer {

this.emit(PlayerEvents.UPGRADE_SKILL, type);

this.scene.sound.play(PlayerAudio.UPGRADE);
this.scene.fx.playSound(PlayerAudio.UPGRADE);
}

private setSkillUpgrade(type: PlayerSkill, level: number) {
Expand Down Expand Up @@ -473,23 +473,20 @@ export class Player extends Sprite implements IPlayer {
public onDamage(amount: number) {
this.scene.camera.shake();

const audio = Phaser.Utils.Array.GetRandom([
this.scene.fx.createBloodEffect(this);
this.scene.fx.playSound([
PlayerAudio.DAMAGE_1,
PlayerAudio.DAMAGE_2,
PlayerAudio.DAMAGE_3,
]);

if (this.scene.game.sound.getAll(audio).length === 0) {
this.scene.game.sound.play(audio);
}

this.scene.fx.createBloodEffect(this);
], {
limit: 1,
});

super.onDamage(amount);
}

public onDead() {
this.scene.sound.play(PlayerAudio.DEAD);
this.scene.fx.playSound(PlayerAudio.DEAD);

this.setVelocity(0, 0);
this.stopMovement();
Expand Down Expand Up @@ -616,7 +613,7 @@ export class Player extends Sprite implements IPlayer {

this.dustEffect?.emitter.start();

this.scene.game.sound.play(PlayerAudio.WALK, {
this.scene.fx.playSound(PlayerAudio.WALK, {
loop: true,
rate: 1.8,
});
Expand Down
6 changes: 3 additions & 3 deletions src/game/scenes/world/entities/shot/ball/ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ export class ShotBall extends Phaser.Physics.Arcade.Image implements IShotBall {
this.scene.physics.world.enable(this, Phaser.Physics.Arcade.DYNAMIC_BODY);
this.scene.physics.moveTo(this, targetPosition.x, targetPosition.y, this.params.speed);

if (this.scene.game.sound.getAll(this.audio).length < 3) {
this.scene.game.sound.play(this.audio);
}
this.scene.fx.playSound(this.audio, {
limit: 3,
});
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
6 changes: 3 additions & 3 deletions src/game/scenes/world/entities/shot/lazer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ export class ShotLazer extends Phaser.GameObjects.Line implements IShotLazer {
this.setActive(true);
this.setVisible(true);

if (this.scene.game.sound.getAll(ShotLazerAudio.LAZER).length < 3) {
this.scene.game.sound.play(ShotLazerAudio.LAZER);
}
this.scene.fx.playSound(ShotLazerAudio.LAZER, {
limit: 3,
});
}

private stop() {
Expand Down
Loading

0 comments on commit ccada5b

Please sign in to comment.