Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (6)
#ifndef __COMPONENT__COLLISION_H__
#define __COMPONENT__COLLISION_H__
#include "components/components-generic.h"
#include "maths/maths.h"
class cCollision : public CComponentGeneric
{
public:
double radius = 0.0;
cCollision(){}
cCollision( float rad )
: radius( rad ) {}
};
#endif // __COMPONENT__COLLISION_H__
\ No newline at end of file
#ifndef __COMPONENT__INPUT_H__
#define __COMPONENT__INPUT_H__
#include "components/components-generic.h"
#include "maths/maths.h"
class cInput : public CComponentGeneric
{
public:
bool up = false;
bool down = false;
bool left = false;
bool right = false;
bool shoot = false;
};
#endif // __COMPONENT__INPUT_H__
#ifndef __COMPONENT__POINT_BAR_H__
#define __COMPONENT__POINT_BAR_H__
#include "components/components-generic.h"
#include "maths/maths.h"
class cPointBar : public CComponentGeneric
{
public:
cPointBar( uint16_t value )
: _total(value), _current(value) {}
private:
uint16_t _total;
uint16_t _current;
};
#endif // __COMPONENT__POINT_BAR_H__
#ifndef __COMPONENT__SHAPE_H__
#define __COMPONENT__SHAPE_H__
#include "components/components-generic.h"
#include "maths/maths.h"
class cShape : public CComponentGeneric
{
public:
cShape( uint8_t verticies )
: _verticies( verticies ) {}
private:
uint8_t _verticies;
};
#endif // __COMPONENT__SHAPE_H__
......@@ -7,10 +7,14 @@
class cTransform : public CComponentGeneric
{
public:
Vec2 position;
Vec2 velocity;
Vec2 scale;
double angle;
Vec2 position = { 0.0, 0.0 };
Vec2 velocity = { 0.0, 0.0 };
Vec2 scale = { 0.0, 0.0 };
double angle = 0.0;
cTransform(){}
cTransform( const Vec2 &pos, const Vec2 &vel, float ang )
: position(pos), velocity(vel), angle(ang) {}
};
......
......@@ -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;
};
......
......@@ -14,6 +14,7 @@
#include "entity-manager.h"
#include "scene.h"
#include "asset.h"
#include "save-manager.h"
// #include "window.h"
#ifdef console
......@@ -41,11 +42,13 @@ class GameEngine
bool isRunning;
void handleInput();
private:
SaveManager _saveManager;
size_t _frame = 0;
std::time_t _frametime = 0;
EntityManager _entityManager;
Logger& _logger;
bool _paused = false;
Window* _window;
std::vector<Asset> _assets; /** @todo : move this to scene */
......@@ -54,7 +57,10 @@ class GameEngine
std::map< std::string, Scene* > _scenes;
/* Systems */
// sUserInput
// void sMovement();
// void sUserInput();
// void sRender();
// void sCollision();
};
#endif // __GAME_H__
#ifndef UTILS_LOGGER_H
#ifndef UTILS_LOGGER_H
#define UTILS_LOGGER_H
#include <string>
......
......@@ -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();
};
......
......@@ -10,6 +10,7 @@ else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
CXX = g++
# CXX = clang
CXXFLAGS = -std=c++17 -Wall -Wreorder
RM = rm -f
endif
......@@ -20,6 +21,8 @@ endif
# DEBUG BUILD
CXXFLAGS += -g --coverage -O0
# CXXFLAGS += -fanalyzer
# CXXFLAGS += -Xanalyzer
# Linker Flags
LDFLAGS =
......@@ -66,11 +69,14 @@ TEST_SRC_OBJ_FILES = $(patsubst $(SRC_DIR)/%.cpp,$(TEST_BUILD_DIR)/%.src.o,$(SRC
# Output executable
TARGET = $(BIN_DIR)/game
all: $(TARGET)
# all: $(TARGET) $(TEST_TARGET)
all: $(TARGET) test
test: $(TEST_TARGET)
./$(TEST_TARGET)
game: $(TARGET)
$(TARGET): $(OBJ_FILES)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJ_FILES) $(SDL_LIBRARY) $(SDL_OPTIONS)
......@@ -95,5 +101,7 @@ clean:
rm -rf ./*~ ./*.swp ./*.gcno
rm -rf *~
remake: clean $(TARGET)
run: $(TARGET)
./$(TARGET)
......@@ -15,7 +15,7 @@ std::string ConfigParser::get( std::string key )
std::string ConfigParser::toString() const
{
std::string str = "Config File: "+this->_path+"\n";
for( const auto [key, value] : this->_config )
for( const auto& [key, value] : this->_config )
{ str += "["+key+"] "+value+"\n"; }
return str;
}
......
......@@ -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
*/
......@@ -40,6 +40,7 @@ void EntityManager::update()
this->_entityMap[entity->tag()].push_back( entity );
}
this->_entitiesToAdd.clear();
return;
}
void EntityManager::removeEntity(){}
......@@ -15,6 +15,7 @@ void GameEngine::init()
{
this->isRunning = false;
this->_currScene = "";
// this->_saveManager.list();
}
void GameEngine::changeScene( std::string label, Scene* scene )
{
......@@ -31,8 +32,8 @@ void GameEngine::run()
{
this->isRunning = true;
this->_frame = 0;
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t _frametime = std::chrono::system_clock::to_time_t(now);
// std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
// std::time_t _frametime = std::chrono::system_clock::to_time_t(now);
while( this->isRunning ) { this->update(); }
}
......
......@@ -21,13 +21,19 @@ Logger::~Logger(){ this->info( "Closing Logger System" ); }
void Logger::log(LogLevel level, const std::string& message)
{
#ifdef __COLOUR_CONSOLE
std::string levelColour = this->getLevelColour(level);
if( level >= currentLogLevel )
{
std::cout << "["<<BLK<<WHTB<<this->getCurrentTimestamp()<<CRESET<<"] "
<< levelColour << message << CRESET << std::endl;
}
#else // __COLOUR_CONSOLE
if( level >= currentLogLevel )
{ std::cout << "[" << this->getCurrentTimestamp() << "] " << message << std::endl; }
#endif // __COLOUR_CONSOLE
}
std::string Logger::logLevelToString( LogLevel level )
{
switch(level)
......
......@@ -3,56 +3,54 @@
#ifndef __TEST_RUNNER__
int main( int argc, char* argv[] ) {
const uint16_t SCREEN_HEIGHT = 600;
const uint16_t SCREEN_WIDTH = 800;
// const uint16_t SCREEN_HEIGHT = 600;
// const uint16_t SCREEN_WIDTH = 800;
GameState state = Uninitialised;
// GameState state = Uninitialised;
// Initialise
Logger& logger = Logger::instance();
int tickCount = 0;
int _frame = 0;
SaveManager saveManager;
state = Initialise;
// SaveManager saveManager;
// state = Initialise;
ConfigParser userSettings = ConfigParser::UserSettings();
// ConfigParser userSettings = ConfigParser::UserSettings();
EntityManager entityManager;
// EntityManager entityManager;
// Seed the random number generator
srand(static_cast<unsigned int>(time(nullptr)));
std::vector< Entity* > entities;
// std::vector< Entity* > entities;
// Genrate a Random chunk
WorldHex2D map( 5 );
// WorldHex2D map( 5 );
//Chunk* chunk = new Chunk( 0, 0, 0);
//std::cout << chunk.getString() << std::endl;
// Chunk* chunk = map.getMidChunk();
// std::cout << chunk->getString() << std::endl;
map.showChunk( 0, 0, 0 );
map.moveInto( -2, 1, 1 );
map.moveInto( -3, 2, 1 );
map.moveInto( 5, -2, -3 )->visualise();
map.visualise();
// map.showChunk( 0, 0, 0 );
// map.moveInto( -2, 1, 1 );
// map.moveInto( -3, 2, 1 );
// map.moveInto( 5, -2, -3 )->visualise();
// map.visualise();
// Menu Loop
// List Saves
saveManager.list();
// saveManager.list();
GameEngine* game = new GameEngine();
game->run();
userSettings.set( "exit", "teset" );
std::cout << userSettings;
// userSettings.set( "exit", "teset" );
// std::cout << userSettings;
// Clean-up
while( entities.size() > 0 )
{
Entity* entity = entities.back();
entities.pop_back();
delete entity;
}
// while( entities.size() > 0 )
// {
// Entity* entity = entities.back();
// entities.pop_back();
// delete entity;
// }
delete game;
// Return
......
......@@ -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()
......
......@@ -46,10 +46,10 @@ void SaveManager::findSaves()
return;
}
for( const auto& entry : std::filesystem::directory_iterator( SAVE_LOCATION ) )
{
// saves.push_back( SaveItem(entry.path().filename() ) );
}
// for( const auto& entry : std::filesystem::directory_iterator( SAVE_LOCATION ) )
// {
// saves.push_back( SaveItem(entry.path().filename() ) );
// }
this->saves = saves;
}
......
......@@ -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