@@ -3,25 +3,19 @@ import { Projectiles } from "../groups/Projectiles.js";
33import { PlayerHealthbar } from "../hud/Healthbar.js" ;
44import { ImageEffect } from "../effects/HitEffect.js" ;
55import { audio } from "../audio/AudioControl.js" ;
6- import { PlayerWalk , PlayerStateMachine } from "../states/PlayerStates.js" ;
6+ import { PlayerWalk , PlayerCrouch , PlayerCrouchWalk , PlayerStateMachine } from "../states/PlayerStates.js" ;
7+ import { drawStatus } from "../hud/Status.js" ;
78
89
910export class Player extends Phaser . Physics . Arcade . Sprite {
10- //state design
11- static Status = {
12- Walking : 1 ,
13- Climbing : 2 ,
14- Jumping : 3 ,
15- Falling : 4
16- }
1711 constructor ( scene , x , y , texture ) {
1812 super ( scene , x , y , texture ) ;
1913
2014 this . scene = scene ;
2115 scene . add . existing ( this ) ;
2216 scene . physics . add . existing ( this ) ;
2317 this . init ( ) ;
24- this . status = Player . Status . Walking ;
18+ this . status ;
2519
2620 }
2721
@@ -52,24 +46,38 @@ export class Player extends Phaser.Physics.Arcade.Sprite{
5246
5347 this
5448 . setOrigin ( 0.5 , 1 )
55- . setSize ( 15 , 40 )
56- . setOffset ( this . width * 0.4 , this . height * 0.4 )
57- . setScale ( 0.5 )
49+ . setSize ( 15 , 35 )
50+ . setOffset ( this . width * 0.4 , this . height * 0.55 )
51+ . setScale ( 0.8 )
5852 . setDepth ( 100 )
5953 . setGravityY ( 982 )
6054 . setCollideWorldBounds ( true ) ;
6155
6256 this . healthbar = new PlayerHealthbar ( this . scene , this ) ;
6357 }
6458
59+ updateBoundingBox ( ) {
60+
61+ if ( this . currentState . name === "PlayerCrouch" || this . currentState . name == "PlayerCrouchWalk" ) {
62+ this . setSize ( 15 , 22 )
63+ if ( this . flipX ) this . setOffset ( this . width * 0.48 , this . height * 0.72 )
64+ else this . setOffset ( this . width * 0.4 , this . height * 0.72 ) ;
65+ }
66+ else if ( this . currentState . name === "PlayerSlide" ) {
67+ this . setSize ( 15 , 15 )
68+ if ( this . flipX ) this . setOffset ( this . width * 0.48 , this . height * 0.72 )
69+ else this . setOffset ( this . width * 0.4 , this . height * 0.82 ) ;
70+ }
71+ else {
72+ this . setSize ( 15 , 35 )
73+ if ( this . flipX ) this . setOffset ( this . width * 0.48 , this . height * 0.55 )
74+ else this . setOffset ( this . width * 0.4 , this . height * 0.55 ) ;
75+ }
76+ }
6577 addCollider ( otherGameObject , callback ) {
6678 this . scene . physics . add . collider ( this , otherGameObject , callback , null , this ) ;
6779 }
6880
69- setStatus ( newStatus ) {
70- this . status = newStatus ;
71- }
72-
7381 addCollider ( otherGameObject , callback ) {
7482 this . scene . physics . add . collider ( this , otherGameObject , callback , null , this ) ;
7583 }
@@ -83,6 +91,7 @@ export class Player extends Phaser.Physics.Arcade.Sprite{
8391 this . decreaseHealth ( enemy ) ;
8492 this . playDamageTween ( enemy ) ;
8593 audio . play ( audio . playerHitSound ) ;
94+
8695 this . lastDirection === "left" ? this . setVelocityX ( this . speedX ) : this . setVelocityX ( - this . speedX ) ;
8796 }
8897 }
@@ -92,10 +101,11 @@ export class Player extends Phaser.Physics.Arcade.Sprite{
92101 this . scene . tweens . add ( {
93102 targets : this ,
94103 health : this . health - source . damage ,
95- duration : 800 ,
104+ duration : 400 ,
96105 repeat : 0 ,
97106 onComplete : ( ) => {
98107 this . hasBeenHit = false ;
108+ this . setVelocity ( 0 , 0 ) ;
99109 if ( this . health <= 0 ) {
100110 this . isDead = true ;
101111 this . setVelocity ( 0 , - 200 ) ;
@@ -124,19 +134,6 @@ export class Player extends Phaser.Physics.Arcade.Sprite{
124134
125135 }
126136
127- handleAnimations ( ) {
128-
129- if ( this . body . onFloor ( ) ) {
130- this . body . velocity . x === 0 ?
131- this . play ( "player-idle" , true ) : this . play ( "player-run" , true ) ;
132- }
133- else {
134- this . body . velocity . y < 0 && this . play ( "player-jump" , true ) ;
135- this . body . velocity . y > 0 && this . play ( "player-fall" , true ) ;
136- }
137- //on ladder
138- this . onLadder && this . play ( 'player-idle' , true ) ;
139- }
140137
141138 shoot ( key , anim ) {
142139 if ( this . hasBeenHit ) return ;
@@ -175,12 +172,12 @@ export class Player extends Phaser.Physics.Arcade.Sprite{
175172
176173 update ( time , delta ) {
177174 if ( ! this . body ) return ;
178- super . update ( time , delta )
175+ super . update ( time , delta ) ;
176+
179177 this . stateMachine . updateState ( this . currentState , time , delta ) ;
180178 this . healthbar . draw ( ) ;
181- this . handleAnimations ( ) ;
182179 this . handleShooting ( ) ;
183-
180+ this . updateBoundingBox ( )
184181 //SETTING PLAYER Hitbox
185182 this . lastDirection = this . flipX ? "left" : "right" ;
186183
0 commit comments