PGE API 0.4
PR00F's Game Engine full documentation
Loading...
Searching...
No Matches
WeaponManager Class Reference

PR00F's Game Engine Weapon Manager. More...

Detailed Description

PR00F's Game Engine Weapon Manager.

The idea is that Weapon instances are created and stored by this manager. Each player has a different instance of Weapon Manager. Thus, for each player, Weapon instances also need to be created. This way, each player can utilize convenient manager functions, such as:

Definition at line 550 of file WeaponManager.h.

#include <WeaponManager.h>

Public Types

typedef std::map< unsigned char, std::string > KeypressToWeaponMap
 

Public Member Functions

 WeaponManager (pge_audio::PgeAudio &audio, PGEcfgProfiles &cfgProfiles, PR00FsUltimateRenderingEngine &gfx, PgeObjectPool< PooledBullet > &bullets)
 
virtual ~WeaponManager ()
 
CConsole & getConsole () const
 Returns access to console preset with logger module name as this class.
 
Weaponload (const char *fname, pge_network::PgeNetworkConnectionHandle connHandle)
 Loads the given weapon file and returns the created Weapon instance.
 
const std::vector< Weapon * > & getWeapons () const
 
WeapongetWeaponById (const WeaponId &id)
 
const WeapongetWeaponById (const WeaponId &id) const
 
WeapongetWeaponByFilename (const std::string &wpnName)
 
const WeapongetWeaponByFilename (const std::string &wpnName) const
 
const std::string & getDefaultAvailableWeaponFilename () const
 
bool setDefaultAvailableWeaponByFilename (const std::string &sFilename)
 Sets default available weapon that should be by default available for any new/respawning player.
 
const WeapongetCurrentWeapon () const
 Gets the current weapon.
 
WeapongetCurrentWeapon ()
 Gets the current weapon.
 
bool setCurrentWeapon (Weapon *wpn, bool bRecordSwitchTime, bool bServer)
 Sets the current weapon.
 
WeapongetPrevAvailableWeapon (unsigned char &cTargetWeapon)
 Gets the previous available weapon.
 
const WeapongetPrevAvailableWeapon (unsigned char &cTargetWeapon) const
 Gets the previous available weapon.
 
WeapongetNextAvailableWeapon (unsigned char &cTargetWeapon)
 Gets the next available weapon.
 
const WeapongetNextAvailableWeapon (unsigned char &cTargetWeapon) const
 Gets the next available weapon.
 
WeapongetNextBestAvailableWeapon (unsigned char &cTargetWeapon, bool bMustHaveMagBullet)
 Gets the next best available weapon.
 
const WeapongetNextBestAvailableWeapon (unsigned char &cTargetWeapon, bool bMustHaveMagBullet) const
 Gets the next best available weapon.
 
const std::chrono::time_point< std::chrono::steady_clock > & getTimeLastWeaponSwitch () const
 
void Clear ()
 Deletes all loaded weapons, sets current weapon to null and clears default available weapon and last weapon switch timestamp.
 
PgeObjectPool< PooledBullet > & getBullets ()
 

Static Public Member Functions

static const char * getLoggerModuleName ()
 Returns the logger module name of this class.
 
static KeypressToWeaponMapgetKeypressToWeaponMap ()
 Returns the only instance of KeypressToWeaponMap.
 

Protected Member Functions

 WeaponManager (const WeaponManager &)
 
WeaponManageroperator= (const WeaponManager &)
 

Private Member Functions

 WeaponManager ()
 

Private Attributes

pge_audio::PgeAudiom_audio
 
PGEcfgProfilesm_cfgProfiles
 
PR00FsUltimateRenderingEngine & m_gfx
 
std::vector< Weapon * > m_weapons
 
Weaponm_pCurrentWpn
 
std::chrono::time_point< std::chrono::steady_clock > m_timeLastWeaponSwitch
 
PgeObjectPool< PooledBullet > & m_bullets
 
std::string m_sDefaultAvailableWeapon
 

Static Private Attributes

static KeypressToWeaponMap m_mapKeypressToWeapon
 

Member Typedef Documentation

◆ KeypressToWeaponMap

std::map<unsigned char, std::string> WeaponManager::KeypressToWeaponMap

Definition at line 557 of file WeaponManager.h.

Constructor & Destructor Documentation

◆ WeaponManager() [1/3]

WeaponManager::WeaponManager ( pge_audio::PgeAudio & audio,
PGEcfgProfiles & cfgProfiles,
PR00FsUltimateRenderingEngine & gfx,
PgeObjectPool< PooledBullet > & bullets )
Parameters
bulletsA bullet pool for storing the bullets that are fired by Weapon instances created by this WeaponManager instance.

