#define OLC_PGE_APPLICATION #include "olcPixelGameEngine.h" using namespace olc; // Override base class with your custom functionality class Example : public olc::PixelGameEngine { public: class Entity{ protected: Entity(vf2d pos) :pos(pos),col(WHITE){} vf2d pos; Entity*copy=nullptr; Pixel col; public: #define className Entity static className*Create(vf2d pos){ className*newEnt=new className(pos); newEnt->copy=new className(pos); return newEnt; }; virtual void Reset(){ pos=copy->pos; } virtual void Update(PixelGameEngine*pge){ pge->DrawCircle(pos,5,col); }; }; class MovingEntity:public Entity{ public: vf2d vel; #define className MovingEntity static className*Create(vf2d pos,vf2d vel){ className*newEnt=new className(pos,vel); newEnt->copy=new className(pos,vel); return newEnt; }; virtual void Update(PixelGameEngine*pge){ pos+=vel*pge->GetElapsedTime(); Entity::Update(pge); }; protected: MovingEntity(vf2d pos,vf2d vel) :Entity(pos),vel(vel){} virtual void Reset()override{ Entity::Reset(); MovingEntity*defaultCopy=(MovingEntity*)copy; vel=defaultCopy->vel; } }; class PlayerEntity:public MovingEntity{ public: #define className PlayerEntity static className*Create(vf2d startingPos){ className*newEnt=new className(startingPos); newEnt->copy=new className(startingPos); return newEnt; }; virtual void Update(PixelGameEngine*pge){ vel={0,0}; if(pge->GetKey(A).bHeld){ vel.x-=12; } if(pge->GetKey(D).bHeld){ vel.x+=12; } if(pge->GetKey(W).bHeld){ vel.y-=12; } if(pge->GetKey(S).bHeld){ vel.y+=12; } MovingEntity::Update(pge); }; protected: PlayerEntity(vf2d startingPos) :MovingEntity(startingPos,{0,0}){ col=BLUE; } virtual void Reset()override{ MovingEntity::Reset(); } }; Example() { // Name your application sAppName = "Example"; } std::vectorlevel1Entities; std::vectorlevel2Entities; std::vectorgameEntities; void LoadLevel(std::vector&entityList){ //Clear out the old entities, we don't care about them anymore. gameEntities.clear(); for(Entity*ent:entityList){ //Reset the entity to default, so we have a fresh level to work with. ent->Reset(); //Load in new entities to our game play area. gameEntities.push_back(ent); } } public: bool OnUserCreate() override { // Called once at the start, so create things here level1Entities.push_back(Entity::Create({20,80})); level1Entities.push_back(Entity::Create({40,100})); level1Entities.push_back(PlayerEntity::Create({20,120})); level1Entities.push_back(MovingEntity::Create({10,70},{1,1})); level2Entities.push_back(Entity::Create({80,70})); level2Entities.push_back(Entity::Create({60,90})); level2Entities.push_back(PlayerEntity::Create({100,120})); level2Entities.push_back(MovingEntity::Create({240,240},{-1,-1})); return true; } bool OnUserUpdate(float fElapsedTime) override { Clear(BLACK); if(GetKey(K1).bPressed){ LoadLevel(level1Entities); } if(GetKey(K2).bPressed){ LoadLevel(level2Entities); } if(GetKey(SPACE).bPressed){ ((MovingEntity*)level2Entities[3])->vel={0,0}; } // Called once per frame, draws random coloured pixels for(Entity*ent:gameEntities){ ent->Update(this); } DrawString({1,1},"Press <1> to load Level 1,\nor <2> to load Level 2"); DrawString({1,16},"WASD to move"); return true; } }; int main() { Example demo; if (demo.Construct(256, 240, 4, 4)) demo.Start(); return 0; }