Skip to content

Commit

Permalink
Merge pull request #4 from joshSi/feature/collision-logic
Browse files Browse the repository at this point in the history
Simple collision logic

TODO:
- Rectangle-Circle snap logic
- Future: Rotated rectangles?
  • Loading branch information
joshSi authored Jun 26, 2024
2 parents b3d26da + 58dff21 commit 532d64e
Show file tree
Hide file tree
Showing 13 changed files with 503 additions and 232 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ project(PhantomForce LANGUAGES CXX)

find_package(SFML 2.5.1 COMPONENTS graphics window system)

set(SOURCES src/Main.cpp src/Game.cpp src/Player.cpp src/TileMap.cpp)
set(HEADERS include/utils/utils.h include/utils/platform_utils.h include/Game.h include/Player.h include/TileMap.h)
set(SOURCES src/Main.cpp src/Game.cpp src/Player.cpp src/TileMap.cpp src/Object.cpp)
set(HEADERS include/utils/utils.h include/utils/platform_utils.h include/Game.h include/Player.h include/TileMap.h include/Object.h)
set(RESOURCE_PATH assets)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install)
file(GLOB_RECURSE RESOURCES "${RESOURCE_PATH}/*")
Expand Down
Binary file added assets/crate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 17 additions & 11 deletions include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
#define GAME_H
#include <SFML/Graphics.hpp>

