1- import { Scene } from "phaser " ;
1+ import { CustomScene } from "@game-types " ;
22import { depthsConfig } from "../configs" ;
33import { GameHelper } from "../helpers" ;
44import { ANIMATION , ArcadeBody , ArcadeSprite } from "../phaser-aliases" ;
55import { AnimationTag , EnnemyTag , SfxTag } from "../tags" ;
66import { Hero } from "./hero.game-object" ;
77
88export interface EnnemyConfig {
9- scene : Scene ;
9+ scene : CustomScene ;
1010 x : number ;
1111 y : number ;
1212 chaseDistance : number ;
@@ -28,6 +28,16 @@ export class Ennemy extends ArcadeSprite {
2828 return this . _config . speed ;
2929 }
3030
31+ public get direction ( ) : number {
32+ return this . _direction ;
33+ }
34+ protected debugGraphics : Phaser . GameObjects . Graphics ;
35+
36+ protected set direction ( value : number ) {
37+ this . _direction = value ;
38+ this . setFlipX ( value < 0 ) ;
39+ }
40+
3141 private _player : Hero ;
3242 private _config : EnnemyConfig ;
3343 private _isAttacking = false ;
@@ -43,8 +53,13 @@ export class Ennemy extends ArcadeSprite {
4353 return this . scene . physics ;
4454 }
4555
56+ private _raycaster : Raycaster ;
57+ private _ray : Raycaster . Ray ;
58+
4659 constructor ( config : EnnemyConfig , player : Hero ) {
4760 super ( config . scene , config . x , config . y , config . sprite ) ;
61+ this . debugGraphics = this . scene . add . graphics ( ) ;
62+
4863 this . _config = config ;
4964
5065 this . _player = player ;
@@ -80,28 +95,38 @@ export class Ennemy extends ArcadeSprite {
8095 this
8196 ) ;
8297
83- this . _direction = Phaser . Math . Distance . Between ( this . x , this . y , this . _player . x , this . _player . y ) > 0 ? 1 : - 1 ;
98+ this . direction = Phaser . Math . Distance . Between ( this . x , this . y , this . _player . x , this . _player . y ) > 0 ? 1 : - 1 ;
8499
85100 this . startPatrol ( ) ;
101+ const plugin : PhaserRaycaster = this . _config . scene . raycasterPlugin ;
102+
103+ this . _raycaster = plugin . createRaycaster ( {
104+ debug : false ,
105+ } ) ;
106+ this . scene . physics . world . staticBodies . entries . forEach ( ( body ) => this . _raycaster . mapGameObjects ( body . gameObject ) ) ;
107+ this . _raycaster . mapGameObjects ( this . _player , true ) ;
108+
109+ this . _ray = this . _raycaster . createRay ( ) ;
110+ this . _ray . setRayRange ( this . _config . chaseDistance ) ;
111+ // this._ray.setRayRange(1000);
86112 }
87113
88114 public update ( _ : number , __ : number ) : void {
89115 if ( this . _config . hp <= 0 || this . _isAttacking ) {
90116 return ;
91117 }
92118
93- this . updateFlipX ( ) ;
94-
95119 const distanceToPlayer = GameHelper . getEdgeToEdgeDistance ( this , this . _player ) ;
96120
97121 if ( distanceToPlayer <= this . _config . range ) {
122+ this . stopPatrol ( ) ;
98123 this . attack ( ) ;
99124 } //
100- else if ( distanceToPlayer <= this . _config . chaseDistance ) {
125+ else if ( this . heroIsInSight ( ) ) {
101126 this . stopPatrol ( ) ;
102127 this . chasePlayer ( ) ;
103128 } //
104- else if ( distanceToPlayer > this . _config . chaseDistance && ! this . _isPatrolling ) {
129+ else if ( ! this . _isPatrolling ) {
105130 this . startPatrol ( ) ;
106131 }
107132
@@ -139,7 +164,7 @@ export class Ennemy extends ArcadeSprite {
139164 return ;
140165 }
141166 const speed = this . _isPatrolling ? this . _config . patrolSpeed : this . _config . speed ;
142- this . body . setVelocityX ( speed * this . _direction ) ;
167+ this . body . setVelocityX ( speed * this . direction ) ;
143168 } ) ;
144169 }
145170
@@ -148,7 +173,7 @@ export class Ennemy extends ArcadeSprite {
148173
149174 GameHelper . animate ( this , AnimationTag . ENNEMY_MOVING ) ;
150175
151- this . body . setVelocityX ( this . _config . patrolSpeed * this . _direction ) ;
176+ this . body . setVelocityX ( this . _config . patrolSpeed * this . direction ) ;
152177 }
153178
154179 private stopPatrol ( ) : void {
@@ -157,8 +182,8 @@ export class Ennemy extends ArcadeSprite {
157182
158183 private updatePatrol ( ) : void {
159184 if ( GameHelper . isObstacleAhead ( this , this . scene ) || GameHelper . isLedgeAhead ( this , this . scene ) ) {
160- this . _direction *= - 1 ;
161- this . body . setVelocityX ( this . _config . patrolSpeed * this . _direction ) ;
185+ this . direction *= - 1 ;
186+ this . body . setVelocityX ( this . _config . patrolSpeed * this . direction ) ;
162187 }
163188 }
164189
@@ -170,7 +195,7 @@ export class Ennemy extends ArcadeSprite {
170195 this . scene . sound . play ( SfxTag . PIXIE_ATTACK ) ;
171196
172197 const hitboxPosition = {
173- x : this . x + this . _direction * this . _attackHitbox . width ,
198+ x : this . x + this . direction * this . _attackHitbox . width ,
174199 y : this . y ,
175200 } ;
176201
@@ -200,27 +225,22 @@ export class Ennemy extends ArcadeSprite {
200225 } ) ;
201226 }
202227
203- private chasePlayer ( ) : void {
204- this . _direction = Math . sign ( this . _player . x - this . x ) ;
205- this . body . setVelocityX ( this . _direction * this . _config . speed ) ;
206- }
228+ private heroIsInSight ( ) : boolean {
229+ this . _ray . setOrigin ( this . x , this . y ) ;
230+ this . _ray . setAngleDeg ( this . _direction === 1 ? 0 : 180 ) ;
231+ this . _ray . setConeDeg ( this . _isPatrolling ? 30 : 90 ) ;
232+ const result = this . _ray . castCone ( ) ;
207233
208- private updateFlipX ( ) : void {
209- // Patrol is using a tween, which updates X position instead of velocity
210- if ( this . _isPatrolling ) {
211- this . setFlipX ( this . _direction < 0 ) ;
212- } //
213- else {
214- const distanceToPlayer = Phaser . Math . Distance . Between ( this . x , this . y , this . _player . x , this . _player . y ) ;
215-
216- if ( distanceToPlayer <= this . _config . chaseDistance ) {
217- this . _direction = Math . sign ( this . _player . x - this . x ) ;
218- this . setFlipX ( this . _direction < 0 ) ;
219- } //
220- else {
221- this . setFlipX ( this . body . velocity . x < 0 ) ;
234+ return result . some ( ( line ) => {
235+ if ( "object" in line ) {
236+ return line . object instanceof Hero ;
222237 }
223- }
238+ } ) ;
239+ }
240+
241+ private chasePlayer ( ) : void {
242+ this . direction = Math . sign ( this . _player . x - this . x ) ;
243+ this . body . setVelocityX ( this . direction * this . _config . speed ) ;
224244 }
225245
226246 private setbodySize ( params : { isAttacking : boolean } ) : void {
0 commit comments