Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
| f43544d15d |
19 changed files with 2379 additions and 2063 deletions
8
.gitignore
vendored
8
.gitignore
vendored
|
|
@ -1,5 +1,5 @@
|
|||
.idea/
|
||||
bin/
|
||||
cmake-build-debug/
|
||||
include/
|
||||
.idea/
|
||||
bin/
|
||||
cmake-build-debug/
|
||||
include/
|
||||
lib/
|
||||
70
Asteroid.h
70
Asteroid.h
|
|
@ -1,35 +1,35 @@
|
|||
//
|
||||
// Created by benmo on 9/28/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_ASTEROID_H
|
||||
#define SFML_TEMPLATE_ASTEROID_H
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
class Asteroid : public Entity {
|
||||
public:
|
||||
Asteroid(int health, int points, const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : Entity(health, points, texture, scale, xPos, yPos, velocity, direction) {
|
||||
size = health;
|
||||
type = "asteroid";
|
||||
}
|
||||
|
||||
int getSize() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
void hit(vector<MySprite> &animations, Sound &sound, Texture &explosion, default_random_engine &gen) {
|
||||
uniform_int_distribution<int> angle(0, 359);
|
||||
animations.emplace_back(explosion, getSize()*65, getXPos(), getYPos(), 0, angle(gen));
|
||||
animations[animations.size() - 1].makeAnimated(5, 5, 0.01,23);
|
||||
|
||||
sound.setVolume(100.0/((4-size)/1.5));
|
||||
sound.play();
|
||||
}
|
||||
|
||||
private:
|
||||
int size;
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_ASTEROID_H
|
||||
//
|
||||
// Created by benmo on 9/28/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_ASTEROID_H
|
||||
#define SFML_TEMPLATE_ASTEROID_H
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
class Asteroid : public Entity {
|
||||
public:
|
||||
Asteroid(int health, int points, const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : Entity(health, points, texture, scale, xPos, yPos, velocity, direction) {
|
||||
size = health;
|
||||
type = "asteroid";
|
||||
}
|
||||
|
||||
int getSize() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
void hit(vector<MySprite> &animations, Sound &sound, Texture &explosion, default_random_engine &gen) {
|
||||
uniform_int_distribution<int> angle(0, 359);
|
||||
animations.emplace_back(explosion, getSize()*65, getXPos(), getYPos(), 0, angle(gen));
|
||||
animations[animations.size() - 1].makeAnimated(5, 5, 0.01,23);
|
||||
|
||||
sound.setVolume(100.0/(4-size));
|
||||
sound.play();
|
||||
}
|
||||
|
||||
private:
|
||||
int size;
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_ASTEROID_H
|
||||
|
|
|
|||
378
Collision.cpp
378
Collision.cpp
|
|
@ -1,190 +1,190 @@
|
|||
/*
|
||||
* File: collision.cpp
|
||||
* Author: Nick (original version), ahnonay (SFML2 compatibility)
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include "Collision.h"
|
||||
|
||||
namespace Collision
|
||||
{
|
||||
class BitmaskManager
|
||||
{
|
||||
public:
|
||||
~BitmaskManager() {
|
||||
std::map<const sf::Texture*, sf::Uint8*>::const_iterator end = Bitmasks.end();
|
||||
for (std::map<const sf::Texture*, sf::Uint8*>::const_iterator iter = Bitmasks.begin(); iter!=end; iter++)
|
||||
delete [] iter->second;
|
||||
}
|
||||
|
||||
sf::Uint8 GetPixel (const sf::Uint8* mask, const sf::Texture* tex, unsigned int x, unsigned int y) {
|
||||
if (x>tex->getSize().x||y>tex->getSize().y)
|
||||
return 0;
|
||||
|
||||
return mask[x+y*tex->getSize().x];
|
||||
}
|
||||
|
||||
sf::Uint8* GetMask (const sf::Texture* tex) {
|
||||
sf::Uint8* mask;
|
||||
std::map<const sf::Texture*, sf::Uint8*>::iterator pair = Bitmasks.find(tex);
|
||||
if (pair==Bitmasks.end())
|
||||
{
|
||||
sf::Image img = tex->copyToImage();
|
||||
mask = CreateMask (tex, img);
|
||||
}
|
||||
else
|
||||
mask = pair->second;
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
sf::Uint8* CreateMask (const sf::Texture* tex, const sf::Image& img) {
|
||||
sf::Uint8* mask = new sf::Uint8[tex->getSize().y*tex->getSize().x];
|
||||
|
||||
for (unsigned int y = 0; y<tex->getSize().y; y++)
|
||||
{
|
||||
for (unsigned int x = 0; x<tex->getSize().x; x++)
|
||||
mask[x+y*tex->getSize().x] = img.getPixel(x,y).a;
|
||||
}
|
||||
|
||||
Bitmasks.insert(std::pair<const sf::Texture*, sf::Uint8*>(tex,mask));
|
||||
|
||||
return mask;
|
||||
}
|
||||
private:
|
||||
std::map<const sf::Texture*, sf::Uint8*> Bitmasks;
|
||||
};
|
||||
|
||||
BitmaskManager Bitmasks;
|
||||
|
||||
bool PixelPerfectTest(const sf::Sprite& Object1, const sf::Sprite& Object2, sf::Uint8 AlphaLimit) {
|
||||
sf::FloatRect Intersection;
|
||||
if (Object1.getGlobalBounds().intersects(Object2.getGlobalBounds(), Intersection)) {
|
||||
sf::IntRect O1SubRect = Object1.getTextureRect();
|
||||
sf::IntRect O2SubRect = Object2.getTextureRect();
|
||||
|
||||
sf::Uint8* mask1 = Bitmasks.GetMask(Object1.getTexture());
|
||||
sf::Uint8* mask2 = Bitmasks.GetMask(Object2.getTexture());
|
||||
|
||||
// Loop through our pixels
|
||||
for (int i = Intersection.left; i < Intersection.left+Intersection.width; i++) {
|
||||
for (int j = Intersection.top; j < Intersection.top+Intersection.height; j++) {
|
||||
|
||||
sf::Vector2f o1v = Object1.getInverseTransform().transformPoint(i, j);
|
||||
sf::Vector2f o2v = Object2.getInverseTransform().transformPoint(i, j);
|
||||
|
||||
// Make sure pixels fall within the sprite's subrect
|
||||
if (o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 &&
|
||||
o1v.x < O1SubRect.width && o1v.y < O1SubRect.height &&
|
||||
o2v.x < O2SubRect.width && o2v.y < O2SubRect.height) {
|
||||
|
||||
if (Bitmasks.GetPixel(mask1, Object1.getTexture(), (int)(o1v.x)+O1SubRect.left, (int)(o1v.y)+O1SubRect.top) > AlphaLimit &&
|
||||
Bitmasks.GetPixel(mask2, Object2.getTexture(), (int)(o2v.x)+O2SubRect.left, (int)(o2v.y)+O2SubRect.top) > AlphaLimit)
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CreateTextureAndBitmask(sf::Texture &LoadInto, const std::string& Filename)
|
||||
{
|
||||
sf::Image img;
|
||||
if (!img.loadFromFile(Filename))
|
||||
return false;
|
||||
if (!LoadInto.loadFromImage(img))
|
||||
return false;
|
||||
|
||||
Bitmasks.CreateMask(&LoadInto, img);
|
||||
return true;
|
||||
}
|
||||
|
||||
sf::Vector2f GetSpriteCenter (const sf::Sprite& Object)
|
||||
{
|
||||
sf::FloatRect AABB = Object.getGlobalBounds();
|
||||
return sf::Vector2f (AABB.left+AABB.width/2.f, AABB.top+AABB.height/2.f);
|
||||
}
|
||||
|
||||
sf::Vector2f GetSpriteSize (const sf::Sprite& Object)
|
||||
{
|
||||
sf::IntRect OriginalSize = Object.getTextureRect();
|
||||
sf::Vector2f Scale = Object.getScale();
|
||||
return sf::Vector2f (OriginalSize.width*Scale.x, OriginalSize.height*Scale.y);
|
||||
}
|
||||
|
||||
bool CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2) {
|
||||
sf::Vector2f Obj1Size = GetSpriteSize(Object1);
|
||||
sf::Vector2f Obj2Size = GetSpriteSize(Object2);
|
||||
float Radius1 = (Obj1Size.x + Obj1Size.y) / 4;
|
||||
float Radius2 = (Obj2Size.x + Obj2Size.y) / 4;
|
||||
|
||||
sf::Vector2f Distance = GetSpriteCenter(Object1)-GetSpriteCenter(Object2);
|
||||
|
||||
return (Distance.x * Distance.x + Distance.y * Distance.y <= (Radius1 + Radius2) * (Radius1 + Radius2));
|
||||
}
|
||||
|
||||
class OrientedBoundingBox // Used in the BoundingBoxTest
|
||||
{
|
||||
public:
|
||||
OrientedBoundingBox (const sf::Sprite& Object) // Calculate the four points of the OBB from a transformed (scaled, rotated...) sprite
|
||||
{
|
||||
sf::Transform trans = Object.getTransform();
|
||||
sf::IntRect local = Object.getTextureRect();
|
||||
Points[0] = trans.transformPoint(0.f, 0.f);
|
||||
Points[1] = trans.transformPoint(local.width, 0.f);
|
||||
Points[2] = trans.transformPoint(local.width, local.height);
|
||||
Points[3] = trans.transformPoint(0.f, local.height);
|
||||
}
|
||||
|
||||
sf::Vector2f Points[4];
|
||||
|
||||
void ProjectOntoAxis (const sf::Vector2f& Axis, float& Min, float& Max) // Project all four points of the OBB onto the given axis and return the dotproducts of the two outermost points
|
||||
{
|
||||
Min = (Points[0].x*Axis.x+Points[0].y*Axis.y);
|
||||
Max = Min;
|
||||
for (int j = 1; j<4; j++)
|
||||
{
|
||||
float Projection = (Points[j].x*Axis.x+Points[j].y*Axis.y);
|
||||
|
||||
if (Projection<Min)
|
||||
Min=Projection;
|
||||
if (Projection>Max)
|
||||
Max=Projection;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool BoundingBoxTest(const sf::Sprite& Object1, const sf::Sprite& Object2) {
|
||||
OrientedBoundingBox OBB1 (Object1);
|
||||
OrientedBoundingBox OBB2 (Object2);
|
||||
|
||||
// Create the four distinct axes that are perpendicular to the edges of the two rectangles
|
||||
sf::Vector2f Axes[4] = {
|
||||
sf::Vector2f (OBB1.Points[1].x-OBB1.Points[0].x,
|
||||
OBB1.Points[1].y-OBB1.Points[0].y),
|
||||
sf::Vector2f (OBB1.Points[1].x-OBB1.Points[2].x,
|
||||
OBB1.Points[1].y-OBB1.Points[2].y),
|
||||
sf::Vector2f (OBB2.Points[0].x-OBB2.Points[3].x,
|
||||
OBB2.Points[0].y-OBB2.Points[3].y),
|
||||
sf::Vector2f (OBB2.Points[0].x-OBB2.Points[1].x,
|
||||
OBB2.Points[0].y-OBB2.Points[1].y)
|
||||
};
|
||||
|
||||
for (int i = 0; i<4; i++) // For each axis...
|
||||
{
|
||||
float MinOBB1, MaxOBB1, MinOBB2, MaxOBB2;
|
||||
|
||||
// ... project the points of both OBBs onto the axis ...
|
||||
OBB1.ProjectOntoAxis(Axes[i], MinOBB1, MaxOBB1);
|
||||
OBB2.ProjectOntoAxis(Axes[i], MinOBB2, MaxOBB2);
|
||||
|
||||
// ... and check whether the outermost projected points of both OBBs overlap.
|
||||
// If this is not the case, the Separating Axis Theorem states that there can be no collision between the rectangles
|
||||
if (!((MinOBB2<=MaxOBB1)&&(MaxOBB2>=MinOBB1)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* File: collision.cpp
|
||||
* Author: Nick (original version), ahnonay (SFML2 compatibility)
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include "Collision.h"
|
||||
|
||||
namespace Collision
|
||||
{
|
||||
class BitmaskManager
|
||||
{
|
||||
public:
|
||||
~BitmaskManager() {
|
||||
std::map<const sf::Texture*, sf::Uint8*>::const_iterator end = Bitmasks.end();
|
||||
for (std::map<const sf::Texture*, sf::Uint8*>::const_iterator iter = Bitmasks.begin(); iter!=end; iter++)
|
||||
delete [] iter->second;
|
||||
}
|
||||
|
||||
sf::Uint8 GetPixel (const sf::Uint8* mask, const sf::Texture* tex, unsigned int x, unsigned int y) {
|
||||
if (x>tex->getSize().x||y>tex->getSize().y)
|
||||
return 0;
|
||||
|
||||
return mask[x+y*tex->getSize().x];
|
||||
}
|
||||
|
||||
sf::Uint8* GetMask (const sf::Texture* tex) {
|
||||
sf::Uint8* mask;
|
||||
std::map<const sf::Texture*, sf::Uint8*>::iterator pair = Bitmasks.find(tex);
|
||||
if (pair==Bitmasks.end())
|
||||
{
|
||||
sf::Image img = tex->copyToImage();
|
||||
mask = CreateMask (tex, img);
|
||||
}
|
||||
else
|
||||
mask = pair->second;
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
sf::Uint8* CreateMask (const sf::Texture* tex, const sf::Image& img) {
|
||||
sf::Uint8* mask = new sf::Uint8[tex->getSize().y*tex->getSize().x];
|
||||
|
||||
for (unsigned int y = 0; y<tex->getSize().y; y++)
|
||||
{
|
||||
for (unsigned int x = 0; x<tex->getSize().x; x++)
|
||||
mask[x+y*tex->getSize().x] = img.getPixel(x,y).a;
|
||||
}
|
||||
|
||||
Bitmasks.insert(std::pair<const sf::Texture*, sf::Uint8*>(tex,mask));
|
||||
|
||||
return mask;
|
||||
}
|
||||
private:
|
||||
std::map<const sf::Texture*, sf::Uint8*> Bitmasks;
|
||||
};
|
||||
|
||||
BitmaskManager Bitmasks;
|
||||
|
||||
bool PixelPerfectTest(const sf::Sprite& Object1, const sf::Sprite& Object2, sf::Uint8 AlphaLimit) {
|
||||
sf::FloatRect Intersection;
|
||||
if (Object1.getGlobalBounds().intersects(Object2.getGlobalBounds(), Intersection)) {
|
||||
sf::IntRect O1SubRect = Object1.getTextureRect();
|
||||
sf::IntRect O2SubRect = Object2.getTextureRect();
|
||||
|
||||
sf::Uint8* mask1 = Bitmasks.GetMask(Object1.getTexture());
|
||||
sf::Uint8* mask2 = Bitmasks.GetMask(Object2.getTexture());
|
||||
|
||||
// Loop through our pixels
|
||||
for (int i = Intersection.left; i < Intersection.left+Intersection.width; i++) {
|
||||
for (int j = Intersection.top; j < Intersection.top+Intersection.height; j++) {
|
||||
|
||||
sf::Vector2f o1v = Object1.getInverseTransform().transformPoint(i, j);
|
||||
sf::Vector2f o2v = Object2.getInverseTransform().transformPoint(i, j);
|
||||
|
||||
// Make sure pixels fall within the sprite's subrect
|
||||
if (o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 &&
|
||||
o1v.x < O1SubRect.width && o1v.y < O1SubRect.height &&
|
||||
o2v.x < O2SubRect.width && o2v.y < O2SubRect.height) {
|
||||
|
||||
if (Bitmasks.GetPixel(mask1, Object1.getTexture(), (int)(o1v.x)+O1SubRect.left, (int)(o1v.y)+O1SubRect.top) > AlphaLimit &&
|
||||
Bitmasks.GetPixel(mask2, Object2.getTexture(), (int)(o2v.x)+O2SubRect.left, (int)(o2v.y)+O2SubRect.top) > AlphaLimit)
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CreateTextureAndBitmask(sf::Texture &LoadInto, const std::string& Filename)
|
||||
{
|
||||
sf::Image img;
|
||||
if (!img.loadFromFile(Filename))
|
||||
return false;
|
||||
if (!LoadInto.loadFromImage(img))
|
||||
return false;
|
||||
|
||||
Bitmasks.CreateMask(&LoadInto, img);
|
||||
return true;
|
||||
}
|
||||
|
||||
sf::Vector2f GetSpriteCenter (const sf::Sprite& Object)
|
||||
{
|
||||
sf::FloatRect AABB = Object.getGlobalBounds();
|
||||
return sf::Vector2f (AABB.left+AABB.width/2.f, AABB.top+AABB.height/2.f);
|
||||
}
|
||||
|
||||
sf::Vector2f GetSpriteSize (const sf::Sprite& Object)
|
||||
{
|
||||
sf::IntRect OriginalSize = Object.getTextureRect();
|
||||
sf::Vector2f Scale = Object.getScale();
|
||||
return sf::Vector2f (OriginalSize.width*Scale.x, OriginalSize.height*Scale.y);
|
||||
}
|
||||
|
||||
bool CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2) {
|
||||
sf::Vector2f Obj1Size = GetSpriteSize(Object1);
|
||||
sf::Vector2f Obj2Size = GetSpriteSize(Object2);
|
||||
float Radius1 = (Obj1Size.x + Obj1Size.y) / 4;
|
||||
float Radius2 = (Obj2Size.x + Obj2Size.y) / 4;
|
||||
|
||||
sf::Vector2f Distance = GetSpriteCenter(Object1)-GetSpriteCenter(Object2);
|
||||
|
||||
return (Distance.x * Distance.x + Distance.y * Distance.y <= (Radius1 + Radius2) * (Radius1 + Radius2));
|
||||
}
|
||||
|
||||
class OrientedBoundingBox // Used in the BoundingBoxTest
|
||||
{
|
||||
public:
|
||||
OrientedBoundingBox (const sf::Sprite& Object) // Calculate the four points of the OBB from a transformed (scaled, rotated...) sprite
|
||||
{
|
||||
sf::Transform trans = Object.getTransform();
|
||||
sf::IntRect local = Object.getTextureRect();
|
||||
Points[0] = trans.transformPoint(0.f, 0.f);
|
||||
Points[1] = trans.transformPoint(local.width, 0.f);
|
||||
Points[2] = trans.transformPoint(local.width, local.height);
|
||||
Points[3] = trans.transformPoint(0.f, local.height);
|
||||
}
|
||||
|
||||
sf::Vector2f Points[4];
|
||||
|
||||
void ProjectOntoAxis (const sf::Vector2f& Axis, float& Min, float& Max) // Project all four points of the OBB onto the given axis and return the dotproducts of the two outermost points
|
||||
{
|
||||
Min = (Points[0].x*Axis.x+Points[0].y*Axis.y);
|
||||
Max = Min;
|
||||
for (int j = 1; j<4; j++)
|
||||
{
|
||||
float Projection = (Points[j].x*Axis.x+Points[j].y*Axis.y);
|
||||
|
||||
if (Projection<Min)
|
||||
Min=Projection;
|
||||
if (Projection>Max)
|
||||
Max=Projection;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool BoundingBoxTest(const sf::Sprite& Object1, const sf::Sprite& Object2) {
|
||||
OrientedBoundingBox OBB1 (Object1);
|
||||
OrientedBoundingBox OBB2 (Object2);
|
||||
|
||||
// Create the four distinct axes that are perpendicular to the edges of the two rectangles
|
||||
sf::Vector2f Axes[4] = {
|
||||
sf::Vector2f (OBB1.Points[1].x-OBB1.Points[0].x,
|
||||
OBB1.Points[1].y-OBB1.Points[0].y),
|
||||
sf::Vector2f (OBB1.Points[1].x-OBB1.Points[2].x,
|
||||
OBB1.Points[1].y-OBB1.Points[2].y),
|
||||
sf::Vector2f (OBB2.Points[0].x-OBB2.Points[3].x,
|
||||
OBB2.Points[0].y-OBB2.Points[3].y),
|
||||
sf::Vector2f (OBB2.Points[0].x-OBB2.Points[1].x,
|
||||
OBB2.Points[0].y-OBB2.Points[1].y)
|
||||
};
|
||||
|
||||
for (int i = 0; i<4; i++) // For each axis...
|
||||
{
|
||||
float MinOBB1, MaxOBB1, MinOBB2, MaxOBB2;
|
||||
|
||||
// ... project the points of both OBBs onto the axis ...
|
||||
OBB1.ProjectOntoAxis(Axes[i], MinOBB1, MaxOBB1);
|
||||
OBB2.ProjectOntoAxis(Axes[i], MinOBB2, MaxOBB2);
|
||||
|
||||
// ... and check whether the outermost projected points of both OBBs overlap.
|
||||
// If this is not the case, the Separating Axis Theorem states that there can be no collision between the rectangles
|
||||
if (!((MinOBB2<=MaxOBB1)&&(MaxOBB2>=MinOBB1)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
154
Collision.h
154
Collision.h
|
|
@ -1,77 +1,77 @@
|
|||
/*
|
||||
* File: collision.h
|
||||
* Authors: Nick Koirala (original version), ahnonay (SFML2 compatibility)
|
||||
*
|
||||
* Collision Detection and handling class
|
||||
* For SFML2.
|
||||
|
||||
Notice from the original version:
|
||||
|
||||
(c) 2009 - LittleMonkey Ltd
|
||||
|
||||
This software is provided 'as-is', without any express or
|
||||
implied warranty. In no event will the authors be held
|
||||
liable for any damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute
|
||||
it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented;
|
||||
you must not claim that you wrote the original software.
|
||||
If you use this software in a product, an acknowledgment
|
||||
in the product documentation would be appreciated but
|
||||
is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such,
|
||||
and must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any
|
||||
source distribution.
|
||||
|
||||
*
|
||||
* Created on 30 January 2009, 11:02
|
||||
*/
|
||||
|
||||
#ifndef SFML_TEMPLATE_COLLISION_H
|
||||
#define SFML_TEMPLATE_COLLISION_H
|
||||
|
||||
#include <SFML\Graphics.hpp>
|
||||
|
||||
namespace Collision {
|
||||
//////
|
||||
/// Test for a collision between two sprites by comparing the alpha values of overlapping pixels
|
||||
/// Supports scaling and rotation
|
||||
/// AlphaLimit: The threshold at which a pixel becomes "solid". If AlphaLimit is 127, a pixel with
|
||||
/// alpha value 128 will cause a collision and a pixel with alpha value 126 will not.
|
||||
///
|
||||
/// This functions creates bitmasks of the textures of the two sprites by
|
||||
/// downloading the textures from the graphics card to memory -> SLOW!
|
||||
/// You can avoid this by using the "CreateTextureAndBitmask" function
|
||||
//////
|
||||
bool PixelPerfectTest(const sf::Sprite& Object1 ,const sf::Sprite& Object2, sf::Uint8 AlphaLimit = 0);
|
||||
|
||||
//////
|
||||
/// Replaces Texture::loadFromFile
|
||||
/// Load an imagefile into the given texture and create a bitmask for it
|
||||
/// This is much faster than creating the bitmask for a texture on the first run of "PixelPerfectTest"
|
||||
///
|
||||
/// The function returns false if the file could not be opened for some reason
|
||||
//////
|
||||
bool CreateTextureAndBitmask(sf::Texture &LoadInto, const std::string& Filename);
|
||||
|
||||
//////
|
||||
/// Test for collision using circle collision dection
|
||||
/// Radius is averaged from the dimensions of the sprite so
|
||||
/// roughly circular objects will be much more accurate
|
||||
//////
|
||||
bool CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2);
|
||||
|
||||
//////
|
||||
/// Test for bounding box collision using the Separating Axis Theorem
|
||||
/// Supports scaling and rotation
|
||||
//////
|
||||
bool BoundingBoxTest(const sf::Sprite& Object1, const sf::Sprite& Object2);
|
||||
}
|
||||
|
||||
#endif //SFML_TEMPLATE_COLLISION_H
|
||||
/*
|
||||
* File: collision.h
|
||||
* Authors: Nick Koirala (original version), ahnonay (SFML2 compatibility)
|
||||
*
|
||||
* Collision Detection and handling class
|
||||
* For SFML2.
|
||||
|
||||
Notice from the original version:
|
||||
|
||||
(c) 2009 - LittleMonkey Ltd
|
||||
|
||||
This software is provided 'as-is', without any express or
|
||||
implied warranty. In no event will the authors be held
|
||||
liable for any damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute
|
||||
it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented;
|
||||
you must not claim that you wrote the original software.
|
||||
If you use this software in a product, an acknowledgment
|
||||
in the product documentation would be appreciated but
|
||||
is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such,
|
||||
and must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any
|
||||
source distribution.
|
||||
|
||||
*
|
||||
* Created on 30 January 2009, 11:02
|
||||
*/
|
||||
|
||||
#ifndef SFML_TEMPLATE_COLLISION_H
|
||||
#define SFML_TEMPLATE_COLLISION_H
|
||||
|
||||
#include <SFML\Graphics.hpp>
|
||||
|
||||
namespace Collision {
|
||||
//////
|
||||
/// Test for a collision between two sprites by comparing the alpha values of overlapping pixels
|
||||
/// Supports scaling and rotation
|
||||
/// AlphaLimit: The threshold at which a pixel becomes "solid". If AlphaLimit is 127, a pixel with
|
||||
/// alpha value 128 will cause a collision and a pixel with alpha value 126 will not.
|
||||
///
|
||||
/// This functions creates bitmasks of the textures of the two sprites by
|
||||
/// downloading the textures from the graphics card to memory -> SLOW!
|
||||
/// You can avoid this by using the "CreateTextureAndBitmask" function
|
||||
//////
|
||||
bool PixelPerfectTest(const sf::Sprite& Object1 ,const sf::Sprite& Object2, sf::Uint8 AlphaLimit = 0);
|
||||
|
||||
//////
|
||||
/// Replaces Texture::loadFromFile
|
||||
/// Load an imagefile into the given texture and create a bitmask for it
|
||||
/// This is much faster than creating the bitmask for a texture on the first run of "PixelPerfectTest"
|
||||
///
|
||||
/// The function returns false if the file could not be opened for some reason
|
||||
//////
|
||||
bool CreateTextureAndBitmask(sf::Texture &LoadInto, const std::string& Filename);
|
||||
|
||||
//////
|
||||
/// Test for collision using circle collision dection
|
||||
/// Radius is averaged from the dimensions of the sprite so
|
||||
/// roughly circular objects will be much more accurate
|
||||
//////
|
||||
bool CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2);
|
||||
|
||||
//////
|
||||
/// Test for bounding box collision using the Separating Axis Theorem
|
||||
/// Supports scaling and rotation
|
||||
//////
|
||||
bool BoundingBoxTest(const sf::Sprite& Object1, const sf::Sprite& Object2);
|
||||
}
|
||||
|
||||
#endif //SFML_TEMPLATE_COLLISION_H
|
||||
|
|
|
|||
68
Entity.h
68
Entity.h
|
|
@ -1,34 +1,34 @@
|
|||
//
|
||||
// Created by benmo on 9/28/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_ENTITY_H
|
||||
#define SFML_TEMPLATE_ENTITY_H
|
||||
|
||||
class Entity : public Mount {
|
||||
public:
|
||||
Entity(float health, int points, const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : Mount(texture, scale, xPos, yPos, velocity, direction) {
|
||||
this->health = health;
|
||||
this->points = points;
|
||||
}
|
||||
|
||||
float getHealth() const {
|
||||
return health;
|
||||
}
|
||||
|
||||
void setHealth(float health) {
|
||||
this->health = health;
|
||||
}
|
||||
|
||||
void hit() {};
|
||||
|
||||
int getPoints() const {
|
||||
return points;
|
||||
}
|
||||
protected:
|
||||
float health;
|
||||
int points;
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_ENTITY_H
|
||||
//
|
||||
// Created by benmo on 9/28/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_ENTITY_H
|
||||
#define SFML_TEMPLATE_ENTITY_H
|
||||
|
||||
class Entity : public Mount {
|
||||
public:
|
||||
Entity(float health, int points, const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : Mount(texture, scale, xPos, yPos, velocity, direction) {
|
||||
this->health = health;
|
||||
this->points = points;
|
||||
}
|
||||
|
||||
float getHealth() const {
|
||||
return health;
|
||||
}
|
||||
|
||||
void setHealth(float health) {
|
||||
this->health = health;
|
||||
}
|
||||
|
||||
void hit() {};
|
||||
|
||||
int getPoints() const {
|
||||
return points;
|
||||
}
|
||||
protected:
|
||||
float health;
|
||||
int points;
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_ENTITY_H
|
||||
|
|
|
|||
6
Game.h
6
Game.h
|
|
@ -16,7 +16,7 @@ private:
|
|||
sf::Texture loadingBarFull;
|
||||
|
||||
//update total
|
||||
int totalTextures = 53;
|
||||
int totalTextures = 58;
|
||||
int loadedTextures = 0;
|
||||
|
||||
sf::Font oxan;
|
||||
|
|
@ -231,7 +231,7 @@ private:
|
|||
pewBuff.loadFromFile("data\\Sounds\\pew.wav");
|
||||
updateLoader(window, "Loading sounds...");
|
||||
pew.setBuffer(pewBuff);
|
||||
pew.setVolume(25);
|
||||
pew.setVolume(15);
|
||||
|
||||
Sound enemyBoom;
|
||||
SoundBuffer enemyBoomBuff;
|
||||
|
|
@ -952,8 +952,6 @@ private:
|
|||
int spaceBetween = 150 - widthEntryDate;
|
||||
int dateLoc = (window.getSize().x - (widthEntry + widthEntryDate + spaceBetween))/2;
|
||||
|
||||
cout << widthEntryDate << " " << widthEntry << " " << spaceBetween << endl;
|
||||
|
||||
highScoreEntriesDates[j].setPosition(dateLoc, window.getSize().y/1.8 + (25 * j));
|
||||
highScoreEntries[j].setPosition(dateLoc + 150, window.getSize().y/1.8 + (25 * j));
|
||||
}
|
||||
|
|
|
|||
104
Mount.h
104
Mount.h
|
|
@ -1,52 +1,52 @@
|
|||
//
|
||||
// Created by benmo on 10/12/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_MOUNT_H
|
||||
#define SFML_TEMPLATE_MOUNT_H
|
||||
|
||||
|
||||
class Mount : public MySprite {
|
||||
protected:
|
||||
vector<Rider> riders;
|
||||
public:
|
||||
Mount(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : MySprite(texture, scale, xPos, yPos, velocity, direction) {}
|
||||
|
||||
bool hasRider() {
|
||||
return !riders.empty();
|
||||
}
|
||||
void update(RenderWindow* window = nullptr) {
|
||||
MySprite::update(window);
|
||||
|
||||
for (Rider &r : riders) {
|
||||
if (r.doesFollowPosition()) r.setPosition(getPosition());
|
||||
if (r.doesFollowDirection()) r.setDirection(getDirection());
|
||||
}
|
||||
}
|
||||
|
||||
void addRider(Rider &sprite, int index = -1) {
|
||||
if (index == -1) {
|
||||
riders.push_back(sprite);
|
||||
riders[riders.size() - 1].setPosition(getPosition());
|
||||
} else {
|
||||
riders.insert(riders.begin() + index, sprite);
|
||||
riders[index].setPosition(getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
void removeRider(int index = -1) {
|
||||
if (index == -1) riders.pop_back();
|
||||
else riders.erase(riders.begin() + index);
|
||||
}
|
||||
|
||||
vector<Rider> getRiders() {
|
||||
return riders;
|
||||
}
|
||||
|
||||
vector<Rider>* getRidersForEdit() {
|
||||
return &riders;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_MOUNT_H
|
||||
//
|
||||
// Created by benmo on 10/12/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_MOUNT_H
|
||||
#define SFML_TEMPLATE_MOUNT_H
|
||||
|
||||
|
||||
class Mount : public MySprite {
|
||||
protected:
|
||||
vector<Rider> riders;
|
||||
public:
|
||||
Mount(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : MySprite(texture, scale, xPos, yPos, velocity, direction) {}
|
||||
|
||||
bool hasRider() {
|
||||
return !riders.empty();
|
||||
}
|
||||
void update(RenderWindow* window = nullptr) {
|
||||
MySprite::update(window);
|
||||
|
||||
for (Rider &r : riders) {
|
||||
if (r.doesFollowPosition()) r.setPosition(getPosition());
|
||||
if (r.doesFollowDirection()) r.setDirection(getDirection());
|
||||
}
|
||||
}
|
||||
|
||||
void addRider(Rider &sprite, int index = -1) {
|
||||
if (index == -1) {
|
||||
riders.push_back(sprite);
|
||||
riders[riders.size() - 1].setPosition(getPosition());
|
||||
} else {
|
||||
riders.insert(riders.begin() + index, sprite);
|
||||
riders[index].setPosition(getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
void removeRider(int index = -1) {
|
||||
if (index == -1) riders.pop_back();
|
||||
else riders.erase(riders.begin() + index);
|
||||
}
|
||||
|
||||
vector<Rider> getRiders() {
|
||||
return riders;
|
||||
}
|
||||
|
||||
vector<Rider>* getRidersForEdit() {
|
||||
return &riders;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_MOUNT_H
|
||||
|
|
|
|||
634
MySprite.h
634
MySprite.h
|
|
@ -1,317 +1,317 @@
|
|||
//
|
||||
// Created by benmo on 2/14/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_SPRITE_H
|
||||
#define SFML_TEMPLATE_SPRITE_H
|
||||
|
||||
#include <string>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <cmath>
|
||||
|
||||
using namespace sf;
|
||||
|
||||
class MySprite: public sf::Sprite{
|
||||
protected:
|
||||
struct Physics {
|
||||
float velocity, direction, acceleration, maxVelocity, minVelocity;
|
||||
float rotVelocity, rotAcceleration, maxRotVelocity;
|
||||
float xPos, yPos;
|
||||
};
|
||||
|
||||
struct Animation {
|
||||
float frameLength;
|
||||
unsigned int currframe = 0, columns, height, width, numFrames;
|
||||
Clock clock;
|
||||
bool isAnimated = false, isPlayed = false;
|
||||
IntRect textureRect;
|
||||
};
|
||||
|
||||
Physics spritePhysics{};
|
||||
|
||||
Animation spriteAnimation{};
|
||||
|
||||
bool toErase = false, circularHitbox = false;
|
||||
|
||||
string type = "Sprite";
|
||||
public:
|
||||
enum EdgeBehavior {
|
||||
IGNORE, LOOP, BOUNCE
|
||||
};
|
||||
|
||||
EdgeBehavior edgeBehavior = IGNORE;
|
||||
|
||||
constexpr static const double PI = 3.1415926;
|
||||
|
||||
static double distance(sf::Vector2f pos1, sf::Vector2f pos2) {
|
||||
return sqrt(pow(pos2.x - pos1.x, 2) + pow(pos2.y - pos1.y, 2));
|
||||
}
|
||||
|
||||
MySprite() : sf::Sprite() {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(getGlobalBounds().width/2,getGlobalBounds().height/2);
|
||||
}
|
||||
|
||||
explicit MySprite(const sf::Texture &texture) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(getGlobalBounds().width/2,getGlobalBounds().height/2);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(getGlobalBounds().width/2,getGlobalBounds().height/2);
|
||||
setScale(scale/100, scale/100);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, const sf::IntRect &rectangle) : sf::Sprite(texture, rectangle) {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(rectangle.width/(float)2.0,rectangle.height/(float)2.0);
|
||||
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float maxVelocity, float direction, float rotVelocity, float maxRotVelocity) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
spritePhysics.rotVelocity = rotVelocity;
|
||||
spritePhysics.maxVelocity = maxVelocity;
|
||||
spritePhysics.maxRotVelocity = maxRotVelocity;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float acceleration, float maxVelocity, float direction, float rotVelocity, float rotAcceleration, float maxRotVelocity) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
spritePhysics.rotVelocity = rotVelocity;
|
||||
spritePhysics.maxVelocity = maxVelocity;
|
||||
spritePhysics.maxRotVelocity = maxRotVelocity;
|
||||
spritePhysics.acceleration = acceleration;
|
||||
spritePhysics.rotAcceleration = rotAcceleration;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float maxVelocity, float direction) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
spritePhysics.maxVelocity = maxVelocity;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
void makeAnimated(int rows, int columns, float length, int frames = 0) {
|
||||
spriteAnimation.height = getTexture()->getSize().y/rows;
|
||||
spriteAnimation.width = getTexture()->getSize().x/columns;
|
||||
spriteAnimation.frameLength = length;
|
||||
spriteAnimation.columns = columns;
|
||||
|
||||
if (frames == 0) spriteAnimation.numFrames = rows*columns;
|
||||
else spriteAnimation.numFrames = frames;
|
||||
|
||||
spriteAnimation.isAnimated = true;
|
||||
|
||||
spriteAnimation.textureRect = IntRect(0, 0, spriteAnimation.width, spriteAnimation.height);
|
||||
setTextureRect(spriteAnimation.textureRect);
|
||||
|
||||
setOrigin(spriteAnimation.width/2.0, spriteAnimation.height/2.0);
|
||||
}
|
||||
|
||||
virtual void update(RenderWindow* window = nullptr) {
|
||||
spritePhysics.xPos += cos(spritePhysics.direction * (PI/180.0)) * spritePhysics.velocity;
|
||||
spritePhysics.yPos += -(sin(spritePhysics.direction * (PI/180.0)) * spritePhysics.velocity);
|
||||
|
||||
if (edgeBehavior == LOOP) {
|
||||
if (spritePhysics.xPos <= -getGlobalBounds().width)
|
||||
spritePhysics.xPos = window->getSize().x + getGlobalBounds().width;
|
||||
else if (spritePhysics.xPos >= window->getSize().x + getGlobalBounds().width)
|
||||
spritePhysics.xPos = -getGlobalBounds().width;
|
||||
|
||||
if (spritePhysics.yPos <= -getGlobalBounds().height)
|
||||
spritePhysics.yPos = window->getSize().y + getGlobalBounds().height;
|
||||
else if (spritePhysics.yPos >= window->getSize().y + getGlobalBounds().height)
|
||||
spritePhysics.yPos = -getGlobalBounds().height;
|
||||
} else if (edgeBehavior == IGNORE) {
|
||||
if (spritePhysics.xPos <= -getGlobalBounds().width ||
|
||||
spritePhysics.xPos >= window->getSize().x + getGlobalBounds().width ||
|
||||
spritePhysics.yPos <= -getGlobalBounds().height ||
|
||||
spritePhysics.yPos >= window->getSize().y + getGlobalBounds().height)
|
||||
toErase = true;
|
||||
}
|
||||
|
||||
spritePhysics.direction -= spritePhysics.rotVelocity;
|
||||
spritePhysics.direction = fmod(spritePhysics.direction, 360);
|
||||
if (spritePhysics.direction < 0)
|
||||
spritePhysics.direction += 360;
|
||||
|
||||
if (spriteAnimation.isAnimated) {
|
||||
if (spriteAnimation.clock.getElapsedTime().asSeconds() > spriteAnimation.frameLength) {
|
||||
spriteAnimation.currframe++;
|
||||
if (spriteAnimation.currframe % spriteAnimation.columns == 0) {
|
||||
spriteAnimation.textureRect.left = 0;
|
||||
spriteAnimation.textureRect.top += spriteAnimation.height;
|
||||
} else
|
||||
spriteAnimation.textureRect.left += spriteAnimation.height;
|
||||
|
||||
setTextureRect(spriteAnimation.textureRect);
|
||||
spriteAnimation.clock.restart();
|
||||
|
||||
if (spriteAnimation.currframe == spriteAnimation.numFrames) {
|
||||
spriteAnimation.isPlayed = true;
|
||||
spriteAnimation.currframe = 0;
|
||||
|
||||
spriteAnimation.textureRect.left = 0;
|
||||
spriteAnimation.textureRect.top = 0;
|
||||
}
|
||||
|
||||
spriteAnimation.clock.restart();
|
||||
}
|
||||
}
|
||||
|
||||
setPosition(spritePhysics.xPos, spritePhysics.yPos);
|
||||
setRotation(-spritePhysics.direction);
|
||||
}
|
||||
|
||||
void accelerate(float override = 0, bool ignoreMax = false) {
|
||||
if (override != 0) spritePhysics.velocity += override;
|
||||
else spritePhysics.velocity += spritePhysics.acceleration;
|
||||
|
||||
if (!ignoreMax && spritePhysics.velocity > spritePhysics.maxVelocity) spritePhysics.velocity = spritePhysics.maxVelocity;
|
||||
if (!ignoreMax && spritePhysics.velocity < spritePhysics.minVelocity) spritePhysics.velocity = spritePhysics.minVelocity;
|
||||
}
|
||||
|
||||
void rotAccel(float override = 0, bool ignoreMax = false) {
|
||||
if (override != 0) spritePhysics.rotVelocity += override;
|
||||
else spritePhysics.rotVelocity += spritePhysics.rotAcceleration;
|
||||
|
||||
if (!ignoreMax && spritePhysics.rotVelocity > spritePhysics.maxRotVelocity) spritePhysics.rotVelocity = spritePhysics.maxRotVelocity;
|
||||
else if (!ignoreMax && -spritePhysics.rotVelocity > spritePhysics.maxRotVelocity) spritePhysics.rotVelocity = -spritePhysics.maxRotVelocity;
|
||||
}
|
||||
|
||||
void turn(float degrees) {
|
||||
spritePhysics.direction -= degrees;
|
||||
|
||||
setRotation(-spritePhysics.direction);
|
||||
}
|
||||
|
||||
float getXPos() {
|
||||
return spritePhysics.xPos;
|
||||
}
|
||||
|
||||
float getYPos() {
|
||||
return spritePhysics.yPos;
|
||||
}
|
||||
|
||||
void setPosition(float xPos, float yPos) {
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
|
||||
sf::Sprite::setPosition(sf::Vector2f(xPos, yPos));
|
||||
}
|
||||
|
||||
void setPosition(const sf::Vector2f &vec) {
|
||||
spritePhysics.xPos = vec.x;
|
||||
spritePhysics.yPos = vec.y;
|
||||
|
||||
sf::Sprite::setPosition(vec);
|
||||
}
|
||||
|
||||
float getDirection() {
|
||||
return spritePhysics.direction;
|
||||
}
|
||||
|
||||
void setDirection(float angle) {
|
||||
spritePhysics.direction = angle;
|
||||
|
||||
setRotation(-angle);
|
||||
}
|
||||
|
||||
void setVelocity(float velo) {
|
||||
spritePhysics.velocity = velo;
|
||||
}
|
||||
|
||||
float getVelocity() {
|
||||
return spritePhysics.velocity;
|
||||
}
|
||||
|
||||
float getRotVelocity() {
|
||||
return spritePhysics.rotVelocity;
|
||||
};
|
||||
|
||||
void setMaxVelocity(float velo) {
|
||||
spritePhysics.maxVelocity = velo;
|
||||
}
|
||||
|
||||
void setMinVelocity(float velo) {
|
||||
spritePhysics.minVelocity = velo;
|
||||
}
|
||||
|
||||
void setMaxRotVelocity(float velo) {
|
||||
spritePhysics.maxRotVelocity = velo;
|
||||
}
|
||||
|
||||
void setEdgeBehavior(EdgeBehavior behavior) {
|
||||
edgeBehavior = behavior;
|
||||
}
|
||||
|
||||
bool toBeErased() const {
|
||||
return toErase;
|
||||
}
|
||||
|
||||
Animation getAnimation() {
|
||||
return spriteAnimation;
|
||||
}
|
||||
|
||||
string getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
void setType(string type) {
|
||||
this->type = std::move(type);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_SPRITE_H
|
||||
//
|
||||
// Created by benmo on 2/14/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_SPRITE_H
|
||||
#define SFML_TEMPLATE_SPRITE_H
|
||||
|
||||
#include <string>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <cmath>
|
||||
|
||||
using namespace sf;
|
||||
|
||||
class MySprite: public sf::Sprite{
|
||||
protected:
|
||||
struct Physics {
|
||||
float velocity, direction, acceleration, maxVelocity, minVelocity;
|
||||
float rotVelocity, rotAcceleration, maxRotVelocity;
|
||||
float xPos, yPos;
|
||||
};
|
||||
|
||||
struct Animation {
|
||||
float frameLength;
|
||||
unsigned int currframe = 0, columns, height, width, numFrames;
|
||||
Clock clock;
|
||||
bool isAnimated = false, isPlayed = false;
|
||||
IntRect textureRect;
|
||||
};
|
||||
|
||||
Physics spritePhysics{};
|
||||
|
||||
Animation spriteAnimation{};
|
||||
|
||||
bool toErase = false, circularHitbox = false;
|
||||
|
||||
string type = "Sprite";
|
||||
public:
|
||||
enum EdgeBehavior {
|
||||
IGNORE, LOOP, BOUNCE
|
||||
};
|
||||
|
||||
EdgeBehavior edgeBehavior = IGNORE;
|
||||
|
||||
constexpr static const double PI = 3.1415926;
|
||||
|
||||
static double distance(sf::Vector2f pos1, sf::Vector2f pos2) {
|
||||
return sqrt(pow(pos2.x - pos1.x, 2) + pow(pos2.y - pos1.y, 2));
|
||||
}
|
||||
|
||||
MySprite() : sf::Sprite() {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(getGlobalBounds().width/2,getGlobalBounds().height/2);
|
||||
}
|
||||
|
||||
explicit MySprite(const sf::Texture &texture) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(getGlobalBounds().width/2,getGlobalBounds().height/2);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(getGlobalBounds().width/2,getGlobalBounds().height/2);
|
||||
setScale(scale/100, scale/100);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, const sf::IntRect &rectangle) : sf::Sprite(texture, rectangle) {
|
||||
spritePhysics.velocity = 0;
|
||||
spritePhysics.xPos = 0;
|
||||
spritePhysics.yPos = 0;
|
||||
spritePhysics.direction = 0;
|
||||
|
||||
setOrigin(rectangle.width/(float)2.0,rectangle.height/(float)2.0);
|
||||
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float maxVelocity, float direction, float rotVelocity, float maxRotVelocity) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
spritePhysics.rotVelocity = rotVelocity;
|
||||
spritePhysics.maxVelocity = maxVelocity;
|
||||
spritePhysics.maxRotVelocity = maxRotVelocity;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float acceleration, float maxVelocity, float direction, float rotVelocity, float rotAcceleration, float maxRotVelocity) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
spritePhysics.rotVelocity = rotVelocity;
|
||||
spritePhysics.maxVelocity = maxVelocity;
|
||||
spritePhysics.maxRotVelocity = maxRotVelocity;
|
||||
spritePhysics.acceleration = acceleration;
|
||||
spritePhysics.rotAcceleration = rotAcceleration;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
MySprite(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float maxVelocity, float direction) : sf::Sprite(texture) {
|
||||
spritePhysics.velocity = velocity;
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
spritePhysics.direction = direction;
|
||||
spritePhysics.maxVelocity = maxVelocity;
|
||||
|
||||
setScale(scale/100, scale/100);
|
||||
setOrigin(getLocalBounds().width/2, getLocalBounds().height/2);
|
||||
setPosition(spritePhysics.xPos,spritePhysics.yPos);
|
||||
setRotation(direction);
|
||||
}
|
||||
|
||||
void makeAnimated(int rows, int columns, float length, int frames = 0) {
|
||||
spriteAnimation.height = getTexture()->getSize().y/rows;
|
||||
spriteAnimation.width = getTexture()->getSize().x/columns;
|
||||
spriteAnimation.frameLength = length;
|
||||
spriteAnimation.columns = columns;
|
||||
|
||||
if (frames == 0) spriteAnimation.numFrames = rows*columns;
|
||||
else spriteAnimation.numFrames = frames;
|
||||
|
||||
spriteAnimation.isAnimated = true;
|
||||
|
||||
spriteAnimation.textureRect = IntRect(0, 0, spriteAnimation.width, spriteAnimation.height);
|
||||
setTextureRect(spriteAnimation.textureRect);
|
||||
|
||||
setOrigin(spriteAnimation.width/2.0, spriteAnimation.height/2.0);
|
||||
}
|
||||
|
||||
virtual void update(RenderWindow* window = nullptr) {
|
||||
spritePhysics.xPos += cos(spritePhysics.direction * (PI/180.0)) * spritePhysics.velocity;
|
||||
spritePhysics.yPos += -(sin(spritePhysics.direction * (PI/180.0)) * spritePhysics.velocity);
|
||||
|
||||
if (edgeBehavior == LOOP) {
|
||||
if (spritePhysics.xPos <= -getGlobalBounds().width)
|
||||
spritePhysics.xPos = window->getSize().x + getGlobalBounds().width;
|
||||
else if (spritePhysics.xPos >= window->getSize().x + getGlobalBounds().width)
|
||||
spritePhysics.xPos = -getGlobalBounds().width;
|
||||
|
||||
if (spritePhysics.yPos <= -getGlobalBounds().height)
|
||||
spritePhysics.yPos = window->getSize().y + getGlobalBounds().height;
|
||||
else if (spritePhysics.yPos >= window->getSize().y + getGlobalBounds().height)
|
||||
spritePhysics.yPos = -getGlobalBounds().height;
|
||||
} else if (edgeBehavior == IGNORE) {
|
||||
if (spritePhysics.xPos <= -getGlobalBounds().width ||
|
||||
spritePhysics.xPos >= window->getSize().x + getGlobalBounds().width ||
|
||||
spritePhysics.yPos <= -getGlobalBounds().height ||
|
||||
spritePhysics.yPos >= window->getSize().y + getGlobalBounds().height)
|
||||
toErase = true;
|
||||
}
|
||||
|
||||
spritePhysics.direction -= spritePhysics.rotVelocity;
|
||||
spritePhysics.direction = fmod(spritePhysics.direction, 360);
|
||||
if (spritePhysics.direction < 0)
|
||||
spritePhysics.direction += 360;
|
||||
|
||||
if (spriteAnimation.isAnimated) {
|
||||
if (spriteAnimation.clock.getElapsedTime().asSeconds() > spriteAnimation.frameLength) {
|
||||
spriteAnimation.currframe++;
|
||||
if (spriteAnimation.currframe % spriteAnimation.columns == 0) {
|
||||
spriteAnimation.textureRect.left = 0;
|
||||
spriteAnimation.textureRect.top += spriteAnimation.height;
|
||||
} else
|
||||
spriteAnimation.textureRect.left += spriteAnimation.height;
|
||||
|
||||
setTextureRect(spriteAnimation.textureRect);
|
||||
spriteAnimation.clock.restart();
|
||||
|
||||
if (spriteAnimation.currframe == spriteAnimation.numFrames) {
|
||||
spriteAnimation.isPlayed = true;
|
||||
spriteAnimation.currframe = 0;
|
||||
|
||||
spriteAnimation.textureRect.left = 0;
|
||||
spriteAnimation.textureRect.top = 0;
|
||||
}
|
||||
|
||||
spriteAnimation.clock.restart();
|
||||
}
|
||||
}
|
||||
|
||||
setPosition(spritePhysics.xPos, spritePhysics.yPos);
|
||||
setRotation(-spritePhysics.direction);
|
||||
}
|
||||
|
||||
void accelerate(float override = 0, bool ignoreMax = false) {
|
||||
if (override != 0) spritePhysics.velocity += override;
|
||||
else spritePhysics.velocity += spritePhysics.acceleration;
|
||||
|
||||
if (!ignoreMax && spritePhysics.velocity > spritePhysics.maxVelocity) spritePhysics.velocity = spritePhysics.maxVelocity;
|
||||
if (!ignoreMax && spritePhysics.velocity < spritePhysics.minVelocity) spritePhysics.velocity = spritePhysics.minVelocity;
|
||||
}
|
||||
|
||||
void rotAccel(float override = 0, bool ignoreMax = false) {
|
||||
if (override != 0) spritePhysics.rotVelocity += override;
|
||||
else spritePhysics.rotVelocity += spritePhysics.rotAcceleration;
|
||||
|
||||
if (!ignoreMax && spritePhysics.rotVelocity > spritePhysics.maxRotVelocity) spritePhysics.rotVelocity = spritePhysics.maxRotVelocity;
|
||||
else if (!ignoreMax && -spritePhysics.rotVelocity > spritePhysics.maxRotVelocity) spritePhysics.rotVelocity = -spritePhysics.maxRotVelocity;
|
||||
}
|
||||
|
||||
void turn(float degrees) {
|
||||
spritePhysics.direction -= degrees;
|
||||
|
||||
setRotation(-spritePhysics.direction);
|
||||
}
|
||||
|
||||
float getXPos() {
|
||||
return spritePhysics.xPos;
|
||||
}
|
||||
|
||||
float getYPos() {
|
||||
return spritePhysics.yPos;
|
||||
}
|
||||
|
||||
void setPosition(float xPos, float yPos) {
|
||||
spritePhysics.xPos = xPos;
|
||||
spritePhysics.yPos = yPos;
|
||||
|
||||
sf::Sprite::setPosition(sf::Vector2f(xPos, yPos));
|
||||
}
|
||||
|
||||
void setPosition(const sf::Vector2f &vec) {
|
||||
spritePhysics.xPos = vec.x;
|
||||
spritePhysics.yPos = vec.y;
|
||||
|
||||
sf::Sprite::setPosition(vec);
|
||||
}
|
||||
|
||||
float getDirection() {
|
||||
return spritePhysics.direction;
|
||||
}
|
||||
|
||||
void setDirection(float angle) {
|
||||
spritePhysics.direction = angle;
|
||||
|
||||
setRotation(-angle);
|
||||
}
|
||||
|
||||
void setVelocity(float velo) {
|
||||
spritePhysics.velocity = velo;
|
||||
}
|
||||
|
||||
float getVelocity() {
|
||||
return spritePhysics.velocity;
|
||||
}
|
||||
|
||||
float getRotVelocity() {
|
||||
return spritePhysics.rotVelocity;
|
||||
};
|
||||
|
||||
void setMaxVelocity(float velo) {
|
||||
spritePhysics.maxVelocity = velo;
|
||||
}
|
||||
|
||||
void setMinVelocity(float velo) {
|
||||
spritePhysics.minVelocity = velo;
|
||||
}
|
||||
|
||||
void setMaxRotVelocity(float velo) {
|
||||
spritePhysics.maxRotVelocity = velo;
|
||||
}
|
||||
|
||||
void setEdgeBehavior(EdgeBehavior behavior) {
|
||||
edgeBehavior = behavior;
|
||||
}
|
||||
|
||||
bool toBeErased() const {
|
||||
return toErase;
|
||||
}
|
||||
|
||||
Animation getAnimation() {
|
||||
return spriteAnimation;
|
||||
}
|
||||
|
||||
string getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
void setType(string type) {
|
||||
this->type = std::move(type);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_SPRITE_H
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
# Asteroids Clone
|
||||
# Asteroids Clone
|
||||
|
|
|
|||
50
Rider.h
50
Rider.h
|
|
@ -1,25 +1,25 @@
|
|||
//
|
||||
// Created by benmo on 10/13/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_RIDER_H
|
||||
#define SFML_TEMPLATE_RIDER_H
|
||||
|
||||
|
||||
class Rider : public MySprite {
|
||||
protected:
|
||||
bool followsDirection = false, followsPosition = true;
|
||||
public:
|
||||
Rider(const sf::Texture &texture, float scale) : MySprite(texture, scale) {}
|
||||
|
||||
bool doesFollowDirection() const {
|
||||
return followsDirection;
|
||||
}
|
||||
|
||||
bool doesFollowPosition() const {
|
||||
return followsPosition;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_RIDER_H
|
||||
//
|
||||
// Created by benmo on 10/13/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_RIDER_H
|
||||
#define SFML_TEMPLATE_RIDER_H
|
||||
|
||||
|
||||
class Rider : public MySprite {
|
||||
protected:
|
||||
bool followsDirection = false, followsPosition = true;
|
||||
public:
|
||||
Rider(const sf::Texture &texture, float scale) : MySprite(texture, scale) {}
|
||||
|
||||
bool doesFollowDirection() const {
|
||||
return followsDirection;
|
||||
}
|
||||
|
||||
bool doesFollowPosition() const {
|
||||
return followsPosition;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_RIDER_H
|
||||
|
|
|
|||
182
Ship.h
182
Ship.h
|
|
@ -1,91 +1,91 @@
|
|||
//
|
||||
// Created by benmo on 9/25/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_SHIP_H
|
||||
#define SFML_TEMPLATE_SHIP_H
|
||||
|
||||
|
||||
class Ship : public Mount {
|
||||
public:
|
||||
Ship(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : Mount(texture, scale, xPos, yPos, velocity, direction) {}
|
||||
|
||||
void shoot(std::vector<MySprite> &projectiles, Sound &sound, sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) {
|
||||
projectiles.emplace_back(texture, scale, xPos, yPos, velocity, direction);
|
||||
sound.play();
|
||||
}
|
||||
|
||||
bool hit(RenderWindow &window, vector<MySprite> &animations, Texture &explosionTexture, default_random_engine &gen) {
|
||||
uniform_int_distribution<int> angle(0, 359);
|
||||
animations.emplace_back(explosionTexture, 100, getXPos(), getYPos(), 0, angle(gen));
|
||||
animations[animations.size() - 1].makeAnimated(5, 5, 0.01,23);
|
||||
|
||||
setPosition(window.getSize().x/2, window.getSize().y/2);
|
||||
setDirection(0);
|
||||
setVelocity(0);
|
||||
lives--;
|
||||
|
||||
return lives <= 0;
|
||||
}
|
||||
|
||||
void newLife(vector<MySprite> &lifeVec, Texture &life, RenderWindow &window, Sound &sound) {
|
||||
setNumLivesAdded(getNumLivesAdded() + 1);
|
||||
setLives(getLives() + 1);
|
||||
lifeVec.emplace_back(life, 20,window.getSize().x - ((getLives() - 1) * 40 + 25), 25, 0,270);
|
||||
sound.play();
|
||||
}
|
||||
|
||||
int getLives() const {
|
||||
return lives;
|
||||
}
|
||||
|
||||
void setLives(int lives) {
|
||||
this->lives = lives;
|
||||
}
|
||||
|
||||
void setShield(bool shield, Texture* texture = nullptr, float scale = 0, Clock* timer = nullptr) {
|
||||
this->shield = shield;
|
||||
|
||||
if (shield) {
|
||||
Rider temp(*texture, scale);
|
||||
temp.setType("Shield");
|
||||
addRider(temp);
|
||||
timer->restart();
|
||||
} else {
|
||||
for (int i = 0; i < riders.size(); i++) {
|
||||
if (riders[i].getType() == "Shield") removeRider(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool hasShield() const {
|
||||
return shield;
|
||||
}
|
||||
|
||||
int getScore() const {
|
||||
return score;
|
||||
}
|
||||
|
||||
void setScore(int score) {
|
||||
this->score = score;
|
||||
}
|
||||
|
||||
int getNewLifeScore() const {
|
||||
return newLifeScore;
|
||||
}
|
||||
|
||||
int getNumLivesAdded() const {
|
||||
return newLives;
|
||||
}
|
||||
|
||||
void setNumLivesAdded(int numLives) {
|
||||
newLives = numLives;
|
||||
}
|
||||
private:
|
||||
int lives = 3, score = 0, newLifeScore = 10000, newLives = 0;
|
||||
|
||||
bool shield;
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_SHIP_H
|
||||
//
|
||||
// Created by benmo on 9/25/2020.
|
||||
//
|
||||
|
||||
#ifndef SFML_TEMPLATE_SHIP_H
|
||||
#define SFML_TEMPLATE_SHIP_H
|
||||
|
||||
|
||||
class Ship : public Mount {
|
||||
public:
|
||||
Ship(const sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) : Mount(texture, scale, xPos, yPos, velocity, direction) {}
|
||||
|
||||
void shoot(std::vector<MySprite> &projectiles, Sound &sound, sf::Texture &texture, float scale, float xPos, float yPos, float velocity, float direction) {
|
||||
projectiles.emplace_back(texture, scale, xPos, yPos, velocity, direction);
|
||||
sound.play();
|
||||
}
|
||||
|
||||
bool hit(RenderWindow &window, vector<MySprite> &animations, Texture &explosionTexture, default_random_engine &gen) {
|
||||
uniform_int_distribution<int> angle(0, 359);
|
||||
animations.emplace_back(explosionTexture, 100, getXPos(), getYPos(), 0, angle(gen));
|
||||
animations[animations.size() - 1].makeAnimated(5, 5, 0.01,23);
|
||||
|
||||
setPosition(window.getSize().x/2, window.getSize().y/2);
|
||||
setDirection(0);
|
||||
setVelocity(0);
|
||||
lives--;
|
||||
|
||||
return lives <= 0;
|
||||
}
|
||||
|
||||
void newLife(vector<MySprite> &lifeVec, Texture &life, RenderWindow &window, Sound &sound) {
|
||||
setNumLivesAdded(getNumLivesAdded() + 1);
|
||||
setLives(getLives() + 1);
|
||||
lifeVec.emplace_back(life, 20,window.getSize().x - ((getLives() - 1) * 40 + 25), 25, 0,270);
|
||||
sound.play();
|
||||
}
|
||||
|
||||
int getLives() const {
|
||||
return lives;
|
||||
}
|
||||
|
||||
void setLives(int lives) {
|
||||
this->lives = lives;
|
||||
}
|
||||
|
||||
void setShield(bool shield, Texture* texture = nullptr, float scale = 0, Clock* timer = nullptr) {
|
||||
this->shield = shield;
|
||||
|
||||
if (shield) {
|
||||
Rider temp(*texture, scale);
|
||||
temp.setType("Shield");
|
||||
addRider(temp);
|
||||
timer->restart();
|
||||
} else {
|
||||
for (int i = 0; i < riders.size(); i++) {
|
||||
if (riders[i].getType() == "Shield") removeRider(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool hasShield() const {
|
||||
return shield;
|
||||
}
|
||||
|
||||
int getScore() const {
|
||||
return score;
|
||||
}
|
||||
|
||||
void setScore(int score) {
|
||||
this->score = score;
|
||||
}
|
||||
|
||||
int getNewLifeScore() const {
|
||||
return newLifeScore;
|
||||
}
|
||||
|
||||
int getNumLivesAdded() const {
|
||||
return newLives;
|
||||
}
|
||||
|
||||
void setNumLivesAdded(int numLives) {
|
||||
newLives = numLives;
|
||||
}
|
||||
private:
|
||||
int lives = 3, score = 0, newLifeScore = 10000, newLives = 0;
|
||||
|
||||
bool shield;
|
||||
};
|
||||
|
||||
|
||||
#endif //SFML_TEMPLATE_SHIP_H
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
72
main.cpp
72
main.cpp
|
|
@ -1,37 +1,37 @@
|
|||
#include <iostream> // for standard input/output
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
using namespace std; // using the standard namespace
|
||||
|
||||
#include <SFML/Graphics.hpp> // include the SFML Graphics Library
|
||||
#include <SFML/Audio.hpp>
|
||||
#include "Collision.h"
|
||||
using namespace sf; // using the sf namespace
|
||||
|
||||
#include "MySprite.h"
|
||||
#include "Rider.h"
|
||||
#include "Mount.h"
|
||||
#include "Ship.h"
|
||||
#include "Asteroid.h"
|
||||
#include "Menu.h"
|
||||
#include "Game.h"
|
||||
|
||||
int main() {
|
||||
bool play = true;
|
||||
Time mTime = Time::Zero;
|
||||
int loop = 0;
|
||||
|
||||
while (play) {
|
||||
Menu menu(loop, mTime);
|
||||
if (menu.result == EXIT_SUCCESS) {
|
||||
Game game(menu.musicPicked, menu.mTime);
|
||||
if (game.result == 1) play = false;
|
||||
|
||||
loop = game.loop;
|
||||
mTime = game.mTime;
|
||||
}
|
||||
else play = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#include <iostream> // for standard input/output
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
using namespace std; // using the standard namespace
|
||||
|
||||
#include <SFML/Graphics.hpp> // include the SFML Graphics Library
|
||||
#include <SFML/Audio.hpp>
|
||||
#include "Collision.h"
|
||||
using namespace sf; // using the sf namespace
|
||||
|
||||
#include "MySprite.h"
|
||||
#include "Rider.h"
|
||||
#include "Mount.h"
|
||||
#include "Ship.h"
|
||||
#include "Asteroid.h"
|
||||
#include "Menu.h"
|
||||
#include "Game.h"
|
||||
|
||||
int main() {
|
||||
bool play = true;
|
||||
Time mTime = Time::Zero;
|
||||
int loop = 0;
|
||||
|
||||
while (play) {
|
||||
Menu menu(loop, mTime);
|
||||
if (menu.result == EXIT_SUCCESS) {
|
||||
Game game(menu.musicPicked, menu.mTime);
|
||||
if (game.result == 1) play = false;
|
||||
|
||||
loop = game.loop;
|
||||
mTime = game.mTime;
|
||||
}
|
||||
else play = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
scores
15
scores
|
|
@ -1,5 +1,10 @@
|
|||
19/10/2020 107800
|
||||
19/10/2020 12100
|
||||
19/10/2020 8700
|
||||
19/10/2020 1300
|
||||
19/10/2020 100
|
||||
19/10/2020 107800
|
||||
19/10/2020 12100
|
||||
19/10/2020 8700
|
||||
21/10/2020 4900
|
||||
21/10/2020 2300
|
||||
21/10/2020 2100
|
||||
21/10/2020 1400
|
||||
19/10/2020 1300
|
||||
21/10/2020 800
|
||||
21/10/2020 200
|
||||
Loading…
Add table
Add a link
Reference in a new issue