Skip to content

Commit

Permalink
Effects refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Nov 3, 2023
1 parent 799b0be commit 4b5182e
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { ENEMY_SIZE_PARAMS, ENEMY_TEXTURE_SIZE } from '~const/world/entities/ene
import { Environment } from '~lib/environment';
import { GameFlag, GameSettings } from '~type/game';
import { IWorld } from '~type/world';
import { IParticlesParent, ParticlesTexture } from '~type/world/effects';
import { IParticlesManager } from '~type/world/effects/particles-manager';
import { EffectTexture, IParticlesParent, ParticlesTexture } from '~type/world/effects';
import { IFXManager } from '~type/world/effects/fx-manager';
import { IBuilding } from '~type/world/entities/building';
import { INPC } from '~type/world/entities/npc';
import { EnemyTexture, IEnemy } from '~type/world/entities/npc/enemy';
import { IPlayer } from '~type/world/entities/player';
import { ISprite } from '~type/world/entities/sprite';
import { PositionAtWorld } from '~type/world/level';

import { Effect } from './effect';
import { Particles } from './particles';

export class ParticlesManager implements IParticlesManager {
export class FXManager implements IFXManager {
private scene: IWorld;

constructor(scene: IWorld) {
Expand Down Expand Up @@ -49,7 +50,7 @@ export class ParticlesManager implements IParticlesManager {
!parent.active
|| !Environment.GetFlag(GameFlag.BLOOD)
|| !this.isEffectsEnabled()
|| ParticlesManager.IsExist(parent, 'blood')
|| FXManager.IsExist(parent, 'blood')
) {
return null;
}
Expand All @@ -76,7 +77,7 @@ export class ParticlesManager implements IParticlesManager {
if (
!parent.active
|| !this.isEffectsEnabled()
|| ParticlesManager.IsExist(parent, 'froze')
|| FXManager.IsExist(parent, 'froze')
) {
return null;
}
Expand All @@ -103,7 +104,7 @@ export class ParticlesManager implements IParticlesManager {
if (
!parent.active
|| !this.isEffectsEnabled()
|| ParticlesManager.IsExist(parent, 'fire')
|| FXManager.IsExist(parent, 'fire')
) {
return null;
}
Expand Down Expand Up @@ -162,7 +163,7 @@ export class ParticlesManager implements IParticlesManager {
if (
!parent.active
|| !this.isEffectsEnabled()
|| ParticlesManager.IsExist(parent, 'lazer')
|| FXManager.IsExist(parent, 'lazer')
) {
return null;
}
Expand Down Expand Up @@ -239,7 +240,7 @@ export class ParticlesManager implements IParticlesManager {
public createHealEffect(parent: ISprite, params: { duration: number }) {
if (
!this.isEffectsEnabled()
|| ParticlesManager.IsExist(parent, 'heal')
|| FXManager.IsExist(parent, 'heal')
) {
return null;
}
Expand Down Expand Up @@ -291,6 +292,59 @@ export class ParticlesManager implements IParticlesManager {
});
}

public createExplosionEffect(parent: ISprite) {
if (!this.isEffectsEnabled()) {
return null;
}

return new Effect(this.scene, {
texture: EffectTexture.EXPLOSION,
position: parent.body.center,
depth: parent.depth + 1,
});
}

public createBloodStainEffect(position: PositionAtWorld) {
if (
!this.isEffectsEnabled()
|| !Environment.GetFlag(GameFlag.BLOOD)
) {
return null;
}

return new Effect(this.scene, {
texture: EffectTexture.BLOOD,
position,
staticFrame: Phaser.Math.Between(0, 3),
});
}

public createDamageEffect(building: IBuilding) {
if (!this.isEffectsEnabled()) {
return null;
}

return new Effect(this.scene, {
texture: EffectTexture.DAMAGE,
position: building.getTopFace(),
depth: building.depth + 1,
rate: 14,
});
}

public createSmokeEffect(building: IBuilding) {
if (!this.isEffectsEnabled()) {
return null;
}

return new Effect(this.scene, {
texture: EffectTexture.SMOKE,
position: building.getBottomFace(),
depth: building.depth + 1,
rate: 18,
});
}

private isEffectsEnabled() {
return this.scene.game.isSettingEnabled(GameSettings.EFFECTS);
}
Expand Down
25 changes: 4 additions & 21 deletions src/game/scenes/world/entities/building/building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import { Live } from '~entity/addons/live';
import { Assets } from '~lib/assets';
import { progressionQuadratic, progressionLinear } from '~lib/progression';
import { Tutorial } from '~lib/tutorial';
import { Effect } from '~scene/world/effects';
import { Level } from '~scene/world/level';
import { GameEvents, GameSettings } from '~type/game';
import { GameEvents } from '~type/game';
import { LangPhrase } from '~type/lang';
import { ILive, LiveEvents } from '~type/live';
import { TutorialStep } from '~type/tutorial';
import { IWorld, WorldEvents, WorldMode } from '~type/world';
import { BuilderEvents } from '~type/world/builder';
import { EffectTexture } from '~type/world/effects';
import { EntityType } from '~type/world/entities';
import {
BuildingData, BuildingEvents, BuildingAudio,
Expand Down Expand Up @@ -491,18 +489,11 @@ export class Building extends Phaser.GameObjects.Image implements IBuilding, ITi
this.scene.game.sound.play(audio);
}

this.scene.fx.createDamageEffect(this);

if (this.scene.isModeActive(WorldMode.AUTO_REPAIR)) {
this.autoRepair();
}

if (this.scene.game.isSettingEnabled(GameSettings.EFFECTS)) {
new Effect(this.scene, {
texture: EffectTexture.DAMAGE,
position: this.getTopFace(),
depth: this.depth + 1,
rate: 14,
});
}
}

private onDead() {
Expand Down Expand Up @@ -728,15 +719,7 @@ export class Building extends Phaser.GameObjects.Image implements IBuilding, ITi

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

if (this.scene.game.isSettingEnabled(GameSettings.EFFECTS)) {
new Effect(this.scene, {
texture: EffectTexture.SMOKE,
position: this.getBottomFace(),
depth: this.depth + 1,
rate: 18,
});
}
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 @@ -70,6 +70,6 @@ export class BuildingGenerator extends Building {

private generateResource() {
this.scene.player.giveResources(1);
this.scene.particles.createGenerationEffect(this);
this.scene.fx.createGenerationEffect(this);
}
}
2 changes: 1 addition & 1 deletion src/game/scenes/world/entities/npc/npc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class NPC extends Sprite implements INPC {
this.freezeTimestamp = this.scene.getTime() + duration;

if (effects) {
this.scene.particles.createFrozeEffect(this);
this.scene.fx.createFrozeEffect(this);

if (this.freezeEffectTimer) {
this.freezeEffectTimer.elapsed = 0;
Expand Down
28 changes: 9 additions & 19 deletions src/game/scenes/world/entities/npc/variants/enemy/enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import {
import { Building } from '~entity/building';
import { NPC } from '~entity/npc';
import { Assets } from '~lib/assets';
import { Environment } from '~lib/environment';
import { progressionLinear, progressionQuadratic } from '~lib/progression';
import { Effect } from '~scene/world/effects';
import { GameFlag, GameSettings } from '~type/game';
import { GameSettings } from '~type/game';
import { InterfaceFont } from '~type/interface';
import { IWorld } from '~type/world';
import { EffectTexture } from '~type/world/effects';
import { EntityType } from '~type/world/entities';
import {
IEnemyTarget, EnemyData, EnemyTexture, IEnemy, EnemyAudio,
Expand Down Expand Up @@ -251,24 +248,17 @@ export class Enemy extends NPC implements IEnemy {
}

private addBloodEffect() {
if (
!this.currentBiome?.solid
|| !this.scene.game.isSettingEnabled(GameSettings.EFFECTS)
|| !Environment.GetFlag(GameFlag.BLOOD)
) {
if (!this.currentBiome?.solid) {
return;
}

const position = this.getBottomFace();
const effect = new Effect(this.scene, {
texture: EffectTexture.BLOOD,
position,
staticFrame: Phaser.Math.Between(0, 3),
});

effect.setAlpha(0.8);
const effect = this.scene.fx.createBloodStainEffect(position);

this.scene.level.effectsOnGround.push(effect);
if (effect) {
effect.setAlpha(0.8);
this.scene.level.effectsOnGround.push(effect);
}
}

private addSpawnEffect() {
Expand All @@ -289,7 +279,7 @@ export class Enemy extends NPC implements IEnemy {
});
}, 0);

this.scene.particles.createSpawnEffect(this);
this.scene.fx.createSpawnEffect(this);
}

private handlePlayerSuperskill() {
Expand All @@ -315,7 +305,7 @@ export class Enemy extends NPC implements IEnemy {
retardationLevel: DIFFICULTY.ENEMY_HEALTH_GROWTH_RETARDATION_LEVEL,
}) * DIFFICULTY.SUPERSKILL_FIRE_FORCE;

this.scene.particles.createLongFireEffect(this, { duration });
this.scene.fx.createLongFireEffect(this, { duration });
this.addOngoingDamage(damage, duration);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import {
} from '~const/world/entities/enemy';
import { LEVEL_MAP_PERSPECTIVE } from '~const/world/level';
import { getIsometricDistance } from '~lib/dimension';
import { Effect } from '~scene/world/effects';
import { GameSettings } from '~type/game';
import { IWorld } from '~type/world';
import { EffectAudio, EffectTexture } from '~type/world/effects';
import { EffectAudio } from '~type/world/effects';
import { EntityType } from '~type/world/entities';
import { IBuilding } from '~type/world/entities/building';
import {
Expand Down Expand Up @@ -52,14 +50,7 @@ export class EnemyExplosive extends Enemy {
});

this.scene.sound.play(EffectAudio.EXPLOSION);

if (this.scene.game.isSettingEnabled(GameSettings.EFFECTS)) {
new Effect(this.scene, {
texture: EffectTexture.EXPLOSION,
position: this.body.center,
depth: this.depth + 1,
});
}
this.scene.fx.createExplosionEffect(this);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class EnemyTelepath extends Enemy {

enemy.live.addHealth(healthAmount);

this.scene.particles.createHealEffect(enemy, {
this.scene.fx.createHealEffect(enemy, {
duration: ENEMY_REGENERATION_EFFECT_DURATION,
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/game/scenes/world/entities/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export class Player extends Sprite implements IPlayer {
this.scene.game.sound.play(audio);
}

this.scene.particles.createBloodEffect(this);
this.scene.fx.createBloodEffect(this);

super.onDamage(amount);
}
Expand Down Expand Up @@ -679,7 +679,7 @@ export class Player extends Sprite implements IPlayer {
return;
}

this.dustEffect = this.scene.particles.createDustEffect(this);
this.dustEffect = this.scene.fx.createDustEffect(this);
}

private removeDustEffect() {
Expand Down
2 changes: 1 addition & 1 deletion src/game/scenes/world/entities/shot/ball/ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ShotBall extends Phaser.Physics.Arcade.Image implements IShotBall {
this.setVisible(true);

if (this.glow) {
this.effect = this.scene.particles.createGlowEffect(this, {
this.effect = this.scene.fx.createGlowEffect(this, {
speed: this.params.speed,
color: this.color,
});
Expand Down
2 changes: 1 addition & 1 deletion src/game/scenes/world/entities/shot/ball/variants/fire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ShotBallFire extends ShotBall {
public hit(target: IEnemy) {
super.hit(target);

this.scene.particles.createFireEffect(target);
this.scene.fx.createFireEffect(target);

if (this.params.damage) {
this.spreadDamage(target, this.params.damage * SHOT_BALL_DAMAGE_SPREAD_FACTOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ShotBallSimple extends ShotBall {
public hit(target: IEnemy) {
super.hit(target);

this.scene.particles.createBloodEffect(target);
this.scene.fx.createBloodEffect(target);

if (this.params.damage) {
target.live.damage(this.params.damage);
Expand Down
5 changes: 1 addition & 4 deletions src/game/scenes/world/entities/shot/lazer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { WORLD_DEPTH_EFFECT } from '~const/world';
import { SHOT_LAZER_DELAY, SHOT_LAZER_REPEAT } from '~const/world/entities/shot';
import { Assets } from '~lib/assets';
import { getIsometricDistance } from '~lib/dimension';
import { Particles } from '~scene/world/effects';
import { GameSettings } from '~type/game';
import { IWorld } from '~type/world';
import { ParticlesTexture } from '~type/world/effects';
import { EntityType } from '~type/world/entities';
import { IEnemy } from '~type/world/entities/npc/enemy';
import {
Expand Down Expand Up @@ -104,7 +101,7 @@ export class ShotLazer extends Phaser.GameObjects.Line implements IShotLazer {
return;
}

this.scene.particles.createLazerEffect(this.target);
this.scene.fx.createLazerEffect(this.target);

const momentDamage = this.params.damage / SHOT_LAZER_REPEAT;

Expand Down
Loading

0 comments on commit 4b5182e

Please sign in to comment.