diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp new file mode 100644 index 00000000..6b762d6e --- /dev/null +++ b/Crawler/Crawler.cpp @@ -0,0 +1,259 @@ +#define OLC_PGE_APPLICATION +#include "olcPixelGameEngine.h" +#define OLC_PGEX_TRANSFORMEDVIEW +#include "olcPGEX_TransformedView.h" +#include "Crawler.h" +#include "olcUTIL_Camera2D.h" + +//192x192 +const vi2d WINDOW_SIZE={24*8,24*8}; +std::mapANIMATION_DATA; +std::vectorMONSTER_LIST; +std::vectorSPAWNER_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"); + + //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(); + 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()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())); + } +} + +int main() +{ + Crawler demo; + if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4)) + demo.Start(); + + return 0; +} \ No newline at end of file diff --git a/Crawler/Crawler.h b/Crawler/Crawler.h new file mode 100644 index 00000000..4e18839b --- /dev/null +++ b/Crawler/Crawler.h @@ -0,0 +1,29 @@ +#pragma once +#include "olcPixelGameEngine.h" +#include "Animation.h" +#include "olcUTIL_Animate2D.h" +#include "Monster.h" +#include "olcPGEX_TransformedView.h" +#include "Player.h" +#include "olcUTIL_Camera2D.h" + + +class Crawler : public olc::PixelGameEngine +{ + const vi2d WORLD_SIZE={64,8}; + Camera2D camera; + TileTransformedView view; + Player player; + Renderable GFX_Pl_Sheet,GFX_Slime_Sheet; + +public: + Crawler(); + +public: + bool OnUserCreate() override; + bool OnUserUpdate(float fElapsedTime) override; + void InitializeAnimations(); + void HandleUserInput(float fElapsedTime); + void UpdateCamera(float fElapsedTime); + void RenderWorld(); +}; \ No newline at end of file diff --git a/Crawler/Crawler.vcxproj b/Crawler/Crawler.vcxproj index aa3c38f3..8bd8d54f 100644 --- a/Crawler/Crawler.vcxproj +++ b/Crawler/Crawler.vcxproj @@ -130,15 +130,18 @@ + + - + + diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index ea456f93..0f1b29de 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -36,9 +36,15 @@ Header Files + + Header Files + + + Header Files + - + Source Files @@ -47,5 +53,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/Crawler/Monster.cpp b/Crawler/Monster.cpp index 7deaa5a2..10d439c1 100644 --- a/Crawler/Monster.cpp +++ b/Crawler/Monster.cpp @@ -5,8 +5,8 @@ extern std::mapMONSTER_DATA; extern std::vectorMONSTER_LIST; MonsterData::MonsterData(){} -MonsterData::MonsterData(int hp,int atk,std::vectoranimations,float moveSpd,float size,MonsterStrategy strategy): - hp(hp),atk(atk),moveSpd(moveSpd),size(size),strategy(strategy),animations(animations){ +MonsterData::MonsterData(MonsterName type,int hp,int atk,std::vectoranimations,float moveSpd,float size,MonsterStrategy strategy): + type(type),hp(hp),atk(atk),moveSpd(moveSpd),size(size),strategy(strategy),animations(animations){ } int MonsterData::GetHealth(){ return hp; @@ -20,13 +20,16 @@ float MonsterData::GetMoveSpdMult(){ float MonsterData::GetSizeMult(){ return size; } +MonsterName MonsterData::GetType(){ + return type; +} MonsterStrategy MonsterData::GetAIStrategy(){ return strategy; } Monster::Monster(){} Monster::Monster(vf2d pos,MonsterData data): - pos(pos),hp(data.GetHealth()),maxhp(data.GetHealth()),atk(data.GetAttack()),moveSpd(data.GetMoveSpdMult()),size(data.GetSizeMult()),strategy(data.GetAIStrategy()){ + pos(pos),hp(data.GetHealth()),maxhp(data.GetHealth()),atk(data.GetAttack()),moveSpd(data.GetMoveSpdMult()),size(data.GetSizeMult()),strategy(data.GetAIStrategy()),type(data.GetType()){ bool firstAnimation=true; for(AnimationState&anim:data.GetAnimations()){ animation.AddState(anim,ANIMATION_DATA[anim]); @@ -58,7 +61,7 @@ Animate2D::Frame Monster::GetFrame(){ void Monster::UpdateAnimation(AnimationState state){ animation.ChangeState(internal_animState,state); } -void Monster::Update(float fElapsedTime){ +bool Monster::Update(float fElapsedTime){ switch(strategy){ RUN_TOWARDS:{ @@ -67,8 +70,37 @@ void Monster::Update(float fElapsedTime){ }break; } + if(hp<=0){ + deathTimer+=fElapsedTime; + if(deathTimer>3){ + return false; + } + } animation.UpdateState(internal_animState,randomFrameOffset+fElapsedTime); randomFrameOffset=0; + return true; +} +AnimationState Monster::GetDeathAnimationName(){ + switch(type){ + case SLIME_GREEN:{ + return AnimationState::GREEN_SLIME_DIE; + } + case SLIME_BLUE:{ + return AnimationState::BLUE_SLIME_DIE; + } + case SLIME_RED:{ + return AnimationState::RED_SLIME_DIE; + } + default:{ + return AnimationState::IDLE_S; + } + } +} +void Monster::Hurt(int damage){ + hp=std::max(0,hp-damage); + if(hp<=0){ + animation.ChangeState(internal_animState,GetDeathAnimationName()); + } } MonsterSpawner::MonsterSpawner(){} diff --git a/Crawler/Monster.h b/Crawler/Monster.h index 3b6dbd85..f2a8bcac 100644 --- a/Crawler/Monster.h +++ b/Crawler/Monster.h @@ -1,3 +1,4 @@ +#pragma once #include "olcPixelGameEngine.h" #include "Animation.h" #include "olcUTIL_Animate2D.h" @@ -7,6 +8,13 @@ enum MonsterStrategy{ SHOOT_AFAR }; +enum MonsterName{ + SLIME_GREEN, + SLIME_BLUE, + SLIME_RED, + SLIME_YELLOW, +}; + struct MonsterData{ private: int hp; @@ -15,27 +23,22 @@ struct MonsterData{ float size; std::vector animations; MonsterStrategy strategy; + MonsterName type; public: MonsterData(); //When specifying animations, the first one will become the default animation. - MonsterData(int hp,int atk,std::vectoranimations,float moveSpd=1.0f,float size=1.0f,MonsterStrategy strategy=RUN_TOWARDS); + MonsterData(MonsterName type,int hp,int atk,std::vectoranimations,float moveSpd=1.0f,float size=1.0f,MonsterStrategy strategy=RUN_TOWARDS); int GetHealth(); int GetAttack(); float GetMoveSpdMult(); float GetSizeMult(); + MonsterName GetType(); MonsterStrategy GetAIStrategy(); std::vectorGetAnimations(){ return animations; } }; -enum MonsterName{ - SLIME_GREEN, - SLIME_BLUE, - SLIME_RED, - SLIME_YELLOW, -}; - struct Monster{ private: vf2d pos; @@ -47,6 +50,9 @@ struct Monster{ Animate2D::Animationanimation; Animate2D::AnimationState internal_animState; float randomFrameOffset=0.f; + float deathTimer=0.f; + MonsterName type; + AnimationState GetDeathAnimationName(); public: Monster(); Monster(vf2d pos,MonsterData data); @@ -57,7 +63,8 @@ struct Monster{ float GetSizeMult(); Animate2D::Frame GetFrame(); void UpdateAnimation(AnimationState state); - void Update(float fElapsedTime); + bool Update(float fElapsedTime); + void Hurt(int damage); }; struct MonsterSpawner{ diff --git a/Crawler/MonsterData.cpp b/Crawler/MonsterData.cpp index eb17c77b..e1d88305 100644 --- a/Crawler/MonsterData.cpp +++ b/Crawler/MonsterData.cpp @@ -3,8 +3,8 @@ #include "Animation.h" std::mapMONSTER_DATA={ - {SLIME_GREEN,MonsterData(10,5,{{AnimationState::GREEN_SLIME_IDLE,AnimationState::GREEN_SLIME_JUMP,AnimationState::GREEN_SLIME_ROLL,AnimationState::GREEN_SLIME_DIE,AnimationState::GREEN_SLIME_SPIT}},1.1f,0.8f,MonsterStrategy::RUN_TOWARDS)}, - {SLIME_BLUE,MonsterData(30,10,{{AnimationState::BLUE_SLIME_IDLE,AnimationState::BLUE_SLIME_JUMP,AnimationState::BLUE_SLIME_ROLL,AnimationState::BLUE_SLIME_DIE,AnimationState::BLUE_SLIME_SPIT}},0.8f,1.0f,MonsterStrategy::SHOOT_AFAR)}, - {SLIME_RED,MonsterData(25,10,{{AnimationState::RED_SLIME_IDLE,AnimationState::RED_SLIME_JUMP,AnimationState::RED_SLIME_ROLL,AnimationState::RED_SLIME_DIE,AnimationState::RED_SLIME_SPIT}},0.95f,1.2f,MonsterStrategy::RUN_TOWARDS)}, + {SLIME_GREEN,MonsterData(MonsterName::SLIME_GREEN,10,5,{{AnimationState::GREEN_SLIME_IDLE,AnimationState::GREEN_SLIME_JUMP,AnimationState::GREEN_SLIME_ROLL,AnimationState::GREEN_SLIME_DIE,AnimationState::GREEN_SLIME_SPIT}},1.1f,0.8f,MonsterStrategy::RUN_TOWARDS)}, + {SLIME_BLUE,MonsterData(MonsterName::SLIME_BLUE,30,10,{{AnimationState::BLUE_SLIME_IDLE,AnimationState::BLUE_SLIME_JUMP,AnimationState::BLUE_SLIME_ROLL,AnimationState::BLUE_SLIME_DIE,AnimationState::BLUE_SLIME_SPIT}},0.8f,1.0f,MonsterStrategy::SHOOT_AFAR)}, + {SLIME_RED,MonsterData(MonsterName::SLIME_RED,25,10,{{AnimationState::RED_SLIME_IDLE,AnimationState::RED_SLIME_JUMP,AnimationState::RED_SLIME_ROLL,AnimationState::RED_SLIME_DIE,AnimationState::RED_SLIME_SPIT}},0.95f,1.2f,MonsterStrategy::RUN_TOWARDS)}, //{SLIME_YELLOW,{}}, }; \ No newline at end of file diff --git a/Crawler/Player.cpp b/Crawler/Player.cpp new file mode 100644 index 00000000..96408dcb --- /dev/null +++ b/Crawler/Player.cpp @@ -0,0 +1,111 @@ +#include "Monster.h" +#include "Player.h" +#include "Crawler.h" + + +extern std::mapMONSTER_DATA; +extern std::vectorMONSTER_LIST; +extern std::mapANIMATION_DATA; +extern std::vectorSPAWNER_LIST; +extern Crawler*game; + +Player::Player(){ +} + +Player::Player(vf2d pos): + pos(pos){ +} + +void Player::SetX(float x){ + pos.x=x; +}; + +void Player::SetY(float y){ + pos.y=y; +} + +void Player::SetPos(vf2d pos){ + this->pos=pos; +} + +vf2d&Player::GetPos(){ + return pos; +} + +float Player::GetX(){ + return pos.x; +} + +float Player::GetY(){ + return pos.y; +} + +int Player::GetHealth(){ + return hp; +} + +int Player::GetMaxHealth(){ + return maxhp; +} + +int Player::GetAttack(){ + return atk; +} + +float Player::GetMoveSpdMult(){ + return moveSpd; +} + +float Player::GetSizeMult(){ + return size; +} + +float Player::GetAttackRangeMult(){ + return attack_range; +} + +void Player::Update(float fElapsedTime){ + attack_cooldown_timer=std::max(0.f,attack_cooldown_timer-fElapsedTime); + animation.UpdateState(internal_animState,fElapsedTime); + if(attack_cooldown_timer==0&&game->GetMouse(0).bHeld){ + bool attack=false; + for(Monster&m:MONSTER_LIST){ + if(geom2d::overlaps(geom2d::circle(pos-vf2d{size*12,size*12},attack_range*size*12),geom2d::circle(m.GetPos()-vf2d{m.GetSizeMult()*12,m.GetSizeMult()*12},m.GetSizeMult()*12))){ + m.Hurt(atk); + attack=true; + break; + } + } + if(attack){ + attack_cooldown_timer=ATTACK_COOLDOWN; + } + } +} + +void Player::AddAnimation(AnimationState state){ + animation.AddState(state,ANIMATION_DATA[state]); +} + +void Player::UpdateAnimation(AnimationState animState){ + animation.ChangeState(internal_animState,animState); +} + +Animate2D::Frame Player::GetFrame(){ + return animation.GetFrame(internal_animState); +} + +void Player::SetLastReleasedMovementKey(Key k){ + lastReleasedMovementKey=k; +} + +Key Player::GetLastReleasedMovementKey(){ + return lastReleasedMovementKey; +} + +void Player::Moved(){ + for(MonsterSpawner&spawner:SPAWNER_LIST){ + if(!spawner.SpawnTriggered()&&geom2d::overlaps(geom2d::circle(pos-vf2d{size*12,size*12},size*12),geom2d::circle(spawner.GetPos(),spawner.GetRange()))){ + spawner.SetTriggered(true); + } + } +} \ No newline at end of file diff --git a/Crawler/Player.h b/Crawler/Player.h new file mode 100644 index 00000000..fed5de18 --- /dev/null +++ b/Crawler/Player.h @@ -0,0 +1,44 @@ +#pragma once +#include "olcUTIL_Animate2D.h" +#include "Animation.h" +#include "Monster.h" + +struct Player{ +private: + int hp=100,maxhp=hp; + int atk=10; + vf2d pos; + float moveSpd=1.0f; + float size=1.0f; + float attack_range=1.5f; + const float ATTACK_COOLDOWN=0.75f; + float attack_cooldown_timer=0; + Animate2D::Animationanimation; + Animate2D::AnimationState internal_animState; + Key lastReleasedMovementKey; +public: + Player(); + Player(vf2d pos); + void SetX(float x); + void SetY(float y); + void SetPos(vf2d pos); + vf2d&GetPos(); + float GetX(); + float GetY(); + int GetHealth(); + int GetMaxHealth(); + int GetAttack(); + float GetMoveSpdMult(); + float GetSizeMult(); + float GetAttackRangeMult(); + + void Update(float fElapsedTime); + void AddAnimation(AnimationState state); + void UpdateAnimation(AnimationState animState); + Animate2D::Frame GetFrame(); + void SetLastReleasedMovementKey(Key k); + Key GetLastReleasedMovementKey(); + + //Triggers when the player has moved. + void Moved(); +}; \ No newline at end of file diff --git a/Crawler/main.cpp b/Crawler/main.cpp deleted file mode 100644 index 3296f6e4..00000000 --- a/Crawler/main.cpp +++ /dev/null @@ -1,356 +0,0 @@ -#define OLC_PGE_APPLICATION -#include "olcPixelGameEngine.h" -#include "olcUTIL_Camera2D.h" -#define OLC_PGEX_TRANSFORMEDVIEW -#include "olcPGEX_TransformedView.h" -#include "olcUTIL_Animate2D.h" -#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: - int hp=100,maxhp=hp; - int atk=10; - vf2d pos; - float moveSpd=1.0f; - float size=1.0f; - float attack_range=1.5f; - Animate2D::Animationanimation; - Animate2D::AnimationState internal_animState; - Key lastReleasedMovementKey; - public: - Player(){}; - Player(vf2d pos): - pos(pos){ - }; - void SetX(float x){ - pos.x=x; - }; - void SetY(float y){ - pos.y=y; - } - void SetPos(vf2d pos){ - this->pos=pos; - } - vf2d&GetPos(){ - return pos; - } - float GetX(){ - return pos.x; - } - float GetY(){ - return pos.y; - } - int GetHealth(){ - return hp; - } - int GetMaxHealth(){ - return maxhp; - } - int GetAttack(){ - return atk; - } - float GetMoveSpdMult(){ - return moveSpd; - } - float GetSizeMult(){ - return size; - } - float GetAttackRangeMult(){ - return attack_range; - } - - void Update(float fElapsedTime){ - animation.UpdateState(internal_animState,fElapsedTime); - } - void AddAnimation(AnimationState state){ - animation.AddState(state,ANIMATION_DATA[state]); - }; - void UpdateAnimation(AnimationState animState){ - animation.ChangeState(internal_animState,animState); - } - - Animate2D::Frame GetFrame(){ - return animation.GetFrame(internal_animState); - } - - void SetLastReleasedMovementKey(Key k){ - lastReleasedMovementKey=k; - } - 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 -{ - const vi2d WORLD_SIZE={64,8}; - Camera2D camera; - TileTransformedView view; - Player player=Player{{}}; - Renderable GFX_Pl_Sheet,GFX_Slime_Sheet; - -public: - Crawler() - { - sAppName = "Crawler Concept"; - } - -public: - bool OnUserCreate() override - { - //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"); - - //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_GREEN,{-32,-40}},{MonsterName::SLIME_GREEN,{64,20}}}})); - - return true; - } - - bool OnUserUpdate(float fElapsedTime) override - { - 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}}}); - 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; - for(int slime=0;slime<4;slime++){ - for(int state=0;state<5;state++){ - Animate2D::FrameSequence anim; - 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 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()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())); - } - } -}; - - -int main() -{ - Crawler demo; - if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4)) - demo.Start(); - - return 0; -} \ No newline at end of file