Skip to content

Commit

Permalink
Rename PlayerModel class to PersistentPlayerState
Browse files Browse the repository at this point in the history
  • Loading branch information
naftalimurgor authored and lethal-guitar committed Sep 9, 2023
1 parent 6656ff0 commit 15308ab
Show file tree
Hide file tree
Showing 29 changed files with 308 additions and 275 deletions.
7 changes: 4 additions & 3 deletions src/data/bonus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ constexpr int asNumber(const Bonus bonus)
}


inline void
addBonusScore(PlayerModel& playerModel, const std::set<Bonus>& bonuses)
inline void addBonusScore(
PersistentPlayerState& persistentPlayerState,
const std::set<Bonus>& bonuses)
{
const auto numBonuses = static_cast<int>(bonuses.size());
playerModel.giveScore(numBonuses * SCORE_ADDED_PER_BONUS);
persistentPlayerState.giveScore(numBonuses * SCORE_ADDED_PER_BONUS);
}

} // namespace rigel::data
62 changes: 32 additions & 30 deletions src/data/player_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace rigel::data
{

PlayerModel::PlayerModel()
PersistentPlayerState::PersistentPlayerState()
: mWeapon(WeaponType::Normal)
, mScore(0)
, mAmmo(MAX_AMMO)
Expand All @@ -34,7 +34,7 @@ PlayerModel::PlayerModel()
}


PlayerModel::PlayerModel(const SavedGame& save)
PersistentPlayerState::PersistentPlayerState(const SavedGame& save)
: mTutorialMessages(save.mTutorialMessagesAlreadySeen)
, mWeapon(save.mWeapon)
, mScore(save.mScore)
Expand All @@ -44,65 +44,66 @@ PlayerModel::PlayerModel(const SavedGame& save)
}


PlayerModel::CheckpointState PlayerModel::makeCheckpoint() const
PersistentPlayerState::CheckpointState
PersistentPlayerState::makeCheckpoint() const
{
return CheckpointState{mWeapon, mAmmo, mHealth};
}


void PlayerModel::restoreFromCheckpoint(const CheckpointState& state)
void PersistentPlayerState::restoreFromCheckpoint(const CheckpointState& state)
{
mHealth = std::max(2, state.mHealth);
mWeapon = state.mWeapon;
mAmmo = state.mAmmo;
}


int PlayerModel::score() const
int PersistentPlayerState::score() const
{
return mScore;
}


void PlayerModel::giveScore(const int amount)
void PersistentPlayerState::giveScore(const int amount)
{
mScore = std::clamp(mScore + amount, 0, MAX_SCORE);
}


int PlayerModel::ammo() const
int PersistentPlayerState::ammo() const
{
return mAmmo;
}


int PlayerModel::currentMaxAmmo() const
int PersistentPlayerState::currentMaxAmmo() const
{
return mWeapon == WeaponType::FlameThrower ? MAX_AMMO_FLAME_THROWER
: MAX_AMMO;
}


WeaponType PlayerModel::weapon() const
WeaponType PersistentPlayerState::weapon() const
{
return mWeapon;
}


bool PlayerModel::currentWeaponConsumesAmmo() const
bool PersistentPlayerState::currentWeaponConsumesAmmo() const
{
return mWeapon != WeaponType::Normal;
}


void PlayerModel::switchToWeapon(const WeaponType type)
void PersistentPlayerState::switchToWeapon(const WeaponType type)
{
mWeapon = type;
mAmmo = currentMaxAmmo();
}


