Implemented spawners and monster rendering.

pull/28/head
sigonasr2 1 year ago
parent 0f449a902a
commit 8bb42df861
  1. 18
      Crawler/Monster.cpp
  2. 9
      Crawler/Monster.h
  3. 29
      Crawler/main.cpp

@ -1,6 +1,8 @@
#include "Monster.h" #include "Monster.h"
extern std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA; extern std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
extern std::map<MonsterName,MonsterData>MONSTER_DATA;
extern std::vector<Monster>MONSTER_LIST;
MonsterData::MonsterData(){} MonsterData::MonsterData(){}
MonsterData::MonsterData(int hp,int atk,std::vector<AnimationState>animations,float moveSpd,float size,MonsterStrategy strategy): MonsterData::MonsterData(int hp,int atk,std::vector<AnimationState>animations,float moveSpd,float size,MonsterStrategy strategy):
@ -33,6 +35,7 @@ Monster::Monster(vf2d pos,MonsterData data):
firstAnimation=false; firstAnimation=false;
} }
} }
randomFrameOffset=(rand()%1000)/1000.f;
} }
vf2d&Monster::GetPos(){ vf2d&Monster::GetPos(){
return pos; return pos;
@ -49,6 +52,9 @@ float Monster::GetMoveSpdMult(){
float Monster::GetSizeMult(){ float Monster::GetSizeMult(){
return size; return size;
} }
Animate2D::Frame Monster::GetFrame(){
return animation.GetFrame(internal_animState);
}
void Monster::UpdateAnimation(AnimationState state){ void Monster::UpdateAnimation(AnimationState state){
animation.ChangeState(internal_animState,state); animation.ChangeState(internal_animState,state);
} }
@ -61,11 +67,12 @@ void Monster::Update(float fElapsedTime){
}break; }break;
} }
animation.UpdateState(internal_animState,fElapsedTime); animation.UpdateState(internal_animState,randomFrameOffset+fElapsedTime);
randomFrameOffset=0;
} }
MonsterSpawner::MonsterSpawner(){} MonsterSpawner::MonsterSpawner(){}
MonsterSpawner::MonsterSpawner(vf2d pos,int range,std::vector<std::pair<MonsterData,vf2d>>monsters): MonsterSpawner::MonsterSpawner(vf2d pos,int range,std::vector<std::pair<MonsterName,vf2d>>monsters):
pos(pos),range(range),monsters(monsters){ pos(pos),range(range),monsters(monsters){
} }
bool MonsterSpawner::SpawnTriggered(){ bool MonsterSpawner::SpawnTriggered(){
@ -77,6 +84,11 @@ int MonsterSpawner::GetRange(){
vf2d MonsterSpawner::GetPos(){ vf2d MonsterSpawner::GetPos(){
return pos; return pos;
} }
void MonsterSpawner::SetTriggered(bool trigger){ void MonsterSpawner::SetTriggered(bool trigger,bool spawnMonsters){
triggered=trigger; triggered=trigger;
if(spawnMonsters){
for(std::pair<MonsterName,vf2d>&monsterInfo:monsters){
MONSTER_LIST.push_back(Monster(pos+monsterInfo.second,MONSTER_DATA[monsterInfo.first]));
}
}
} }

@ -46,6 +46,7 @@ struct Monster{
MonsterStrategy strategy; MonsterStrategy strategy;
Animate2D::Animation<AnimationState>animation; Animate2D::Animation<AnimationState>animation;
Animate2D::AnimationState internal_animState; Animate2D::AnimationState internal_animState;
float randomFrameOffset=0.f;
public: public:
Monster(); Monster();
Monster(vf2d pos,MonsterData data); Monster(vf2d pos,MonsterData data);
@ -54,6 +55,7 @@ struct Monster{
int GetAttack(); int GetAttack();
float GetMoveSpdMult(); float GetMoveSpdMult();
float GetSizeMult(); float GetSizeMult();
Animate2D::Frame GetFrame();
void UpdateAnimation(AnimationState state); void UpdateAnimation(AnimationState state);
void Update(float fElapsedTime); void Update(float fElapsedTime);
}; };
@ -62,13 +64,14 @@ struct MonsterSpawner{
private: private:
vf2d pos; vf2d pos;
int range; int range;
std::vector<std::pair<MonsterData,vf2d>>monsters; std::vector<std::pair<MonsterName,vf2d>>monsters;
bool triggered; bool triggered;
public: public:
MonsterSpawner(); MonsterSpawner();
MonsterSpawner(vf2d pos,int range,std::vector<std::pair<MonsterData,vf2d>>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<std::pair<MonsterName,vf2d>>MONSTER_LIST);
bool SpawnTriggered(); bool SpawnTriggered();
int GetRange(); int GetRange();
vf2d GetPos(); vf2d GetPos();
void SetTriggered(bool trigger); void SetTriggered(bool trigger,bool spawnMonsters=true);
}; };

@ -7,10 +7,13 @@
#include "Monster.h" #include "Monster.h"
#include "Animation.h" #include "Animation.h"
//192x192
const vi2d WINDOW_SIZE={24*8,24*8}; const vi2d WINDOW_SIZE={24*8,24*8};
extern std::map<MonsterName,MonsterData>MONSTER_DATA; extern std::map<MonsterName,MonsterData>MONSTER_DATA;
std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA; std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
std::vector<Monster>MONSTER_LIST;
std::vector<MonsterSpawner>SPAWNER_LIST;
struct Player{ struct Player{
private: private:
@ -85,6 +88,15 @@ struct Player{
Key GetLastReleasedMovementKey(){ Key GetLastReleasedMovementKey(){
return lastReleasedMovementKey; return lastReleasedMovementKey;
} }
//Triggers when the player has moved.
void Moved(){
for(MonsterSpawner&spawner:SPAWNER_LIST){
if(!spawner.SpawnTriggered()&&geom2d::overlaps(geom2d::circle<float>(pos,size*12),geom2d::circle<float>(spawner.GetPos(),spawner.GetRange()))){
spawner.SetTriggered(true);
}
}
}
}; };
class Crawler : public olc::PixelGameEngine class Crawler : public olc::PixelGameEngine
@ -94,8 +106,6 @@ class Crawler : public olc::PixelGameEngine
TileTransformedView view; TileTransformedView view;
Player player=Player{{}}; Player player=Player{{}};
Renderable GFX_Pl_Sheet,GFX_Slime_Sheet; Renderable GFX_Pl_Sheet,GFX_Slime_Sheet;
std::vector<MonsterSpawner>spawners;
std::vector<Monster>monsters;
public: public:
Crawler() Crawler()
@ -133,7 +143,7 @@ public:
player.SetPos({4*24,4*24}); player.SetPos({4*24,4*24});
player.UpdateAnimation(AnimationState::IDLE_S); 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; return true;
} }
@ -142,11 +152,14 @@ public:
{ {
HandleUserInput(fElapsedTime); HandleUserInput(fElapsedTime);
player.Update(fElapsedTime); player.Update(fElapsedTime);
for(Monster&m:MONSTER_LIST){
m.Update(fElapsedTime);
}
UpdateCamera(fElapsedTime); UpdateCamera(fElapsedTime);
RenderWorld(); RenderWorld();
return true; return true;
} }
void InitializeAnimations(){ void InitializeAnimations(){
Animate2D::FrameSequence pl_walk_s{0.2}; Animate2D::FrameSequence pl_walk_s{0.2};
pl_walk_s.AddFrame({&GFX_Pl_Sheet,{vi2d{0,0}*24,{24,24}}}); pl_walk_s.AddFrame({&GFX_Pl_Sheet,{vi2d{0,0}*24,{24,24}}});
@ -307,6 +320,9 @@ public:
player.UpdateAnimation(AnimationState::IDLE_S); 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.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()));
}
} }
}; };

Loading…
Cancel
Save