From 3a880c55a2309502d5f0333e9e0bdd71e2a3aee5 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 22 Jul 2023 04:21:10 -0500 Subject: [PATCH] Class abilities are statically set so they are initialized on load to allow for proper swapping. --- Crawler/Ability.h | 4 +++- Crawler/Crawler.cpp | 12 ++++++++++- Crawler/Crawler.h | 1 + Crawler/DEFINES.h | 4 ++-- Crawler/Player.cpp | 5 +++-- Crawler/Player.h | 19 ++++++++++------- Crawler/Ranger.cpp | 8 ++++---- Crawler/Thief.cpp | 8 ++++---- Crawler/Trapper.cpp | 8 ++++---- Crawler/Version.h | 2 +- Crawler/Warrior.cpp | 50 ++++++++++++++++++++++----------------------- Crawler/Witch.cpp | 8 ++++---- Crawler/Wizard.cpp | 47 +++++++++++++++++++++--------------------- 13 files changed, 98 insertions(+), 78 deletions(-) diff --git a/Crawler/Ability.h b/Crawler/Ability.h index ab36841a..9c12c35d 100644 --- a/Crawler/Ability.h +++ b/Crawler/Ability.h @@ -1,6 +1,8 @@ #pragma once #include "olcPixelGameEngine.h" +struct Player; + struct PrecastData{ float castTime; float range; @@ -19,7 +21,7 @@ struct Ability{ int manaCost=0; Pixel barColor1,barColor2; PrecastData precastInfo; - std::functionaction=[&](vf2d){return false;}; + std::functionaction=[](Player*,vf2d){return false;}; Ability(); Ability(std::string name,float cooldownTime,int manaCost,Pixel barColor1=VERY_DARK_RED,Pixel barColor2=DARK_RED,PrecastData precastInfo={}); }; \ No newline at end of file diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp index af889341..867d12e0 100644 --- a/Crawler/Crawler.cpp +++ b/Crawler/Crawler.cpp @@ -87,7 +87,9 @@ bool Crawler::OnUserCreate(){ view=TileTransformedView{GetScreenSize(),{1,1}}; LoadLevel(CAMPAIGN_1_1); + InitializeClassAbilities(); ChangePlayerClass(WARRIOR); + //Warrior::ability4=Wizard::ability3; //Class ability swapping demonstration. return true; } @@ -1040,10 +1042,18 @@ void Crawler::ChangePlayerClass(Class cl){ }break; } sig::Animation::SetupPlayerAnimations(); - GetPlayer()->InitializeClassAbilities(); GetPlayer()->UpdateIdleAnimation(DOWN); } +void Crawler::InitializeClassAbilities(){ + Warrior::InitializeClassAbilities(); + Thief::InitializeClassAbilities(); + Ranger::InitializeClassAbilities(); + Trapper::InitializeClassAbilities(); + Wizard::InitializeClassAbilities(); + Witch::InitializeClassAbilities(); +} + int main() { Crawler demo; diff --git a/Crawler/Crawler.h b/Crawler/Crawler.h index ad7a9fc7..291c8fb5 100644 --- a/Crawler/Crawler.h +++ b/Crawler/Crawler.h @@ -49,6 +49,7 @@ private: std::vectorupperForegroundTileGroups; int bridgeLayerIndex=-1; float bridgeFadeFactor=0.f; + void InitializeClassAbilities(); public: Crawler(); diff --git a/Crawler/DEFINES.h b/Crawler/DEFINES.h index 3b2bdf7b..f39a61a4 100644 --- a/Crawler/DEFINES.h +++ b/Crawler/DEFINES.h @@ -14,9 +14,9 @@ #define INFINITE 999999 #define SETUP_CLASS(class) \ -class::class(){InitializeClassAbilities();} \ +class::class(){} \ class::class(Player*player) \ - :Player::Player(player){InitializeClassAbilities();} \ + :Player::Player(player){} \ Class class::GetClass(){return cl;} \ std::string class::GetClassName(){return name;} \ Ability&class::GetRightClickAbility(){return rightClickAbility;}; \ diff --git a/Crawler/Player.cpp b/Crawler/Player.cpp index fe782da1..d41eb9a3 100644 --- a/Crawler/Player.cpp +++ b/Crawler/Player.cpp @@ -166,7 +166,7 @@ void Player::Update(float fElapsedTime){ if(castInfo.castTimer>0){ castInfo.castTimer-=fElapsedTime; if(castInfo.castTimer<=0){ - if(castPrepAbility->action(castInfo.castPos)){ + if(castPrepAbility->action(this,castInfo.castPos)){ castPrepAbility->cooldown=castPrepAbility->COOLDOWN_TIME; mana-=castPrepAbility->manaCost; } @@ -269,6 +269,7 @@ void Player::Update(float fElapsedTime){ ability.cooldown=std::max(0.f,ability.cooldown-fElapsedTime); ability2.cooldown=std::max(0.f,ability2.cooldown-fElapsedTime); ability3.cooldown=std::max(0.f,ability3.cooldown-fElapsedTime); + ability4.cooldown=std::max(0.f,ability4.cooldown-fElapsedTime); for(Monster&m:MONSTER_LIST){ if(iframe_time==0&&OnUpperLevel()==m.OnUpperLevel()&&geom2d::overlaps(geom2d::circle(pos,12*size/2),geom2d::circle(m.GetPos(),12*m.GetSizeMult()/2))){ if(m.IsAlive()){ @@ -309,7 +310,7 @@ void Player::Update(float fElapsedTime){ if(ability.name!="???"){ if(ability.cooldown==0&&GetMana()>=ability.manaCost){ if(key.bHeld||key.bReleased&&&ability==castPrepAbility&&GetState()==State::PREP_CAST){ - if(AllowedToCast(ability)&&ability.action({})){ + if(AllowedToCast(ability)&&ability.action(this,{})){ ability.cooldown=ability.COOLDOWN_TIME; mana-=ability.manaCost; }else diff --git a/Crawler/Player.h b/Crawler/Player.h index b9710195..7ee1e4bf 100644 --- a/Crawler/Player.h +++ b/Crawler/Player.h @@ -19,6 +19,12 @@ struct CastInfo{ struct Player{ friend class Crawler; friend class sig::Animation; + friend class Warrior; + friend class Thief; + friend class Ranger; + friend class Trapper; + friend class Wizard; + friend class Witch; private: int hp=100,maxhp=hp; int mana=100,maxmana=mana; @@ -119,7 +125,6 @@ public: virtual Class GetClass()=0; virtual bool AutoAttack()=0; virtual void OnUpdate(float fElapsedTime)=0; - virtual void InitializeClassAbilities()=0; virtual std::string GetClassName()=0; virtual Ability&GetRightClickAbility()=0; virtual Ability&GetAbility1()=0; @@ -148,7 +153,7 @@ struct Warrior:Player{ Class GetClass()override; bool AutoAttack()override; void OnUpdate(float fElapsedTime)override; - void InitializeClassAbilities()override; + static void InitializeClassAbilities(); std::string GetClassName()override; Ability&GetRightClickAbility()override; Ability&GetAbility1()override; @@ -175,7 +180,7 @@ struct Thief:Player{ Class GetClass()override; bool AutoAttack()override; void OnUpdate(float fElapsedTime)override; - void InitializeClassAbilities()override; + static void InitializeClassAbilities(); std::string GetClassName()override; Ability&GetRightClickAbility()override; Ability&GetAbility1()override; @@ -202,7 +207,7 @@ struct Ranger:Player{ Class GetClass()override; bool AutoAttack()override; void OnUpdate(float fElapsedTime)override; - void InitializeClassAbilities()override; + static void InitializeClassAbilities(); std::string GetClassName()override; Ability&GetRightClickAbility()override; Ability&GetAbility1()override; @@ -229,7 +234,7 @@ struct Trapper:Player{ Class GetClass()override; bool AutoAttack()override; void OnUpdate(float fElapsedTime)override; - void InitializeClassAbilities()override; + static void InitializeClassAbilities(); std::string GetClassName()override; Ability&GetRightClickAbility()override; Ability&GetAbility1()override; @@ -256,7 +261,7 @@ struct Wizard:Player{ Class GetClass()override; bool AutoAttack()override; void OnUpdate(float fElapsedTime)override; - void InitializeClassAbilities()override; + static void InitializeClassAbilities(); std::string GetClassName()override; Ability&GetRightClickAbility()override; Ability&GetAbility1()override; @@ -283,7 +288,7 @@ struct Witch:Player{ Class GetClass()override; bool AutoAttack()override; void OnUpdate(float fElapsedTime)override; - void InitializeClassAbilities()override; + static void InitializeClassAbilities(); std::string GetClassName()override; Ability&GetRightClickAbility()override; Ability&GetAbility1()override; diff --git a/Crawler/Ranger.cpp b/Crawler/Ranger.cpp index 11db4bb8..56833a13 100644 --- a/Crawler/Ranger.cpp +++ b/Crawler/Ranger.cpp @@ -37,25 +37,25 @@ bool Ranger::AutoAttack(){ void Ranger::InitializeClassAbilities(){ #pragma region Ranger Right-click Ability (???) Ranger::rightClickAbility.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Ranger Ability 1 (???) Ranger::ability1.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Ranger Ability 2 (???) Ranger::ability2.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Ranger Ability 3 (???) Ranger::ability3.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion diff --git a/Crawler/Thief.cpp b/Crawler/Thief.cpp index f84439c0..74f77001 100644 --- a/Crawler/Thief.cpp +++ b/Crawler/Thief.cpp @@ -37,25 +37,25 @@ bool Thief::AutoAttack(){ void Thief::InitializeClassAbilities(){ #pragma region Thief Right-click Ability (???) Thief::rightClickAbility.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Thief Ability 1 (???) Thief::ability1.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Thief Ability 2 (???) Thief::ability2.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Thief Ability 3 (???) Thief::ability3.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion diff --git a/Crawler/Trapper.cpp b/Crawler/Trapper.cpp index 6d15206e..b08f4aed 100644 --- a/Crawler/Trapper.cpp +++ b/Crawler/Trapper.cpp @@ -37,25 +37,25 @@ bool Trapper::AutoAttack(){ void Trapper::InitializeClassAbilities(){ #pragma region Trapper Right-click Ability (???) Trapper::rightClickAbility.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Trapper Ability 1 (???) Trapper::ability1.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Trapper Ability 2 (???) Trapper::ability2.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Trapper Ability 3 (???) Trapper::ability3.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion diff --git a/Crawler/Version.h b/Crawler/Version.h index 46bda7ea..33c6147c 100644 --- a/Crawler/Version.h +++ b/Crawler/Version.h @@ -2,7 +2,7 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_PATCH 0 -#define VERSION_BUILD 747 +#define VERSION_BUILD 759 #define stringify(a) stringify_(a) #define stringify_(a) #a diff --git a/Crawler/Warrior.cpp b/Crawler/Warrior.cpp index d0d95ecf..d421fa19 100644 --- a/Crawler/Warrior.cpp +++ b/Crawler/Warrior.cpp @@ -70,11 +70,11 @@ bool Warrior::AutoAttack(){ void Warrior::InitializeClassAbilities(){ #pragma region Warrior Right-click Ability (Block) Warrior::rightClickAbility.action= - [&](vf2d pos={}){ - if(GetState()==State::NORMAL){ + [](Player*p,vf2d pos={}){ + if(p->GetState()==State::NORMAL){ rightClickAbility.cooldown=rightClickAbility.COOLDOWN_TIME; - SetState(State::BLOCK); - AddBuff(BuffType::SLOWDOWN,3,0.3); + p->SetState(State::BLOCK); + p->AddBuff(BuffType::SLOWDOWN,3,0.3); return true; } return false; @@ -82,12 +82,12 @@ void Warrior::InitializeClassAbilities(){ #pragma endregion #pragma region Warrior Ability 1 (Battlecry) Warrior::ability1.action= - [&](vf2d pos={}){ - game->AddEffect(std::make_unique(GetPos(),0.1,AnimationState::BATTLECRY_EFFECT,upperLevel,1,0.3)); - AddBuff(BuffType::ATTACK_UP,10,0.1); - AddBuff(BuffType::DAMAGE_REDUCTION,10,0.1); + [](Player*p,vf2d pos={}){ + game->AddEffect(std::make_unique(p->GetPos(),0.1,AnimationState::BATTLECRY_EFFECT,p->upperLevel,1,0.3)); + p->AddBuff(BuffType::ATTACK_UP,10,0.1); + p->AddBuff(BuffType::DAMAGE_REDUCTION,10,0.1); for(Monster&m:MONSTER_LIST){ - if(m.GetSizeMult()<=1&&geom2d::overlaps(geom2d::circle(GetPos(),12*3.5),geom2d::circle(m.GetPos(),m.GetSizeMult()*12))){ + if(m.GetSizeMult()<=1&&geom2d::overlaps(geom2d::circle(p->GetPos(),12*3.5),geom2d::circle(m.GetPos(),m.GetSizeMult()*12))){ m.AddBuff(BuffType::SLOWDOWN,5,0.3); } } @@ -96,41 +96,41 @@ void Warrior::InitializeClassAbilities(){ #pragma endregion #pragma region Warrior Ability 2 (Ground Slam) Warrior::ability2.action= - [&](vf2d pos={}){ - Spin(GROUND_SLAM_SPIN_TIME,14*PI); - iframe_time=GROUND_SLAM_SPIN_TIME+0.1; + [](Player*p,vf2d pos={}){ + p->Spin(GROUND_SLAM_SPIN_TIME,14*PI); + p->iframe_time=GROUND_SLAM_SPIN_TIME+0.1; return true; }; #pragma endregion #pragma region Warrior Ability 3 (Sonic Slash) Warrior::ability3.action= - [&](vf2d pos={}){ - SetState(State::SWING_SONIC_SWORD); - AddBuff(BuffType::SLOWDOWN,0.5,1); + [](Player*p,vf2d pos={}){ + p->SetState(State::SWING_SONIC_SWORD); + p->AddBuff(BuffType::SLOWDOWN,0.5,1); vf2d bulletVel={}; - switch(GetFacingDirection()){ + switch(p->GetFacingDirection()){ case UP:{ - vel.y=70; + p->vel.y=70; bulletVel.y=-400; - UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_N,WARRIOR); + p->UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_N,WARRIOR); }break; case LEFT:{ - vel.x=70; + p->vel.x=70; bulletVel.x=-400; - UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_W,WARRIOR); + p->UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_W,WARRIOR); }break; case RIGHT:{ - vel.x=-70; + p->vel.x=-70; bulletVel.x=400; - UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_E,WARRIOR); + p->UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_E,WARRIOR); }break; case DOWN:{ - vel.y=-70; + p->vel.y=-70; bulletVel.y=400; - UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_S,WARRIOR); + p->UpdateAnimation(AnimationState::WARRIOR_SWINGSONICSWORD_S,WARRIOR); }break; } - BULLET_LIST.push_back(std::make_unique(GetPos(),bulletVel,30,GetAttack()*8,AnimationState::SONICSLASH,upperLevel,true,2.25,true,true,WHITE)); + BULLET_LIST.push_back(std::make_unique(p->GetPos(),bulletVel,30,p->GetAttack()*8,AnimationState::SONICSLASH,p->upperLevel,true,2.25,true,true,WHITE)); game->SetupWorldShake(0.5); return true; }; diff --git a/Crawler/Witch.cpp b/Crawler/Witch.cpp index 0f5d9e20..813aa601 100644 --- a/Crawler/Witch.cpp +++ b/Crawler/Witch.cpp @@ -37,25 +37,25 @@ bool Witch::AutoAttack(){ void Witch::InitializeClassAbilities(){ #pragma region Witch Right-click Ability (???) Witch::rightClickAbility.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Witch Ability 1 (???) Witch::ability1.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Witch Ability 2 (???) Witch::ability2.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion #pragma region Witch Ability 3 (???) Witch::ability3.action= - [&](vf2d pos={}){ + [](Player*p,vf2d pos={}){ return false; }; #pragma endregion diff --git a/Crawler/Wizard.cpp b/Crawler/Wizard.cpp index 69077baf..2f9dfa67 100644 --- a/Crawler/Wizard.cpp +++ b/Crawler/Wizard.cpp @@ -75,53 +75,54 @@ bool Wizard::AutoAttack(){ void Wizard::InitializeClassAbilities(){ #pragma region Wizard Right-click Ability (Teleport) Wizard::rightClickAbility.action= - [&](vf2d pos={}){ - float pointMouseDirection=atan2(game->GetWorldMousePos().y-GetPos().y,game->GetWorldMousePos().x-GetPos().x); + [](Player*p,vf2d pos={}){ + if(p->GetState()==State::CASTING)return false; + float pointMouseDirection=atan2(game->GetWorldMousePos().y-p->GetPos().y,game->GetWorldMousePos().x-p->GetPos().x); vf2d pointTowardsMouse={cos(pointMouseDirection),sin(pointMouseDirection)}; - float dist=std::clamp(geom2d::line{GetPos(),game->GetWorldMousePos()}.length(),0.f,6.5f*24); + float dist=std::clamp(geom2d::line{p->GetPos(),game->GetWorldMousePos()}.length(),0.f,6.5f*24); if(dist<12)return false; - vf2d teleportPoint=GetPos()+pointTowardsMouse*dist; - while(dist>0&&game->HasTileCollision(game->GetCurrentLevel(),teleportPoint)&&CanPathfindTo(GetPos(),teleportPoint)){ + vf2d teleportPoint=p->GetPos()+pointTowardsMouse*dist; + while(dist>0&&game->HasTileCollision(game->GetCurrentLevel(),teleportPoint)&&p->CanPathfindTo(p->GetPos(),teleportPoint)){ dist-=24; - teleportPoint=GetPos()+pointTowardsMouse*dist; + teleportPoint=p->GetPos()+pointTowardsMouse*dist; } - if(dist>0&&CanPathfindTo(GetPos(),teleportPoint)){ - SetState(State::TELEPORT); - teleportAnimationTimer=0.35; - teleportTarget=teleportPoint; - teleportStartPosition=GetPos(); - iframe_time=0.35; + if(dist>0&&p->CanPathfindTo(p->GetPos(),teleportPoint)){ + p->SetState(State::TELEPORT); + p->teleportAnimationTimer=0.35; + p->teleportTarget=teleportPoint; + p->teleportStartPosition=p->GetPos(); + p->iframe_time=0.35; for(int i=0;i<16;i++){ - game->AddEffect(std::make_unique(GetPos()+vf2d{(rand()%160-80)/10.f,(rand()%160-80)/10.f},float(rand()%300)/1000,AnimationState::DOT_PARTICLE,upperLevel,0.3,0.2,vf2d{float(rand()%1000-500)/100,float(rand()%1000-500)/100},BLACK)); + game->AddEffect(std::make_unique(p->GetPos()+vf2d{(rand()%160-80)/10.f,(rand()%160-80)/10.f},float(rand()%300)/1000,AnimationState::DOT_PARTICLE,p->upperLevel,0.3,0.2,vf2d{float(rand()%1000-500)/100,float(rand()%1000-500)/100},BLACK)); } return true; } else { - notificationDisplay={"Cannot Teleport to that location!",0.5}; + p->notificationDisplay={"Cannot Teleport to that location!",0.5}; return false; } }; #pragma endregion #pragma region Wizard Ability 1 (Fire Bolt) Wizard::ability1.action= - [&](vf2d pos={}){ - float angleToCursor=atan2(game->GetWorldMousePos().y-GetPos().y,game->GetWorldMousePos().x-GetPos().x); - BULLET_LIST.push_back(std::make_unique(FireBolt(GetPos(),{cos(angleToCursor)*275,sin(angleToCursor)*275},12,GetAttack(),upperLevel,true,{240,120,60}))); + [](Player*p,vf2d pos={}){ + float angleToCursor=atan2(game->GetWorldMousePos().y-p->GetPos().y,game->GetWorldMousePos().x-p->GetPos().x); + BULLET_LIST.push_back(std::make_unique(FireBolt(p->GetPos(),{cos(angleToCursor)*275,sin(angleToCursor)*275},12,p->GetAttack(),p->upperLevel,true,{240,120,60}))); return true; }; #pragma endregion #pragma region Wizard Ability 2 (Lightning Bolt) Wizard::ability2.action= - [&](vf2d pos={}){ - float angleToCursor=atan2(game->GetWorldMousePos().y-GetPos().y,game->GetWorldMousePos().x-GetPos().x); - BULLET_LIST.push_back(std::make_unique(LightningBolt(GetPos(),{cos(angleToCursor)*230,sin(angleToCursor)*230},12,GetAttack()*4,upperLevel,true,WHITE))); + [](Player*p,vf2d pos={}){ + float angleToCursor=atan2(game->GetWorldMousePos().y-p->GetPos().y,game->GetWorldMousePos().x-p->GetPos().x); + BULLET_LIST.push_back(std::make_unique(LightningBolt(p->GetPos(),{cos(angleToCursor)*230,sin(angleToCursor)*230},12,p->GetAttack()*4,p->upperLevel,true,WHITE))); return true; }; #pragma endregion #pragma region Wizard Ability 3 (Meteor) Wizard::ability3.action= - [&](vf2d pos={}){ - CastSpell(Wizard::ability3); - game->AddEffect(std::make_unique(pos,3,AnimationState::METEOR,OnUpperLevel(),vf2d{1,1},2)); + [](Player*p,vf2d pos={}){ + p->CastSpell(Wizard::ability3); + game->AddEffect(std::make_unique(pos,3,AnimationState::METEOR,p->OnUpperLevel(),vf2d{1,1},2)); return true; }; #pragma endregion