@@ -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
2425export 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}
0 commit comments