diff --git a/Crawler/Monster.cpp b/Crawler/Monster.cpp index 297c82cf..7deaa5a2 100644 --- a/Crawler/Monster.cpp +++ b/Crawler/Monster.cpp @@ -1,6 +1,8 @@ #include "Monster.h" extern std::mapANIMATION_DATA; +extern std::mapMONSTER_DATA; +extern std::vectorMONSTER_LIST; MonsterData::MonsterData(){} MonsterData::MonsterData(int hp,int atk,std::vectoranimations,float moveSpd,float size,MonsterStrategy strategy): @@ -33,6 +35,7 @@ Monster::Monster(vf2d pos,MonsterData data): firstAnimation=false; } } + randomFrameOffset=(rand()%1000)/1000.f; } vf2d&Monster::GetPos(){ return pos; @@ -49,6 +52,9 @@ float Monster::GetMoveSpdMult(){ float Monster::GetSizeMult(){ return size; } +Animate2D::Frame Monster::GetFrame(){ + return animation.GetFrame(internal_animState); +} void Monster::UpdateAnimation(AnimationState state){ animation.ChangeState(internal_animState,state); } @@ -61,11 +67,12 @@ void Monster::Update(float fElapsedTime){ }break; } - animation.UpdateState(internal_animState,fElapsedTime); + animation.UpdateState(internal_animState,randomFrameOffset+fElapsedTime); + randomFrameOffset=0; } MonsterSpawner::MonsterSpawner(){} -MonsterSpawner::MonsterSpawner(vf2d pos,int range,std::vector>monsters): +MonsterSpawner::MonsterSpawner(vf2d pos,int range,std::vector>monsters): pos(pos),range(range),monsters(monsters){ } bool MonsterSpawner::SpawnTriggered(){ @@ -77,6 +84,11 @@ int MonsterSpawner::GetRange(){ vf2d MonsterSpawner::GetPos(){ return pos; } -void MonsterSpawner::SetTriggered(bool trigger){ +void MonsterSpawner::SetTriggered(bool trigger,bool spawnMonsters){ triggered=trigger; + if(spawnMonsters){ + for(std::pair&monsterInfo:monsters){ + MONSTER_LIST.push_back(Monster(pos+monsterInfo.second,MONSTER_DATA[monsterInfo.first])); + } + } } \ No newline at end of file diff --git a/Crawler/Monster.h b/Crawler/Monster.h index 9ba1163b..3b6dbd85 100644 --- a/Crawler/Monster.h +++ b/Crawler/Monster.h @@ -46,6 +46,7 @@ struct Monster{ MonsterStrategy strategy; Animate2D::Animationanimation; Animate2D::AnimationState internal_animState; + float randomFrameOffset=0.f; public: Monster(); Monster(vf2d pos,MonsterData data); @@ -54,6 +55,7 @@ struct Monster{ int GetAttack(); float GetMoveSpdMult(); float GetSizeMult(); + Animate2D::Frame GetFrame(); void UpdateAnimation(AnimationState state); void Update(float fElapsedTime); }; @@ -62,13 +64,14 @@ struct MonsterSpawner{ private: vf2d pos; int range; - std::vector>monsters; + std::vector>monsters; bool triggered; public: MonsterSpawner(); - MonsterSpawner(vf2d pos,int range,std::vector>monsters); + //For the monster list, the second pair item is the position relative to the spawner to spawn the monster. + MonsterSpawner(vf2d pos,int range,std::vector>MONSTER_LIST); bool SpawnTriggered(); int GetRange(); vf2d GetPos(); - void SetTriggered(bool trigger); + void SetTriggered(bool trigger,bool spawnMonsters=true); }; \ No newline at end of file diff --git a/Crawler/main.cpp b/Crawler/main.cpp index eb8fe2fe..3296f6e4 100644 --- a/Crawler/main.cpp +++ b/Crawler/main.cpp @@ -7,10 +7,13 @@ #include "Monster.h" #include "Animation.h" +//192x192 const vi2d WINDOW_SIZE={24*8,24*8}; extern std::mapMONSTER_DATA; std::mapANIMATION_DATA; +std::vectorMONSTER_LIST; +std::vectorSPAWNER_LIST; struct Player{ private: @@ -85,6 +88,15 @@ struct Player{ Key GetLastReleasedMovementKey(){ return lastReleasedMovementKey; } + + //Triggers when the player has moved. + void Moved(){ + for(MonsterSpawner&spawner:SPAWNER_LIST){ + if(!spawner.SpawnTriggered()&&geom2d::overlaps(geom2d::circle(pos,size*12),geom2d::circle(spawner.GetPos(),spawner.GetRange()))){ + spawner.SetTriggered(true); + } + } + } }; class Crawler : public olc::PixelGameEngine @@ -94,8 +106,6 @@ class Crawler : public olc::PixelGameEngine TileTransformedView view; Player player=Player{{}}; Renderable GFX_Pl_Sheet,GFX_Slime_Sheet; - std::vectorspawners; - std::vectormonsters; public: Crawler() @@ -133,7 +143,7 @@ public: player.SetPos({4*24,4*24}); player.UpdateAnimation(AnimationState::IDLE_S); - + SPAWNER_LIST.push_back(MonsterSpawner({336,96},4*24,{{{MonsterName::SLIME_GREEN,{-32,-40}},{MonsterName::SLIME_GREEN,{64,20}}}})); return true; } @@ -142,11 +152,14 @@ public: { HandleUserInput(fElapsedTime); player.Update(fElapsedTime); + for(Monster&m:MONSTER_LIST){ + m.Update(fElapsedTime); + } UpdateCamera(fElapsedTime); RenderWorld(); return true; } - + void InitializeAnimations(){ Animate2D::FrameSequence pl_walk_s{0.2}; pl_walk_s.AddFrame({&GFX_Pl_Sheet,{vi2d{0,0}*24,{24,24}}}); @@ -307,6 +320,9 @@ public: player.UpdateAnimation(AnimationState::IDLE_S); } } + } else { + //We have moved. + player.Moved(); } } @@ -322,7 +338,10 @@ public: view.DrawRect(vi2d{x,y}*24,{24,24},VERY_DARK_GREY); } } - view.DrawPartialDecal(player.GetPos()-vi2d{12,12},player.GetFrame().GetSourceImage()->Decal(),player.GetFrame().GetSourceRect().pos,player.GetFrame().GetSourceRect().size); + 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){ + 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())); + } } };