diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp index 6b762d6e..51c16160 100644 --- a/Crawler/Crawler.cpp +++ b/Crawler/Crawler.cpp @@ -4,12 +4,14 @@ #include "olcPGEX_TransformedView.h" #include "Crawler.h" #include "olcUTIL_Camera2D.h" +#include "DamageNumber.h" //192x192 const vi2d WINDOW_SIZE={24*8,24*8}; std::mapANIMATION_DATA; std::vectorMONSTER_LIST; std::vectorSPAWNER_LIST; +std::vectorDAMAGENUMBER_LIST; Crawler*game; Crawler::Crawler() @@ -57,7 +59,7 @@ bool Crawler::OnUserUpdate(float fElapsedTime){ m.Update(fElapsedTime); } UpdateCamera(fElapsedTime); - RenderWorld(); + RenderWorld(fElapsedTime); return true; } @@ -236,17 +238,40 @@ void Crawler::UpdateCamera(float fElapsedTime){ view.SetWorldOffset(vi2d(camera.GetViewPosition())); } -void Crawler::RenderWorld(){ +void Crawler::RenderWorld(float fElapsedTime){ Clear({100,180,100}); for (int x = view.GetTopLeftTile().x/24-1; x <= view.GetBottomRightTile().x/24; x++){ for (int y = view.GetTopLeftTile().y/24-1; y <= view.GetBottomRightTile().y/24; y++){ view.DrawRect(vi2d{x,y}*24,{24,24},VERY_DARK_GREY); } } + std::vectormonstersBefore,monstersAfter; + Player&pl=player; + std::copy_if(MONSTER_LIST.begin(),MONSTER_LIST.end(),std::back_inserter(monstersBefore),[&pl](Monster&m){return m.GetPos().y=pl.GetPos().y;}); + std::sort(monstersBefore.begin(),monstersBefore.end(),[](Monster&m1,Monster&m2){return m1.GetPos().yDecal(),m.GetFrame().GetSourceRect().pos,m.GetFrame().GetSourceRect().size,vf2d(m.GetSizeMult(),m.GetSizeMult())); + } view.DrawPartialDecal(player.GetPos()-vi2d{12,12}*player.GetSizeMult(),player.GetFrame().GetSourceImage()->Decal(),player.GetFrame().GetSourceRect().pos,player.GetFrame().GetSourceRect().size,vf2d(player.GetSizeMult(),player.GetSizeMult())); - for(Monster&m:MONSTER_LIST){ + for(Monster&m:monstersAfter){ view.DrawPartialDecal(m.GetPos()-vi2d{12,12}*m.GetSizeMult(),m.GetFrame().GetSourceImage()->Decal(),m.GetFrame().GetSourceRect().pos,m.GetFrame().GetSourceRect().size,vf2d(m.GetSizeMult(),m.GetSizeMult())); } + for(std::vector::iterator it=DAMAGENUMBER_LIST.begin();it!=DAMAGENUMBER_LIST.end();++it){ + DamageNumber&dn=*it; + dn.lifeTime+=fElapsedTime; + if(dn.lifeTime>1){ + it=DAMAGENUMBER_LIST.erase(it); + if(it==DAMAGENUMBER_LIST.end()){ + break; + } + } else { + dn.pos.y-=20*fElapsedTime; + std::string text=std::to_string(dn.damage); + view.DrawStringPropDecal(dn.pos-GetTextSizeProp(text)/2,text,DARK_RED); + } + } } int main() diff --git a/Crawler/Crawler.h b/Crawler/Crawler.h index 4e18839b..2ba1e615 100644 --- a/Crawler/Crawler.h +++ b/Crawler/Crawler.h @@ -25,5 +25,5 @@ public: void InitializeAnimations(); void HandleUserInput(float fElapsedTime); void UpdateCamera(float fElapsedTime); - void RenderWorld(); + void RenderWorld(float fElapsedTime); }; \ No newline at end of file diff --git a/Crawler/Crawler.vcxproj b/Crawler/Crawler.vcxproj index 8bd8d54f..7cfaf575 100644 --- a/Crawler/Crawler.vcxproj +++ b/Crawler/Crawler.vcxproj @@ -131,6 +131,7 @@ + @@ -141,6 +142,7 @@ + diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index 0f1b29de..3674c956 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -42,6 +42,9 @@ Header Files + + Header Files + @@ -56,5 +59,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/Crawler/DamageNumber.cpp b/Crawler/DamageNumber.cpp new file mode 100644 index 00000000..e0baedba --- /dev/null +++ b/Crawler/DamageNumber.cpp @@ -0,0 +1,8 @@ +#include "DamageNumber.h" + +DamageNumber::DamageNumber(){ +} + +DamageNumber::DamageNumber(vf2d pos,int damage): + pos(pos),damage(damage){ +} \ No newline at end of file diff --git a/Crawler/DamageNumber.h b/Crawler/DamageNumber.h new file mode 100644 index 00000000..9ecfef47 --- /dev/null +++ b/Crawler/DamageNumber.h @@ -0,0 +1,11 @@ +#pragma once +#include "olcPixelGameEngine.h" + +struct DamageNumber{ + vf2d pos; + int damage; + float moveUpTime; + float lifeTime; + DamageNumber(); + DamageNumber(vf2d pos,int damage); +}; \ No newline at end of file diff --git a/Crawler/Monster.cpp b/Crawler/Monster.cpp index 10d439c1..ee2fab8c 100644 --- a/Crawler/Monster.cpp +++ b/Crawler/Monster.cpp @@ -1,8 +1,10 @@ #include "Monster.h" +#include "DamageNumber.h" extern std::mapANIMATION_DATA; extern std::mapMONSTER_DATA; extern std::vectorMONSTER_LIST; +extern std::vectorDAMAGENUMBER_LIST; MonsterData::MonsterData(){} MonsterData::MonsterData(MonsterName type,int hp,int atk,std::vectoranimations,float moveSpd,float size,MonsterStrategy strategy): @@ -98,11 +100,16 @@ AnimationState Monster::GetDeathAnimationName(){ } void Monster::Hurt(int damage){ hp=std::max(0,hp-damage); + DAMAGENUMBER_LIST.push_back(DamageNumber(pos,damage)); if(hp<=0){ animation.ChangeState(internal_animState,GetDeathAnimationName()); } } +bool Monster::IsAlive(){ + return hp>0; +} + MonsterSpawner::MonsterSpawner(){} MonsterSpawner::MonsterSpawner(vf2d pos,int range,std::vector>monsters): pos(pos),range(range),monsters(monsters){ diff --git a/Crawler/Monster.h b/Crawler/Monster.h index f2a8bcac..df0e2894 100644 --- a/Crawler/Monster.h +++ b/Crawler/Monster.h @@ -65,6 +65,7 @@ struct Monster{ void UpdateAnimation(AnimationState state); bool Update(float fElapsedTime); void Hurt(int damage); + bool IsAlive(); }; struct MonsterSpawner{ @@ -72,7 +73,7 @@ struct MonsterSpawner{ vf2d pos; int range; std::vector>monsters; - bool triggered; + bool triggered=false; public: MonsterSpawner(); //For the monster list, the second pair item is the position relative to the spawner to spawn the monster. diff --git a/Crawler/Player.cpp b/Crawler/Player.cpp index 96408dcb..f99d6ee2 100644 --- a/Crawler/Player.cpp +++ b/Crawler/Player.cpp @@ -1,12 +1,14 @@ #include "Monster.h" #include "Player.h" #include "Crawler.h" +#include "DamageNumber.h" extern std::mapMONSTER_DATA; extern std::vectorMONSTER_LIST; extern std::mapANIMATION_DATA; extern std::vectorSPAWNER_LIST; +extern std::vectorDAMAGENUMBER_LIST; extern Crawler*game; Player::Player(){ @@ -70,7 +72,7 @@ void Player::Update(float fElapsedTime){ if(attack_cooldown_timer==0&&game->GetMouse(0).bHeld){ bool attack=false; for(Monster&m:MONSTER_LIST){ - if(geom2d::overlaps(geom2d::circle(pos-vf2d{size*12,size*12},attack_range*size*12),geom2d::circle(m.GetPos()-vf2d{m.GetSizeMult()*12,m.GetSizeMult()*12},m.GetSizeMult()*12))){ + if(m.IsAlive()&&geom2d::overlaps(geom2d::circle(pos-vf2d{size*12,size*12},attack_range*size*12),geom2d::circle(m.GetPos()-vf2d{m.GetSizeMult()*12,m.GetSizeMult()*12},m.GetSizeMult()*12))){ m.Hurt(atk); attack=true; break; @@ -82,6 +84,11 @@ void Player::Update(float fElapsedTime){ } } +void Player::Hurt(int damage){ + hp=std::max(0,hp-damage); + DAMAGENUMBER_LIST.push_back(DamageNumber(pos,damage)); +} + void Player::AddAnimation(AnimationState state){ animation.AddState(state,ANIMATION_DATA[state]); } diff --git a/Crawler/Player.h b/Crawler/Player.h index fed5de18..d3b4717d 100644 --- a/Crawler/Player.h +++ b/Crawler/Player.h @@ -32,6 +32,8 @@ public: float GetSizeMult(); float GetAttackRangeMult(); + void Hurt(int damage); + void Update(float fElapsedTime); void AddAnimation(AnimationState state); void UpdateAnimation(AnimationState animState);