Skip to content

Commit 532d64e

Browse files
authored
Merge pull request #4 from joshSi/feature/collision-logic
Simple collision logic TODO: - Rectangle-Circle snap logic - Future: Rotated rectangles?
2 parents b3d26da + 58dff21 commit 532d64e

13 files changed

+503
-232
lines changed

Diff for: CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ project(PhantomForce LANGUAGES CXX)
33

44
find_package(SFML 2.5.1 COMPONENTS graphics window system)
55

6-
set(SOURCES src/Main.cpp src/Game.cpp src/Player.cpp src/TileMap.cpp)
7-
set(HEADERS include/utils/utils.h include/utils/platform_utils.h include/Game.h include/Player.h include/TileMap.h)
6+
set(SOURCES src/Main.cpp src/Game.cpp src/Player.cpp src/TileMap.cpp src/Object.cpp)
7+
set(HEADERS include/utils/utils.h include/utils/platform_utils.h include/Game.h include/Player.h include/TileMap.h include/Object.h)
88
set(RESOURCE_PATH assets)
99
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install)
1010
file(GLOB_RECURSE RESOURCES "${RESOURCE_PATH}/*")

Diff for: assets/crate.png

287 Bytes
Loading

Diff for: include/Game.h

+17-11
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
#define GAME_H
33
#include <SFML/Graphics.hpp>
44