void PlayerModel::useAmmo()
void PersistentPlayerState::useAmmo()
{
if (currentWeaponConsumesAmmo())
{
Expand All @@ -115,64 +116,64 @@ void PlayerModel::useAmmo()
}


void PlayerModel::setAmmo(int amount)
void PersistentPlayerState::setAmmo(int amount)
{
assert(amount >= 0 && amount <= currentMaxAmmo());
mAmmo = amount;
}


int PlayerModel::health() const
int PersistentPlayerState::health() const
{
return mHealth;
}


bool PlayerModel::isAtFullHealth() const
bool PersistentPlayerState::isAtFullHealth() const
{
return mHealth == MAX_HEALTH;
}


bool PlayerModel::isDead() const
bool PersistentPlayerState::isDead() const
{
return mHealth <= 0;
}


void PlayerModel::takeDamage(const int amount)
void PersistentPlayerState::takeDamage(const int amount)
{
mHealth = std::clamp(mHealth - amount, 0, MAX_HEALTH);
}


void PlayerModel::takeFatalDamage()
void PersistentPlayerState::takeFatalDamage()
{
mHealth = 0;
}


void PlayerModel::giveHealth(const int amount)
void PersistentPlayerState::giveHealth(const int amount)
{
mHealth = std::clamp(mHealth + amount, 0, MAX_HEALTH);
}


const std::vector<InventoryItemType>& PlayerModel::inventory() const
const std::vector<InventoryItemType>& PersistentPlayerState::inventory() const
{
return mInventory;
}


bool PlayerModel::hasItem(const InventoryItemType type) const
bool PersistentPlayerState::hasItem(const InventoryItemType type) const
{
using namespace std;

return find(begin(mInventory), end(mInventory), type) != end(mInventory);
}


void PlayerModel::giveItem(InventoryItemType type)
void PersistentPlayerState::giveItem(InventoryItemType type)
{
using namespace std;

Expand All @@ -191,7 +192,7 @@ void PlayerModel::giveItem(InventoryItemType type)
}


void PlayerModel::removeItem(const InventoryItemType type)
void PersistentPlayerState::removeItem(const InventoryItemType type)
{
using namespace std;

Expand All @@ -203,14 +204,15 @@ void PlayerModel::removeItem(const InventoryItemType type)
}


const std::vector<CollectableLetterType>& PlayerModel::collectedLetters() const
const std::vector<CollectableLetterType>&
PersistentPlayerState::collectedLetters() const
{
return mCollectedLetters;
}


PlayerModel::LetterCollectionState
PlayerModel::addLetter(const CollectableLetterType type)
PersistentPlayerState::LetterCollectionState
PersistentPlayerState::addLetter(const CollectableLetterType type)
{
using L = CollectableLetterType;
static std::vector<L> sExpectedOrder = {L::N, L::U, L::K, L::E, L::M};
Expand All @@ -231,28 +233,28 @@ PlayerModel::LetterCollectionState
}


void PlayerModel::resetForNewLevel()
void PersistentPlayerState::resetForNewLevel()
{
mHealth = MAX_HEALTH;
mCollectedLetters.clear();
mInventory.clear();
}


void PlayerModel::resetHealthAndScore()
void PersistentPlayerState::resetHealthAndScore()
{
mHealth = MAX_HEALTH;
mScore = 0;
}


TutorialMessageState& PlayerModel::tutorialMessages()
TutorialMessageState& PersistentPlayerState::tutorialMessages()
{
return mTutorialMessages;
}


const TutorialMessageState& PlayerModel::tutorialMessages() const
const TutorialMessageState& PersistentPlayerState::tutorialMessages() const
{
return mTutorialMessages;
}
Expand Down
6 changes: 3 additions & 3 deletions src/data/player_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ constexpr auto MAX_AMMO_FLAME_THROWER = 64;
constexpr auto MAX_HEALTH = 9;


class PlayerModel
class PersistentPlayerState
{
public:
struct CheckpointState
Expand All @@ -85,8 +85,8 @@ class PlayerModel
InOrder
};

PlayerModel();
explicit PlayerModel(const SavedGame& save);
PersistentPlayerState();
explicit PersistentPlayerState(const SavedGame& save);

CheckpointState makeCheckpoint() const;
void restoreFromCheckpoint(const CheckpointState& state);
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/demo_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void DemoPlayer::updateAndRender(const engine::TimeDelta dt)
if (!mpWorld)
{
mpWorld = std::make_unique<GameWorld_Classic>(
&mPlayerModel,
&mPersistentPlayerState,
demoSessionId(0),
mContext,
std::nullopt,
Expand Down Expand Up @@ -144,10 +144,10 @@ void DemoPlayer::updateAndRender(const engine::TimeDelta dt)

++mLevelIndex;

mPlayerModel.resetForNewLevel();
mPersistentPlayerState.resetForNewLevel();

mpWorld = std::make_unique<GameWorld_Classic>(
&mPlayerModel,
&mPersistentPlayerState,
demoSessionId(mLevelIndex),
mContext,
std::nullopt,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/demo_player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DemoPlayer

private:
GameMode::Context mContext;
data::PlayerModel mPlayerModel;
data::PersistentPlayerState mPersistentPlayerState;

std::vector<DemoInput> mFrames;
std::size_t mCurrentFrameIndex = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/game_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ std::unique_ptr<game_logic::IGameWorld>


GameRunner::GameRunner(
data::PlayerModel* pPlayerModel,
data::PersistentPlayerState* pPersistentPlayerState,
const data::GameSessionId& sessionId,
GameMode::Context context,
const std::optional<base::Vec2> playerPositionOverride,
const bool showWelcomeMessage)
: mContext(context)
, mpWorld(createGameWorld(
context.mpUserProfile->mOptions.mGameplayStyle,
pPlayerModel,
pPersistentPlayerState,
sessionId,
context,
playerPositionOverride,
showWelcomeMessage))
, mInputHandler(&context.mpUserProfile->mOptions)
, mMenu(context, pPlayerModel, mpWorld.get(), sessionId)
, mMenu(context, pPersistentPlayerState, mpWorld.get(), sessionId)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/game_runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GameRunner
{
public:
GameRunner(
data::PlayerModel* pPlayerModel,
data::PersistentPlayerState* pPersistentPlayerState,
const data::GameSessionId& sessionId,
GameMode::Context context,
std::optional<base::Vec2> playerPositionOverride = std::nullopt,
Expand Down
Loading

0 comments on commit 15308ab

Please sign in to comment.