|
|
|
#define OLC_PGE_APPLICATION
|
|
|
|
#include "olcPixelGameEngine.h"
|
|
|
|
#include "olcUTIL_Camera2D.h"
|
|
|
|
#define OLC_PGEX_TRANSFORMEDVIEW
|
|
|
|
#include "olcPGEX_TransformedView.h"
|
|
|
|
|
|
|
|
using namespace olc;
|
|
|
|
using namespace olc::utils;
|
|
|
|
|
|
|
|
const vi2d WINDOW_SIZE={24*8,24*8};
|
|
|
|
|
|
|
|
struct Player{
|
|
|
|
vf2d pos;
|
|
|
|
float moveSpd;
|
|
|
|
Player(){};
|
|
|
|
Player(vf2d pos,float moveSpd):
|
|
|
|
pos(pos),moveSpd(moveSpd){};
|
|
|
|
};
|
|
|
|
|
|
|
|
class Crawler : public olc::PixelGameEngine
|
|
|
|
{
|
|
|
|
const vi2d WORLD_SIZE={64,64};
|
|
|
|
Camera2D camera;
|
|
|
|
TileTransformedView view;
|
|
|
|
Player player=Player{{},100};
|
|
|
|
|
|
|
|
public:
|
|
|
|
Crawler()
|
|
|
|
{
|
|
|
|
sAppName = "Crawler Concept";
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool OnUserCreate() override
|
|
|
|
{
|
|
|
|
camera=Camera2D{WINDOW_SIZE};
|
|
|
|
camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow);
|
|
|
|
camera.SetTarget(player.pos);
|
|
|
|
camera.SetWorldBoundary({0,0},WORLD_SIZE*24);
|
|
|
|
camera.EnableWorldBoundary(false);
|
|
|
|
view=TileTransformedView{GetScreenSize(),{1,1}};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override
|
|
|
|
{
|
|
|
|
HandleUserInput(fElapsedTime);
|
|
|
|
UpdateCamera(fElapsedTime);
|
|
|
|
RenderWorld();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleUserInput(float fElapsedTime){
|
|
|
|
if(GetKey(RIGHT).bHeld){
|
|
|
|
player.pos.x+=fElapsedTime*player.moveSpd;
|
|
|
|
}
|
|
|
|
if(GetKey(LEFT).bHeld){
|
|
|
|
player.pos.x-=fElapsedTime*player.moveSpd;
|
|
|
|
}
|
|
|
|
if(GetKey(UP).bHeld){
|
|
|
|
player.pos.y-=fElapsedTime*player.moveSpd;
|
|
|
|
}
|
|
|
|
if(GetKey(DOWN).bHeld){
|
|
|
|
player.pos.y+=fElapsedTime*player.moveSpd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateCamera(float fElapsedTime){
|
|
|
|
camera.Update(fElapsedTime);
|
|
|
|
view.SetWorldOffset(vi2d(camera.GetViewPosition()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWorld(){
|
|
|
|
Clear(BLACK);
|
|
|
|
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.DrawCircle(player.pos,8);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
Crawler demo;
|
|
|
|
if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4))
|
|
|
|
demo.Start();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|