Skip to content

Commit

Permalink
close #148
Browse files Browse the repository at this point in the history
  • Loading branch information
stohrendorf committed Feb 22, 2021
1 parent f12138f commit 9df1fd2
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 47 deletions.
3 changes: 3 additions & 0 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ std::pair<RunResult, std::optional<size_t>> Engine::run(World& world, bool isCut
{
gl::Framebuffer::unbindAll();
if(!isCutscene)
{
world.getObjectManager().getLara().m_state.health = world.getPlayer().laraHealth;
world.getObjectManager().getLara().initWeaponAnimData();
}

const bool godMode
= core::get<bool>(core::get<pybind11::dict>(pybind11::globals(), "cheats").value_or(pybind11::dict{}), "godMode")
Expand Down
30 changes: 18 additions & 12 deletions src/engine/inventory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "inventory.h"

#include "engine/player.h"
#include "engine/world.h"
#include "items_tr1.h"
#include "objects/laraobject.h"
#include "serialization/map.h"
Expand Down Expand Up @@ -113,43 +115,47 @@ bool Inventory::tryUse(objects::LaraObject& lara, const TR1ItemId id)
if(count(TR1ItemId::Shotgun) == 0)
return false;

lara.requestedGunType = WeaponId::Shotgun;
if(lara.getHandStatus() == objects::HandStatus::None && lara.gunType == lara.requestedGunType)
lara.getWorld().getPlayer().requestedGunType = WeaponId::Shotgun;
if(lara.getHandStatus() == objects::HandStatus::None
&& lara.getWorld().getPlayer().gunType == lara.getWorld().getPlayer().requestedGunType)
{
lara.gunType = WeaponId::None;
lara.getWorld().getPlayer().gunType = WeaponId::None;
}
}
else if(id == TR1ItemId::Pistols || id == TR1ItemId::PistolsSprite)
{
if(count(TR1ItemId::Pistols) == 0)
return false;

lara.requestedGunType = WeaponId::Pistols;
if(lara.getHandStatus() == objects::HandStatus::None && lara.gunType == lara.requestedGunType)
lara.getWorld().getPlayer().requestedGunType = WeaponId::Pistols;
if(lara.getHandStatus() == objects::HandStatus::None
&& lara.getWorld().getPlayer().gunType == lara.getWorld().getPlayer().requestedGunType)
{
lara.gunType = WeaponId::None;
lara.getWorld().getPlayer().gunType = WeaponId::None;
}
}
else if(id == TR1ItemId::Magnums || id == TR1ItemId::MagnumsSprite)
{
if(count(TR1ItemId::Magnums) == 0)
return false;

lara.requestedGunType = WeaponId::Magnums;
if(lara.getHandStatus() == objects::HandStatus::None && lara.gunType == lara.requestedGunType)
lara.getWorld().getPlayer().requestedGunType = WeaponId::Magnums;
if(lara.getHandStatus() == objects::HandStatus::None
&& lara.getWorld().getPlayer().gunType == lara.getWorld().getPlayer().requestedGunType)
{
lara.gunType = WeaponId::None;
lara.getWorld().getPlayer().gunType = WeaponId::None;
}
}
else if(id == TR1ItemId::Uzis || id == TR1ItemId::UzisSprite)
{
if(count(TR1ItemId::Uzis) == 0)
return false;

lara.requestedGunType = WeaponId::Uzis;
if(lara.getHandStatus() == objects::HandStatus::None && lara.gunType == lara.requestedGunType)
lara.getWorld().getPlayer().requestedGunType = WeaponId::Uzis;
if(lara.getHandStatus() == objects::HandStatus::None
&& lara.getWorld().getPlayer().gunType == lara.getWorld().getPlayer().requestedGunType)
{
lara.gunType = WeaponId::None;
lara.getWorld().getPlayer().gunType = WeaponId::None;
}
}
else if(id == TR1ItemId::LargeMedipack || id == TR1ItemId::LargeMedipackSprite)
Expand Down
51 changes: 25 additions & 26 deletions src/engine/objects/laraobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ void LaraObject::updateLarasWeaponsStatus()
doHolsterUpdate = true;
}
}
else if(requestedGunType == gunType)
else if(getWorld().getPlayer().requestedGunType == getWorld().getPlayer().gunType)
{
if(getWorld().getPresenter().getInputHandler().hasDebouncedAction(hid::Action::Holster))
{
Expand All @@ -654,13 +654,13 @@ void LaraObject::updateLarasWeaponsStatus()
}
else if(m_handStatus == HandStatus::None)
{
gunType = requestedGunType;
unholster();
getWorld().getPlayer().gunType = getWorld().getPlayer().requestedGunType;
initWeaponAnimData();
doHolsterUpdate = true;
}
}

