11import { Scene } from "phaser" ;
22import { depthsConfig } from "../configs" ;
33import { GameHelper } from "../helpers" ;
4- import { ArcadeBody , Sprite } from "../phaser-aliases" ;
5- import { AnimationTag , EnnemyTag , SfxTag , SpritesheetTag } from "../tags" ;
4+ import { ANIMATION , ArcadeBody , ArcadeSprite } from "../phaser-aliases" ;
5+ import { AnimationTag , EnnemyTag , SfxTag } from "../tags" ;
66import { Hero } from "./hero.game-object" ;
77
88export interface EnnemyConfig {
@@ -14,57 +14,74 @@ export interface EnnemyConfig {
1414 speed : number ;
1515 patrolSpeed : number ;
1616 sprite : EnnemyTag ;
17+ attackSprite ?: EnnemyTag ;
1718 hp : number ;
1819 range : number ;
1920 atkCooldown : number ;
2021 damage : number ;
2122}
2223
23- export class Ennemy extends Sprite {
24+ export class Ennemy extends ArcadeSprite {
2425 public declare body : ArcadeBody ;
2526
2627 private _player : Hero ;
27- private _atkHitbox : Phaser . Types . Physics . Arcade . SpriteWithDynamicBody ;
2828 private _startingX : number ;
2929 private _patrolDirection = 1 ;
3030 private _patrolTween : Phaser . Tweens . Tween | null = null ;
3131 private _config : EnnemyConfig ;
3232 private _attacking = false ;
3333 private _canAttack = true ;
34+ private _defaultWidth : number ;
35+ private _attackHitbox : Phaser . GameObjects . Rectangle & {
36+ body : ArcadeBody ;
37+ } ;
3438
3539 private get _isPatrolling ( ) : boolean {
3640 return this . _patrolTween !== null ;
3741 }
42+ private get _physics ( ) : Phaser . Physics . Arcade . ArcadePhysics {
43+ return this . scene . physics ;
44+ }
3845
3946 constructor ( config : EnnemyConfig , player : Hero ) {
4047 super ( config . scene , config . x , config . y , config . sprite ) ;
4148 this . _startingX = config . x ;
4249 this . _config = config ;
4350
4451 this . _player = player ;
52+ this . _defaultWidth = this . width ;
4553
4654 this . scene . add . existing ( this ) ;
47- this . scene . physics . add . existing ( this ) ;
55+ this . _physics . add . existing ( this ) ;
4856
49- const hitbox = this . scene . add
50- . sprite ( this . x , this . y , SpritesheetTag . PIXIE_HITBOX )
51- . setVisible ( false )
52- . setDepth ( depthsConfig . atkHitbox ) ;
57+ this . flipX = true ;
5358
54- this . _atkHitbox = this . scene . physics . add . existing ( hitbox ) as Phaser . Types . Physics . Arcade . SpriteWithDynamicBody ;
55- this . scene . add . existing ( this . _atkHitbox ) ;
59+ this . setDepth ( depthsConfig . ennemies ) ;
60+ this . setbodySize ( {
61+ isAttacking : false ,
62+ } ) ;
5663
57- this . _atkHitbox . body . setAllowGravity ( false ) ;
58- this . _atkHitbox . body . setEnable ( false ) ;
59- this . scene . physics . world . remove ( this . _atkHitbox . body ) ;
64+ const attackHitbox = this . scene . add . rectangle ( 0 , 0 , 32 , 32 , 0xffffff , 0 ) ;
6065
61- this . flipX = true ;
66+ this . _attackHitbox = this . _physics . add . existing ( attackHitbox ) as Phaser . GameObjects . Rectangle & {
67+ body : ArcadeBody ;
68+ } ;
6269
63- this . setDepth ( depthsConfig . ennemies ) ;
70+ this . _attackHitbox . body . setAllowGravity ( false ) ;
71+ this . _attackHitbox . body . setEnable ( false ) ;
72+ this . _physics . world . remove ( this . _attackHitbox . body ) ;
6473
65- this . scene . physics . add . collider ( this . _atkHitbox , this . _player , ( ) => {
66- this . _player . hurt ( this . _config . damage ) ;
67- } ) ;
74+ this . _physics . add . collider ( this . _attackHitbox , this . _player ) ;
75+
76+ this . _physics . add . overlap (
77+ this . _attackHitbox ,
78+ this . _player ,
79+ ( ) => {
80+ this . _player . hurt ( this . _config . damage ) ;
81+ } ,
82+ undefined ,
83+ this
84+ ) ;
6885
6986 this . createAnimations ( ) ;
7087 this . startPatrol ( ) ;
@@ -102,7 +119,7 @@ export class Ennemy extends Sprite {
102119 this . anims . play ( AnimationTag . ENNEMY_DEATH ) ;
103120 this . body . setVelocityX ( 0 ) ;
104121 this . body . setEnable ( false ) ;
105- this . on ( GameHelper . animCompleteEvent ( AnimationTag . ENNEMY_DEATH ) , ( ) => this . destroy ( ) ) ;
122+ this . on ( ANIMATION . COMPLETE_KEY + AnimationTag . ENNEMY_DEATH , ( ) => this . destroy ( ) ) ;
106123 this . scene . sound . play ( SfxTag . PIXIE_DEAD ) ;
107124 } //
108125 else {
@@ -137,24 +154,33 @@ export class Ennemy extends Sprite {
137154
138155 this . scene . sound . play ( SfxTag . PIXIE_ATTACK ) ;
139156 const direction = this . flipX ? - 1 : 1 ;
140- this . _atkHitbox . setPosition ( this . x + direction * this . _atkHitbox . width , this . y ) . setVisible ( true ) ;
141- this . _atkHitbox . setFlipX ( this . flipX ) ;
142- this . _atkHitbox . body . setEnable ( true ) ;
143- this . scene . physics . world . add ( this . _atkHitbox . body ) ;
144- this . _atkHitbox . anims . play ( AnimationTag . ENNEMY_ATTACK_HITBOX ) ;
145- this . _atkHitbox . once ( GameHelper . animCompleteEvent ( AnimationTag . ENNEMY_ATTACK_HITBOX ) , ( ) => {
146- this . _atkHitbox . setVisible ( false ) ;
147- this . _atkHitbox . body . setEnable ( false ) ;
148- this . scene . physics . world . remove ( this . _atkHitbox . body ) ;
149- } ) ;
157+ const hitboxPosition = {
158+ x : this . x + direction * this . _attackHitbox . width ,
159+ y : this . y ,
160+ } ;
161+
162+ this . _attackHitbox . setPosition ( hitboxPosition . x , hitboxPosition . y ) . setVisible ( true ) ;
163+ this . _attackHitbox . body . setEnable ( true ) ;
164+ this . _physics . world . add ( this . _attackHitbox . body ) ;
150165
151166 this . _attacking = true ;
152167 this . _canAttack = false ;
153168 this . body . setVelocityX ( 0 ) ;
154169 GameHelper . animate ( this , AnimationTag . ENNEMY_ATTACK ) ;
155170
156- this . once ( GameHelper . animCompleteEvent ( AnimationTag . ENNEMY_ATTACK ) , ( ) => {
171+ this . setbodySize ( {
172+ isAttacking : true ,
173+ } ) ;
174+
175+ this . once ( ANIMATION . COMPLETE_KEY + AnimationTag . ENNEMY_ATTACK , ( ) => {
176+ this . _attackHitbox . body . setEnable ( false ) ;
177+ this . _physics . world . remove ( this . _attackHitbox . body ) ;
178+
179+ this . anims . play ( AnimationTag . ENNEMY_IDLE ) ;
157180 this . _attacking = false ;
181+ this . setbodySize ( {
182+ isAttacking : false ,
183+ } ) ;
158184 this . scene . time . delayedCall ( this . _config . atkCooldown , ( ) => ( this . _canAttack = true ) ) ;
159185 } ) ;
160186 }
@@ -199,6 +225,16 @@ export class Ennemy extends Sprite {
199225 }
200226 }
201227
228+ private setbodySize ( params : { isAttacking : boolean } ) : void {
229+ if ( params . isAttacking ) {
230+ this . body . setSize ( this . _defaultWidth , this . body . height , true ) ;
231+ // this.setOrigin(0.5);
232+ } else {
233+ const hitboxHeight = this . height * 0.8 ;
234+ this . body . setSize ( this . _defaultWidth , hitboxHeight ) ;
235+ }
236+ }
237+
202238 private createAnimations ( ) : void {
203239 this . anims . create ( {
204240 key : AnimationTag . ENNEMY_IDLE ,
@@ -213,8 +249,8 @@ export class Ennemy extends Sprite {
213249 this . anims . create ( {
214250 key : AnimationTag . ENNEMY_MOVING ,
215251 frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
216- start : 5 ,
217- end : 8 ,
252+ start : 4 ,
253+ end : 7 ,
218254 } ) ,
219255 frameRate : 10 ,
220256 repeat : - 1 ,
@@ -223,8 +259,8 @@ export class Ennemy extends Sprite {
223259 this . anims . create ( {
224260 key : AnimationTag . ENNEMY_HURT ,
225261 frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
226- start : 10 ,
227- end : 11 ,
262+ start : 8 ,
263+ end : 9 ,
228264 } ) ,
229265 frameRate : 20 ,
230266 repeat : 3 ,
@@ -233,25 +269,27 @@ export class Ennemy extends Sprite {
233269 this . anims . create ( {
234270 key : AnimationTag . ENNEMY_DEATH ,
235271 frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
236- start : 12 ,
237- end : 13 ,
272+ start : 10 ,
273+ end : 11 ,
238274 } ) ,
239275 frameRate : 20 ,
240276 repeat : 3 ,
241277 } ) ;
242278
279+ const attackSprite = this . _config . attackSprite || this . _config . sprite ;
280+
243281 this . anims . create ( {
244282 key : AnimationTag . ENNEMY_ATTACK ,
245- frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
246- start : 15 ,
247- end : 19 ,
283+ frames : this . anims . generateFrameNumbers ( attackSprite , {
284+ start : 0 ,
285+ end : 4 ,
248286 } ) ,
249287 frameRate : 10 ,
250288 } ) ;
251289
252- this . _atkHitbox . anims . create ( {
290+ this . anims . create ( {
253291 key : AnimationTag . ENNEMY_ATTACK_HITBOX ,
254- frames : this . anims . generateFrameNumbers ( SpritesheetTag . PIXIE_HITBOX , {
292+ frames : this . anims . generateFrameNumbers ( attackSprite , {
255293 start : 0 ,
256294 end : 4 ,
257295 } ) ,
0 commit comments