Skip to content

Commit 815e171

Browse files
committed
- fix stupid performance problem
1 parent 5c13327 commit 815e171

File tree

14 files changed

+177
-102
lines changed

14 files changed

+177
-102
lines changed
64 Bytes
Loading

src/game-objects/ennemy.game-object.ts

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ export interface EnnemyConfig {
1414
speed: number;
1515
patrolSpeed: number;
1616
sprite: EnnemyTag;
17-
attackSprite?: EnnemyTag;
17+
attackSprite: EnnemyTag;
1818
hp: number;
1919
range: number;
2020
atkCooldown: number;
2121
damage: number;
22+
id: number;
2223
}
2324

2425
export class Ennemy extends ArcadeSprite {
@@ -29,7 +30,7 @@ export class Ennemy extends ArcadeSprite {
2930
private _patrolDirection = 1;
3031
private _patrolTween: Phaser.Tweens.Tween | null = null;
3132
private _config: EnnemyConfig;
32-
private _attacking = false;
33+
private _isAttacking = false;
3334
private _canAttack = true;
3435
private _defaultWidth: number;
3536
private _attackHitbox: Phaser.GameObjects.Rectangle & {
@@ -61,7 +62,7 @@ export class Ennemy extends ArcadeSprite {
6162
isAttacking: false,
6263
});
6364

64-
const attackHitbox = this.scene.add.rectangle(0, 0, 32, 32, 0xffffff, 0);
65+
const attackHitbox = this.scene.add.rectangle(0, 0, this._config.range, 32, 0xffffff, 0);
6566

6667
this._attackHitbox = this._physics.add.existing(attackHitbox) as Phaser.GameObjects.Rectangle & {
6768
body: ArcadeBody;
@@ -71,8 +72,6 @@ export class Ennemy extends ArcadeSprite {
7172
this._attackHitbox.body.setEnable(false);
7273
this._physics.world.remove(this._attackHitbox.body);
7374

74-
this._physics.add.collider(this._attackHitbox, this._player);
75-
7675
this._physics.add.overlap(
7776
this._attackHitbox,
7877
this._player,
@@ -83,18 +82,17 @@ export class Ennemy extends ArcadeSprite {
8382
this
8483
);
8584

86-
this.createAnimations();
8785
this.startPatrol();
8886
}
8987

9088
public update(): void {
91-
if (this._config.hp <= 0) {
89+
if (this._config.hp <= 0 || this._isAttacking) {
9290
return;
9391
}
9492

9593
this.updateFlipX();
9694

97-
const distanceToPlayer = Phaser.Math.Distance.Between(this.x, this.y, this._player.x, this._player.y);
95+
const distanceToPlayer = GameHelper.getEdgeToEdgeDistance(this, this._player);
9896

9997
if (distanceToPlayer <= this._config.range) {
10098
this.attack();
@@ -148,7 +146,7 @@ export class Ennemy extends ArcadeSprite {
148146
}
149147

150148
private attack(): void {
151-
if (this._attacking || !this._canAttack) {
149+
if (this._isAttacking || !this._canAttack) {
152150
return;
153151
}
154152

@@ -163,7 +161,7 @@ export class Ennemy extends ArcadeSprite {
163161
this._attackHitbox.body.setEnable(true);
164162
this._physics.world.add(this._attackHitbox.body);
165163

166-
this._attacking = true;
164+
this._isAttacking = true;
167165
this._canAttack = false;
168166
this.body.setVelocityX(0);
169167
GameHelper.animate(this, AnimationTag.ENNEMY_ATTACK);
@@ -177,7 +175,7 @@ export class Ennemy extends ArcadeSprite {
177175
this._physics.world.remove(this._attackHitbox.body);
178176

179177
this.anims.play(AnimationTag.ENNEMY_IDLE);
180-
this._attacking = false;
178+
this._isAttacking = false;
181179
this.setbodySize({
182180
isAttacking: false,
183181
});
@@ -234,66 +232,4 @@ export class Ennemy extends ArcadeSprite {
234232
this.body.setSize(this._defaultWidth, hitboxHeight);
235233
}
236234
}
237-
238-
private createAnimations(): void {
239-
this.anims.create({
240-
key: AnimationTag.ENNEMY_IDLE,
241-
frames: this.anims.generateFrameNumbers(this._config.sprite, {
242-
start: 0,
243-
end: 3,
244-
}),
245-
frameRate: 5,
246-
repeat: -1,
247-
});
248-
249-
this.anims.create({
250-
key: AnimationTag.ENNEMY_MOVING,
251-
frames: this.anims.generateFrameNumbers(this._config.sprite, {
252-
start: 4,
253-
end: 7,
254-
}),
255-
frameRate: 10,
256-
repeat: -1,
257-
});
258-
259-
this.anims.create({
260-
key: AnimationTag.ENNEMY_HURT,
261-
frames: this.anims.generateFrameNumbers(this._config.sprite, {
262-
start: 8,
263-
end: 9,
264-
}),
265-
frameRate: 20,
266-
repeat: 3,
267-
});
268-
269-
this.anims.create({
270-
key: AnimationTag.ENNEMY_DEATH,
271-
frames: this.anims.generateFrameNumbers(this._config.sprite, {
272-
start: 10,
273-
end: 11,
274-
}),
275-
frameRate: 20,
276-
repeat: 3,
277-
});
278-
279-
const attackSprite = this._config.attackSprite || this._config.sprite;
280-
281-
this.anims.create({
282-
key: AnimationTag.ENNEMY_ATTACK,
283-
frames: this.anims.generateFrameNumbers(attackSprite, {
284-
start: 0,
285-
end: 4,
286-
}),
287-
frameRate: 10,
288-
});
289-
290-
this.anims.create({
291-
key: AnimationTag.ENNEMY_ATTACK_HITBOX,
292-
frames: this.anims.generateFrameNumbers(attackSprite, {
293-
start: 0,
294-
end: 4,
295-
}),
296-
frameRate: 10,
297-
});
298-
}
299235
}

src/game-objects/hero.game-object.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class Hero extends ArcadeSprite {
9999
if (this._hp <= 0) {
100100
return;
101101
}
102+
102103
this.handleMovements(time);
103104
this.handleProjectiles();
104105
}

src/helpers/animations.manager.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Scene } from "phaser";
2+
3+
export abstract class AnimationsManager<Tag extends string> {
4+
private _registeredAnimations: Partial<Record<Tag, boolean>> = {};
5+
6+
constructor(protected scene: Scene) {}
7+
8+
public register(sprite: Tag, attackSprite: Tag): void {
9+
if (this._registeredAnimations[sprite]) {
10+
return;
11+
}
12+
13+
this._registeredAnimations[sprite] = true;
14+
15+
this.createAnimations(sprite, attackSprite);
16+
}
17+
18+
protected abstract createAnimations(sprite: Tag, attackSprite?: Tag): void;
19+
}

src/helpers/game.helper.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Sprite } from "../phaser-aliases";
2-
import { AnimationTag } from "../tags";
1+
import { Sprite, Tween } from "@phaser-aliases";
2+
import { AnimationTag } from "@tags";
3+
import { Scene } from "phaser";
34

45
export class GameHelper {
56
public static get isDev(): boolean {
@@ -47,4 +48,22 @@ export class GameHelper {
4748
public static getAnimationRepetition(duration: number, frameCount: number, frameRate: number): number {
4849
return ((duration / 1000) * frameRate) / frameCount;
4950
}
51+
52+
// TODO: make it works
53+
public static getEdgeToEdgeDistance(sprite1: Sprite, sprite2: Sprite): number {
54+
const distance = Phaser.Math.Distance.BetweenPoints(sprite1.getBounds(), sprite2.getBounds());
55+
56+
return distance;
57+
}
58+
59+
public static flashSprite(scene: Scene, sprite: Sprite): Tween {
60+
return scene.tweens.add({
61+
targets: sprite,
62+
alpha: { from: 1, to: 0 },
63+
ease: "Linear",
64+
duration: 100,
65+
repeat: -1,
66+
yoyo: true,
67+
});
68+
}
5069
}

src/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './animations.manager';
12
export * from './function';
23
export * from './game.helper';
34
export * from './hero.state';

src/levels/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './1-forest.level';
1+
export * from './level-1';
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Scene } from "phaser";
2-
import { depthsConfig } from "../configs";
3-
import { Ennemy, EnnemyConfig, Hero } from "../game-objects";
4-
import { GameHelper, isEnumValue } from "../helpers";
5-
import { Sprite, Tilemap } from "../phaser-aliases";
1+
import { depthsConfig } from "@configs";
2+
import { Ennemy, EnnemyConfig, Hero } from "@game-objects";
3+
import { GameHelper, isEnumValue } from "@helpers";
4+
import { Sprite, Tilemap } from "@phaser-aliases";
65
import {
76
BackgroundSound,
87
EnnemyTag,
@@ -13,7 +12,9 @@ import {
1312
TilemapObjectsType,
1413
TilemapTag,
1514
TilesetTag,
16-
} from "../tags";
15+
} from "@tags";
16+
import { Scene } from "phaser";
17+
import { ForestAnimations } from "./forest.animations";
1718

1819
type ValueOf<T> = T[keyof T];
1920

@@ -82,12 +83,16 @@ export class ForestLevel {
8283
{ name: "plant3", tag: TilesetTag.FOREST_PLANT_3_ANIMATION },
8384
];
8485

86+
private _animationsManager: ForestAnimations;
87+
8588
constructor(public hero: Hero, private _scene: Scene) {
8689
this._scene.sound.play(BackgroundSound.RIVER_FLOWING_INSECTS, {
8790
loop: true,
8891
volume: GameHelper.audioIsEnabled ? 1 : 0,
8992
});
9093

94+
this._animationsManager = new ForestAnimations(_scene);
95+
9196
this._mainCam.setBounds(0, 0, this._sceneWidth, this._scene.scale.height);
9297

9398
this.addBackgrounds();
@@ -190,7 +195,7 @@ export class ForestLevel {
190195

191196
const patrols = positions.filter((position) => position.type === "ennemy_patrol");
192197

193-
patrols.forEach((patrol) => {
198+
patrols.forEach((patrol, index) => {
194199
if (!patrol.x || !patrol.y || !patrol.polyline || patrol.polyline.length < 2) {
195200
throw Error("Invalid patrol object");
196201
}
@@ -213,11 +218,14 @@ export class ForestLevel {
213218
attackSprite: EnnemyTag.PIXIE_ATTACK,
214219
scene: this._scene,
215220
hp: 30,
216-
range: 40,
221+
range: 30,
217222
atkCooldown: 1000,
218223
damage: 1,
224+
id: index,
219225
};
220226

227+
this._animationsManager.register(ennemyConfig.sprite, ennemyConfig.attackSprite);
228+
221229
const ennemyObject = new Ennemy(ennemyConfig, this.hero);
222230

223231
this._ennemies.add(ennemyObject);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { AnimationsManager } from "@helpers";
2+
import { AnimationTag, EnnemyTag } from "@tags";
3+
4+
export class ForestAnimations extends AnimationsManager<EnnemyTag> {
5+
protected createAnimations(sprite: EnnemyTag, attackSprite?: EnnemyTag): void {
6+
this.scene.anims.create({
7+
key: AnimationTag.ENNEMY_IDLE,
8+
frames: this.scene.anims.generateFrameNumbers(sprite, {
9+
start: 0,
10+
end: 3,
11+
}),
12+
frameRate: 5,
13+
repeat: -1,
14+
});
15+
16+
this.scene.anims.create({
17+
key: AnimationTag.ENNEMY_MOVING,
18+
frames: this.scene.anims.generateFrameNumbers(sprite, {
19+
start: 4,
20+
end: 7,
21+
}),
22+
frameRate: 10,
23+
repeat: -1,
24+
});
25+
26+
this.scene.anims.create({
27+
key: AnimationTag.ENNEMY_HURT,
28+
frames: this.scene.anims.generateFrameNumbers(sprite, {
29+
start: 8,
30+
end: 9,
31+
}),
32+
frameRate: 20,
33+
repeat: 3,
34+
});
35+
36+
this.scene.anims.create({
37+
key: AnimationTag.ENNEMY_DEATH,
38+
frames: this.scene.anims.generateFrameNumbers(sprite, {
39+
start: 10,
40+
end: 11,
41+
}),
42+
frameRate: 20,
43+
repeat: 3,
44+
});
45+
46+
if (attackSprite) {
47+
this.scene.anims.create({
48+
key: AnimationTag.ENNEMY_ATTACK,
49+
frames: this.scene.anims.generateFrameNumbers(attackSprite, {
50+
start: 0,
51+
end: 4,
52+
}),
53+
frameRate: 10,
54+
});
55+
}
56+
}
57+
}

src/levels/level-1/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './1-forest.level';
2+
export * from './forest.animations';

0 commit comments

Comments
 (0)