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. 27
      Crawler/main.cpp

@ -1,6 +1,8 @@
#include "Monster.h"
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(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;
}
}
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<std::pair<MonsterData,vf2d>>monsters):
MonsterSpawner::MonsterSpawner(vf2d pos,int range,std::vector<std::pair<MonsterName,vf2d>>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<MonsterName,vf2d>&monsterInfo:monsters){
MONSTER_LIST.push_back(Monster(pos+monsterInfo.second,MONSTER_DATA[monsterInfo.first]));
}
}
}

@ -46,6 +46,7 @@ struct Monster{
MonsterStrategy strategy;
Animate2D::Animation<AnimationState>animation;
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<std::pair<MonsterData,vf2d>>monsters;
std::vector<std::pair<MonsterName,vf2d>>monsters;
bool triggered;
public:
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();
int GetRange();
vf2d GetPos();
void SetTriggered(bool trigger);
void SetTriggered(bool trigger,bool spawnMonsters=true);
};

@ -7,10 +7,13 @@
#include "Monster.h"
#include "Animation.h"
//192x192
const vi2d WINDOW_SIZE={24*8,24*8};
extern std::map<MonsterName,MonsterData>MONSTER_DATA;
std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
std::vector<Monster>MONSTER_LIST;
std::vector<MonsterSpawner>SPAWNER_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<float>(pos,size*12),geom2d::circle<float>(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::vector<MonsterSpawner>spawners;
std::vector<Monster>monsters;
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,6 +152,9 @@ public:
{
HandleUserInput(fElapsedTime);
player.Update(fElapsedTime);
for(Monster&m:MONSTER_LIST){
m.Update(fElapsedTime);
}
UpdateCamera(fElapsedTime);
RenderWorld();
return true;
@ -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()));
}
}
};

Loading…
Cancel
Save