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.

138 lines
3.2 KiB

#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include "Rect.h"
#include "Enemy.h"
#include "Player.h"
#include "Bullet.h"
// Override base class with your custom functionality
class ShooterExample : public olc::PixelGameEngine
{
public:
ShooterExample()
{
// Name your application
sAppName = "Shooter Example";
}
public:
olc::Renderable spriteSheet;
Rect enemyA={5,21, 11,9};
Rect enemyB={19,22, 12,6};
Rect enemyC={34,20, 11,8};
Rect ship={35,6, 11,7};
std::vector<Enemy>enemies;
std::vector<Bullet>bullets;
Player player{{ScreenWidth()/2,ScreenHeight()-36},&ship};
int alienShift=0;
bool movingRight=true;
float lastShiftTime=0;
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++){
switch(rand()%3){
case 0:{
Enemy enemy{alienOffset+olc::vi2d{x*16,y*12},&enemyA};
enemies.push_back(enemy);
}break;
case 1:{
Enemy enemy{alienOffset+olc::vi2d{x*16,y*12},&enemyB};
enemies.push_back(enemy);
}break;
case 2:{
Enemy enemy{alienOffset+olc::vi2d{x*16,y*12},&enemyC,2};
enemies.push_back(enemy);
}break;
}
}
}
return true;
}
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;
if(abs(alienShift)>=3){
alienShift+=movingRight?-1:1;
movingRight=!movingRight;
for(Enemy&e:enemies){
e.Move({0,12});
}
} else
if(movingRight){
alienShift++;
for(Enemy&e:enemies){
e.Move({12,0});
}
} else {
alienShift--;
for(Enemy&e:enemies){
e.Move({-12,0});
}
}
}
//Player regions
if(GetKey(olc::SPACE).bHeld){
if(player.CanShoot()){
player.OnShoot();
bullets.push_back({player.GetPos()+player.GetSprSize()});
}
}
if(GetKey(olc::LEFT).bHeld){
player.Move({-48*fElapsedTime,0});
}
if(GetKey(olc::RIGHT).bHeld){
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();
}
DrawLineDecal(b.GetPos(),b.GetPos()+olc::vi2d{0,-4},olc::WHITE);
}
DrawPartialDecal(e.GetPos()+e.GetSprSize()/2,e.GetSprSize(),spriteSheet.Decal(),e.GetSprPos(),e.GetSprSize(),e.GetColor());
}
DrawPartialDecal(player.GetPos()+player.GetSprSize()/2,player.GetSprSize(),spriteSheet.Decal(),player.GetSprPos(),player.GetSprSize());
enemies.erase(std::remove_if(enemies.begin(),enemies.end(),[](Enemy&e){return !e.IsAlive();}),enemies.end());
bullets.erase(std::remove_if(bullets.begin(),bullets.end(),[](Bullet&b){return !b.IsAlive();}),bullets.end());
return true;
}
};
int main()
{
ShooterExample demo;
if (demo.Construct(256, 240, 4, 4))
demo.Start();
return 0;
}