From d1c62bd7bd0ee06579a445e0b819dffe5ba8d6ce Mon Sep 17 00:00:00 2001 From: Alfred Burgess <alfred.burgess95@gmail.com> Date: Thu, 28 Dec 2023 21:58:12 +0800 Subject: [PATCH] various changes --- include/entities/entity.h | 17 +++++++++++------ include/game-engine.h | 6 +++++- include/logger.h | 2 +- include/maths/vec2.h | 10 +++++++++- src/entity-manager.cpp | 2 +- src/maths/vec2.cpp | 17 +++++++++++++++++ tests/maths/vec2.test.hpp | 33 ++++++++++++++++++++++++++++++++- 7 files changed, 76 insertions(+), 11 deletions(-) diff --git a/include/entities/entity.h b/include/entities/entity.h index 1f84b03..ff450c5 100644 --- a/include/entities/entity.h +++ b/include/entities/entity.h @@ -6,29 +6,34 @@ #include "logger.h" #include "components/cTransform.h" #include "components/cScore.h" +#include "components/cShape.h" +#include "components/cCollision.h" class Entity { - // friend class EntityManager; + friend class EntityManager; public: - Entity( const std::string& tag, size_t id ); /** @todo make this private */ ~Entity(); - bool isAlive() { return this->_alive; } - bool isDead() { return ! this->_alive; } - void kill() { this->_alive = false; } + bool isActive() { return this->_active; } + bool isDestroyed() { return ! this->_active; } + void destroy() { this->_active = false; } + const size_t id() const { return this->_id; } // Component Variables const std::string& tag() { return this->_tag; } private: + Entity( const std::string& tag, size_t id ); /** @todo make this private */ Logger& _logger; cScore _score; cTransform _transform; + cShape _shape = {0}; + cCollision _collision; + bool _active = true; const size_t _id = 0; const std::string _tag = "Default"; - bool _alive = true; }; diff --git a/include/game-engine.h b/include/game-engine.h index e3d7fbc..e45cccf 100644 --- a/include/game-engine.h +++ b/include/game-engine.h @@ -46,6 +46,7 @@ class GameEngine EntityManager _entityManager; Logger& _logger; + bool _paused = false; Window* _window; std::vector<Asset> _assets; /** @todo : move this to scene */ @@ -54,7 +55,10 @@ class GameEngine std::map< std::string, Scene* > _scenes; /* Systems */ - // sUserInput + // void sMovement(); + // void sUserInput(); + // void sRender(); + // void sCollision(); }; #endif // __GAME_H__ diff --git a/include/logger.h b/include/logger.h index 333e5bd..685ae94 100644 --- a/include/logger.h +++ b/include/logger.h @@ -1,4 +1,4 @@ -#ifndef UTILS_LOGGER_H + #ifndef UTILS_LOGGER_H #define UTILS_LOGGER_H #include <string> diff --git a/include/maths/vec2.h b/include/maths/vec2.h index e750bb4..7a19220 100644 --- a/include/maths/vec2.h +++ b/include/maths/vec2.h @@ -12,15 +12,23 @@ class Vec2 Vec2( float xIn, float yIn ); bool operator == ( const Vec2 rhs ) const; + bool operator != ( const Vec2 rhs ) const; + Vec2 operator + ( const Vec2 rhs ) const; - Vec2 operator * ( const Vec2 rhs ) const; Vec2 operator - ( const Vec2 rhs ) const; + Vec2 operator * ( const Vec2 rhs ) const; Vec2 operator * ( const float scale ) const; + Vec2 operator / ( const Vec2 rhs ) const; + Vec2 operator / ( const float scale ) const; + void operator += ( const Vec2 & rhs ); void operator -= ( const Vec2 & rhs ); void operator *= ( const Vec2 & rhs ); void operator *= ( const float rhs ); + void operator /= ( const Vec2 & rhs ); + void operator /= ( const float rhs ); + float dist( const Vec2 &rhs ) const; float length(); void normalise(); }; diff --git a/src/entity-manager.cpp b/src/entity-manager.cpp index 0c098dd..c568bf7 100644 --- a/src/entity-manager.cpp +++ b/src/entity-manager.cpp @@ -26,7 +26,7 @@ void EntityManager::update() { for( auto entity : this->_entities ) { - if( entity->isDead() ) + if( entity->isDestroyed() ) { /** @todo Implement this with a clean deletion taking into account iterator invalidation */ diff --git a/src/maths/vec2.cpp b/src/maths/vec2.cpp index aee6719..b411160 100644 --- a/src/maths/vec2.cpp +++ b/src/maths/vec2.cpp @@ -6,6 +6,13 @@ Vec2::Vec2( float xIn, float yIn ) : x( xIn ), y( yIn ) {}; +bool Vec2::operator != ( const Vec2 rhs ) const { + return !( + abs( this->x - rhs.x ) < 0.01 + ) && ( + abs( this->y - rhs.y ) < 0.01 + ); +} bool Vec2::operator == ( const Vec2 rhs ) const { return ( abs( this->x - rhs.x ) < 0.01 @@ -16,12 +23,22 @@ bool Vec2::operator == ( const Vec2 rhs ) const { Vec2 Vec2::operator + ( const Vec2 rhs ) const { return Vec2( this->x + rhs.x, this->y + rhs.y ); } Vec2 Vec2::operator - ( const Vec2 rhs ) const { return Vec2( this->x - rhs.x, this->y - rhs.y ); } Vec2 Vec2::operator * ( const Vec2 rhs ) const { return Vec2( this->x * rhs.x, this->y * rhs.y ); } +Vec2 Vec2::operator / ( const Vec2 rhs ) const { return Vec2( this->x / rhs.x, this->y / rhs.y ); } Vec2 Vec2::operator * ( const float scale ) const { return Vec2( this->x * scale, this->y * scale ); } +Vec2 Vec2::operator / ( const float scale ) const { return Vec2( this->x / scale, this->y / scale ); } void Vec2::operator += ( const Vec2 & rhs ) { this->x += rhs.x; this->y += rhs.y; } void Vec2::operator -= ( const Vec2 & rhs ) { this->x -= rhs.x; this->y -= rhs.y; } void Vec2::operator *= ( const Vec2 & rhs ) { this->x *= rhs.x; this->y *= rhs.y; } void Vec2::operator *= ( const float scale ) { this->x *= scale; this->y *= scale; } +void Vec2::operator /= ( const Vec2 & rhs ) { this->x /= rhs.x; this->y /= rhs.y; } +void Vec2::operator /= ( const float scale ) { this->x /= scale; this->y /= scale; } +float Vec2::dist( const Vec2 &rhs ) const +{ + float x = this->x - rhs.x; + float y = this->y - rhs.y; + return sqrtf( x*x + y*y ); +} float Vec2::length() { return sqrtf( this->x*this->x + this->y*this->y ); } void Vec2::normalise() diff --git a/tests/maths/vec2.test.hpp b/tests/maths/vec2.test.hpp index babe35e..adfe210 100644 --- a/tests/maths/vec2.test.hpp +++ b/tests/maths/vec2.test.hpp @@ -5,8 +5,13 @@ #include "maths/vec2.h" TEST_CASE( "Testing Operator Comparisons" ) { + float epsilon = 1e-10; + Vec2 a( 1, 5 ); Vec2 b( 2, 4 ); + float scale_a = 100; + float scale_b = 0.5; + float scale_c = 0; SUBCASE( "Addition" ) { @@ -14,10 +19,36 @@ TEST_CASE( "Testing Operator Comparisons" ) { CHECK( c.x == 3 ); CHECK( c.y == 9 ); } + SUBCASE( "Multiplication" ) + { + Vec2 c = a * b; + CHECK( c.x == 2 ); + CHECK( c.y == 20 ); + + Vec2 d = c * c; + CHECK( d.x == 4 ); + CHECK( d.y == 400 ); + } + SUBCASE( "Division" ) + { + Vec2 c = a / b; + Vec2 d = b / a; + + CHECK( abs( c.x - (1.0 / 2.0) ) < epsilon ); + CHECK( abs( c.y - (5.0 / 4.0) ) < epsilon ); + CHECK( abs( d.x - (2.0) ) < epsilon ); + CHECK( abs( d.y - (4.0 / 5.0) ) < epsilon ); + } SUBCASE( "Subtraction" ) { Vec2 c = a - b; CHECK( c.x == -1 ); CHECK( c.y == 1 ); } -} + SUBCASE( "Division" ) + { + Vec2 c = a * scale_c; + CHECK( c.x == 0.0 ); + CHECK( c.y == 0.0 ); + } +} \ No newline at end of file -- GitLab