diff --git a/include/entities/entity.h b/include/entities/entity.h index 1f84b03296775cacd3e5323b8ae70eadfd64b98b..ff450c547cc399aa2f79c4fc58f4d9c24218120b 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 e3d7fbcef96cfe5c760ad8aef471f9c172ba2039..e45cccfb9bdf4a48091e2929121323b6c8a9d8e9 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 333e5bdf7208f75c7bd0451c1dca0663794376bc..685ae94012d4999df80c2525493714ce01cc0d08 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 e750bb4d992177674fbd06e884b3da9b9af43a2d..7a19220e0e8c6f2560ce4eeffd42d3a0fc31eefe 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 0c098ddec20157d13dc816998a7d08e3a5c209a4..c568bf7203e93ee0b4e1273a8458bacc75e7b43e 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 aee6719eca9998cfe71c7094beeecd8925494870..b41116002cf938574c104c44b45dffb553703c8b 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 babe35eda721897423926b8c26ae05c25a438192..adfe21053ea5796df2c7e52f793ac917adf3187b 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