|
|
|
@ -1,97 +1,10 @@ |
|
|
|
|
#define OLC_PGE_APPLICATION |
|
|
|
|
#include "olcPixelGameEngine.h" |
|
|
|
|
|
|
|
|
|
struct Rect{ |
|
|
|
|
int x,y,w,h; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class Enemy{ |
|
|
|
|
olc::vf2d pos; |
|
|
|
|
Rect*sprite; |
|
|
|
|
int health; |
|
|
|
|
public: |
|
|
|
|
Enemy(olc::vi2d pos,Rect*sprite,int health=1) |
|
|
|
|
:pos(pos),sprite(sprite),health(health){}; |
|
|
|
|
olc::vf2d GetPos(){ |
|
|
|
|
return pos;
|
|
|
|
|
} |
|
|
|
|
olc::vi2d GetSprPos(){ |
|
|
|
|
return {sprite->x,sprite->y};
|
|
|
|
|
} |
|
|
|
|
olc::vi2d GetSprSize(){ |
|
|
|
|
return {sprite->w,sprite->h};
|
|
|
|
|
} |
|
|
|
|
void Hurt(int damage=1){ |
|
|
|
|
health-=damage; |
|
|
|
|
} |
|
|
|
|
bool IsAlive(){ |
|
|
|
|
return health>0; |
|
|
|
|
} |
|
|
|
|
void Move(olc::vi2d moveAmt){ |
|
|
|
|
pos+=moveAmt; |
|
|
|
|
} |
|
|
|
|
olc::Pixel GetColor(){ |
|
|
|
|
if(health>1){ |
|
|
|
|
return {255,96,96}; |
|
|
|
|
} else { |
|
|
|
|
return olc::GREEN; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class Player{ |
|
|
|
|
olc::vf2d pos; |
|
|
|
|
Rect*sprite; |
|
|
|
|
float lastShootTime=0; |
|
|
|
|
public: |
|
|
|
|
Player(olc::vi2d pos,Rect*sprite) |
|
|
|
|
:pos(pos),sprite(sprite){} |
|
|
|
|
olc::vf2d GetPos(){ |
|
|
|
|
return pos; |
|
|
|
|
} |
|
|
|
|
olc::vi2d GetSprPos(){ |
|
|
|
|
return {sprite->x,sprite->y};
|
|
|
|
|
} |
|
|
|
|
olc::vi2d GetSprSize(){ |
|
|
|
|
return {sprite->w,sprite->h};
|
|
|
|
|
} |
|
|
|
|
void Update(float fElapsedTime){ |
|
|
|
|
lastShootTime=std::max(lastShootTime-fElapsedTime,0.f); |
|
|
|
|
} |
|
|
|
|
bool CanShoot(){ |
|
|
|
|
return lastShootTime==0; |
|
|
|
|
} |
|
|
|
|
void OnShoot(){ |
|
|
|
|
lastShootTime=0.5; |
|
|
|
|
} |
|
|
|
|
void Move(olc::vf2d moveAmt){ |
|
|
|
|
pos+=moveAmt; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class Bullet{ |
|
|
|
|
olc::vf2d pos; |
|
|
|
|
bool alive=true; |
|
|
|
|
public: |
|
|
|
|
Bullet(olc::vi2d pos) |
|
|
|
|
:pos(pos){} |
|
|
|
|
olc::vf2d GetPos(){ |
|
|
|
|
return pos; |
|
|
|
|
} |
|
|
|
|
void Update(float fElapsedTime){ |
|
|
|
|
pos+={0,-72*fElapsedTime}; |
|
|
|
|
} |
|
|
|
|
bool CollidesWith(Enemy&enemy){ |
|
|
|
|
float dist2=pow(enemy.GetPos().x+enemy.GetSprSize().x-pos.x,2)+pow(enemy.GetPos().y+enemy.GetSprSize().y-pos.y,2); |
|
|
|
|
return dist2<64; |
|
|
|
|
} |
|
|
|
|
void KillBullet(){ |
|
|
|
|
alive=false; |
|
|
|
|
} |
|
|
|
|
bool IsAlive(){ |
|
|
|
|
return alive||pos.y<0; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
#include "Rect.h" |
|
|
|
|
#include "Enemy.h" |
|
|
|
|
#include "Player.h" |
|
|
|
|
#include "Bullet.h" |
|
|
|
|
|
|
|
|
|
// Override base class with your custom functionality
|
|
|
|
|
class ShooterExample : public olc::PixelGameEngine |
|
|
|
@ -120,8 +33,10 @@ public: |
|
|
|
|
|
|
|
|
|
bool OnUserCreate() override |
|
|
|
|
{ |
|
|
|
|
//Load sprites
|
|
|
|
|
spriteSheet.Load("8-bit Aliens.png"); |
|
|
|
|
|
|
|
|
|
//Create all enemies in a 12x4 grid, selecting one of three alien types at random.
|
|
|
|
|
olc::vf2d alienOffset={24,38}; |
|
|
|
|
for (int y=0;y<4;y++){ |
|
|
|
|
for (int x=0;x<12;x++){ |
|
|
|
@ -146,12 +61,14 @@ public: |
|
|
|
|
|
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override |
|
|
|
|
{ |
|
|
|
|
//Perform player and bullet game updates.
|
|
|
|
|
player.Update(fElapsedTime); |
|
|
|
|
|
|
|
|
|
for(Bullet&b:bullets){ |
|
|
|
|
b.Update(fElapsedTime); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Update all enemies to move in a zig-zag pattern.
|
|
|
|
|
lastShiftTime+=fElapsedTime; |
|
|
|
|
if(lastShiftTime>0.8){ |
|
|
|
|
lastShiftTime-=0.8; |
|
|
|
@ -175,6 +92,7 @@ public: |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Player regions
|
|
|
|
|
if(GetKey(olc::SPACE).bHeld){ |
|
|
|
|
if(player.CanShoot()){ |
|
|
|
|
player.OnShoot(); |
|
|
|
@ -188,10 +106,12 @@ public: |
|
|
|
|
player.Move({48*fElapsedTime,0}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Draw and render all objects
|
|
|
|
|
Clear(olc::BLACK); |
|
|
|
|
for(Enemy&e:enemies){ |
|
|
|
|
for(Bullet&b:bullets){ |
|
|
|
|
if(!b.IsAlive())continue; |
|
|
|
|
//Handle Bullet collisions per Enemy
|
|
|
|
|
if(b.CollidesWith(e)){ |
|
|
|
|
e.Hurt(); |
|
|
|
|
b.KillBullet(); |
|
|
|
|