parent
8bb42df861
commit
6584a240b1
@ -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::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA; |
||||||
|
std::vector<Monster>MONSTER_LIST; |
||||||
|
std::vector<MonsterSpawner>SPAWNER_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()<WORLD_SIZE.x*24){ |
||||||
|
player.SetX(player.GetX()+fElapsedTime*100*player.GetMoveSpdMult()); |
||||||
|
} else { |
||||||
|
player.SetX(WORLD_SIZE.x*24-12); |
||||||
|
} |
||||||
|
player.UpdateAnimation(AnimationState::WALK_E); |
||||||
|
setIdleAnimation=false; |
||||||
|
} |
||||||
|
if(GetKey(LEFT).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()<WORLD_SIZE.y*24){ |
||||||
|
player.SetY(player.GetY()+fElapsedTime*100*player.GetMoveSpdMult()); |
||||||
|
} else { |
||||||
|
player.SetY(WORLD_SIZE.y*24-12); |
||||||
|
} |
||||||
|
if(setIdleAnimation){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_S); |
||||||
|
} |
||||||
|
setIdleAnimation=false; |
||||||
|
} |
||||||
|
|
||||||
|
if(GetKey(UP).bReleased){ |
||||||
|
player.SetLastReleasedMovementKey(UP); |
||||||
|
if(GetKey(RIGHT).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_E); |
||||||
|
} else |
||||||
|
if(GetKey(DOWN).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_S); |
||||||
|
} else |
||||||
|
if(GetKey(LEFT).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_W); |
||||||
|
} |
||||||
|
} |
||||||
|
if(GetKey(RIGHT).bReleased){ |
||||||
|
player.SetLastReleasedMovementKey(RIGHT); |
||||||
|
if(GetKey(UP).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_N); |
||||||
|
} else |
||||||
|
if(GetKey(DOWN).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_S); |
||||||
|
} else |
||||||
|
if(GetKey(LEFT).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_W); |
||||||
|
} |
||||||
|
} |
||||||
|
if(GetKey(LEFT).bReleased){ |
||||||
|
player.SetLastReleasedMovementKey(LEFT); |
||||||
|
if(GetKey(RIGHT).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_E); |
||||||
|
} else |
||||||
|
if(GetKey(DOWN).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_S); |
||||||
|
} else |
||||||
|
if(GetKey(UP).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_N); |
||||||
|
} |
||||||
|
} |
||||||
|
if(GetKey(DOWN).bReleased){ |
||||||
|
player.SetLastReleasedMovementKey(DOWN); |
||||||
|
if(GetKey(RIGHT).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_E); |
||||||
|
} else |
||||||
|
if(GetKey(UP).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_N); |
||||||
|
} else |
||||||
|
if(GetKey(LEFT).bHeld){ |
||||||
|
player.UpdateAnimation(AnimationState::WALK_W); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(setIdleAnimation){ |
||||||
|
switch(player.GetLastReleasedMovementKey()){ |
||||||
|
case UP:{ |
||||||
|
player.UpdateAnimation(AnimationState::IDLE_N); |
||||||
|
}break; |
||||||
|
case DOWN:{ |
||||||
|
player.UpdateAnimation(AnimationState::IDLE_S); |
||||||
|
}break; |
||||||
|
case LEFT:{ |
||||||
|
player.UpdateAnimation(AnimationState::IDLE_W); |
||||||
|
}break; |
||||||
|
case RIGHT:{ |
||||||
|
player.UpdateAnimation(AnimationState::IDLE_E); |
||||||
|
}break; |
||||||
|
default:{ |
||||||
|
player.UpdateAnimation(AnimationState::IDLE_S); |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
//We have moved.
|
||||||
|
player.Moved(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Crawler::UpdateCamera(float fElapsedTime){ |
||||||
|
camera.Update(fElapsedTime); |
||||||
|
view.SetWorldOffset(vi2d(camera.GetViewPosition())); |
||||||
|
} |
||||||
|
|
||||||
|
void Crawler::RenderWorld(){ |
||||||
|
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); |
||||||
|
} |
||||||
|
} |
||||||
|
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())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
int main() |
||||||
|
{ |
||||||
|
Crawler demo; |
||||||
|
if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4)) |
||||||
|
demo.Start(); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -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(); |
||||||
|
}; |
@ -0,0 +1,111 @@ |
|||||||
|
#include "Monster.h" |
||||||
|
#include "Player.h" |
||||||
|
#include "Crawler.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 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<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; |
||||||
|
} |
||||||
|
} |
||||||
|
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<float>(pos-vf2d{size*12,size*12},size*12),geom2d::circle<float>(spawner.GetPos(),spawner.GetRange()))){ |
||||||
|
spawner.SetTriggered(true); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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::Animation<AnimationState>animation; |
||||||
|
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(); |
||||||
|
}; |
@ -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::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: |
|
||||||
int hp=100,maxhp=hp; |
|
||||||
int atk=10; |
|
||||||
vf2d pos; |
|
||||||
float moveSpd=1.0f; |
|
||||||
float size=1.0f; |
|
||||||
float attack_range=1.5f; |
|
||||||
Animate2D::Animation<AnimationState>animation; |
|
||||||
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<float>(pos,size*12),geom2d::circle<float>(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()<WORLD_SIZE.x*24){ |
|
||||||
player.SetX(player.GetX()+fElapsedTime*100*player.GetMoveSpdMult()); |
|
||||||
} else { |
|
||||||
player.SetX(WORLD_SIZE.x*24-12); |
|
||||||
} |
|
||||||
player.UpdateAnimation(AnimationState::WALK_E); |
|
||||||
setIdleAnimation=false; |
|
||||||
} |
|
||||||
if(GetKey(LEFT).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()<WORLD_SIZE.y*24){ |
|
||||||
player.SetY(player.GetY()+fElapsedTime*100*player.GetMoveSpdMult()); |
|
||||||
} else { |
|
||||||
player.SetY(WORLD_SIZE.y*24-12); |
|
||||||
} |
|
||||||
if(setIdleAnimation){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_S); |
|
||||||
} |
|
||||||
setIdleAnimation=false; |
|
||||||
} |
|
||||||
|
|
||||||
if(GetKey(UP).bReleased){ |
|
||||||
player.SetLastReleasedMovementKey(UP); |
|
||||||
if(GetKey(RIGHT).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_E); |
|
||||||
} else |
|
||||||
if(GetKey(DOWN).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_S); |
|
||||||
} else |
|
||||||
if(GetKey(LEFT).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_W); |
|
||||||
} |
|
||||||
} |
|
||||||
if(GetKey(RIGHT).bReleased){ |
|
||||||
player.SetLastReleasedMovementKey(RIGHT); |
|
||||||
if(GetKey(UP).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_N); |
|
||||||
} else |
|
||||||
if(GetKey(DOWN).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_S); |
|
||||||
} else |
|
||||||
if(GetKey(LEFT).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_W); |
|
||||||
} |
|
||||||
} |
|
||||||
if(GetKey(LEFT).bReleased){ |
|
||||||
player.SetLastReleasedMovementKey(LEFT); |
|
||||||
if(GetKey(RIGHT).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_E); |
|
||||||
} else |
|
||||||
if(GetKey(DOWN).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_S); |
|
||||||
} else |
|
||||||
if(GetKey(UP).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_N); |
|
||||||
} |
|
||||||
} |
|
||||||
if(GetKey(DOWN).bReleased){ |
|
||||||
player.SetLastReleasedMovementKey(DOWN); |
|
||||||
if(GetKey(RIGHT).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_E); |
|
||||||
} else |
|
||||||
if(GetKey(UP).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_N); |
|
||||||
} else |
|
||||||
if(GetKey(LEFT).bHeld){ |
|
||||||
player.UpdateAnimation(AnimationState::WALK_W); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if(setIdleAnimation){ |
|
||||||
switch(player.GetLastReleasedMovementKey()){ |
|
||||||
case UP:{ |
|
||||||
player.UpdateAnimation(AnimationState::IDLE_N); |
|
||||||
}break; |
|
||||||
case DOWN:{ |
|
||||||
player.UpdateAnimation(AnimationState::IDLE_S); |
|
||||||
}break; |
|
||||||
case LEFT:{ |
|
||||||
player.UpdateAnimation(AnimationState::IDLE_W); |
|
||||||
}break; |
|
||||||
case RIGHT:{ |
|
||||||
player.UpdateAnimation(AnimationState::IDLE_E); |
|
||||||
}break; |
|
||||||
default:{ |
|
||||||
player.UpdateAnimation(AnimationState::IDLE_S); |
|
||||||
} |
|
||||||
} |
|
||||||
} else { |
|
||||||
//We have moved.
|
|
||||||
player.Moved(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void UpdateCamera(float fElapsedTime){ |
|
||||||
camera.Update(fElapsedTime); |
|
||||||
view.SetWorldOffset(vi2d(camera.GetViewPosition())); |
|
||||||
} |
|
||||||
|
|
||||||
void RenderWorld(){ |
|
||||||
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); |
|
||||||
} |
|
||||||
} |
|
||||||
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())); |
|
||||||
} |
|
||||||
} |
|
||||||
}; |
|
||||||
|
|
||||||
|
|
||||||
int main() |
|
||||||
{ |
|
||||||
Crawler demo; |
|
||||||
if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4)) |
|
||||||
demo.Start(); |
|
||||||
|
|
||||||
return 0; |
|
||||||
} |
|
Loading…
Reference in new issue