class Game
{
public:
Game(int framerate = 60);
Game(const Game&) = delete;
void operator=(const Game&) = delete;
~Game();
#include "Object.h"

void pollEvents();
private:
sf::RenderWindow* m_window;
uint8_t m_input;
class Game {
public:
Game(int framerate = 60);
Game(const Game&) = delete;
void operator=(const Game&) = delete;
~Game();

void pollEvents();

private:
sf::Clock m_clock;
sf::RenderWindow* m_window;
// Up to 4 drawing layers
std::vector<sf::Sprite*> m_sprite_layer[4];
std::vector<Object*> m_object_list;
uint8_t m_input;
};

#endif
63 changes: 63 additions & 0 deletions include/Object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef OBJECT_H
#define OBJECT_H
#include <SFML/Graphics.hpp>

class Object : virtual public sf::Sprite {
public:
Object();
Object(sf::Texture &tex);
virtual bool checkCollision(Object *obj) const = 0;
virtual void snapCollision(Object *obj) = 0;
virtual void drawCollision(sf::RenderTarget *target) const = 0;

sf::Vector2f m_spd_vec;
float m_mass; // 0 if static
static bool g_draw_collisions;
};

class Circle;
class Rectangle;

class Circle : public Object {
public:
Circle() : Object(), m_radius(1) {}
Circle(sf::Texture &tex);
Circle(sf::Texture &tex, float r) : Object(tex), m_radius(r) {}

bool checkCollision(Object *obj) const override;
bool checkCollision(Circle *obj) const;
bool checkCollision(Rectangle *obj) const;

void snapCollision(Object *obj) override;
void snapCollision(Circle *obj);
void snapCollision(Rectangle *obj);

void drawCollision(sf::RenderTarget *target) const override;
float getRadius() const { return m_radius; }

private:
float m_radius;
};

class Rectangle : public Object {
public:
Rectangle() : Object(), m_size(2, 2) {}
Rectangle(sf::Texture &tex);
Rectangle(sf::Texture &tex, sf::Vector2f size) : Object(tex), m_size(size) {}

bool checkCollision(Object *obj) const override;
bool checkCollision(Rectangle *obj) const;
bool checkCollision(Circle *obj) const;

void snapCollision(Object *obj) override;
void snapCollision(Circle *obj);
void snapCollision(Rectangle *obj);

void drawCollision(sf::RenderTarget *target) const override;
const sf::Vector2f getSize() const { return m_size; }

private:
sf::Vector2f m_size;
};

#endif
29 changes: 16 additions & 13 deletions include/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
#define PLAYER_H
#include <SFML/Graphics.hpp>

#include "Object.h"

struct MoveStats {
float max_spd;
float accel;
float fric;
float reload_rate;
float max_spd;
float accel;
float fric;
float reload_rate;
};

class Player : public sf::Sprite
{
public:
Player();
Player(sf::Texture& tex, MoveStats* s);
void move(sf::Vector2f velocity, float fr, bool sprint, uint8_t input);
class Player : public Circle {
public:
Player(sf::Texture& tex, MoveStats* s, float r);
void move(sf::Vector2f velocity, float fr, bool sprint, uint8_t input);
void setObjects(std::vector<Object*>* objs);

private:
sf::Vector2f spd_vec;
MoveStats* stat;
private:
std::vector<Object*>* m_objects_ref;
sf::Vector2f m_last_pos;
void checkCollision();
MoveStats* m_stat;
};

#endif
39 changes: 21 additions & 18 deletions include/TileMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@
#define TILEMAP_H
#include <SFML/Graphics.hpp>

class TileMap : public sf::Drawable, public sf::Transformable
{
public:
bool loadTileset(const std::string& tileset, sf::Vector2u tileSize);
void loadMap(const int* tiles, unsigned int mapWidth, unsigned int mapHeight, sf::View view);
void loadVertexChunk(sf::Vector2f view_coord);
// Flashes the chunk with tile v as the topleft corner
void flash(sf::Vector2i v);
class TileMap : public sf::Drawable {
public:
bool loadTileset(const std::string& tileset, sf::Vector2u tileSize);
void loadMap(const int* tiles, unsigned int mapWidth, unsigned int mapHeight,
sf::View view);
void loadVertexChunk(sf::Vector2f view_coord);
// Flashes the chunk with tile v as the topleft corner
void flash(sf::Vector2i v);

private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

sf::VertexArray m_vertices; // The vertex array to draw the tiles onto the view
sf::Vector2u m_chunkSize; // The dimensions of a VertexArray chunk when rendered (in tiles)
sf::Texture m_tileset; // The tileset texture
sf::Vector2u m_tileSize; // The dimensions of each tile (in pixels)
std::vector<int> m_tiles; // The tile map
sf::Vector2u m_mapSize; // The dimensions of the entire tile map (in tiles)
sf::Vector2f m_center; // The center of the view since loadVertexChunk is last called
sf::VertexArray
m_vertices; // The vertex array to draw the tiles onto the view
sf::Vector2u m_chunkSize; // The dimensions of a VertexArray chunk when
// rendered (in tiles)
sf::Texture m_tileset; // The tileset texture
sf::Vector2u m_tileSize; // The dimensions of each tile (in pixels)
std::vector<int> m_tiles; // The tile map
sf::Vector2u m_mapSize; // The dimensions of the entire tile map (in tiles)
sf::Vector2f
m_center; // The center of the view since loadVertexChunk is last called
};

#endif
#endif
37 changes: 19 additions & 18 deletions include/utils/platform_utils.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
#ifndef PLATFORM_UTILS_H
#ifndef PLATFORM_UTILS_H
#define PLATFORM_UTILS_H
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#endif
#include <string>

std::string getResourcePath()
{
#ifdef __APPLE__
CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef macPath = CFURLCopyFileSystemPath(appUrlRef, kCFURLPOSIXPathStyle);
const char *pathPtr = CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
std::string path(pathPtr);
std::string resourcePath = path + "/Contents/Resources/";
std::string getResourcePath() {
#ifdef __APPLE__
CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef macPath =
CFURLCopyFileSystemPath(appUrlRef, kCFURLPOSIXPathStyle);
const char *pathPtr =
CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
std::string path(pathPtr);
std::string resourcePath = path + "/Contents/Resources/";

CFRelease(appUrlRef);
CFRelease(macPath);
CFRelease(appUrlRef);
CFRelease(macPath);

return resourcePath;
#elif defined(_WIN32)
return "assets/";
#else
return "assets/";
#endif
return resourcePath;
#elif defined(_WIN32)
return "assets/";
#else
return "assets/";
#endif
}

#endif
#endif
48 changes: 21 additions & 27 deletions include/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,41 @@
#define GLOBAL_H
#define _USE_MATH_DEFINES
#include <math.h>

#include <SFML/Graphics.hpp>

// Weighted avg between x and y
template<typename T>
T converge(T x, T y, T weight)
{
return (weight * x) + (1 - weight) * y;
template <typename T>
T converge(T x, T y, T weight) {
return (weight * x) + (1 - weight) * y;
}

// Angle of Vector2<T>
template<typename T>
float get_angle(sf::Vector2<T> v)
{
return (atan2f(v.y, v.x) * 180 / M_PI);
template <typename T>
float get_angle(sf::Vector2<T> v) {
return (atan2f(v.y, v.x) * 180 / M_PI);
}

// Length (magnitude) of Vector2<T>
template<typename T>
float len(const sf::Vector2<T> v)
{
return sqrtf(powf(v.x, 2) + powf(v.y, 2));
template <typename T>
float len(const sf::Vector2<T> v) {
return sqrtf(powf(v.x, 2) + powf(v.y, 2));
}

// Helper function keeps x between -180 & 180 degrees
template<typename T>
void normalize(T& x)
{
if (x > 180)
{
x -= 360;
}
if (x <= -180)
{
x += 360;
}
template <typename T>
void normalize(T& x) {
if (x > 180) {
x -= 360;
}
if (x <= -180) {
x += 360;
}
}

template<typename T>
sf::Vector2<T> max(sf::Vector2<T> a, sf::Vector2<T> b)
{
return sf::Vector2<T>(std::max(a.x, b.x), std::max(a.y, b.y));
template <typename T>
sf::Vector2<T> max(sf::Vector2<T> a, sf::Vector2<T> b) {
return sf::Vector2<T>(std::max(a.x, b.x), std::max(a.y, b.y));
}

#endif
Loading

0 comments on commit 532d64e

Please sign in to comment.