Definition at line 1718 of file WeaponManager.cpp.

◆ ~WeaponManager()

WeaponManager::~WeaponManager ( )
virtual

Definition at line 1732 of file WeaponManager.cpp.

◆ WeaponManager() [2/3]

WeaponManager::WeaponManager ( const WeaponManager & )
inlineprotected

Definition at line 606 of file WeaponManager.h.

◆ WeaponManager() [3/3]

WeaponManager::WeaponManager ( )
private

Member Function Documentation

◆ Clear()

void WeaponManager::Clear ( )

Deletes all loaded weapons, sets current weapon to null and clears default available weapon and last weapon switch timestamp.

Definition at line 2235 of file WeaponManager.cpp.

◆ getBullets()

PgeObjectPool< PooledBullet > & WeaponManager::getBullets ( )
Returns
Reference to the list storing bullets created by firing any weapon managed by this WeaponManager instance.

Definition at line 2250 of file WeaponManager.cpp.

◆ getConsole()

CConsole & WeaponManager::getConsole ( ) const

Returns access to console preset with logger module name as this class.

Definition at line 1737 of file WeaponManager.cpp.

◆ getCurrentWeapon() [1/2]

Weapon * WeaponManager::getCurrentWeapon ( )

Gets the current weapon.

Useful for a player object for tracking player's current weapon. Note that WeaponManager doesn't know about any player entity, you need to connect this logic with your player entity.

Returns
Current weapon. Can be null if setCurrentWeapon() was not yet called.

Definition at line 1886 of file WeaponManager.cpp.

◆ getCurrentWeapon() [2/2]

const Weapon * WeaponManager::getCurrentWeapon ( ) const

Gets the current weapon.

Useful for a player object for tracking player's current weapon. Note that WeaponManager doesn't know about any player entity, you need to connect this logic with your player entity.

Returns
Current weapon. Can be null if setCurrentWeapon() was not yet called.

Definition at line 1874 of file WeaponManager.cpp.

◆ getDefaultAvailableWeaponFilename()

const std::string & WeaponManager::getDefaultAvailableWeaponFilename ( ) const
Returns
Default available weapon that was previously set by setDefaultAvailableWeaponByFilename().

Definition at line 1846 of file WeaponManager.cpp.

◆ getKeypressToWeaponMap()

WeaponManager::KeypressToWeaponMap & WeaponManager::getKeypressToWeaponMap ( )
static

Returns the only instance of KeypressToWeaponMap.

This map allows the user to define keyboard key mapping to weapons. Currently it is still the user's responsibility to handle these keypresses in game code, however this map is also used by WeaponManager's getPrev/NextWeapon() functions since this map defines the desired logical order of the weapons. Note that this logical order is independent from the power order which is based on the DPFR value of weapons.

Definition at line 1755 of file WeaponManager.cpp.

◆ getLoggerModuleName()

const char * WeaponManager::getLoggerModuleName ( )
static

Returns the logger module name of this class.

Definition at line 1742 of file WeaponManager.cpp.

◆ getNextAvailableWeapon() [1/2]

Weapon * WeaponManager::getNextAvailableWeapon ( unsigned char & cTargetWeapon)

Gets the next available weapon.

Typically you need this function when the user is browsing their weapons forward e.g. scrolling mousewheel forward.

Parameters
cTargetWeaponOutput argument, the key associated to the next available weapon in KeypressToWeaponMap will be set in this output argument. Will be set to '\0' (null-char) if no current weapon is set or the file name of the current weapon is not present in KeypressToWeaponMap. Will be set to the key of the current weapon if we could not find another available weapon.
Returns
The next available weapon based on logical order defined by KeypressToWeaponMap. Nullptr if there is no current weapon set or the file name of the current weapon is not present in KeypressToWeaponMap. Same weapon as current weapon if we could not find another available weapon. Note that owner of weapon is not taken into consideration, because the idea is that each player entity has their own WeaponManager, storing only their owned weapons.

Definition at line 2051 of file WeaponManager.cpp.

◆ getNextAvailableWeapon() [2/2]

const Weapon * WeaponManager::getNextAvailableWeapon ( unsigned char & cTargetWeapon) const

Gets the next available weapon.

Typically you need this function when the user is browsing their weapons forward e.g. scrolling mousewheel forward.

Parameters
cTargetWeaponOutput argument, the key associated to the next available weapon in KeypressToWeaponMap will be set in this output argument. Will be set to '\0' (null-char) if no current weapon is set or the file name of the current weapon is not present in KeypressToWeaponMap. Will be set to the key of the current weapon if we could not find another available weapon.
Returns
The next available weapon based on logical order defined by KeypressToWeaponMap. Nullptr if there is no current weapon set or the file name of the current weapon is not present in KeypressToWeaponMap. Same weapon as current weapon if we could not find another available weapon. Note that owner of weapon is not taken into consideration, because the idea is that each player entity has their own WeaponManager, storing only their owned weapons.

