@@ -2,7 +2,7 @@ import { Scene } from "phaser";
22import { depthsConfig } from "../configs" ;
33import { GameHelper } from "../helpers" ;
44import { ArcadeBody , Sprite } from "../phaser-aliases" ;
5- import { AnimationTag , EnnemyTag } from "../tags" ;
5+ import { AnimationTag , EnnemyTag , SfxTag } from "../tags" ;
66import { Hero } from "./hero.game-object" ;
77
88export interface EnnemyConfig {
@@ -12,20 +12,19 @@ export interface EnnemyConfig {
1212 patrolDistance : number ;
1313 chaseDistance : number ;
1414 speed : number ;
15+ patrolSpeed : number ;
1516 sprite : EnnemyTag ;
17+ hp : number ;
1618}
1719
1820export class Ennemy extends Sprite {
1921 public declare body : ArcadeBody ;
2022
21- private _patrolDistance : number ;
22- private _chaseDistance : number ;
2323 private _player : Hero ;
2424 private _startingX : number ;
25- private _spriteTag : EnnemyTag ;
2625 private _patrolDirection = 1 ;
27- private _speed = 60 ;
2826 private _patrolTween : Phaser . Tweens . Tween | null = null ;
27+ private _config : EnnemyConfig ;
2928
3029 private get _isPatrolling ( ) : boolean {
3130 return this . _patrolTween !== null ;
@@ -34,10 +33,7 @@ export class Ennemy extends Sprite {
3433 constructor ( config : EnnemyConfig , player : Hero ) {
3534 super ( config . scene , config . x , config . y , config . sprite ) ;
3635 this . _startingX = config . x ;
37- this . _patrolDistance = config . patrolDistance ;
38- this . _chaseDistance = config . chaseDistance ;
39- this . _speed = config . speed ;
40- this . _spriteTag = config . sprite ;
36+ this . _config = config ;
4137
4238 this . _player = player ;
4339
@@ -54,30 +50,52 @@ export class Ennemy extends Sprite {
5450 }
5551
5652 public update ( ) : void {
53+ if ( this . _config . hp <= 0 ) {
54+ return ;
55+ }
56+
5757 this . updateFlipX ( ) ;
5858
5959 const distanceToPlayer = Phaser . Math . Distance . Between ( this . x , this . y , this . _player . x , this . _player . y ) ;
6060
61- if ( distanceToPlayer <= this . _chaseDistance ) {
61+ if ( distanceToPlayer <= this . _config . chaseDistance ) {
6262 this . stopPatrol ( ) ;
6363 this . chasePlayer ( ) ;
6464 } //
65- else if ( distanceToPlayer > this . _chaseDistance && this . _patrolTween === null ) {
65+ else if ( distanceToPlayer > this . _config . chaseDistance && this . _patrolTween === null ) {
6666 this . returnToStart ( ) ;
6767 }
68+
69+ GameHelper . animate ( this , AnimationTag . ENNEMY_MOVING , {
70+ exceptIf : [ AnimationTag . ENNEMY_DEATH , AnimationTag . ENNEMY_HURT ] ,
71+ } ) ;
72+ }
73+
74+ public hurt ( damage : number ) : void {
75+ this . _config . hp -= damage ;
76+
77+ if ( this . _config . hp <= 0 ) {
78+ this . anims . play ( AnimationTag . ENNEMY_DEATH ) ;
79+ this . body . setVelocityX ( 0 ) ;
80+ this . on ( "animationcomplete" , ( ) => this . destroy ( ) ) ;
81+ this . scene . sound . play ( SfxTag . PIXIE_DEAD ) ;
82+ } else {
83+ this . anims . play ( AnimationTag . ENNEMY_HURT ) ;
84+ this . scene . sound . play ( SfxTag . PIXIE_HURT ) ;
85+ }
6886 }
6987
7088 private startPatrol ( ) : void {
7189 if ( this . _patrolTween !== null ) {
7290 return ;
7391 }
7492
75- this . _patrolDirection = Math . sign ( this . _patrolDistance ) ;
93+ this . _patrolDirection = Math . sign ( this . _config . patrolDistance ) ;
7694
7795 this . _patrolTween = this . scene . tweens . add ( {
7896 targets : this ,
79- x : this . _startingX + this . _patrolDistance ,
80- duration : Math . abs ( this . _patrolDistance ) * 10 ,
97+ x : this . _startingX + this . _config . patrolDistance ,
98+ duration : ( Math . abs ( this . _config . patrolDistance ) / this . _config . patrolSpeed ) * 1000 ,
8199 yoyo : true ,
82100 repeat : - 1 ,
83101 onYoyo : ( ) => ( this . _patrolDirection *= - 1 ) ,
@@ -94,15 +112,14 @@ export class Ennemy extends Sprite {
94112
95113 private chasePlayer ( ) : void {
96114 const direction = Math . sign ( this . _player . x - this . x ) ;
97- this . body . setVelocityX ( direction * this . _speed ) ;
115+ this . body . setVelocityX ( direction * this . _config . speed ) ;
98116 }
99117
100118 private returnToStart ( ) : void {
101119 if ( ! GameHelper . isCloseEnough ( this . x , this . _startingX ) ) {
102120 const direction = Math . sign ( this . _startingX - this . x ) ;
103- this . body . setVelocityX ( direction * this . _speed ) ;
121+ this . body . setVelocityX ( direction * this . _config . patrolSpeed ) ;
104122 } else {
105- console . log ( "reached start" ) ;
106123 this . body . setVelocityX ( 0 ) ;
107124 this . startPatrol ( ) ;
108125 }
@@ -120,7 +137,7 @@ export class Ennemy extends Sprite {
120137 private createAnimations ( ) : void {
121138 this . anims . create ( {
122139 key : AnimationTag . ENNEMY_IDLE ,
123- frames : this . anims . generateFrameNumbers ( this . _spriteTag , {
140+ frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
124141 start : 0 ,
125142 end : 3 ,
126143 } ) ,
@@ -130,12 +147,32 @@ export class Ennemy extends Sprite {
130147
131148 this . anims . create ( {
132149 key : AnimationTag . ENNEMY_MOVING ,
133- frames : this . anims . generateFrameNumbers ( this . _spriteTag , {
150+ frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
134151 start : 4 ,
135152 end : 7 ,
136153 } ) ,
137154 frameRate : 10 ,
138155 repeat : - 1 ,
139156 } ) ;
157+
158+ this . anims . create ( {
159+ key : AnimationTag . ENNEMY_HURT ,
160+ frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
161+ start : 8 ,
162+ end : 9 ,
163+ } ) ,
164+ frameRate : 20 ,
165+ repeat : 3 ,
166+ } ) ;
167+
168+ this . anims . create ( {
169+ key : AnimationTag . ENNEMY_DEATH ,
170+ frames : this . anims . generateFrameNumbers ( this . _config . sprite , {
171+ start : 10 ,
172+ end : 11 ,
173+ } ) ,
174+ frameRate : 20 ,
175+ repeat : 3 ,
176+ } ) ;
140177 }
141178}
0 commit comments