if(doHolsterUpdate && gunType != WeaponId::None)
if(doHolsterUpdate && getWorld().getPlayer().gunType != WeaponId::None)
{
if(m_handStatus == HandStatus::None)
{
Expand All @@ -676,18 +676,18 @@ void LaraObject::updateLarasWeaponsStatus()

if(m_handStatus == HandStatus::Unholster)
{
if(gunType >= WeaponId::Pistols)
if(getWorld().getPlayer().gunType >= WeaponId::Pistols)
{
if(gunType <= WeaponId::Uzis)
if(getWorld().getPlayer().gunType <= WeaponId::Uzis)
{
if(getWorld().getCameraController().getMode() != CameraMode::Cinematic
&& getWorld().getCameraController().getMode() != CameraMode::FreeLook)
{
getWorld().getCameraController().setMode(CameraMode::Combat);
}
unholsterGuns(gunType);
unholsterGuns(getWorld().getPlayer().gunType);
}
else if(gunType == WeaponId::Shotgun)
else if(getWorld().getPlayer().gunType == WeaponId::Shotgun)
{
if(getWorld().getCameraController().getMode() != CameraMode::Cinematic
&& getWorld().getCameraController().getMode() != CameraMode::FreeLook)
Expand All @@ -707,13 +707,13 @@ void LaraObject::updateLarasWeaponsStatus()
getSkeleton()->rebuildMesh();
}

if(gunType >= WeaponId::Pistols)
if(getWorld().getPlayer().gunType >= WeaponId::Pistols)
{
if(gunType <= WeaponId::Uzis)
if(getWorld().getPlayer().gunType <= WeaponId::Uzis)
{
holsterGuns(gunType);
holsterGuns(getWorld().getPlayer().gunType);
}
else if(gunType == WeaponId::Shotgun)
else if(getWorld().getPlayer().gunType == WeaponId::Shotgun)
{
holsterShotgun();
}
Expand All @@ -728,7 +728,7 @@ void LaraObject::updateLarasWeaponsStatus()
getSkeleton()->rebuildMesh();
}

switch(gunType)
switch(getWorld().getPlayer().gunType)
{
case WeaponId::Pistols:
if(getWorld().getPlayer().getInventory().getAmmo(WeaponId::Pistols)->ammo != 0)
Expand All @@ -746,7 +746,7 @@ void LaraObject::updateLarasWeaponsStatus()
{
getWorld().getCameraController().setMode(CameraMode::Combat);
}
updateGuns(gunType);
updateGuns(getWorld().getPlayer().gunType);
break;
case WeaponId::Magnums:
if(getWorld().getPlayer().getInventory().getAmmo(WeaponId::Magnums)->ammo != 0)
Expand All @@ -764,7 +764,7 @@ void LaraObject::updateLarasWeaponsStatus()
{
getWorld().getCameraController().setMode(CameraMode::Combat);
}
updateGuns(gunType);
updateGuns(getWorld().getPlayer().gunType);
break;
case WeaponId::Uzis:
if(getWorld().getPlayer().getInventory().getAmmo(WeaponId::Uzis)->ammo != 0)
Expand All @@ -782,7 +782,7 @@ void LaraObject::updateLarasWeaponsStatus()
{
getWorld().getCameraController().setMode(CameraMode::Combat);
}
updateGuns(gunType);
updateGuns(getWorld().getPlayer().gunType);
break;
case WeaponId::Shotgun:
if(getWorld().getPlayer().getInventory().getAmmo(WeaponId::Shotgun)->ammo != 0)
Expand Down Expand Up @@ -918,7 +918,7 @@ void LaraObject::updateAimingState(const Weapon& weapon)
m_weaponTargetVector = targetVector;
}

void LaraObject::unholster()
void LaraObject::initWeaponAnimData()
{
rightArm.frame = 0_frame;
leftArm.frame = 0_frame;
Expand All @@ -931,14 +931,15 @@ void LaraObject::unholster()
rightArm.flashTimeout = 0_frame;
leftArm.flashTimeout = 0_frame;
target = nullptr;
if(gunType == WeaponId::None)
if(getWorld().getPlayer().gunType == WeaponId::None)
{
const auto* positionData = getWorld().findAnimatedModelForType(TR1ItemId::Lara)->frames;

rightArm.weaponAnimData = positionData;
leftArm.weaponAnimData = positionData;
}
else if(gunType == WeaponId::Pistols || gunType == WeaponId::Magnums || gunType == WeaponId::Uzis)
else if(getWorld().getPlayer().gunType == WeaponId::Pistols || getWorld().getPlayer().gunType == WeaponId::Magnums
|| getWorld().getPlayer().gunType == WeaponId::Uzis)
{
const auto* positionData = getWorld().findAnimatedModelForType(TR1ItemId::LaraPistolsAnim)->frames;

Expand All @@ -947,10 +948,10 @@ void LaraObject::unholster()

if(m_handStatus != HandStatus::None)
{
overrideLaraMeshesUnholsterGuns(gunType);
overrideLaraMeshesUnholsterGuns(getWorld().getPlayer().gunType);
}
}
else if(gunType == WeaponId::Shotgun)
else if(getWorld().getPlayer().gunType == WeaponId::Shotgun)
{
const auto* positionData = getWorld().findAnimatedModelForType(TR1ItemId::LaraShotgunAnim)->frames;

Expand Down Expand Up @@ -1636,7 +1637,7 @@ bool LaraObject::fireWeapon(const WeaponId weaponId,
if(ammoPtr->ammo == 0)
{
playSoundEffect(TR1SoundEffect::EmptyAmmo);
requestedGunType = WeaponId::Pistols;
getWorld().getPlayer().requestedGunType = WeaponId::Pistols;
return false;
}

Expand Down Expand Up @@ -1971,7 +1972,7 @@ void LaraObject::drawRoutine()
WeaponId activeGunType = WeaponId::None;
if(m_handStatus == HandStatus::Combat || m_handStatus == HandStatus::Unholster || m_handStatus == HandStatus::Holster)
{
activeGunType = gunType;
activeGunType = getWorld().getPlayer().gunType;
}

matrixStack.pop();
Expand Down Expand Up @@ -2064,7 +2065,7 @@ void LaraObject::drawRoutineInterpolated(const SkeletalModelNode::InterpolationI
WeaponId activeGunType = WeaponId::None;
if(m_handStatus == HandStatus::Combat || m_handStatus == HandStatus::Unholster || m_handStatus == HandStatus::Holster)
{
activeGunType = gunType;
activeGunType = getWorld().getPlayer().gunType;
}

matrixStack.pop();
Expand Down Expand Up @@ -2215,8 +2216,6 @@ void LaraObject::serialize(const serialization::Serializer<World>& ser)
// FIXME S_NVP(forceSourcePosition),
S_NV("leftArm", leftArm),
S_NV("rightArm", rightArm),
S_NV("gunType", gunType),
S_NV("requestedGunType", requestedGunType),
S_NV("weaponTargetVector", m_weaponTargetVector),
S_NV("weapons", weapons));

Expand Down
5 changes: 1 addition & 4 deletions src/engine/objects/laraobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,6 @@ class LaraObject final : public ModelObject
AimInfo leftArm;
AimInfo rightArm;

WeaponId gunType = WeaponId::None;
WeaponId requestedGunType = WeaponId::None;

std::shared_ptr<ModelObject> target{nullptr};

struct Range
Expand Down Expand Up @@ -382,7 +379,7 @@ class LaraObject final : public ModelObject

void updateAimingState(const Weapon& weapon);

void unholster();
void initWeaponAnimData();

static core::RoomBoundPosition getUpperThirdBBoxCtr(const ModelObject& object);

Expand Down
3 changes: 2 additions & 1 deletion src/engine/objects/mutant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "engine/lara/abstractstatehandler.h"
#include "engine/particle.h"
#include "engine/player.h"
#include "engine/world.h"
#include "laraobject.h"
#include "mutantegg.h"
Expand Down Expand Up @@ -505,7 +506,7 @@ void TorsoBoss::update()
lara.m_state.rotation = {0_deg, m_state.rotation.Y, 0_deg};
lara.m_state.health = core::DeadHealth;
lara.setAir(-1_frame);
lara.gunType = WeaponId::None;
getWorld().getPlayer().gunType = WeaponId::None;
lara.setHandStatus(HandStatus::Grabbing);
lara.m_state.falling = false;
getWorld().getCameraController().setMode(CameraMode::FixedPosition);
Expand Down
3 changes: 2 additions & 1 deletion src/engine/objects/trex.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "trex.h"

#include "engine/cameracontroller.h"
#include "engine/player.h"
#include "engine/world.h"
#include "laraobject.h"
#include "serialization/serialization.h"
Expand Down Expand Up @@ -92,7 +93,7 @@ void TRex::update()
getWorld().getObjectManager().getLara().setCurrentAnimState(loader::file::LaraStateId::BoulderDeath);
getWorld().getObjectManager().getLara().setGoalAnimState(loader::file::LaraStateId::BoulderDeath);
getWorld().getObjectManager().getLara().setHandStatus(HandStatus::Grabbing);
getWorld().getObjectManager().getLara().gunType = WeaponId::None;
getWorld().getPlayer().gunType = WeaponId::None;
getWorld().getCameraController().setModifier(CameraModifier::FollowCenter);
getWorld().getCameraController().setRotationAroundLara(-25_deg, 170_deg);
getWorld().getObjectManager().getLara().setAir(-1_frame);
Expand Down
12 changes: 11 additions & 1 deletion src/engine/player.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include "player.h"

#include "objects/laraobject.h"
#include "serialization/quantity.h"
#include "serialization/serialization.h"
#include "world.h"

namespace engine
{
void Player::serialize(const serialization::Serializer<World>& ser)
{
ser(S_NV("inventory", m_inventory), S_NV("laraHealth", laraHealth));
ser(S_NV("inventory", m_inventory),
S_NV("laraHealth", laraHealth),
S_NV("gunType", gunType),
S_NV("requestedGunType", requestedGunType));

if(ser.loading && ser.context.getObjectManager().getLaraPtr() != nullptr)
ser.lazy([](const serialization::Serializer<World>& ser) {
ser.context.getObjectManager().getLara().initWeaponAnimData();
});
}
} // namespace engine
2 changes: 2 additions & 0 deletions src/engine/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Player
void serialize(const serialization::Serializer<World>& ser);

core::Health laraHealth{core::LaraHealth};
WeaponId gunType = WeaponId::None;
WeaponId requestedGunType = WeaponId::None;

private:
Inventory m_inventory{};
Expand Down
5 changes: 3 additions & 2 deletions src/engine/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,11 +1030,11 @@ void World::gameLoop(bool godMode)
doGlobalEffect();
getPresenter().drawBars(ui, *m_level->m_palette, getObjectManager());
if(getObjectManager().getLara().getHandStatus() == engine::objects::HandStatus::Combat
&& getObjectManager().getLara().gunType != WeaponId::Pistols)
&& m_player->gunType != WeaponId::Pistols)
{
size_t n = 0;
std::string suffix;
switch(getObjectManager().getLara().gunType)
switch(m_player->gunType)
{
case WeaponId::Shotgun:
n = m_player->getInventory().getAmmo(WeaponId::Shotgun)->ammo / 6;
Expand Down Expand Up @@ -1091,6 +1091,7 @@ void World::load(const std::filesystem::path& filename)
}
doc.load("data", *this, *this);
m_objectManager.getLara().m_state.health = m_player->laraHealth;
m_objectManager.getLara().initWeaponAnimData();
m_level->updateRoomBasedCaches();
}

Expand Down

0 comments on commit 9df1fd2

Please sign in to comment.