remove unit inheritence, twas a silly idea, and remove usage of shared ptr and unique ptr not needed anymore
This commit is contained in:
parent
0700a13129
commit
0c7721cb17
8 changed files with 101 additions and 142 deletions
|
@ -13,7 +13,6 @@ set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11" )
|
|||
# Add the sources
|
||||
set( SOURCES
|
||||
game.cpp
|
||||
unitv.cpp
|
||||
unit.cpp
|
||||
orders.cpp
|
||||
)
|
||||
|
|
|
@ -64,33 +64,32 @@ int CTTRTSGame::SimulateToNextTurn()
|
|||
}
|
||||
|
||||
// Add a unit, nonzero return value indicates error
|
||||
int CTTRTSGame::AddUnit( std::shared_ptr<CUnit> unit )
|
||||
int CTTRTSGame::AddUnit( CUnit&& unit )
|
||||
{
|
||||
// Verify the unit
|
||||
const int val = unit->valid();
|
||||
const int val = unit.valid();
|
||||
if( val )
|
||||
return val;
|
||||
|
||||
// Verify if the unit can be placed on the current board
|
||||
const uvector2 pos = unit->getPos();
|
||||
const uvector2 pos = unit.getPos();
|
||||
if( (pos.x < dimentions.x) && (pos.y < dimentions.y) )
|
||||
return 1;
|
||||
|
||||
m_allUnits.push_back(unit);
|
||||
|
||||
m_allUnits.push_back(std::move(unit));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Add a units, nonzero return value indicates error
|
||||
int CTTRTSGame::AddUnits( sharedUnitVector_t units )
|
||||
int CTTRTSGame::AddUnits( CUnitVector&& units )
|
||||
{
|
||||
sharedUnitVector_t::iterator it;
|
||||
CUnitVector::iterator it;
|
||||
|
||||
for ( it = units.begin(); it != units.end(); it++ )
|
||||
{
|
||||
// Attempt the unit add
|
||||
if ( AddUnit(*it) )
|
||||
if ( AddUnit( std::move(*it) ) )
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -106,9 +105,9 @@ int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order )
|
|||
|
||||
// Attempt to find the unit
|
||||
bool unitFound = false;
|
||||
for ( sharedUnitVector_t::const_iterator it = m_allUnits.begin(); it != m_allUnits.end(); it++ )
|
||||
for ( CUnitVector::const_iterator it = m_allUnits.begin(); it != m_allUnits.end(); it++ )
|
||||
{
|
||||
if ( (*it)->getID() == unitID )
|
||||
if ( (*it).getID() == unitID )
|
||||
{
|
||||
unitFound = true;
|
||||
break;
|
||||
|
|
10
game/game.h
10
game/game.h
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include <memory> // unique_ptr and shared_ptr
|
||||
|
||||
typedef std::vector< std::shared_ptr<CUnit> > sharedUnitVector_t;
|
||||
typedef std::vector< CUnit > CUnitVector;
|
||||
|
||||
class CTTRTSGame
|
||||
{
|
||||
|
@ -33,16 +33,16 @@ public:
|
|||
int SimulateToNextTurn();
|
||||
|
||||
// Add a unit, nonzero return value indicates error
|
||||
int AddUnit( std::shared_ptr<CUnit> unit );
|
||||
int AddUnit( CUnit&& unit );
|
||||
|
||||
// Add a units, nonzero return value indicates error
|
||||
int AddUnits( sharedUnitVector_t units );
|
||||
int AddUnits( CUnitVector&& units );
|
||||
|
||||
// Get the number of units
|
||||
inline unsigned int GetNumUnits() const { return m_allUnits.size(); }
|
||||
|
||||
// Get unit by index as above (not unit ID)
|
||||
inline const CUnit& GetUnitByIndex( unsigned int i ) const { return *m_allUnits[i]; }
|
||||
inline const CUnit& GetUnitByIndex( unsigned int i ) const { return m_allUnits[i]; }
|
||||
|
||||
// Get the number of order
|
||||
inline unsigned int GetNumOrders() const { return m_orders.size(); }
|
||||
|
@ -68,7 +68,7 @@ private:
|
|||
int VerifyUnit( const CUnit& unit );
|
||||
|
||||
// Vector to store points to all units
|
||||
sharedUnitVector_t m_allUnits;
|
||||
CUnitVector m_allUnits;
|
||||
|
||||
// Orders to execute this turn
|
||||
COrderVector m_orders;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#include "unit.h"
|
||||
|
||||
// Unit types
|
||||
#include "unitv.h"
|
||||
#include <map> // for std::map
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -11,37 +10,83 @@ namespace
|
|||
static unit_id_t p = 0;
|
||||
return p++;
|
||||
}
|
||||
|
||||
// Map of visual representation of unit V
|
||||
typedef std::map< dir_t, unitVis_c > dir_to_vis_map;
|
||||
|
||||
// Helper function to get the vis map during static init
|
||||
const dir_to_vis_map& get_vis_map_V()
|
||||
{
|
||||
static const dir_to_vis_map sk_visMap =
|
||||
{
|
||||
{dir_t::N,'^'},
|
||||
{dir_t::E,'>'},
|
||||
{dir_t::S,'v'},
|
||||
{dir_t::W,'<'},
|
||||
};
|
||||
|
||||
return sk_visMap;
|
||||
}
|
||||
}
|
||||
|
||||
// Plain constructor
|
||||
CUnit::CUnit()
|
||||
: unit_id ( get_unique_unit_id() )
|
||||
, team_id ( team_id_invalid )
|
||||
, player_id ( player_id_invalid )
|
||||
, unit_vis ( unitVis_invalid )
|
||||
, dir ( dir_t::S )
|
||||
{
|
||||
|
||||
updateMyVisual();
|
||||
};
|
||||
|
||||
std::unique_ptr<CUnit> CUnit::getUnitFromVis( unitVis_c vis )
|
||||
// Move constructor
|
||||
CUnit::CUnit(CUnit&& unit)
|
||||
: unit_id ( std::move(unit.unit_id) )
|
||||
, team_id ( std::move(unit.team_id) )
|
||||
, player_id ( std::move(unit.player_id) )
|
||||
, unit_vis ( std::move(unit.unit_vis) )
|
||||
, dir ( std::move(unit.dir) )
|
||||
{
|
||||
switch( vis )
|
||||
{
|
||||
// Match with any image for a V
|
||||
case '^':
|
||||
case '>':
|
||||
case 'v':
|
||||
case '<':
|
||||
{
|
||||
// Create a V
|
||||
std::unique_ptr<CUnit> p = std::unique_ptr<CUnit>(new CUnitV);
|
||||
if( (bool)p && p->setFromVisual(vis) )
|
||||
{
|
||||
return std::move(p);
|
||||
updateMyVisual();
|
||||
}
|
||||
break;
|
||||
|
||||
CUnit&& CUnit::getUnitFromVis( unitVis_c vis )
|
||||
{
|
||||
CUnit unit;
|
||||
unit.setFromVisual(vis);
|
||||
return std::move(unit);
|
||||
}
|
||||
|
||||
// Update the visual representation of the unit
|
||||
unitVis_c CUnit::updateMyVisual()
|
||||
{
|
||||
// Start at invalid
|
||||
setVisual(unitVis_invalid);
|
||||
|
||||
dir_to_vis_map::const_iterator it = get_vis_map_V().find(dir);
|
||||
|
||||
// If found set to new vis
|
||||
if( it != get_vis_map_V().end() )
|
||||
setVisual(it->second);
|
||||
|
||||
return getVisual();
|
||||
}
|
||||
|
||||
bool CUnit::setFromVisual( const unitVis_c& vis )
|
||||
{
|
||||
dir_to_vis_map::const_iterator it;
|
||||
|
||||
for( it = get_vis_map_V().begin(); it != get_vis_map_V().end(); it++ )
|
||||
{
|
||||
if( it->second == vis )
|
||||
{
|
||||
dir == it->first;
|
||||
updateMyVisual();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// No unit found, return nullptr
|
||||
return std::move(std::unique_ptr<CUnit>(nullptr));
|
||||
// No matching direction to visual
|
||||
return false;
|
||||
}
|
19
game/unit.h
19
game/unit.h
|
@ -11,8 +11,10 @@
|
|||
class CUnit
|
||||
{
|
||||
public:
|
||||
virtual ~CUnit() = default;
|
||||
|
||||
CUnit();
|
||||
CUnit(CUnit&& unit);
|
||||
~CUnit() = default;
|
||||
|
||||
inline const unit_id_t& getID() const { return unit_id; }
|
||||
inline const team_id_t& getTeam() const { return team_id; }
|
||||
|
@ -31,19 +33,20 @@ public:
|
|||
inline bool valid() const;
|
||||
|
||||
// Set a unit based solely on it's visual
|
||||
// Maybe make non-virtual at some point to avoid vtable lookups
|
||||
virtual bool setFromVisual(unitVis_c& vis) = 0;
|
||||
bool setFromVisual( const unitVis_c& vis);
|
||||
|
||||
// Factory function for creating units from a visual
|
||||
static std::unique_ptr<CUnit> getUnitFromVis( unitVis_c vis );
|
||||
static CUnit&& getUnitFromVis( unitVis_c vis );
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
// Protected constructor, cannot be constructed as base type
|
||||
CUnit();
|
||||
|
||||
private:
|
||||
|
||||
// Update the visual of V
|
||||
unitVis_c updateMyVisual();
|
||||
|
||||
// Unit ID
|
||||
const unit_id_t unit_id;
|
||||
|
||||
|
@ -56,8 +59,10 @@ private:
|
|||
// Owner ID
|
||||
player_id_t player_id;
|
||||
|
||||
// Direction
|
||||
dir_t dir;
|
||||
|
||||
// All units must have position
|
||||
// Position
|
||||
uvector2 pos;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
#include "unitv.h"
|
||||
|
||||
#include <map> // for std::map
|
||||
|
||||
CUnitV::CUnitV()
|
||||
: dir(dir_t::S)
|
||||
{
|
||||
updateMyVisual();
|
||||
}
|
||||
|
||||
|
||||
// Map of visual representation of unitv
|
||||
namespace
|
||||
{
|
||||
typedef std::map< dir_t, unitVis_c > dir_to_vis_map;
|
||||
|
||||
// Helper function to get the vis map during static init
|
||||
const dir_to_vis_map& get_vis_map()
|
||||
{
|
||||
static const dir_to_vis_map sk_visMap =
|
||||
{
|
||||
{dir_t::N,'^'},
|
||||
{dir_t::E,'>'},
|
||||
{dir_t::S,'v'},
|
||||
{dir_t::W,'<'},
|
||||
};
|
||||
|
||||
return sk_visMap;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the visual representation of the unit
|
||||
unitVis_c CUnitV::updateMyVisual()
|
||||
{
|
||||
// Start at invalid
|
||||
setVisual(unitVis_invalid);
|
||||
|
||||
dir_to_vis_map::const_iterator it = get_vis_map().find(dir);
|
||||
|
||||
// If found set to new vis
|
||||
if( it != get_vis_map().end() )
|
||||
setVisual(it->second);
|
||||
|
||||
return getVisual();
|
||||
}
|
||||
|
||||
bool CUnitV::setFromVisual( unitVis_c& vis )
|
||||
{
|
||||
dir_to_vis_map::const_iterator it;
|
||||
|
||||
for( it = get_vis_map().begin(); it != get_vis_map().end(); it++ )
|
||||
{
|
||||
if( it->second == vis )
|
||||
{
|
||||
dir == it->first;
|
||||
updateMyVisual();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// No matching direction to visual
|
||||
return false;
|
||||
}
|
26
game/unitv.h
26
game/unitv.h
|
@ -1,26 +0,0 @@
|
|||
#ifndef _UNITV_H_
|
||||
#define _UNITV_H_
|
||||
|
||||
#include "unit.h"
|
||||
|
||||
// V unit
|
||||
class CUnitV
|
||||
: public CUnit
|
||||
{
|
||||
public:
|
||||
CUnitV();
|
||||
virtual ~CUnitV() = default;
|
||||
|
||||
// Set from a visual
|
||||
virtual bool setFromVisual( unitVis_c& vis ) override;
|
||||
|
||||
private:
|
||||
|
||||
// Update the visual of V
|
||||
unitVis_c updateMyVisual();
|
||||
|
||||
// V also has a direction
|
||||
dir_t dir;
|
||||
};
|
||||
|
||||
#endif //_UNITV_H_
|
|
@ -1,6 +1,5 @@
|
|||
#include <iostream> // std::cout
|
||||
|
||||
#include "unitv.h"
|
||||
#include "board.h"
|
||||
#include "orders.h"
|
||||
#include "game.h"
|
||||
|
@ -14,21 +13,22 @@ const char* tests()
|
|||
}
|
||||
|
||||
{
|
||||
CUnitV myV;
|
||||
if( myV.getVisual() != 'v' && myV.getVisual() != '<' && myV.getVisual() != '^' && myV.getVisual() != '>' )
|
||||
CUnit unit;
|
||||
unit.setFromVisual('v');
|
||||
if( unit.getVisual() != 118 )
|
||||
return "failed to properly create V unit";
|
||||
}
|
||||
|
||||
{
|
||||
CUnitV myV;
|
||||
CUnitV myV2;
|
||||
if( myV.getID() == myV2.getID() )
|
||||
CUnit unit;
|
||||
CUnit unit2;
|
||||
if( unit.getID() == unit2.getID() )
|
||||
return "Unit IDs the same";
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<CUnit> myV = CUnit::getUnitFromVis('v');
|
||||
if( myV->getVisual() != 'v' )
|
||||
CUnit unit = CUnit::getUnitFromVis('v');
|
||||
if( unit.getVisual() != 'v' )
|
||||
return "failed to properly create V unit";
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue