Skip to content

Commit

Permalink
Refactoring, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kiner-shah committed Jul 8, 2024
1 parent f022d95 commit ab24cb8
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 261 deletions.
27 changes: 17 additions & 10 deletions include/gameObject.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
#pragma once
#include "SFML/Graphics.hpp"
#include "typedefs.hpp"
#include <SFML/Graphics.hpp>
#include <memory>

class gameObject;

using gameObjectPtr = std::shared_ptr<gameObject>;

class gameObject
{
protected:
sf::Vector2f position;
float hitboxRadius = 2;
float hitboxRadius = 2.0f;

public:
gameObject();
~gameObject();
gameObject() = default;
virtual ~gameObject() = default;

virtual sf::Vector2f getPosition();
virtual void draw(sf::RenderWindow &window);
virtual void update();
virtual void setPosition(sf::Vector2f position);

void setHitboxRadius(float range);
float getHitboxRadius();
bool intersects(gameObjectPtr other);
sf::Vector2f getPosition() const;
void setPosition(const sf::Vector2f &position);

void setHitboxRadius(float range);
float getHitboxRadius() const;

bool intersects(gameObjectPtr other) const;
};
60 changes: 30 additions & 30 deletions include/particle.hpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
#pragma once
#include "gameObject.hpp"
#include <memory>

class particle;

using particlePtr = std::shared_ptr<particle>;

class particle : public gameObject
{
private:
float size;
sf::FloatRect separationRect;
sf::FloatRect searchRect;
sf::Vector2f velocity;
float speed=1;
float maxSpeed=1;

sf::Vector2f velocity;
/// graphics
sf::Sprite sprite;
sf::Texture texture;
bool textureLoaded = false;

const float separationWeigth = 0.1f;
const float alignmentWeight = 0.05f;
const float cohesionWeight = 0.0003f;
const float separationRange = 20.0f;
const float searchRange = 60.0f;
const float speed = 1.0f;
const float maxSpeed = 1.0f;

public:
float separationWeigth = 0.1;
float alignmentWeight = 0.05;
float cohesionWeight = 0.0003;
particle(const sf::Vector2f &position, float size);
~particle() = default;

float separationRange = 20;
float searchRange = 60;

particle(sf::Vector2f position, float size);
~particle();
void draw(sf::RenderWindow &window) override;
void update() override;

sf::FloatRect getSearchRect() const;
void setSearchRect(const sf::FloatRect& radius);
sf::FloatRect getSeparationRect() const;
void setSeparationRect(const sf::FloatRect& radius);

void setVelocity(const sf::Vector2f& velocity);
sf::Vector2f getVelocity() const;

virtual void draw(sf::RenderWindow &window) override;
void update() override;
void setSearchRect(const sf::FloatRect &radius);

void separation(std::vector<particlePtr> &obj);
void alignment(std::vector<particlePtr> &obj);
void cohesion(std::vector<particlePtr> &obj);
sf::FloatRect getSeparationRect() const;
void setSeparationRect(const sf::FloatRect &radius);

sf::Vector2f getVelocity() const;
void setVelocity(const sf::Vector2f &velocity);

///graphics
sf::Sprite sprite;
sf::Texture texture;
void separation(const std::vector<particlePtr> &obj);
void alignment(const std::vector<particlePtr> &obj);
void cohesion(const std::vector<particlePtr> &obj);
};


43 changes: 22 additions & 21 deletions include/quadTree.hpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "particle.hpp"
#include <memory>
#include <vector>

class gameObject;
class quadTree;

using quadTreePtr = std::shared_ptr<quadTree>;

class quadTree
{
private:
// sf::Vector2f size;
sf::RectangleShape rect;
sf::FloatRect boundary;
int capacity;
std::vector<particlePtr> objects;
bool divided;
std::vector<particlePtr> objects;
bool divided = false;

//child quadTrees, remains NULL till division
// child quadTrees, remains NULL till division
quadTreePtr nw;
quadTreePtr ne;
quadTreePtr sw;
quadTreePtr se;

void subdivide();

public:
//used to make empty quadtree objects available in the parent quadtree before subdivision
quadTree();
// used to make empty quadtree objects available in the parent quadtree before subdivision
quadTree() = default;
// quadTree(sf::Vector2f size,int capacity);
quadTree(float x,float y, float a, float b, int capacity);
~quadTree();
quadTree(float x, float y, float a, float b, int capacity);
~quadTree() = default;

void subdivide();
void draw(sf::RenderWindow& window);
void draw(sf::RenderWindow &window);
void insert(particlePtr object);

const sf::Vector2f getPosition();
sf::FloatRect* getBoundary();
sf::RectangleShape* getRect();
sf::Vector2f getPosition() const;
// sf::FloatRect *getBoundary();
// sf::RectangleShape *getRect();


/// @brief Finds other gameObjects in the specified range
/// @brief Finds other gameObjects in the specified range
/// @param range the rectangle within a search occurs
/// @param found Found objectswill be returned in this array
/// @return
void query(sf::FloatRect range,std::vector<particlePtr> &found);
/// @return
void query(sf::FloatRect range, std::vector<particlePtr> &found);
};


11 changes: 0 additions & 11 deletions include/typedefs.hpp

This file was deleted.

9 changes: 4 additions & 5 deletions include/vectorFunctions.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "SFML/Graphics.hpp"
#include "math.h"
#include <SFML/Graphics.hpp>

float magnitude(sf::Vector2f vec);
sf::Vector2f normalize(sf::Vector2f vec);
float distance(sf::Vector2f a, sf::Vector2f b);
float magnitude(const sf::Vector2f &vec);
sf::Vector2f normalize(const sf::Vector2f &vec);
float distance(const sf::Vector2f &a, const sf::Vector2f &b);
27 changes: 9 additions & 18 deletions src/gameObject.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
#include "typedefs.hpp"
#include "gameObject.hpp"
#include "math.h"
#include "vectorFunctions.hpp"
#include <cmath>

gameObject::gameObject(){

}

gameObject::~gameObject(){

}

sf::Vector2f gameObject::getPosition()
sf::Vector2f gameObject::getPosition() const
{
return position;
}

void gameObject::draw(sf::RenderWindow &window)
void gameObject::draw(sf::RenderWindow & /*window*/)
{

}

void gameObject::update()
{
}

void gameObject::setPosition(sf::Vector2f pos)
void gameObject::setPosition(const sf::Vector2f &pos)
{
position = pos;
position = pos;
}

void gameObject::setHitboxRadius(float range)
{
hitboxRadius = range;
}

float gameObject::getHitboxRadius()
float gameObject::getHitboxRadius() const
{
return hitboxRadius;
}

bool gameObject::intersects(gameObjectPtr other)
bool gameObject::intersects(gameObjectPtr other) const
{
return hitboxRadius*2 >= sqrt((other->getPosition().x - position.x)*(other->getPosition().x - position.x) + (other->getPosition().y - position.y)*(other->getPosition().y - position.y));
return hitboxRadius * 2.0f >= distance(other->getPosition(), position);
}
Loading

0 comments on commit ab24cb8

Please sign in to comment.