Add position boundary checking

pull/28/head
sigonasr2 2 years ago
parent 2d1b8472e1
commit 8547054a60
  1. 29
      Crawler/main.cpp

@ -52,7 +52,7 @@ struct Player{
class Crawler : public olc::PixelGameEngine class Crawler : public olc::PixelGameEngine
{ {
const vi2d WORLD_SIZE={64,64}; const vi2d WORLD_SIZE={64,8};
Camera2D camera; Camera2D camera;
TileTransformedView view; TileTransformedView view;
Player player=Player{{},100}; Player player=Player{{},100};
@ -72,7 +72,7 @@ public:
camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow); camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow);
camera.SetTarget(player.pos); camera.SetTarget(player.pos);
camera.SetWorldBoundary({0,0},WORLD_SIZE*24); camera.SetWorldBoundary({0,0},WORLD_SIZE*24);
camera.EnableWorldBoundary(false); camera.EnableWorldBoundary(true);
//Graphics //Graphics
GFX_Pl_sheet.Load("assets/nico-warrior.png"); GFX_Pl_sheet.Load("assets/nico-warrior.png");
@ -118,6 +118,7 @@ public:
player.AddAnimation(IDLE_W,pl_idle_w); player.AddAnimation(IDLE_W,pl_idle_w);
view=TileTransformedView{GetScreenSize(),{1,1}}; view=TileTransformedView{GetScreenSize(),{1,1}};
player.pos={4*24,4*24};
player.UpdateAnimation(IDLE_S); player.UpdateAnimation(IDLE_S);
return true; return true;
@ -135,26 +136,42 @@ public:
void HandleUserInput(float fElapsedTime){ void HandleUserInput(float fElapsedTime){
bool setIdleAnimation=true; bool setIdleAnimation=true;
if(GetKey(RIGHT).bHeld){ if(GetKey(RIGHT).bHeld){
player.pos.x+=fElapsedTime*player.moveSpd; if(player.pos.x+12+fElapsedTime*player.moveSpd<WORLD_SIZE.x*24){
player.pos.x+=fElapsedTime*player.moveSpd;
} else {
player.pos.x=WORLD_SIZE.x*24-12;
}
player.UpdateAnimation(WALK_E); player.UpdateAnimation(WALK_E);
setIdleAnimation=false; setIdleAnimation=false;
} }
if(GetKey(LEFT).bHeld){ if(GetKey(LEFT).bHeld){
player.pos.x-=fElapsedTime*player.moveSpd; if(player.pos.x-12+fElapsedTime*player.moveSpd>0){
player.pos.x-=fElapsedTime*player.moveSpd;
} else {
player.pos.x=12;
}
if(setIdleAnimation){ if(setIdleAnimation){
player.UpdateAnimation(WALK_W); player.UpdateAnimation(WALK_W);
} }
setIdleAnimation=false; setIdleAnimation=false;
} }
if(GetKey(UP).bHeld){ if(GetKey(UP).bHeld){
player.pos.y-=fElapsedTime*player.moveSpd; if(player.pos.y-12+fElapsedTime*player.moveSpd>0){
player.pos.y-=fElapsedTime*player.moveSpd;
} else {
player.pos.y=12;
}
if(setIdleAnimation){ if(setIdleAnimation){
player.UpdateAnimation(WALK_N); player.UpdateAnimation(WALK_N);
} }
setIdleAnimation=false; setIdleAnimation=false;
} }
if(GetKey(DOWN).bHeld){ if(GetKey(DOWN).bHeld){
player.pos.y+=fElapsedTime*player.moveSpd; if(player.pos.y+12+fElapsedTime*player.moveSpd<WORLD_SIZE.y*24){
player.pos.y+=fElapsedTime*player.moveSpd;
} else {
player.pos.y=WORLD_SIZE.y*24-12;
}
if(setIdleAnimation){ if(setIdleAnimation){
player.UpdateAnimation(WALK_S); player.UpdateAnimation(WALK_S);
} }

Loading…
Cancel
Save