Shooter example split into multiple files
This commit is contained in:
parent
a502db0af9
commit
9d5830c690
20
ShooterExample/Bullet.cpp
Normal file
20
ShooterExample/Bullet.cpp
Normal file
@ -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;
|
||||
}
|
15
ShooterExample/Bullet.h
Normal file
15
ShooterExample/Bullet.h
Normal file
@ -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();
|
||||
};
|
29
ShooterExample/Enemy.cpp
Normal file
29
ShooterExample/Enemy.cpp
Normal file
@ -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;
|
||||
}
|
||||
}
|
18
ShooterExample/Enemy.h
Normal file
18
ShooterExample/Enemy.h
Normal file
@ -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();
|
||||
};
|
25
ShooterExample/Player.cpp
Normal file
25
ShooterExample/Player.cpp
Normal file
@ -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;
|
||||
}
|
18
ShooterExample/Player.h
Normal file
18
ShooterExample/Player.h
Normal file
@ -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);
|
||||
};
|
13
ShooterExample/Rect.cpp
Normal file
13
ShooterExample/Rect.cpp
Normal file
@ -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};
|
||||
}
|
11
ShooterExample/Rect.h
Normal file
11
ShooterExample/Rect.h
Normal file
@ -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();
|
||||
};
|
@ -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();
|
||||
|
@ -127,11 +127,21 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Bullet.h" />
|
||||
<ClInclude Include="Enemy.h" />
|
||||
<ClInclude Include="olcPixelGameEngine.h" />
|
||||
<ClInclude Include="Rect.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Bullet.cpp" />
|
||||
<ClCompile Include="Enemy.cpp" />
|
||||
<ClCompile Include="Player.cpp" />
|
||||
<ClCompile Include="Rect.cpp" />
|
||||
<ClCompile Include="ShooterExample.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Player.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
@ -18,10 +18,36 @@
|
||||
<ClInclude Include="olcPixelGameEngine.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Rect.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Enemy.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Bullet.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="ShooterExample.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Rect.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Enemy.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Player.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Bullet.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Player.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
x
Reference in New Issue
Block a user