You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ObjectStorage/main.cpp

102 lines
1.9 KiB

#define OLC_PGE_APPLICATION
2 years ago
#include "pixelGameEngine.h"
using namespace olc;
struct Object{
Decal*spr;
vf2d pos;
Object(){}
Object(Decal*spr,vf2d pos)
:spr(spr),pos(pos){}
};
2 years ago
class Example : public PixelGameEngine
2 years ago
{
public:
Example()
{
sAppName = "Example";
}
public:
Object player;
std::array<Object*,8> enemyList;
std::array<Object*,3> bulletList;
Decal*spaceship_spr;
Decal*enemy_spr;
Decal*bullet_spr;
float shotCooldown=0;
2 years ago
bool OnUserCreate() override
{
// Called once at the start, so create things here
spaceship_spr = new Decal(new Sprite("space_ship.png"));
enemy_spr = new Decal(new Sprite("space_alien.png"));
bullet_spr = new Decal(new Sprite("bullet.png"));
for (int i=0;i<8;i++){
enemyList[i]=new Object(enemy_spr,{(float)(rand()%224),16});
}
for (int i=0;i<bulletList.size();i++){
bulletList[i]=new Object(bullet_spr,{0,-999});
}
player = Object(spaceship_spr,{32,180});
2 years ago
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
bool addBullet=false;
2 years ago
shotCooldown-=fElapsedTime;
2 years ago
if (GetKey(SPACE).bHeld&&shotCooldown<=0){
shotCooldown=0.25;
addBullet=true;
}
2 years ago
player.pos.x+=(GetKey(RIGHT).bHeld-GetKey(LEFT).bHeld)*50*fElapsedTime;
DrawDecal(player.pos,player.spr);
for (Object*obj : enemyList){
for (int i=0;i<bulletList.size();i++){
Object*bullet=bulletList[i];
if (bullet->pos.x+8>=obj->pos.x&&bullet->pos.x+8<=obj->pos.x+16&&
bullet->pos.y>=obj->pos.y&&bullet->pos.y<=obj->pos.y+16){
obj->pos={-999,-999};
}
}
DrawDecal(obj->pos,obj->spr);
}
for (int i=0;i<bulletList.size();i++){
Object*obj=bulletList[i];
if (obj->pos.y<-32&&addBullet){
addBullet=false;
obj->pos=player.pos;
}
obj->pos.y-=160*fElapsedTime;
DrawDecal(obj->pos,obj->spr);
}
DrawStringDecal({0,0},std::to_string(bulletList.size()));
return true;
}
2 years ago
};
int main()
{
2 years ago
Example demo;
if (demo.Construct(256, 240, 4, 4))
2 years ago
demo.Start();
return 0;
}