Definition at line 2070 of file WeaponManager.cpp.

◆ getNextBestAvailableWeapon() [1/2]

Weapon * WeaponManager::getNextBestAvailableWeapon ( unsigned char & cTargetWeapon,
bool bMustHaveMagBullet )

Gets the next best available weapon.

Typically you need this when you run out of ammo of your current weapon and want to switch to another most useful weapon. Unlike the getPrev/NextAvailableWeapon() functions, here we follow the power order of available weapons defined by their DPSR value, and we don't consider weapons for which we don't have any kind (mag and unmag) ammo.

Parameters
cTargetWeaponOutput-only argument, the key associated to the next best available weapon. Set to '\0' in case of error. Set to key associated to current weapon if we could not find a best next available weapon. Set to key associated to the found next best available weapon if we found one.
bMustHaveMagBulletIf true, only weapons with non-zero mag bullet count will be considered.
Returns
The next best available weapon based on power order defined by DPSR value of weapons. Nullptr if there is no current weapon set or the file name of the next best available weapon is not present in KeypressToWeaponMap. Same weapon as current weapon if we could not find a best next available weapon. Note that owner of weapon is not taken into consideration, because the idea is that each player entity has their own WeaponManager, storing only their owned weapons.

Definition at line 2146 of file WeaponManager.cpp.

◆ getNextBestAvailableWeapon() [2/2]

const Weapon * WeaponManager::getNextBestAvailableWeapon ( unsigned char & cTargetWeapon,
bool bMustHaveMagBullet ) const

Gets the next best available weapon.

Typically you need this when you run out of ammo of your current weapon and want to switch to another most useful weapon. Unlike the getPrev/NextAvailableWeapon() functions, here we follow the power order of available weapons defined by their DPSR value, and we don't consider weapons for which we don't have any kind (mag and unmag) ammo.

Parameters
cTargetWeaponOutput-only argument, the key associated to the next best available weapon. Set to '\0' in case of error. Set to key associated to current weapon if we could not find a best next available weapon. Set to key associated to the found next best available weapon if we found one.
bMustHaveMagBulletIf true, only weapons with non-zero mag bullet count will be considered.
Returns
The next best available weapon based on power order defined by DPSR value of weapons. Nullptr if there is no current weapon set or the file name of the next best available weapon is not present in KeypressToWeaponMap. Same weapon as current weapon if we could not find a best next available weapon. Note that owner of weapon is not taken into consideration, because the idea is that each player entity has their own WeaponManager, storing only their owned weapons.

Definition at line 2169 of file WeaponManager.cpp.

◆ getPrevAvailableWeapon() [1/2]

Weapon * WeaponManager::getPrevAvailableWeapon ( unsigned char & cTargetWeapon)

Gets the previous available weapon.

Typically you need this function when the user is browsing their weapons backwards e.g. scrolling mousewheel backward.

Parameters
cTargetWeaponOutput argument, the key associated to the previous available weapon in KeypressToWeaponMap will be set in this output argument. Will be set to '\0' (null-char) if no current weapon is set or the file name of the current weapon is not present in KeypressToWeaponMap. Will be set to the key of the current weapon if we could not find another available weapon.
Returns
The previous available weapon based on logical order defined by KeypressToWeaponMap. Nullptr if there is no current weapon set or the file name of the current weapon is not present in KeypressToWeaponMap. Same weapon as current weapon if we could not find another available weapon. Note that owner of weapon is not taken into consideration, because the idea is that each player entity has their own WeaponManager, storing only their owned weapons.

Definition at line 1957 of file WeaponManager.cpp.

◆ getPrevAvailableWeapon() [2/2]

const Weapon * WeaponManager::getPrevAvailableWeapon ( unsigned char & cTargetWeapon) const

Gets the previous available weapon.

Typically you need this function when the user is browsing their weapons backwards e.g. scrolling mousewheel backward.

Parameters
cTargetWeaponOutput argument, the key associated to the previous available weapon in KeypressToWeaponMap will be set in this output argument. Will be set to '\0' (null-char) if no current weapon is set or the file name of the current weapon is not present in KeypressToWeaponMap. Will be set to the key of the current weapon if we could not find another available weapon.
Returns
The previous available weapon based on logical order defined by KeypressToWeaponMap. Nullptr if there is no current weapon set or the file name of the current weapon is not present in KeypressToWeaponMap. Same weapon as current weapon if we could not find another available weapon. Note that owner of weapon is not taken into consideration, because the idea is that each player entity has their own WeaponManager, storing only their owned weapons.

Definition at line 1976 of file WeaponManager.cpp.

◆ getTimeLastWeaponSwitch()

const std::chrono::time_point< std::chrono::steady_clock > & WeaponManager::getTimeLastWeaponSwitch ( ) const
Returns
Timestamp of last successful setCurrentWeapon() with bRecordSwitchTime as true.

Definition at line 2227 of file WeaponManager.cpp.

◆ getWeaponByFilename() [1/2]

Weapon * WeaponManager::getWeaponByFilename ( const std::string & wpnName)

Definition at line 1827 of file WeaponManager.cpp.

◆ getWeaponByFilename() [2/2]

const Weapon * WeaponManager::getWeaponByFilename ( const std::string & wpnName) const

Definition at line 1832 of file WeaponManager.cpp.

◆ getWeaponById() [1/2]

Weapon * WeaponManager::getWeaponById ( const WeaponId & id)

Definition at line 1811 of file WeaponManager.cpp.

◆ getWeaponById() [2/2]

const Weapon * WeaponManager::getWeaponById ( const WeaponId & id) const

Definition at line 1816 of file WeaponManager.cpp.

◆ getWeapons()

const std::vector< Weapon * > & WeaponManager::getWeapons ( ) const

Definition at line 1806 of file WeaponManager.cpp.

◆ load()

Weapon * WeaponManager::load ( const char * fname,
pge_network::PgeNetworkConnectionHandle connHandleServerSide )

Loads the given weapon file and returns the created Weapon instance.

Parameters
fnameWeapon file name.
connHandleServerSideThe server-side connection handle of the owner of the loaded weapon. Basically this connects the weapon to the user of the weapon.
Returns
Nullptr on error loading the file, otherwise pointer to the created Weapon instance.

Definition at line 1768 of file WeaponManager.cpp.

◆ operator=()

WeaponManager & WeaponManager::operator= ( const WeaponManager & )
inlineprotected

Definition at line 614 of file WeaponManager.h.

◆ setCurrentWeapon()

bool WeaponManager::setCurrentWeapon ( Weapon * wpn,
bool bRecordSwitchTime,
bool bServer )

Sets the current weapon.

Useful for a player object for tracking player's current weapon. Note that WeaponManager doesn't know about any player entity, you need to connect this logic with your player entity.

Parameters
wpnThe weapon you want to set to be current. Cannot be null.
bRecordSwitchTimeSet it to true if you want the timestamp of this change to be saved for later query by getTimeLastWeaponSwitch(). Ignored if this is the very first time you invoke this function, since that won't be a real switch at all.
bServerSet it to true if we are server instance, false if we are client. Only server does availability check because clients might not be up-to-date about weapon availability.
Returns
True if current weapon has been set to the given weapon, false otherwise.

Definition at line 1904 of file WeaponManager.cpp.

◆ setDefaultAvailableWeaponByFilename()

bool WeaponManager::setDefaultAvailableWeaponByFilename ( const std::string & sFilename)

Sets default available weapon that should be by default available for any new/respawning player.

Note that WeaponManager doesn't know about any player entity, you need to connect this logic with your player entity.

Definition at line 1855 of file WeaponManager.cpp.

Member Data Documentation

◆ m_audio

pge_audio::PgeAudio& WeaponManager::m_audio
private

Definition at line 622 of file WeaponManager.h.

◆ m_bullets

PgeObjectPool<PooledBullet>& WeaponManager::m_bullets
private

Definition at line 628 of file WeaponManager.h.

◆ m_cfgProfiles

PGEcfgProfiles& WeaponManager::m_cfgProfiles
private

Definition at line 623 of file WeaponManager.h.

◆ m_gfx

PR00FsUltimateRenderingEngine& WeaponManager::m_gfx
private

Definition at line 624 of file WeaponManager.h.

◆ m_mapKeypressToWeapon

WeaponManager::KeypressToWeaponMap WeaponManager::m_mapKeypressToWeapon
staticprivate

Definition at line 620 of file WeaponManager.h.

◆ m_pCurrentWpn

Weapon* WeaponManager::m_pCurrentWpn
private

Definition at line 626 of file WeaponManager.h.

◆ m_sDefaultAvailableWeapon

std::string WeaponManager::m_sDefaultAvailableWeapon
private

Definition at line 629 of file WeaponManager.h.

◆ m_timeLastWeaponSwitch

std::chrono::time_point<std::chrono::steady_clock> WeaponManager::m_timeLastWeaponSwitch
private

Definition at line 627 of file WeaponManager.h.

◆ m_weapons

std::vector<Weapon*> WeaponManager::m_weapons
private

Definition at line 625 of file WeaponManager.h.


The documentation for this class was generated from the following files: