Monsters in front/behind now sorted proper.

pull/28/head
sigonasr2 2 years ago
parent 6584a240b1
commit 15c246689f
  1. 31
      Crawler/Crawler.cpp
  2. 2
      Crawler/Crawler.h
  3. 2
      Crawler/Crawler.vcxproj
  4. 6
      Crawler/Crawler.vcxproj.filters
  5. 8
      Crawler/DamageNumber.cpp
  6. 11
      Crawler/DamageNumber.h
  7. 7
      Crawler/Monster.cpp
  8. 3
      Crawler/Monster.h
  9. 9
      Crawler/Player.cpp
  10. 2
      Crawler/Player.h

@ -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::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
std::vector<Monster>MONSTER_LIST;
std::vector<MonsterSpawner>SPAWNER_LIST;
std::vector<DamageNumber>DAMAGENUMBER_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::vector<Monster>monstersBefore,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::copy_if(MONSTER_LIST.begin(),MONSTER_LIST.end(),std::back_inserter(monstersAfter),[&pl](Monster&m){return m.GetPos().y>=pl.GetPos().y;});
std::sort(monstersBefore.begin(),monstersBefore.end(),[](Monster&m1,Monster&m2){return m1.GetPos().y<m2.GetPos().y;});
std::sort(monstersAfter.begin(),monstersAfter.end(),[](Monster&m1,Monster&m2){return m1.GetPos().y<m2.GetPos().y;});
for(Monster&m:monstersBefore){
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()));
}
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<DamageNumber>::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()

@ -25,5 +25,5 @@ public:
void InitializeAnimations();
void HandleUserInput(float fElapsedTime);
void UpdateCamera(float fElapsedTime);
void RenderWorld();
void RenderWorld(float fElapsedTime);
};

@ -131,6 +131,7 @@
<ItemGroup>
<ClInclude Include="Animation.h" />
<ClInclude Include="Crawler.h" />
<ClInclude Include="DamageNumber.h" />
<ClInclude Include="Monster.h" />
<ClInclude Include="olcPGEX_TransformedView.h" />
<ClInclude Include="olcPixelGameEngine.h" />
@ -141,6 +142,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="Crawler.cpp" />
<ClCompile Include="DamageNumber.cpp" />
<ClCompile Include="Player.cpp" />
<ClCompile Include="Monster.cpp" />
<ClCompile Include="MonsterData.cpp" />

@ -42,6 +42,9 @@
<ClInclude Include="Player.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DamageNumber.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Player.cpp">
@ -56,5 +59,8 @@
<ClCompile Include="Crawler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DamageNumber.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

@ -0,0 +1,8 @@
#include "DamageNumber.h"
DamageNumber::DamageNumber(){
}
DamageNumber::DamageNumber(vf2d pos,int damage):
pos(pos),damage(damage){
}

@ -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);
};

@ -1,8 +1,10 @@
#include "Monster.h"
#include "DamageNumber.h"
extern std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
extern std::map<MonsterName,MonsterData>MONSTER_DATA;
extern std::vector<Monster>MONSTER_LIST;
extern std::vector<DamageNumber>DAMAGENUMBER_LIST;
MonsterData::MonsterData(){}
MonsterData::MonsterData(MonsterName type,int hp,int atk,std::vector<AnimationState>animations,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<std::pair<MonsterName,vf2d>>monsters):
pos(pos),range(range),monsters(monsters){

@ -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<std::pair<MonsterName,vf2d>>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.

@ -1,12 +1,14 @@
#include "Monster.h"
#include "Player.h"
#include "Crawler.h"
#include "DamageNumber.h"
extern std::map<MonsterName,MonsterData>MONSTER_DATA;
extern std::vector<Monster>MONSTER_LIST;
extern std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
extern std::vector<MonsterSpawner>SPAWNER_LIST;
extern std::vector<DamageNumber>DAMAGENUMBER_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<float>(pos-vf2d{size*12,size*12},attack_range*size*12),geom2d::circle<float>(m.GetPos()-vf2d{m.GetSizeMult()*12,m.GetSizeMult()*12},m.GetSizeMult()*12))){
if(m.IsAlive()&&geom2d::overlaps(geom2d::circle<float>(pos-vf2d{size*12,size*12},attack_range*size*12),geom2d::circle<float>(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]);
}

@ -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);

Loading…
Cancel
Save