diff --git a/ShooterExample/Bullet.cpp b/ShooterExample/Bullet.cpp new file mode 100644 index 0000000..6f20b48 --- /dev/null +++ b/ShooterExample/Bullet.cpp @@ -0,0 +1,20 @@ +#include "Bullet.h" + +Bullet::Bullet(olc::vi2d pos) + :pos(pos){} +olc::vf2d Bullet::GetPos(){ + return pos; +} +void Bullet::Update(float fElapsedTime){ + pos+={0,-72*fElapsedTime}; +} +bool Bullet::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 Bullet::KillBullet(){ + alive=false; +} +bool Bullet::IsAlive(){ + return alive||pos.y<0; +} \ No newline at end of file diff --git a/ShooterExample/Bullet.h b/ShooterExample/Bullet.h new file mode 100644 index 0000000..f329367 --- /dev/null +++ b/ShooterExample/Bullet.h @@ -0,0 +1,15 @@ +#pragma once +#include "olcPixelGameEngine.h" +#include "Enemy.h" + +class Bullet{ + olc::vf2d pos; + bool alive=true; +public: + Bullet(olc::vi2d pos); + olc::vf2d GetPos(); + void Update(float fElapsedTime); + bool CollidesWith(Enemy&enemy); + void KillBullet(); + bool IsAlive(); +}; \ No newline at end of file diff --git a/ShooterExample/Enemy.cpp b/ShooterExample/Enemy.cpp new file mode 100644 index 0000000..aa1cd49 --- /dev/null +++ b/ShooterExample/Enemy.cpp @@ -0,0 +1,29 @@ +#include "Enemy.h" + +Enemy::Enemy(olc::vi2d pos,Rect*sprite,int health) + :pos(pos),sprite(sprite),health(health){}; +olc::vf2d Enemy::GetPos(){ + return pos; +} +olc::vi2d Enemy::GetSprPos(){ + return sprite->GetPos(); +} +olc::vi2d Enemy::GetSprSize(){ + return sprite->GetSize(); +} +void Enemy::Hurt(int damage){ + health-=damage; +} +bool Enemy::IsAlive(){ + return health>0; +} +void Enemy::Move(olc::vi2d moveAmt){ + pos+=moveAmt; +} +olc::Pixel Enemy::GetColor(){ + if(health>1){ + return {255,96,96}; + } else { + return olc::GREEN; + } +} \ No newline at end of file diff --git a/ShooterExample/Enemy.h b/ShooterExample/Enemy.h new file mode 100644 index 0000000..6c0ee4d --- /dev/null +++ b/ShooterExample/Enemy.h @@ -0,0 +1,18 @@ +#pragma once +#include "Rect.h" +#include "olcPixelGameEngine.h" + +class Enemy{ + olc::vf2d pos; + Rect*sprite; + int health; +public: + Enemy(olc::vi2d pos,Rect*sprite,int health=1); + olc::vf2d GetPos(); + olc::vi2d GetSprPos(); + olc::vi2d GetSprSize(); + void Hurt(int damage=1); + bool IsAlive(); + void Move(olc::vi2d moveAmt); + olc::Pixel GetColor(); +}; \ No newline at end of file diff --git a/ShooterExample/Player.cpp b/ShooterExample/Player.cpp new file mode 100644 index 0000000..8dbee39 --- /dev/null +++ b/ShooterExample/Player.cpp @@ -0,0 +1,25 @@ +#include "Player.h" + +Player::Player(olc::vi2d pos,Rect*sprite) + :pos(pos),sprite(sprite){} +olc::vf2d Player::GetPos(){ + return pos; +} +olc::vi2d Player::GetSprPos(){ + return sprite->GetPos(); +} +olc::vi2d Player::GetSprSize(){ + return sprite->GetSize(); +} +void Player::Update(float fElapsedTime){ + lastShootTime=std::max(lastShootTime-fElapsedTime,0.f); +} +bool Player::CanShoot(){ + return lastShootTime==0; +} +void Player::OnShoot(){ + lastShootTime=0.5; +} +void Player::Move(olc::vf2d moveAmt){ + pos+=moveAmt; +} \ No newline at end of file diff --git a/ShooterExample/Player.h b/ShooterExample/Player.h new file mode 100644 index 0000000..ee64eae --- /dev/null +++ b/ShooterExample/Player.h @@ -0,0 +1,18 @@ +#pragma once +#include "olcPixelGameEngine.h" +#include "Rect.h" + +class Player{ + olc::vf2d pos; + Rect*sprite; + float lastShootTime=0; +public: + Player(olc::vi2d pos,Rect*sprite); + olc::vf2d GetPos(); + olc::vi2d GetSprPos(); + olc::vi2d GetSprSize(); + void Update(float fElapsedTime); + bool CanShoot(); + void OnShoot(); + void Move(olc::vf2d moveAmt); +}; \ No newline at end of file diff --git a/ShooterExample/Rect.cpp b/ShooterExample/Rect.cpp new file mode 100644 index 0000000..6620878 --- /dev/null +++ b/ShooterExample/Rect.cpp @@ -0,0 +1,13 @@ +#include "olcPixelGameEngine.h" +#include "Rect.h" + +Rect::Rect(int x,int y,int w, int h) + :x(x),y(y),w(w),h(h){} + +olc::vi2d Rect::GetPos(){ + return {x,y}; +} + +olc::vi2d Rect::GetSize(){ + return {w,h}; +} \ No newline at end of file diff --git a/ShooterExample/Rect.h b/ShooterExample/Rect.h new file mode 100644 index 0000000..685bb6a --- /dev/null +++ b/ShooterExample/Rect.h @@ -0,0 +1,11 @@ +#pragma once +#include "olcPixelGameEngine.h" + +struct Rect{ +private: + int x,y,w,h; +public: + Rect(int x,int y,int w, int h); + olc::vi2d GetPos(); + olc::vi2d GetSize(); +}; \ No newline at end of file diff --git a/ShooterExample/ShooterExample.cpp b/ShooterExample/ShooterExample.cpp index 0a1f946..547cb8c 100644 --- a/ShooterExample/ShooterExample.cpp +++ b/ShooterExample/ShooterExample.cpp @@ -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(); diff --git a/ShooterExample/ShooterExample.vcxproj b/ShooterExample/ShooterExample.vcxproj index 89b97f1..5f561f1 100644 --- a/ShooterExample/ShooterExample.vcxproj +++ b/ShooterExample/ShooterExample.vcxproj @@ -127,11 +127,21 @@ + + + + + + + + + + diff --git a/ShooterExample/ShooterExample.vcxproj.filters b/ShooterExample/ShooterExample.vcxproj.filters index da416cd..db3cf70 100644 --- a/ShooterExample/ShooterExample.vcxproj.filters +++ b/ShooterExample/ShooterExample.vcxproj.filters @@ -18,10 +18,36 @@ Header Files + + Header Files + + + Header Files + + + Header Files + Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + \ No newline at end of file