#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() { sAppName = "Crawler Concept"; game=this; } bool Crawler::OnUserCreate(){ //Initialize Camera. camera=Camera2D{WINDOW_SIZE}; camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow); camera.SetTarget(player.GetPos()); camera.SetWorldBoundary({0,0},WORLD_SIZE*24); camera.EnableWorldBoundary(true); //Graphics GFX_Pl_Sheet.Load("assets/nico-warrior.png"); GFX_Slime_Sheet.Load("assets/slime.png"); GFX_Circle.Load("assets/circle.png"); //Animations InitializeAnimations(); player.AddAnimation(AnimationState::WALK_N); player.AddAnimation(AnimationState::WALK_E); player.AddAnimation(AnimationState::WALK_S); player.AddAnimation(AnimationState::WALK_W); player.AddAnimation(AnimationState::IDLE_N); player.AddAnimation(AnimationState::IDLE_E); player.AddAnimation(AnimationState::IDLE_S); player.AddAnimation(AnimationState::IDLE_W); view=TileTransformedView{GetScreenSize(),{1,1}}; player.SetPos({4*24,4*24}); player.UpdateAnimation(AnimationState::IDLE_S); SPAWNER_LIST.push_back(MonsterSpawner({336,96},4*24,{{{MonsterName::SLIME_BLUE,{-32,-40}},{MonsterName::SLIME_GREEN,{64,20}}}})); return true; } bool Crawler::OnUserUpdate(float fElapsedTime){ HandleUserInput(fElapsedTime); player.Update(fElapsedTime); for(Monster&m:MONSTER_LIST){ m.Update(fElapsedTime); } UpdateCamera(fElapsedTime); RenderWorld(fElapsedTime); return true; } void Crawler::InitializeAnimations(){ 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{1,0}*24,{24,24}}}); pl_walk_s.AddFrame({&GFX_Pl_Sheet,{vi2d{0,0}*24,{24,24}}}); pl_walk_s.AddFrame({&GFX_Pl_Sheet,{vi2d{2,0}*24,{24,24}}}); ANIMATION_DATA[AnimationState::WALK_S]=pl_walk_s; Animate2D::FrameSequence pl_walk_e{0.2}; pl_walk_e.AddFrame({&GFX_Pl_Sheet,{vi2d{0,3}*24,{24,24}}}); pl_walk_e.AddFrame({&GFX_Pl_Sheet,{vi2d{1,3}*24,{24,24}}}); pl_walk_e.AddFrame({&GFX_Pl_Sheet,{vi2d{0,3}*24,{24,24}}}); pl_walk_e.AddFrame({&GFX_Pl_Sheet,{vi2d{2,3}*24,{24,24}}}); ANIMATION_DATA[AnimationState::WALK_E]=pl_walk_e; Animate2D::FrameSequence pl_walk_w{0.2}; pl_walk_w.AddFrame({&GFX_Pl_Sheet,{vi2d{0,2}*24,{24,24}}}); pl_walk_w.AddFrame({&GFX_Pl_Sheet,{vi2d{1,2}*24,{24,24}}}); pl_walk_w.AddFrame({&GFX_Pl_Sheet,{vi2d{0,2}*24,{24,24}}}); pl_walk_w.AddFrame({&GFX_Pl_Sheet,{vi2d{2,2}*24,{24,24}}}); ANIMATION_DATA[AnimationState::WALK_W]=pl_walk_w; Animate2D::FrameSequence pl_walk_n{0.2}; pl_walk_n.AddFrame({&GFX_Pl_Sheet,{vi2d{0,1}*24,{24,24}}}); pl_walk_n.AddFrame({&GFX_Pl_Sheet,{vi2d{1,1}*24,{24,24}}}); pl_walk_n.AddFrame({&GFX_Pl_Sheet,{vi2d{0,1}*24,{24,24}}}); pl_walk_n.AddFrame({&GFX_Pl_Sheet,{vi2d{2,1}*24,{24,24}}}); ANIMATION_DATA[AnimationState::WALK_N]=pl_walk_n; Animate2D::FrameSequence pl_idle_s; pl_idle_s.AddFrame({&GFX_Pl_Sheet,{vi2d{0,0}*24,{24,24}}}); ANIMATION_DATA[AnimationState::IDLE_S]=pl_idle_s; Animate2D::FrameSequence pl_idle_e; pl_idle_e.AddFrame({&GFX_Pl_Sheet,{vi2d{0,3}*24,{24,24}}}); ANIMATION_DATA[AnimationState::IDLE_E]=pl_idle_e; Animate2D::FrameSequence pl_idle_w; pl_idle_w.AddFrame({&GFX_Pl_Sheet,{vi2d{0,2}*24,{24,24}}}); ANIMATION_DATA[AnimationState::IDLE_W]=pl_idle_w; Animate2D::FrameSequence pl_idle_n; pl_idle_n.AddFrame({&GFX_Pl_Sheet,{vi2d{0,1}*24,{24,24}}}); ANIMATION_DATA[AnimationState::IDLE_N]=pl_idle_n; //Load slime animations. for(int slime=0;slime<4;slime++){ for(int state=0;state<5;state++){ Animate2D::FrameSequence anim; if(state==4){//These are death animations. anim=Animate2D::FrameSequence(0.1f,Animate2D::Style::OneShot); } for (int frame=0;frame<10;frame++){ anim.AddFrame({&GFX_Slime_Sheet,{vi2d{frame,state+5*slime}*24,{24,24}}}); } ANIMATION_DATA[AnimationState(AnimationState::GREEN_SLIME_IDLE+state+slime*5)]=anim; } } } void Crawler::HandleUserInput(float fElapsedTime){ bool setIdleAnimation=true; if(GetKey(RIGHT).bHeld){ if(player.GetPos().x+12+fElapsedTime*100*player.GetMoveSpdMult()0){ player.SetX(player.GetX()-fElapsedTime*100*player.GetMoveSpdMult()); } else { player.SetX(12); } if(setIdleAnimation){ player.UpdateAnimation(AnimationState::WALK_W); } setIdleAnimation=false; } if(GetKey(UP).bHeld){ if(player.GetPos().y-12+fElapsedTime*100*player.GetMoveSpdMult()>0){ player.SetY(player.GetY()-fElapsedTime*100*player.GetMoveSpdMult()); } else { player.SetY(12); } if(setIdleAnimation){ player.UpdateAnimation(AnimationState::WALK_N); } setIdleAnimation=false; } if(GetKey(DOWN).bHeld){ if(player.GetPos().y+12+fElapsedTime*100*player.GetMoveSpdMult()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::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()-vf2d{12,12}*player.GetSizeMult(),player.GetFrame().GetSourceImage()->Decal(),player.GetFrame().GetSourceRect().pos,player.GetFrame().GetSourceRect().size,vf2d(player.GetSizeMult(),player.GetSizeMult())); for(Monster&m:monstersAfter){ view.DrawPartialDecal(m.GetPos()-vf2d{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() { Crawler demo; if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4)) demo.Start(); return 0; }