Various work towards a basic game implementation
Get rid of some virtual functions that weren't needed. Fix some functions to work during static init if need be. Units now have unique incremental IDs
This commit is contained in:
parent
a6e1319fad
commit
008739dee6
6 changed files with 135 additions and 40 deletions
|
@ -66,24 +66,35 @@ int CTTRTSGame::SimulateToNextTurn()
|
|||
// Add a unit, nonzero return value indicates error
|
||||
int CTTRTSGame::AddUnit( std::shared_ptr<CUnit> unit )
|
||||
{
|
||||
// Verify the unit
|
||||
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();
|
||||
if( (pos.x < cols) && (pos.y < rows) )
|
||||
return 1;
|
||||
|
||||
m_allUnits.push_back(unit);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Add a units, nonzero return value indicates error
|
||||
int CTTRTSGame::AddUnits( sharedUnitVector_t units )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
sharedUnitVector_t::iterator it;
|
||||
|
||||
// Simulate all movements
|
||||
int CTTRTSGame::SimulateMovements()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
for ( it = units.begin(); it != units.end(); it++ )
|
||||
{
|
||||
// Attempt the unit add
|
||||
if ( AddUnit(*it) )
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Simulate all actions
|
||||
int CTTRTSGame::SimulateActions()
|
||||
{
|
||||
// All units added successfully
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -107,3 +118,15 @@ int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order )
|
|||
// for now, as long as the unit exists we can attempt the order
|
||||
return unitFound;
|
||||
}
|
||||
|
||||
// Simulate all movements
|
||||
int CTTRTSGame::SimulateMovements()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Simulate all actions
|
||||
int CTTRTSGame::SimulateActions()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -62,6 +62,9 @@ private:
|
|||
// Verify any order
|
||||
int VerifyOrder( player_id_t player, const COrder& order );
|
||||
|
||||
// Verify any order
|
||||
int VerifyUnit( const CUnit& unit );
|
||||
|
||||
// Vector to store points to all units
|
||||
sharedUnitVector_t m_allUnits;
|
||||
|
||||
|
|
|
@ -3,6 +3,25 @@
|
|||
// Unit types
|
||||
#include "unitv.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
// Helper function for generating unique unit ids during static init
|
||||
unit_id_t get_unique_unit_id()
|
||||
{
|
||||
static unit_id_t p = 0;
|
||||
return p++;
|
||||
}
|
||||
}
|
||||
|
||||
CUnit::CUnit()
|
||||
: unit_id ( get_unique_unit_id() )
|
||||
, team_id ( team_id_invalid )
|
||||
, player_id ( player_id_invalid )
|
||||
, unit_vis ( unitVis_invalid )
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
std::unique_ptr<CUnit> CUnit::getUnitFromVis( unitVis_c vis )
|
||||
{
|
||||
switch( vis )
|
||||
|
|
44
game/unit.h
44
game/unit.h
|
@ -13,29 +13,61 @@ class CUnit
|
|||
public:
|
||||
virtual ~CUnit() = default;
|
||||
|
||||
virtual unitVis_c getVisual() const = 0;
|
||||
|
||||
inline const unit_id_t& getID() const { return unit_id; }
|
||||
inline const team_id_t& getTeam() const { return team_id; }
|
||||
inline const player_id_t& getPlayer() const { return player_id; }
|
||||
inline const unitVis_c& getVisual() const { return unit_vis; }
|
||||
|
||||
// Return non-zero values on error
|
||||
inline int setTeam(const team_id_t& v) { return (v == team_id_invalid) ? -1 : (( team_id = v ), 0); }
|
||||
inline int setPlayer(const player_id_t& v) { return (v == player_id_invalid) ? -1 : (( player_id = v ), 0); }
|
||||
inline int setVisual(const unitVis_c& v) { return (v == unitVis_invalid) ? -1 : (( unit_vis = v ), 0); }
|
||||
|
||||
inline const uvector2& getPos() const { return pos; }
|
||||
inline void setPos(const uvector2& v) { pos = v; }
|
||||
|
||||
// Check unit is valid
|
||||
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;
|
||||
|
||||
// Factory function for creating units from a visual
|
||||
static std::unique_ptr<CUnit> getUnitFromVis( unitVis_c vis );
|
||||
|
||||
inline unit_id_t getID() const { return id; }
|
||||
|
||||
protected:
|
||||
CUnit() = default;
|
||||
|
||||
// Protected constructor, cannot be constructed as base type
|
||||
CUnit();
|
||||
|
||||
private:
|
||||
|
||||
// Unit ID
|
||||
unit_id_t id;
|
||||
const unit_id_t unit_id;
|
||||
|
||||
// Visual
|
||||
unitVis_c unit_vis;
|
||||
|
||||
// Team ID
|
||||
team_id_t team_id;
|
||||
|
||||
// Owner ID
|
||||
player_id_t owner_id;
|
||||
player_id_t player_id;
|
||||
|
||||
|
||||
// All units must have position
|
||||
uvector2 pos;
|
||||
};
|
||||
|
||||
// Simple validation
|
||||
inline bool CUnit::valid() const
|
||||
{
|
||||
return (unit_id != unit_id_invalid )
|
||||
&& (team_id != team_id_invalid )
|
||||
&& (player_id != player_id_invalid)
|
||||
&& (unit_vis != unitVis_invalid);
|
||||
}
|
||||
|
||||
#endif //_UNIT_H_
|
|
@ -5,40 +5,55 @@
|
|||
CUnitV::CUnitV()
|
||||
: dir(dir_t::S)
|
||||
{
|
||||
|
||||
updateMyVisual();
|
||||
}
|
||||
|
||||
|
||||
// Map of visual representation of unitv
|
||||
static const std::map< dir_t, unitVis_c > sk_visMap =
|
||||
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,'<'},
|
||||
};
|
||||
};
|
||||
|
||||
unitVis_c CUnitV::getVisual() const
|
||||
{
|
||||
std::map< dir_t, char >::const_iterator it = sk_visMap.find(dir);
|
||||
|
||||
if( it == sk_visMap.end() )
|
||||
{
|
||||
return 0;
|
||||
return sk_visMap;
|
||||
}
|
||||
}
|
||||
|
||||
return it->second;
|
||||
// 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 )
|
||||
{
|
||||
std::map< dir_t, char >::const_iterator it;
|
||||
dir_to_vis_map::const_iterator it;
|
||||
|
||||
for( it = sk_visMap.begin(); it != sk_visMap.end(); it++ )
|
||||
for( it = get_vis_map().begin(); it != get_vis_map().end(); it++ )
|
||||
{
|
||||
if( it->second == vis )
|
||||
{
|
||||
dir == it->first;
|
||||
updateMyVisual();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,11 +11,14 @@ public:
|
|||
CUnitV();
|
||||
virtual ~CUnitV() = default;
|
||||
|
||||
virtual unitVis_c getVisual() const override;
|
||||
// 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;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue