2023-06-11 22:57:43 -05:00
#include "Monster.h"
#include "Player.h"
#include "Crawler.h"
2023-06-12 00:37:55 -05:00
#include "DamageNumber.h"
2023-06-20 19:13:45 -07:00
#include "Bullet.h"
2023-06-30 15:44:41 -07:00
#include "BulletTypes.h"
2023-06-15 00:11:39 -05:00
#include "DEFINES.h"
2023-08-06 19:00:09 -05:00
#include "safemap.h"
2023-09-19 05:17:57 -05:00
#include "utils.h"
2023-09-26 02:40:38 -05:00
#include "Key.h"
2023-09-29 00:03:20 -05:00
#include "Menu.h"
2023-10-29 17:30:09 -05:00
#include "GameState.h"
2023-06-11 22:57:43 -05:00
2023-06-15 00:11:39 -05:00
INCLUDE_MONSTER_DATA
INCLUDE_MONSTER_LIST
INCLUDE_ANIMATION_DATA
INCLUDE_SPAWNER_LIST
2023-07-13 20:24:47 +00:00
INCLUDE_BULLET_LIST
2023-06-15 00:11:39 -05:00
INCLUDE_DAMAGENUMBER_LIST
INCLUDE_game
2023-10-22 00:48:09 -05:00
INCLUDE_DATA
2023-06-11 22:57:43 -05:00
2023-07-28 03:28:42 -05:00
float Player :: GROUND_SLAM_SPIN_TIME = 0.6f ;
2023-06-12 18:14:22 -05:00
2023-09-26 02:40:38 -05:00
InputGroup Player :: KEY_ABILITY1 ;
InputGroup Player :: KEY_ABILITY2 ;
InputGroup Player :: KEY_ABILITY3 ;
InputGroup Player :: KEY_ABILITY4 ;
InputGroup Player :: KEY_DEFENSIVE ;
2023-07-14 15:44:17 +00:00
Player :: Player ()
2023-07-28 03:28:42 -05:00
: lastReleasedMovementKey ( DOWN ), facingDirection ( DOWN ), state ( State :: NORMAL ){
Initialize ();
}
2023-06-17 22:03:20 -05:00
2023-07-14 19:14:04 +00:00
Player :: Player ( Player * player )
2023-07-21 15:20:56 -05:00
: pos ( player -> GetPos ()), vel ( player -> GetVelocity ()), iframe_time ( player -> iframe_time ), lastReleasedMovementKey ( DOWN ),
2023-07-28 03:28:42 -05:00
facingDirection ( DOWN ), state ( State :: NORMAL ){
Initialize ();
}
void Player :: Initialize (){
Player :: GROUND_SLAM_SPIN_TIME = "Warrior.Ability 2.SpinTime" _F ;
}
2023-07-14 19:14:04 +00:00
2023-07-05 04:31:31 -05:00
bool Player :: SetX ( float x ){
2023-06-24 00:00:14 -07:00
vf2d newPos = { x , pos . y };
2023-10-04 03:00:02 -05:00
vi2d tilePos = vi2d ( newPos / game -> GetCurrentMap (). tilewidth ) * game -> GetCurrentMap (). tilewidth ;
2023-07-06 14:59:21 -05:00
geom2d :: rect < int > collisionRect = game -> GetTileCollision ( game -> GetCurrentLevel (), newPos , upperLevel );
2023-07-05 04:31:31 -05:00
#pragma region lambdas
auto NoTileCollisionExistsHere = [ & ](){ return collisionRect . pos == game -> NO_COLLISION . pos && collisionRect . size == game -> NO_COLLISION . size ;};
#pragma endregion
if ( NoTileCollisionExistsHere ()){
2023-10-04 03:00:02 -05:00
pos . x = std :: clamp ( x , game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult (), float ( game -> GetCurrentMap (). width * game -> GetCurrentMap (). tilewidth - game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult ()));
2023-07-07 04:31:36 -05:00
Moved ();
2023-07-05 04:31:31 -05:00
return true ;
2023-06-24 00:00:14 -07:00
} else {
geom2d :: rect < float > collision = { collisionRect . pos , collisionRect . size };
2023-07-05 04:31:31 -05:00
#pragma region lambdas
auto NoPlayerCollisionWithTile = [ & ](){ return ! geom2d :: overlaps ( newPos , collision );};
#pragma endregion
2023-06-24 00:00:14 -07:00
collision . pos += tilePos ;
2023-07-05 04:31:31 -05:00
if ( NoPlayerCollisionWithTile ()){
2023-10-04 03:00:02 -05:00
pos . x = std :: clamp ( x , game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult (), float ( game -> GetCurrentMap (). width * game -> GetCurrentMap (). tilewidth - game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult ()));
2023-07-07 04:31:36 -05:00
Moved ();
2023-07-05 04:31:31 -05:00
return true ;
2023-06-24 00:00:14 -07:00
}
}
2023-07-05 04:31:31 -05:00
return false ;
2023-06-11 22:57:43 -05:00
};
2023-07-05 04:31:31 -05:00
bool Player :: SetY ( float y ){
2023-06-24 00:00:14 -07:00
vf2d newPos = { pos . x , y };
2023-10-04 03:00:02 -05:00
vi2d tilePos = vi2d ( newPos / game -> GetCurrentMap (). tilewidth ) * game -> GetCurrentMap (). tilewidth ;
2023-07-06 14:59:21 -05:00
geom2d :: rect < int > collisionRect = game -> GetTileCollision ( game -> GetCurrentLevel (), newPos , upperLevel );
2023-07-05 04:31:31 -05:00
#pragma region lambdas
auto NoTileCollisionExistsHere = [ & ](){ return collisionRect . pos == game -> NO_COLLISION . pos && collisionRect . size == game -> NO_COLLISION . size ;};
#pragma endregion
if ( NoTileCollisionExistsHere ()){
2023-10-04 03:00:02 -05:00
pos . y = std :: clamp ( y , game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult (), float ( game -> GetCurrentMap (). height * game -> GetCurrentMap (). tilewidth - game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult ()));
2023-07-07 04:31:36 -05:00
Moved ();
2023-07-05 04:31:31 -05:00
return true ;
2023-06-24 00:00:14 -07:00
} else {
geom2d :: rect < float > collision = { collisionRect . pos , collisionRect . size };
2023-07-05 04:31:31 -05:00
#pragma region lambdas
auto NoPlayerCollisionWithTile = [ & ](){ return ! geom2d :: overlaps ( newPos , collision );};
2023-07-06 15:07:44 -05:00
#pragma endregion
2023-06-24 00:00:14 -07:00
collision . pos += tilePos ;
2023-07-05 04:31:31 -05:00
if ( NoPlayerCollisionWithTile ()){
2023-10-04 03:00:02 -05:00
pos . y = std :: clamp ( y , game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult (), float ( game -> GetCurrentMap (). height * game -> GetCurrentMap (). tilewidth - game -> GetCurrentMap (). tilewidth / 2 * GetSizeMult ()));
2023-07-07 04:31:36 -05:00
Moved ();
2023-07-05 04:31:31 -05:00
return true ;
2023-06-24 00:00:14 -07:00
}
}
2023-07-05 04:31:31 -05:00
return false ;
2023-06-11 22:57:43 -05:00
}
2023-06-12 20:22:40 +00:00
void Player :: SetZ ( float z ){
this -> z = z ;
}
2023-07-05 04:31:31 -05:00
bool Player :: SetPos ( vf2d pos ){
2023-07-06 14:24:43 -05:00
bool resultX = SetX ( pos . x );
bool resultY = SetY ( pos . y );
if ( resultY &&! resultX ){
resultX = SetX ( pos . x );
}
return resultX | resultY ;
2023-06-11 22:57:43 -05:00
}
vf2d & Player :: GetPos (){
return pos ;
}
float Player :: GetX (){
return pos . x ;
}
float Player :: GetY (){
return pos . y ;
}
2023-06-12 20:22:40 +00:00
float Player :: GetZ (){
return z ;
}
2023-06-11 22:57:43 -05:00
int Player :: GetHealth (){
return hp ;
}
int Player :: GetMaxHealth (){
return maxhp ;
}
2023-06-19 04:03:04 -05:00
int Player :: GetMana (){
return mana ;
}
int Player :: GetMaxMana () {
return maxmana ;
}
2023-06-11 22:57:43 -05:00
int Player :: GetAttack (){
2023-06-19 03:25:01 -05:00
float mod_atk = atk ;
for ( Buff & b : GetBuffs ( BuffType :: ATTACK_UP )){
mod_atk += atk * b . intensity ;
}
return int ( mod_atk );
2023-06-11 22:57:43 -05:00
}
float Player :: GetMoveSpdMult (){
2023-06-19 03:25:01 -05:00
float mod_moveSpd = moveSpd ;
for ( Buff & b : GetBuffs ( BuffType :: SLOWDOWN )){
2023-07-29 10:21:53 -05:00
mod_moveSpd -= mod_moveSpd * b . intensity ;
}
for ( Buff & b : GetBuffs ( BuffType :: BLOCK_SLOWDOWN )){
mod_moveSpd -= mod_moveSpd * b . intensity ;
2023-06-19 03:25:01 -05:00
}
return mod_moveSpd ;
2023-06-11 22:57:43 -05:00
}
float Player :: GetSizeMult (){
return size ;
}
float Player :: GetAttackRangeMult (){
return attack_range ;
}
2023-06-12 18:14:22 -05:00
float Player :: GetSpinAngle (){
return spin_angle ;
}
2023-09-09 07:10:31 -05:00
State :: State Player :: GetState (){
2023-06-12 18:14:22 -05:00
return state ;
}
2023-08-06 15:07:53 -05:00
void Player :: Knockback ( vf2d vel ){
this -> vel += vel ;
}
2023-06-11 22:57:43 -05:00
void Player :: Update ( float fElapsedTime ){
2023-07-13 20:24:47 +00:00
Ability & rightClickAbility = GetRightClickAbility (),
2023-07-19 20:36:01 +00:00
& ability = GetAbility1 (),
2023-07-14 15:44:17 +00:00
& ability2 = GetAbility2 (),
& ability3 = GetAbility3 (),
& ability4 = GetAbility4 ();
2023-06-11 22:57:43 -05:00
attack_cooldown_timer = std :: max ( 0.f , attack_cooldown_timer - fElapsedTime );
2023-06-17 19:51:56 -05:00
iframe_time = std :: max ( 0.f , iframe_time - fElapsedTime );
2023-06-19 04:03:04 -05:00
notEnoughManaDisplay . second = std :: max ( 0.f , notEnoughManaDisplay . second - fElapsedTime );
2023-07-06 02:38:43 -05:00
notificationDisplay . second = std :: max ( 0.f , notificationDisplay . second - fElapsedTime );
2023-07-25 19:48:24 -05:00
lastHitTimer = std :: max ( 0.f , lastHitTimer - fElapsedTime );
2023-07-29 10:21:53 -05:00
blockTimer = std :: max ( 0.f , blockTimer - fElapsedTime );
2023-06-19 04:03:04 -05:00
manaTickTimer -= fElapsedTime ;
2023-07-20 19:07:36 +00:00
if ( castInfo . castTimer > 0 ){
castInfo . castTimer -= fElapsedTime ;
if ( castInfo . castTimer <= 0 ){
2023-07-22 04:21:10 -05:00
if ( castPrepAbility -> action ( this , castInfo . castPos )){
2023-07-20 19:07:36 +00:00
castPrepAbility -> cooldown = castPrepAbility -> COOLDOWN_TIME ;
2023-10-06 18:54:17 -05:00
ConsumeMana ( castPrepAbility -> manaCost );
2023-07-20 19:07:36 +00:00
}
castInfo . castTimer = 0 ;
SetState ( State :: NORMAL );
}
}
2023-06-19 04:03:04 -05:00
while ( manaTickTimer <= 0 ){
manaTickTimer += 0.2 ;
2023-10-06 18:54:17 -05:00
RestoreMana ( 1 );
2023-06-19 04:03:04 -05:00
}
2023-06-19 03:25:01 -05:00
for ( std :: vector < Buff >:: iterator it = buffList . begin (); it != buffList . end (); ++ it ){
Buff & b =* it ;
b . duration -= fElapsedTime ;
if ( b . duration <= 0 ){
it = buffList . erase ( it );
if ( it == buffList . end ()) break ;
}
}
2023-06-24 02:14:11 -07:00
//Class-specific update events.
2023-07-14 15:44:17 +00:00
OnUpdate ( fElapsedTime );
2023-06-12 18:14:22 -05:00
switch ( state ){
2023-09-09 07:10:31 -05:00
case State :: SPIN :{
2023-06-13 06:29:32 -05:00
switch ( facingDirection ){
2023-06-12 18:14:22 -05:00
case UP :{
if ( lastAnimationFlip == 0 ){
lastAnimationFlip = 0.03 ;
2023-06-13 06:29:32 -05:00
facingDirection = DOWN ;
2023-08-06 19:00:09 -05:00
animation . ChangeState ( internal_animState , "WARRIOR_WALK_S" );
2023-06-12 18:14:22 -05:00
}
} break ;
case DOWN :{
if ( lastAnimationFlip == 0 ){
lastAnimationFlip = 0.03 ;
2023-06-13 06:29:32 -05:00
facingDirection = UP ;
2023-08-06 19:00:09 -05:00
animation . ChangeState ( internal_animState , "WARRIOR_WALK_N" );
2023-06-12 18:14:22 -05:00
}
} break ;
}
2023-06-13 06:29:32 -05:00
if ( facingDirection == RIGHT ){
2023-06-12 18:14:22 -05:00
spin_angle += spin_spd * fElapsedTime ;
2023-06-13 06:29:32 -05:00
} else {
spin_angle -= spin_spd * fElapsedTime ;
2023-06-12 18:14:22 -05:00
}
if ( spin_attack_timer > 0 ){
2023-09-15 01:02:10 -05:00
z = float ( "Warrior.Ability 2.SpinMaxHeight" _I ) * sin ( 3.3 * ( GROUND_SLAM_SPIN_TIME - spin_attack_timer ) / GROUND_SLAM_SPIN_TIME );
2023-06-12 18:14:22 -05:00
spin_attack_timer = std :: max ( 0.f , spin_attack_timer - fElapsedTime );
} else {
2023-09-09 07:10:31 -05:00
SetState ( State :: NORMAL );
2023-06-12 18:14:22 -05:00
spin_angle = 0 ;
z = 0 ;
2023-07-07 19:48:45 +00:00
float numb = 4 ;
2023-09-11 05:27:36 -05:00
game -> HurtEnemies ( pos , "Warrior.Ability 2.Range" _F / 100 * 12 , GetAttack () * "Warrior.Ability 2.DamageMult" _F , OnUpperLevel (), 0 );
2023-09-16 07:00:38 -05:00
game -> AddEffect ( std :: make_unique < Effect > ( GetPos (), "Warrior.Ability 2.EffectLifetime" _F , "ground-slam-attack-front.png" , upperLevel , "Warrior.Ability 2.Range" _F / 300 * 1.33f , "Warrior.Ability 2.EffectFadetime" _F ), std :: make_unique < Effect > ( GetPos (), "Warrior.Ability 2.EffectLifetime" _F , "ground-slam-attack-back.png" , upperLevel , "Warrior.Ability 2.Range" _F / 300 * 1.33f , "Warrior.Ability 2.EffectFadetime" _F ));
2023-06-12 18:14:22 -05:00
}
2023-06-13 06:29:32 -05:00
if ( lastAnimationFlip > 0 ){
lastAnimationFlip = std :: max ( 0.f , lastAnimationFlip - fElapsedTime );
}
2023-06-17 21:19:08 -05:00
animation . UpdateState ( internal_animState , fElapsedTime );
} break ;
2023-09-09 07:10:31 -05:00
case State :: BLOCK :{
2023-07-29 10:21:53 -05:00
if ( blockTimer <= 0 ){
2023-09-09 07:10:31 -05:00
SetState ( State :: NORMAL );
2023-06-17 21:19:08 -05:00
}
2023-06-12 18:14:22 -05:00
} break ;
2023-09-09 07:10:31 -05:00
case State :: SWING_SONIC_SWORD :{
2023-06-20 19:13:45 -07:00
if ( ability3 . COOLDOWN_TIME - ability3 . cooldown > 0.5 ){
2023-09-09 07:10:31 -05:00
SetState ( State :: NORMAL );
2023-06-20 19:13:45 -07:00
switch ( facingDirection ){
case DOWN :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_IDLE_S" );
2023-06-20 19:13:45 -07:00
} break ;
case RIGHT :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_IDLE_E" );
2023-06-20 19:13:45 -07:00
} break ;
case LEFT :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_IDLE_W" );
2023-06-20 19:13:45 -07:00
} break ;
case UP :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_IDLE_N" );
2023-06-20 19:13:45 -07:00
} break ;
}
}
animation . UpdateState ( internal_animState , fElapsedTime );
} break ;
2023-09-09 07:10:31 -05:00
case State :: TELEPORT :{
2023-07-05 19:40:05 +00:00
teleportAnimationTimer = std :: max ( 0.f , teleportAnimationTimer - fElapsedTime );
if ( teleportAnimationTimer <= 0 ){
SetPos ( teleportTarget );
2023-09-09 07:10:31 -05:00
SetState ( State :: NORMAL );
2023-07-05 19:40:05 +00:00
}
animation . UpdateState ( internal_animState , fElapsedTime );
} break ;
2023-06-12 18:14:22 -05:00
default : {
2023-06-17 21:19:08 -05:00
//Update animations normally.
animation . UpdateState ( internal_animState , fElapsedTime );
2023-06-12 18:14:22 -05:00
}
}
2023-07-23 12:33:20 -05:00
rightClickAbility . cooldown -= fElapsedTime ;
ability . cooldown -= fElapsedTime ;
ability2 . cooldown -= fElapsedTime ;
ability3 . cooldown -= fElapsedTime ;
ability4 . cooldown -= fElapsedTime ;
if ( rightClickAbility . cooldown < 0 ){
rightClickAbility . cooldown = 0 ;
}
if ( ability . cooldown < 0 ){
ability . cooldown = 0 ;
}
if ( ability2 . cooldown < 0 ){
ability2 . cooldown = 0 ;
}
if ( ability3 . cooldown < 0 ){
ability3 . cooldown = 0 ;
}
if ( ability4 . cooldown < 0 ){
ability4 . cooldown = 0 ;
}
2023-06-15 23:44:34 -05:00
for ( Monster & m : MONSTER_LIST ){
2023-09-09 07:10:31 -05:00
if ( ! HasIframes () && abs ( m . GetZ () - GetZ ()) <= 1 && OnUpperLevel () == m . OnUpperLevel () && geom2d :: overlaps ( geom2d :: circle ( pos , 12 * size / 2 ), geom2d :: circle ( m . GetPos (), 12 * m . GetSizeMult () / 2 ))){
2023-06-15 23:44:34 -05:00
if ( m . IsAlive ()){
2023-07-14 15:44:17 +00:00
m . Collision ( this );
2023-06-15 23:44:34 -05:00
}
geom2d :: line line ( pos , m . GetPos ());
float dist = line . length ();
2023-09-19 05:17:57 -05:00
if ( dist <= 0.001 ){
m . SetPos ( m . GetPos () + vf2d { util :: random ( 2 ) - 1 , util :: random ( 2 ) - 1 });
} else {
m . SetPos ( line . rpoint ( dist * 1.1 ));
}
2023-06-15 23:44:34 -05:00
if ( m . IsAlive ()){
vel = line . vector (). norm () *- 128 ;
}
}
}
if ( vel . x > 0 ){
vel . x = std :: max ( 0.f , vel . x - friction * fElapsedTime );
} else {
vel . x = std :: min ( 0.f , vel . x + friction * fElapsedTime );
}
if ( vel . y > 0 ){
vel . y = std :: max ( 0.f , vel . y - friction * fElapsedTime );
} else {
vel . y = std :: min ( 0.f , vel . y + friction * fElapsedTime );
}
2023-06-24 00:00:14 -07:00
if ( vel != vf2d { 0 , 0 }){
float newX = pos . x + vel . x * fElapsedTime ;
float newY = pos . y + vel . y * fElapsedTime ;
SetX ( newX );
SetY ( newY );
}
2023-07-19 20:36:01 +00:00
2023-09-29 00:03:20 -05:00
if ( Menu :: stack . empty ()){
if ( CanAct () && attack_cooldown_timer == 0 && Crawler :: KEY_ATTACK . Held ()){
AutoAttack ();
}
2023-07-19 20:36:01 +00:00
2023-09-29 00:03:20 -05:00
auto AllowedToCast = [ & ]( Ability & ability ){ return ! ability . precastInfo . precastTargetingRequired && GetState () != State :: ANIMATION_LOCK ;};
//If pressed is set to false, uses held instead.
auto CheckAndPerformAbility = [ & ]( Ability & ability , InputGroup key ){
if ( ability . name != "???" ){
if ( CanAct ( ability )){
if ( ability . cooldown == 0 && GetMana () >= ability . manaCost ){
if ( key . Held () || key . Released () &&& ability == castPrepAbility && GetState () == State :: PREP_CAST ){
if ( GetState () == State :: CASTING ){
SetState ( State :: NORMAL );
castInfo . castTimer = castInfo . castTotalTime = 0 ;
}
if ( AllowedToCast ( ability ) && ability . action ( this ,{})){
ability . cooldown = ability . COOLDOWN_TIME ;
2023-10-06 18:54:17 -05:00
ConsumeMana ( ability . manaCost );
2023-09-29 00:03:20 -05:00
} else
if ( ability . precastInfo . precastTargetingRequired && GetState () == State :: NORMAL ){
PrepareCast ( ability );
}
2023-09-07 01:35:23 -05:00
}
2023-09-29 00:03:20 -05:00
} else
if ( ability . cooldown == 0 && GetMana () < ability . manaCost && key . Pressed ()){
notEnoughManaDisplay = { ability . name , 1 };
2023-07-21 18:47:45 +00:00
}
2023-07-19 20:36:01 +00:00
}
}
2023-09-29 00:03:20 -05:00
};
CheckAndPerformAbility ( GetAbility1 (), Player :: KEY_ABILITY1 );
CheckAndPerformAbility ( GetAbility2 (), Player :: KEY_ABILITY2 );
CheckAndPerformAbility ( GetAbility3 (), Player :: KEY_ABILITY3 );
CheckAndPerformAbility ( GetAbility4 (), Player :: KEY_ABILITY4 );
CheckAndPerformAbility ( GetRightClickAbility (), Player :: KEY_DEFENSIVE );
if ( GetState () == State :: PREP_CAST ){
#define CheckAbilityKeyReleasedAndCastSpell(ability,releaseState) \
if(&ability==castPrepAbility&&releaseState){CastSpell(ability);}
CheckAbilityKeyReleasedAndCastSpell ( rightClickAbility , Player :: KEY_DEFENSIVE . Released ())
else
CheckAbilityKeyReleasedAndCastSpell ( ability , Player :: KEY_ABILITY1 . Released ())
else
CheckAbilityKeyReleasedAndCastSpell ( ability2 , Player :: KEY_ABILITY2 . Released ())
else
CheckAbilityKeyReleasedAndCastSpell ( ability3 , Player :: KEY_ABILITY3 . Released ())
else
CheckAbilityKeyReleasedAndCastSpell ( ability4 , Player :: KEY_ABILITY4 . Released ())
2023-06-17 23:07:26 -05:00
}
2023-09-29 00:03:20 -05:00
2023-06-17 21:19:08 -05:00
}
2023-07-13 20:24:47 +00:00
2023-07-25 17:34:53 -05:00
#pragma region Warrior
switch ( GetState ()){
2023-09-09 07:10:31 -05:00
case State :: SWING_SWORD :{
2023-07-25 17:34:53 -05:00
switch ( GetFacingDirection ()){
case UP :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_SWINGSWORD_N" );
2023-07-25 17:34:53 -05:00
} break ;
case DOWN :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_SWINGSWORD_S" );
2023-07-25 17:34:53 -05:00
} break ;
case LEFT :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_SWINGSWORD_W" );
2023-07-25 17:34:53 -05:00
} break ;
case RIGHT :{
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "WARRIOR_SWINGSWORD_E" );
2023-07-25 17:34:53 -05:00
} break ;
}
SetSwordSwingTimer ( GetSwordSwingTimer () - fElapsedTime );
if ( GetSwordSwingTimer () <= 0 ){
SetSwordSwingTimer ( 0 );
SetState ( State :: NORMAL );
}
} break ;
}
#pragma endregion
#pragma region Ranger
2023-09-09 07:10:31 -05:00
if ( GetState () == State :: SHOOT_ARROW ){
2023-07-25 17:34:53 -05:00
if ( attack_cooldown_timer <= ARROW_ATTACK_COOLDOWN - 0.3 ){
2023-09-09 07:10:31 -05:00
SetState ( State :: NORMAL );
2023-07-13 20:24:47 +00:00
}
2023-07-25 17:34:53 -05:00
}
if ( retreatTimer > 0 ){
SetZ ( 6 * sin ( PI / RETREAT_TIME * retreatTimer ));
retreatTimer -= fElapsedTime ;
if ( retreatTimer <= 0 ){
SetVelocity ({});
SetZ ( 0 );
SetState ( State :: NORMAL );
2023-07-13 20:24:47 +00:00
}
2023-07-25 17:34:53 -05:00
}
if ( ghostRemoveTimer > 0 ){
ghostRemoveTimer -= fElapsedTime ;
if ( ghostRemoveTimer <= 0 ){
if ( ghostPositions . size () > 0 ){
ghostPositions . erase ( ghostPositions . begin ());
if ( ghostPositions . size () > 0 ){
ghostRemoveTimer = RETREAT_GHOST_FRAME_DELAY ;
}
}
}
}
if ( ghostFrameTimer > 0 ){
ghostFrameTimer -= fElapsedTime ;
if ( ghostFrameTimer <= 0 && GetState () == State :: RETREAT ){
ghostPositions . push_back ( GetPos () + vf2d { 0 , - GetZ ()});
ghostFrameTimer = RETREAT_GHOST_FRAME_DELAY ;
}
}
2023-07-25 17:41:17 -05:00
if ( rapidFireTimer > 0 ){
rapidFireTimer -= fElapsedTime ;
if ( rapidFireTimer <= 0 ){
if ( remainingRapidFireShots > 0 ){
remainingRapidFireShots -- ;
geom2d :: line pointTowardsCursor ( GetPos (), game -> GetWorldMousePos ());
vf2d extendedLine = pointTowardsCursor . upoint ( 1.1 );
float angleToCursor = atan2 ( extendedLine . y - GetPos (). y , extendedLine . x - GetPos (). x );
attack_cooldown_timer = ARROW_ATTACK_COOLDOWN ;
2023-08-06 15:07:53 -05:00
BULLET_LIST . push_back ( std :: make_unique < Arrow > ( Arrow ( GetPos (), extendedLine , vf2d { cos ( angleToCursor ) * "Ranger.Ability 1.ArrowSpd" _F , float ( sin ( angleToCursor ) * "Ranger.Ability 1.ArrowSpd" _F - PI / 8 * "Ranger.Ability 1.ArrowSpd" _F )} + movementVelocity , 12 * "Ranger.Ability 1.ArrowRadius" _F / 100 , GetAttack () * "Ranger.Ability 1.DamageMult" _F , OnUpperLevel (), true )));
2023-07-25 17:41:17 -05:00
SetAnimationBasedOnTargetingDirection ( angleToCursor );
rapidFireTimer = RAPID_FIRE_SHOOT_DELAY ;
} else {
SetState ( State :: NORMAL );
}
}
}
2023-07-25 17:34:53 -05:00
#pragma endregion
2023-06-11 22:57:43 -05:00
}
2023-06-17 19:34:16 -05:00
float Player :: GetSwordSwingTimer (){
return swordSwingTimer ;
}
void Player :: SetSwordSwingTimer ( float val ){
swordSwingTimer = val ;
}
2023-09-09 07:10:31 -05:00
void Player :: SetState ( State :: State newState ){
2023-07-29 10:21:53 -05:00
if ( GetState () == State :: BLOCK ){
RemoveAllBuffs ( BuffType :: BLOCK_SLOWDOWN );
}
2023-06-17 19:34:16 -05:00
state = newState ;
}
2023-06-15 23:44:34 -05:00
vf2d Player :: GetVelocity (){
return vel ;
}
2023-07-13 01:35:51 -05:00
bool Player :: CanMove (){
2023-07-25 20:32:29 +00:00
return state != State :: CASTING && state != State :: ANIMATION_LOCK ;
2023-07-13 01:35:51 -05:00
}
2023-09-07 01:35:23 -05:00
bool Player :: CanAct (){
2023-09-26 02:40:38 -05:00
Ability dummyAbility ;
2023-09-15 16:32:04 -05:00
return CanAct ( dummyAbility );
}
bool Player :: CanAct ( Ability & ability ){
2023-10-29 17:30:09 -05:00
return ( ability . canCancelCast || state != State :: CASTING ) && state != State :: ANIMATION_LOCK && GameState :: STATE == GameState :: states [ States :: GAME_RUN ];
2023-09-07 01:35:23 -05:00
}
2023-06-17 19:51:56 -05:00
bool Player :: HasIframes (){
return iframe_time > 0 ;
}
2023-09-11 05:27:36 -05:00
bool Player :: Hurt ( int damage , bool onUpperLevel , float z ){
if ( hp <= 0 || HasIframes () || OnUpperLevel () != onUpperLevel || abs ( GetZ () - z ) > 1 ) return false ;
2023-07-29 10:21:53 -05:00
if ( GetState () == State :: BLOCK ) damage *= 1 - "Warrior.Right Click Ability.DamageReduction" _F ;
2023-06-19 03:25:01 -05:00
float mod_dmg = damage ;
for ( Buff & b : GetBuffs ( BuffType :: DAMAGE_REDUCTION )){
mod_dmg -= damage * b . intensity ;
}
hp = std :: max ( 0 , hp - int ( mod_dmg ));
2023-07-25 19:48:24 -05:00
if ( lastHitTimer > 0 ){
damageNumberPtr . get () -> damage += int ( mod_dmg );
damageNumberPtr . get () -> pauseTime = 0.4 ;
} else {
2023-09-25 23:51:56 -05:00
damageNumberPtr = std :: make_shared < DamageNumber > ( pos , int ( mod_dmg ), true );
DAMAGENUMBER_LIST . push_back ( damageNumberPtr );
2023-07-25 19:48:24 -05:00
}
lastHitTimer = 0.05 ;
2023-06-17 23:29:04 -05:00
return true ;
2023-06-12 00:37:55 -05:00
}
2023-08-06 19:00:09 -05:00
void Player :: AddAnimation ( std :: string state ){
2023-10-03 04:09:42 -05:00
animation . AddState ( state , ANIMATION_DATA . at ( state ));
2023-06-11 22:57:43 -05:00
}
2023-08-06 19:00:09 -05:00
void Player :: UpdateAnimation ( std :: string animState , int specificClass ){
2023-07-14 15:44:17 +00:00
if ( specificClass == ANY || specificClass & GetClass ()){
animation . ChangeState ( internal_animState , animState );
}
2023-06-11 22:57:43 -05:00
}
Animate2D :: Frame Player :: GetFrame (){
return animation . GetFrame ( internal_animState );
}
void Player :: SetLastReleasedMovementKey ( Key k ){
lastReleasedMovementKey = k ;
}
Key Player :: GetLastReleasedMovementKey (){
return lastReleasedMovementKey ;
}
2023-06-13 06:29:32 -05:00
void Player :: SetFacingDirection ( Key direction ){
facingDirection = direction ;
}
Key Player :: GetFacingDirection (){
return facingDirection ;
}
2023-06-11 22:57:43 -05:00
void Player :: Moved (){
for ( MonsterSpawner & spawner : SPAWNER_LIST ){
2023-07-10 19:22:33 -05:00
if ( ! spawner . SpawnTriggered () && spawner . DoesUpperLevelSpawning () == OnUpperLevel () && geom2d :: contains ( geom2d :: rect < float > { spawner . GetPos (), spawner . GetRange ()}, pos )){
2023-06-11 22:57:43 -05:00
spawner . SetTriggered ( true );
}
}
2023-07-07 04:31:36 -05:00
ZoneData & zoneData = game -> GetZoneData ( game -> GetCurrentLevel ());
for ( geom2d :: rect < int >& upperLevelZone : zoneData [ "UpperZone" ]){
if ( geom2d :: overlaps ( upperLevelZone , pos )){
upperLevel = true ;
}
}
for ( geom2d :: rect < int >& lowerLevelZone : zoneData [ "LowerZone" ]){
if ( geom2d :: overlaps ( lowerLevelZone , pos )){
upperLevel = false ;
}
}
2023-06-12 18:14:22 -05:00
}
void Player :: Spin ( float duration , float spinSpd ){
2023-07-29 10:21:53 -05:00
SetState ( State :: SPIN );
2023-06-12 18:14:22 -05:00
spin_attack_timer = duration ;
spin_spd = spinSpd ;
spin_angle = 0 ;
2023-06-17 23:07:26 -05:00
}
void Player :: UpdateWalkingAnimation ( Key direction ){
2023-08-06 19:00:09 -05:00
std :: string anim ;
2023-06-17 23:07:26 -05:00
switch ( direction ){
2023-07-13 20:24:47 +00:00
case UP : anim = GetWalkNAnimation (); break ;
case RIGHT : anim = GetWalkEAnimation (); break ;
case DOWN : anim = GetWalkSAnimation (); break ;
case LEFT : anim = GetWalkWAnimation (); break ;
2023-06-17 23:07:26 -05:00
}
UpdateAnimation ( anim );
}
void Player :: UpdateIdleAnimation ( Key direction ){
2023-08-06 19:00:09 -05:00
std :: string anim ;
2023-06-17 23:07:26 -05:00
switch ( direction ){
2023-07-13 20:24:47 +00:00
case UP : anim = GetIdleNAnimation (); break ;
case RIGHT : anim = GetIdleEAnimation (); break ;
case DOWN : anim = GetIdleSAnimation (); break ;
case LEFT : anim = GetIdleWAnimation (); break ;
2023-06-17 23:07:26 -05:00
}
UpdateAnimation ( anim );
2023-06-19 03:25:01 -05:00
}
void Player :: AddBuff ( BuffType type , float duration , float intensity ){
buffList . push_back ( Buff { type , duration , intensity });
}
2023-07-07 19:48:45 +00:00
bool Player :: OnUpperLevel (){
return upperLevel ;
}
2023-06-19 03:25:01 -05:00
std :: vector < Buff > Player :: GetBuffs ( BuffType buff ){
std :: vector < Buff > filteredBuffs ;
std :: copy_if ( buffList . begin (), buffList . end (), std :: back_inserter ( filteredBuffs ),[ buff ]( Buff & b ){ return b . type == buff ;});
return filteredBuffs ;
2023-07-05 21:14:36 +00:00
}
2023-07-29 10:21:53 -05:00
void Player :: RemoveBuff ( BuffType buff ){
for ( auto it = buffList . begin (); it != buffList . end (); ++ it ){
Buff & b =* it ;
if ( b . type == buff ){
buffList . erase ( it );
return ;
}
}
}
void Player :: RemoveAllBuffs ( BuffType buff ){
std :: erase_if ( buffList ,[ & ]( Buff & b ){ return b . type == buff ;});
}
void Player :: RemoveAllBuffs (){
buffList . clear ();
}
2023-07-19 20:36:01 +00:00
void Player :: CastSpell ( Ability & ability ){
2023-07-22 03:19:52 -05:00
vf2d castPosition = game -> GetWorldMousePos ();
float distance = sqrt ( pow ( GetX () - game -> GetWorldMousePos (). x , 2 ) + pow ( GetY () - game -> GetWorldMousePos (). y , 2 ));
if ( distance > ability . precastInfo . range ){ //Clamp the distance.
vf2d pointToCursor = { game -> GetWorldMousePos (). x - GetX (), game -> GetWorldMousePos (). y - GetY ()};
pointToCursor = pointToCursor . norm () * ability . precastInfo . range ;
castPosition = GetPos () + pointToCursor ;
}
castInfo = { ability . name , ability . precastInfo . castTime , ability . precastInfo . castTime , castPosition };
2023-07-20 19:07:36 +00:00
SetState ( State :: CASTING );
2023-07-18 19:43:51 +00:00
}
CastInfo & Player :: GetCastInfo (){
return castInfo ;
}
2023-07-06 03:22:05 -05:00
bool Player :: CanPathfindTo ( vf2d pos , vf2d targetPos , float range ){
2023-10-04 02:02:10 -05:00
if ( targetPos . x < 0 || targetPos . y < 0 || targetPos . x > game -> GetCurrentMap (). width * game -> GetCurrentMap (). tilewidth || targetPos . y > game -> GetCurrentMap (). height * game -> GetCurrentMap (). tileheight ) return false ;
2023-07-28 04:41:18 -05:00
std :: vector < vf2d > pathing = game -> pathfinder . Solve_AStar ( pos , targetPos , range , upperLevel );
return pathing . size () > 0 && pathing . size () < range ; //We'll say 7 tiles or less is close enough to 650 range. Have a little bit of wiggle room.
2023-07-19 20:36:01 +00:00
}
void Player :: PrepareCast ( Ability & ability ){
castPrepAbility =& ability ;
2023-07-25 18:37:12 -05:00
if ( ability . precastInfo . range > 0 ){
SetState ( State :: PREP_CAST );
} else {
CastSpell ( ability );
}
2023-07-24 20:58:29 +00:00
}
void Player :: SetVelocity ( vf2d vel ){
this -> vel = vel ;
2023-07-24 23:20:28 -05:00
}
void Player :: SetAnimationBasedOnTargetingDirection ( float targetDirection ){
2023-07-25 21:34:38 +00:00
if ( GetClass () == Class :: RANGER ){
if ( targetDirection <= PI / 4 && targetDirection >- PI / 4 ){
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "RANGER_SHOOT_E" );
2023-07-25 21:34:38 +00:00
} else
if ( targetDirection >= 3 * PI / 4 || targetDirection <- 3 * PI / 4 ){
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "RANGER_SHOOT_W" );
2023-07-25 21:34:38 +00:00
} else
if ( targetDirection <= 3 * PI / 4 && targetDirection > PI / 4 ){
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "RANGER_SHOOT_S" );
2023-07-25 21:34:38 +00:00
} else
if ( targetDirection >=- 3 * PI / 4 && targetDirection <- PI / 4 ){
2023-08-06 19:00:09 -05:00
UpdateAnimation ( "RANGER_SHOOT_N" );
2023-07-25 21:34:38 +00:00
}
2023-07-24 23:20:28 -05:00
}
2023-09-19 05:17:57 -05:00
}
void Player :: SetIframes ( float duration ){
iframe_time = duration ;
2023-10-01 01:48:27 -05:00
}
2023-10-06 18:46:13 -05:00
bool Player :: Heal ( int damage ){
hp = std :: clamp ( hp + damage , 0 , maxhp );
return true ;
2023-10-06 18:54:17 -05:00
}
void Player :: RestoreMana ( int amt ){
mana = std :: clamp ( mana + amt , 0 , maxmana );
}
void Player :: ConsumeMana ( int amt ){
mana = std :: clamp ( mana - amt , 0 , maxmana );
2023-10-29 18:11:54 -05:00
}
void Player :: SetSizeMult ( float size ){
this -> size = size ;
2023-11-13 19:48:39 -06:00
}
Item & Player :: GetLoadoutItem ( int slot ){
if ( slot < 0 || slot > loadout . size () - 1 ) ERR ( "Invalid inventory slot " + std :: to_string ( slot ) + ", please choose a slot in range (0-" + std :: to_string ( loadout . size () - 1 ) + ")." );
return loadout [ slot ];
}
void Player :: SetLoadoutItem ( int slot , std :: string itemName ){
if ( slot < 0 || slot > loadout . size () - 1 ) ERR ( "Invalid inventory slot " + std :: to_string ( slot ) + ", please choose a slot in range (0-" + std :: to_string ( loadout . size () - 1 ) + ")." );
if ( Inventory :: GetItemCount ( itemName ) > 0 ){
GetLoadoutItem ( slot ) = Inventory :: GetItem ( itemName );
GetLoadoutItem ( slot ). amt = std :: min (( uint32_t ) "Player.Item Loadout Limit" _I , GetLoadoutItem ( slot ). Amt ()); //We are only allowed to have up to 10 maximum of an item on a journey.
} else {
ERR ( "Trying to set item " + itemName + " in Loadout slot " + std :: to_string ( slot ) + " when said item does not exist in our inventory!" );
}
}
void Player :: UseLoadoutItem ( int slot ){
if ( slot < 0 || slot > loadout . size () - 1 ) ERR ( "Invalid inventory slot " + std :: to_string ( slot ) + ", please choose a slot in range (0-" + std :: to_string ( loadout . size () - 1 ) + ")." );
if ( GetLoadoutItem ( slot ). Amt () > 0 ){
Inventory :: UseItem ( loadout [ slot ]. Name ());
GetLoadoutItem ( slot ). OnUseAction ();
GetLoadoutItem ( slot ). amt -- ;
}
2023-06-11 22:57:43 -05:00
}