5-
class Game
6-
{
7-
public:
8-
Game(int framerate = 60);
9-
Game(const Game&) = delete;
10-
void operator=(const Game&) = delete;
11-
~Game();
5+
#include "Object.h"
126

13-
void pollEvents();
14-
private:
15-
sf::RenderWindow* m_window;
16-
uint8_t m_input;
7+
class Game {
8+
public:
9+
Game(int framerate = 60);
10+
Game(const Game&) = delete;
11+
void operator=(const Game&) = delete;
12+
~Game();
13+
14+
void pollEvents();
15+
16+
private:
17+
sf::Clock m_clock;
18+
sf::RenderWindow* m_window;
19+
// Up to 4 drawing layers
20+
std::vector<sf::Sprite*> m_sprite_layer[4];
21+
std::vector<Object*> m_object_list;
22+
uint8_t m_input;
1723
};
1824

1925
#endif

Diff for: include/Object.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef OBJECT_H
2+
#define OBJECT_H
3+
#include <SFML/Graphics.hpp>
4+
5+
class Object : virtual public sf::Sprite {
6+
public:
7+
Object();
8+
Object(sf::Texture &tex);
9+
virtual bool checkCollision(Object *obj) const = 0;
10+
virtual void snapCollision(Object *obj) = 0;
11+
virtual void drawCollision(sf::RenderTarget *target) const = 0;
12+
13+
sf::Vector2f m_spd_vec;
14+
float m_mass; // 0 if static
15+
static bool g_draw_collisions;
16+
};
17+
18+
class Circle;
19+
class Rectangle;
20+
21+
class Circle : public Object {
22+
public:
23+
Circle() : Object(), m_radius(1) {}
24+
Circle(sf::Texture &tex);
25+
Circle(sf::Texture &tex, float r) : Object(tex), m_radius(r) {}
26+
27+
bool checkCollision(Object *obj) const override;
28+
bool checkCollision(Circle *obj) const;
29+
bool checkCollision(Rectangle *obj) const;
30+
31+
void snapCollision(Object *obj) override;
32+
void snapCollision(Circle *obj);
33+
void snapCollision(Rectangle *obj);
34+
35+
void drawCollision(sf::RenderTarget *target) const override;
36+
float getRadius() const { return m_radius; }
37+
38+
private:
39+
float m_radius;
40+
};
41+
42+
class Rectangle : public Object {
43+
public:
44+
Rectangle() : Object(), m_size(2, 2) {}
45+
Rectangle(sf::Texture &tex);
46+
Rectangle(sf::Texture &tex, sf::Vector2f size) : Object(tex), m_size(size) {}
47+
48+
bool checkCollision(Object *obj) const override;
49+
bool checkCollision(Rectangle *obj) const;
50+
bool checkCollision(Circle *obj) const;
51+
52+
void snapCollision(Object *obj) override;
53+
void snapCollision(Circle *obj);
54+
void snapCollision(Rectangle *obj);
55+
56+
void drawCollision(sf::RenderTarget *target) const override;
57+
const sf::Vector2f getSize() const { return m_size; }
58+
59+
private:
60+
sf::Vector2f m_size;
61+
};
62+
63+
#endif

Diff for: include/Player.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
#define PLAYER_H
33
#include <SFML/Graphics.hpp>
44

5+
#include "Object.h"
6+
57
struct MoveStats {
6-
float max_spd;
7-
float accel;
8-
float fric;
9-
float reload_rate;
8+
float max_spd;
9+
float accel;
10+
float fric;
11+
float reload_rate;
1012
};
1113

12-
class Player : public sf::Sprite
13-
{
14-
public:
15-
Player();
16-
Player(sf::Texture& tex, MoveStats* s);
17-
void move(sf::Vector2f velocity, float fr, bool sprint, uint8_t input);
14+
class Player : public Circle {
15+
public:
16+
Player(sf::Texture& tex, MoveStats* s, float r);
17+
void move(sf::Vector2f velocity, float fr, bool sprint, uint8_t input);
18+
void setObjects(std::vector<Object*>* objs);
1819

19-
private:
20-
sf::Vector2f spd_vec;
21-
MoveStats* stat;
20+
private:
21+
std::vector<Object*>* m_objects_ref;
22+
sf::Vector2f m_last_pos;
23+
void checkCollision();
24+
MoveStats* m_stat;
2225
};
2326

2427
#endif

Diff for: include/TileMap.h

+21-18
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
#define TILEMAP_H
33
#include <SFML/Graphics.hpp>
44

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

14-
private:
15-
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
14+
private:
15+
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
1616

17-
sf::VertexArray m_vertices; // The vertex array to draw the tiles onto the view
18-
sf::Vector2u m_chunkSize; // The dimensions of a VertexArray chunk when rendered (in tiles)
19-
sf::Texture m_tileset; // The tileset texture
20-
sf::Vector2u m_tileSize; // The dimensions of each tile (in pixels)
21-
std::vector<int> m_tiles; // The tile map
22-
sf::Vector2u m_mapSize; // The dimensions of the entire tile map (in tiles)
23-
sf::Vector2f m_center; // The center of the view since loadVertexChunk is last called
17+
sf::VertexArray
18+
m_vertices; // The vertex array to draw the tiles onto the view
19+
sf::Vector2u m_chunkSize; // The dimensions of a VertexArray chunk when
20+
// rendered (in tiles)
21+
sf::Texture m_tileset; // The tileset texture
22+
sf::Vector2u m_tileSize; // The dimensions of each tile (in pixels)
23+
std::vector<int> m_tiles; // The tile map
24+
sf::Vector2u m_mapSize; // The dimensions of the entire tile map (in tiles)
25+
sf::Vector2f
26+
m_center; // The center of the view since loadVertexChunk is last called
2427
};
2528

26-
#endif
29+
#endif

Diff for: include/utils/platform_utils.h

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
#ifndef PLATFORM_UTILS_H
1+
#ifndef PLATFORM_UTILS_H
22
#define PLATFORM_UTILS_H
33
#ifdef __APPLE__
44
#include <CoreFoundation/CoreFoundation.h>
55
#endif
66
#include <string>
77

8-
std::string getResourcePath()
9-
{
10-
#ifdef __APPLE__
11-
CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
12-
CFStringRef macPath = CFURLCopyFileSystemPath(appUrlRef, kCFURLPOSIXPathStyle);
13-
const char *pathPtr = CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
14-
std::string path(pathPtr);
15-
std::string resourcePath = path + "/Contents/Resources/";
8+
std::string getResourcePath() {
9+
#ifdef __APPLE__
10+
CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
11+
CFStringRef macPath =
12+
CFURLCopyFileSystemPath(appUrlRef, kCFURLPOSIXPathStyle);
13+
const char *pathPtr =
14+
CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
15+
std::string path(pathPtr);
16+
std::string resourcePath = path + "/Contents/Resources/";
1617

17-
CFRelease(appUrlRef);
18-
CFRelease(macPath);
18+
CFRelease(appUrlRef);
19+
CFRelease(macPath);
1920

20-
return resourcePath;
21-
#elif defined(_WIN32)
22-
return "assets/";
23-
#else
24-
return "assets/";
25-
#endif
21+
return resourcePath;
22+
#elif defined(_WIN32)
23+
return "assets/";
24+
#else
25+
return "assets/";
26+
#endif
2627
}
2728

28-
#endif
29+
#endif

Diff for: include/utils/utils.h

+21-27
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,41 @@
22
#define GLOBAL_H
33
#define _USE_MATH_DEFINES
44
#include <math.h>
5+
56
#include <SFML/Graphics.hpp>
67

78
// Weighted avg between x and y
8-
template<typename T>
9-
T converge(T x, T y, T weight)
10-
{
11-
return (weight * x) + (1 - weight) * y;
9+
template <typename T>
10+
T converge(T x, T y, T weight) {
11+
return (weight * x) + (1 - weight) * y;
1212
}
1313

1414
// Angle of Vector2<T>
15-
template<typename T>
16-
float get_angle(sf::Vector2<T> v)
17-
{
18-
return (atan2f(v.y, v.x) * 180 / M_PI);
15+
template <typename T>
16+
float get_angle(sf::Vector2<T> v) {
17+
return (atan2f(v.y, v.x) * 180 / M_PI);
1918
}
2019

2120
// Length (magnitude) of Vector2<T>
22-
template<typename T>
23-
float len(const sf::Vector2<T> v)
24-
{
25-
return sqrtf(powf(v.x, 2) + powf(v.y, 2));
21+
template <typename T>
22+
float len(const sf::Vector2<T> v) {
23+
return sqrtf(powf(v.x, 2) + powf(v.y, 2));
2624
}
2725

2826
// Helper function keeps x between -180 & 180 degrees
29-
template<typename T>
30-
void normalize(T& x)
31-
{
32-
if (x > 180)
33-
{
34-
x -= 360;
35-
}
36-
if (x <= -180)
37-
{
38-
x += 360;
39-
}
27+
template <typename T>
28+
void normalize(T& x) {
29+
if (x > 180) {
30+
x -= 360;
31+
}
32+
if (x <= -180) {
33+
x += 360;
34+
}
4035
}
4136

42-
template<typename T>
43-
sf::Vector2<T> max(sf::Vector2<T> a, sf::Vector2<T> b)
44-
{
45-
return sf::Vector2<T>(std::max(a.x, b.x), std::max(a.y, b.y));
37+
template <typename T>
38+
sf::Vector2<T> max(sf::Vector2<T> a, sf::Vector2<T> b) {
39+
return sf::Vector2<T>(std::max(a.x, b.x), std::max(a.y, b.y));
4640
}
4741

4842
#endif

0 commit comments

Comments
 (0)