11import { Scene } from "phaser" ;
22import { depthsConfig } from "../configs" ;
3+ import { HeroState } from "../helpers" ;
34import { GameHelper } from "../helpers/game.helper" ;
45import { ArcadeBody , ArcadeSprite , Sprite } from "../phaser-aliases" ;
56import { AnimationTag , ImageTag , SfxTag , SpritesheetTag } from "../tags" ;
67
78export class Hero extends Sprite {
89 public declare body : ArcadeBody ;
910 public projectiles : Phaser . Physics . Arcade . Group ;
11+
1012 public get isMovingLeft ( ) : boolean {
1113 return this . _cursors . left . isDown ;
1214 }
@@ -19,6 +21,9 @@ export class Hero extends Sprite {
1921 public get damage ( ) : number {
2022 return this . _damage ;
2123 }
24+ public get offset ( ) : { x : number ; y : number } {
25+ return this . _offset ;
26+ }
2227
2328 private _speed = 160 ;
2429 private _jumpSpeed = 360 ;
@@ -35,6 +40,7 @@ export class Hero extends Sprite {
3540 x : 8 ,
3641 y : 14 ,
3742 } ;
43+ private _state : HeroState ;
3844
3945 constructor ( scene : Scene ) {
4046 super ( scene , 0 , 0 , SpritesheetTag . HERO ) ;
@@ -45,6 +51,8 @@ export class Hero extends Sprite {
4551 throw Error ( "Keyboard plugin is not available" ) ;
4652 }
4753
54+ this . _state = new HeroState ( this ) ;
55+
4856 this . _keyboard = this . scene . input . keyboard ;
4957 this . _cursors = this . _keyboard . createCursorKeys ( ) ;
5058 this . _shootKey = this . _keyboard . addKey ( Phaser . Input . Keyboard . KeyCodes . SPACE ) ;
@@ -66,32 +74,46 @@ export class Hero extends Sprite {
6674 const hitboxHeight = this . height * 0.8 ;
6775 this . body . setSize ( hitboxWidth , hitboxHeight ) ;
6876 this . body . setOffset ( this . _offset . x , this . _offset . y ) ;
77+
78+ this . _state . set ( { action : "IDLE" } ) ;
6979 }
7080
7181 public update ( time : number , _ : number ) : void {
7282 this . handleMovements ( time ) ;
7383 this . handleProjectiles ( ) ;
7484 }
7585
76- private handleMovements ( time : number ) : void {
77- const isGrounded = this . body . touching . down ;
78-
79- if ( isGrounded ) {
80- if ( this . isMovingLeft || this . isMovingRight ) {
81- GameHelper . animate ( this , AnimationTag . WALK , {
82- exceptIf : [ AnimationTag . JUMP , AnimationTag . SHOOT ] ,
83- } ) ;
84- } //
85- else {
86- GameHelper . animate ( this , AnimationTag . IDLE , {
87- exceptIf : [ AnimationTag . JUMP , AnimationTag . SHOOT ] ,
88- } ) ;
89-
90- this . body . setVelocityX ( 0 ) ;
91- }
86+ public shoot ( ) : void {
87+ this . scene . sound . play ( SfxTag . ARROW_SHOOT ) ;
88+
89+ GameHelper . animate ( this , AnimationTag . HERO_SHOOT , {
90+ ignoreIfPlaying : false ,
91+ } ) ;
92+ const velocity = this . flipX ? - 600 : 600 ;
93+
94+ const projectile : ArcadeSprite = this . projectiles . create ( this . x , this . y , ImageTag . PROJECTILE_ARROW ) ;
95+ projectile . setFlipX ( this . flipX ) ;
96+ projectile . setVelocityX ( velocity ) ;
97+ }
9298
93- // for some reason, this "isGrounded" is sometimes true, right after jumping
99+ public jump ( ) : void {
100+ if ( this . _noOfJump <= 0 ) {
101+ return ;
102+ }
103+
104+ this . _noOfJump -- ;
105+ this . _isJumping = true ;
106+
107+ this . body . setVelocityY ( - this . _jumpSpeed ) ;
108+ this . scene . sound . play ( SfxTag . JUMP ) ;
109+ GameHelper . animate ( this , AnimationTag . HERO_JUMP ) ;
110+ }
111+
112+ private handleMovements ( time : number ) : void {
113+ if ( this . body . touching . down ) {
114+ // for some reason, touching.down is sometimes true, right after jumping
94115 const stupidHack = time - this . _lastJumpTime > 100 ;
116+
95117 //~ Is landing
96118 if ( this . _isJumping && stupidHack ) {
97119 this . _isJumping = false ;
@@ -101,24 +123,24 @@ export class Hero extends Sprite {
101123 }
102124
103125 if ( this . isMovingRight ) {
104- this . body . setVelocityX ( this . _speed ) ;
105- this . setFlipX ( false ) ;
106- this . body . setOffset ( this . _offset . x , this . _offset . y ) ;
107- }
108-
109- if ( this . isMovingLeft ) {
110- this . body . setVelocityX ( - this . _speed ) ;
111- this . setFlipX ( true ) ;
112- this . body . setOffset ( this . _offset . x + 6 , this . _offset . y ) ;
126+ this . _state . set ( { action : "MOVING-RIGHT" } ) ;
127+ } else if ( this . isMovingLeft ) {
128+ this . _state . set ( { action : "MOVING-LEFT" } ) ;
129+ } else {
130+ this . body . setVelocityX ( 0 ) ;
113131 }
114132
115133 if ( Phaser . Input . Keyboard . JustDown ( this . _cursors . up ) ) {
116134 this . _lastJumpTime = time ;
117- this . jump ( ) ;
135+ this . _state . set ( { action : "JUMPING" } ) ;
118136 }
119137
120138 if ( Phaser . Input . Keyboard . JustDown ( this . _shootKey ) ) {
121- this . shoot ( ) ;
139+ this . _state . set ( { action : "SHOOTING" } ) ;
140+ }
141+
142+ if ( this . body . velocity . x === 0 && this . body . velocity . y === 0 ) {
143+ this . _state . set ( { action : "IDLE" } ) ;
122144 }
123145 }
124146
@@ -138,22 +160,9 @@ export class Hero extends Sprite {
138160 } , this ) ;
139161 }
140162
141- private jump ( ) : void {
142- if ( this . _noOfJump <= 0 ) {
143- return ;
144- }
145-
146- this . _noOfJump -- ;
147- this . _isJumping = true ;
148-
149- this . body . setVelocityY ( - this . _jumpSpeed ) ;
150- this . scene . sound . play ( SfxTag . JUMP ) ;
151- GameHelper . animate ( this , AnimationTag . JUMP ) ;
152- }
153-
154163 private createAnimations ( ) : void {
155164 this . anims . create ( {
156- key : AnimationTag . IDLE ,
165+ key : AnimationTag . HERO_IDLE ,
157166 frames : this . anims . generateFrameNumbers ( SpritesheetTag . HERO , {
158167 start : 0 ,
159168 end : 3 ,
@@ -163,7 +172,7 @@ export class Hero extends Sprite {
163172 } ) ;
164173
165174 this . anims . create ( {
166- key : AnimationTag . WALK ,
175+ key : AnimationTag . HERO_WALK ,
167176 frames : this . anims . generateFrameNumbers ( SpritesheetTag . HERO , {
168177 start : 5 ,
169178 end : 8 ,
@@ -173,7 +182,7 @@ export class Hero extends Sprite {
173182 } ) ;
174183
175184 this . anims . create ( {
176- key : AnimationTag . JUMP ,
185+ key : AnimationTag . HERO_JUMP ,
177186 frames : this . anims . generateFrameNumbers ( SpritesheetTag . HERO , {
178187 start : 20 ,
179188 end : 22 ,
@@ -182,25 +191,30 @@ export class Hero extends Sprite {
182191 } ) ;
183192
184193 this . anims . create ( {
185- key : AnimationTag . SHOOT ,
194+ key : AnimationTag . HERO_SHOOT ,
186195 frames : this . anims . generateFrameNumbers ( SpritesheetTag . HERO , {
187196 start : 15 ,
188197 end : 19 ,
189198 } ) ,
190199 frameRate : 15 ,
191200 } ) ;
192- }
193-
194- private shoot ( ) : void {
195- this . scene . sound . play ( SfxTag . ARROW_SHOOT ) ;
196201
197- GameHelper . animate ( this , AnimationTag . SHOOT , {
198- ignoreIfPlaying : false ,
202+ this . anims . create ( {
203+ key : AnimationTag . HERO_HURT ,
204+ frames : this . anims . generateFrameNumbers ( SpritesheetTag . HERO , {
205+ start : 9 ,
206+ end : 10 ,
207+ } ) ,
208+ frameRate : 10 ,
199209 } ) ;
200- const velocity = this . flipX ? - 600 : 600 ;
201210
202- const projectile : ArcadeSprite = this . projectiles . create ( this . x , this . y , ImageTag . PROJECTILE_ARROW ) ;
203- projectile . setFlipX ( this . flipX ) ;
204- projectile . setVelocityX ( velocity ) ;
211+ this . anims . create ( {
212+ key : AnimationTag . HERO_DIE ,
213+ frames : this . anims . generateFrameNumbers ( SpritesheetTag . HERO , {
214+ start : 11 ,
215+ end : 12 ,
216+ } ) ,
217+ frameRate : 10 ,
218+ } ) ;
205219 }
206220}
0 commit comments