Snap camera to integer values so there's no floating point errors.

pull/28/head
sigonasr2 2 years ago
parent 3dac286089
commit 3a8db50da8
  1. 25
      Crawler/main.cpp

@ -37,13 +37,20 @@ 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(true); camera.EnableWorldBoundary(false);
view=TileTransformedView{GetScreenSize(),{1,1}}; view=TileTransformedView{GetScreenSize(),{1,1}};
return true; return true;
} }
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
HandleUserInput(fElapsedTime);
UpdateCamera(fElapsedTime);
RenderWorld();
return true;
}
void HandleUserInput(float fElapsedTime){
if(GetKey(RIGHT).bHeld){ if(GetKey(RIGHT).bHeld){
player.pos.x+=fElapsedTime*player.moveSpd; player.pos.x+=fElapsedTime*player.moveSpd;
} }
@ -56,19 +63,21 @@ public:
if(GetKey(DOWN).bHeld){ if(GetKey(DOWN).bHeld){
player.pos.y+=fElapsedTime*player.moveSpd; player.pos.y+=fElapsedTime*player.moveSpd;
} }
}
void UpdateCamera(float fElapsedTime){
camera.Update(fElapsedTime); camera.Update(fElapsedTime);
view.SetWorldOffset(camera.GetViewPosition()); view.SetWorldOffset(vi2d(camera.GetViewPosition()));
//view.HandlePanAndZoom(); }
// called once per frame
void RenderWorld(){
Clear(BLACK); Clear(BLACK);
for (int x = view.GetTopLeftTile().x/24; x <= view.GetBottomRightTile().x/24; x++){ for (int x = view.GetTopLeftTile().x/24-1; x <= view.GetBottomRightTile().x/24; x++){
for (int y = view.GetTopLeftTile().y/24; y <= view.GetBottomRightTile().y/24; y++){ for (int y = view.GetTopLeftTile().y/24-1; y <= view.GetBottomRightTile().y/24; y++){
view.DrawRect(vi2d{x,y}*24,{24,24},DARK_GREY); view.DrawRect(vi2d{x,y}*24,{24,24},VERY_DARK_GREY);
} }
} }
view.DrawCircle(player.pos,8); view.DrawCircle(player.pos,8);
return true;
} }
}; };

Loading…
Cancel
Save