Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thokkat committed Nov 6, 2022
1 parent cfcb435 commit 03606c6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 67 deletions.
65 changes: 37 additions & 28 deletions game/game/playercontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "ui/inventorymenu.h"
#include "gothic.h"

using MobsiAction=Interactive::MobsiAction;

PlayerControl::PlayerControl(DialogMenu& dlg, InventoryMenu &inv)
:dlg(dlg),inv(inv) {
}
Expand Down Expand Up @@ -176,19 +178,6 @@ void PlayerControl::onKeyReleased(KeyCodec::Action a) {
} else {
std::memset(actrl,0,sizeof(actrl));
}

if(pl!=nullptr && pl->interactive()!=nullptr) {
auto inter = pl->interactive();
if (inter->isLadder()) {
bool g1c=Gothic::inst().settingsGetI("GAME","USEGOTHIC1CONTROLS")!=0 ;
if (a==KeyCodec::ActionGeneric && !g1c) {
inter->nextState(*pl,4);
return;
}
if ((a==KeyCodec::Forward || a==KeyCodec::Back) && (g1c || (!g1c && isPressed(KeyCodec::ActionGeneric))))
inter->nextState(*pl,0);
}
}
}

bool PlayerControl::isPressed(KeyCodec::Action a) const {
Expand Down Expand Up @@ -805,7 +794,9 @@ void PlayerControl::implMove(uint64_t dt) {
void PlayerControl::implMoveMobsi(Npc& pl, uint64_t /*dt*/) {
// animation handled in MOBSI
auto inter = pl.interactive();
if(ctrl[KeyCodec::Back]) {
bool g1c=Gothic::inst().settingsGetI("GAME","USEGOTHIC1CONTROLS")!=0;

if(ctrl[KeyCodec::Back] && !inter->isLadder()) {
pl.setInteraction(nullptr);
return;
}
Expand All @@ -820,6 +811,30 @@ void PlayerControl::implMoveMobsi(Npc& pl, uint64_t /*dt*/) {
if(inter->canQuitAtState(pl,stateId-ladder))
pl.setInteraction(nullptr,ladder);
}

if (inter->isLadder()) {
if(!g1c && !ctrl[KeyCodec::ActionGeneric]) {
if (inter->stateId()<1) {
inter->nextState(pl,MobsiAction::Prev);
return;
}
if (inter->stateId()>inter->stateCount()-2) {
inter->nextState(pl,MobsiAction::Next);
return;
}
inter->nextState(pl,MobsiAction::Quit);
return;
}
if(ctrl[KeyCodec::Forward]) {
inter->nextState(pl,MobsiAction::Next);
return;
}
if(ctrl[KeyCodec::Back]) {
inter->nextState(pl,MobsiAction::Prev);
if (inter->stateId()==-1)
ctrl[KeyCodec::ActionGeneric] = false;
}
}
}

void PlayerControl::processPickLock(Npc& pl, Interactive& inter, KeyCodec::Action k) {
Expand Down Expand Up @@ -873,31 +888,25 @@ void PlayerControl::processPickLock(Npc& pl, Interactive& inter, KeyCodec::Actio
}

void PlayerControl::processLadder(Npc& pl, Interactive& inter, KeyCodec::Action key) {
bool g1c=Gothic::inst().settingsGetI("GAME","USEGOTHIC1CONTROLS")!=0 ;

bool g1c=Gothic::inst().settingsGetI("GAME","USEGOTHIC1CONTROLS")!=0;
if(key==KeyCodec::ActionGeneric) {
ctrl[key] = true;
if (g1c) {
pl.stopAnim("");
pl.setInteraction(nullptr);
inter.nextState(pl,MobsiAction::Quit);
ctrl[key] = false;
}
else {
inter.nextState(pl,0);
}
return;
}
if (!g1c && !isPressed(KeyCodec::ActionGeneric))
if (!g1c && !ctrl[KeyCodec::ActionGeneric])
return;
if(key==KeyCodec::Forward ) {
inter.nextState(pl,1);
if(key==KeyCodec::Forward) {
ctrl[key] = true;
inter.nextState(pl,MobsiAction::Next);
return;
}
if(key==KeyCodec::Back) {
if (g1c)
inter.nextState(pl,2);
else
inter.nextState(pl,3);
ctrl[key] = true;
inter.nextState(pl,MobsiAction::Prev);
}
}

Expand Down
3 changes: 2 additions & 1 deletion game/graphics/mdlvisual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "graphics/mesh/skeleton.h"
#include "game/serialize.h"
#include "world/objects/npc.h"
#include "world/objects/interactive.h"
#include "world/objects/item.h"
#include "world/world.h"
#include "utils/fileext.h"
Expand Down Expand Up @@ -590,7 +591,7 @@ const Animation::Sequence* MdlVisual::startAnimAndGet(Npc& npc, AnimationSolver:
auto inter = npc.interactive();
const Animation::Sequence *sq = solver.solveAnim(inter,a,*skInst);
if(sq!=nullptr){
auto bs=sq->name.find("T_LADDER")==0 ? BS_CLIMB : BS_MOBINTERACT;
auto bs=inter->isLadder() ? BS_CLIMB : BS_MOBINTERACT;
if(skInst->startAnim(solver,sq,comb,bs,Pose::NoHint,npc.world().tickCount()))
return sq;
}
Expand Down
79 changes: 43 additions & 36 deletions game/world/objects/interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ void Interactive::implTick(Pos& p, uint64_t /*dt*/) {
mat.project(x0,y0,z0);
mat.project(x1,y1,z1);
npc.setDirectionY(y1-y0);
wait = true;
}
auto sq = npc.setAnimAngGet(Npc::Anim::InteractFromStand);
uint64_t t = sq==nullptr ? 0 : uint64_t(sq->totalTime());
Expand All @@ -272,10 +271,15 @@ void Interactive::implTick(Pos& p, uint64_t /*dt*/) {
}
}

if(isLadder() && wait && p.started)
if(isLadder()) {
if (state==-1) {
loopState=true;
reverseState=false;
}
return;
}

if((!attach && state==0) || (isLadder() && attach && state==stateNum-1)) {
if(!attach && state==0) {
implQuitInteract(p);
return;
}
Expand Down Expand Up @@ -318,49 +322,52 @@ void Interactive::implTick(Pos& p, uint64_t /*dt*/) {
loopState = (prev==state);
}

void Interactive::nextState(Npc& npc, const uint8_t st) {
wait = !st;
switch(st){
case 1 :
reverseState=false;
return;
case 2 :
reverseState=true;
return;
case 3 :
if (state==0)
wait=true;
else
reverseState=true;
void Interactive::nextState(Npc& npc, MobsiAction act) {
if (act==MobsiAction::Quit) {
npc.stopAnim("");
npc.setInteraction(nullptr);
return;
}

if(world.tickCount()<waitAnim)
return;

const int prev = state;
if (act==MobsiAction::Next)
reverseState=false;
if (act==MobsiAction::Prev)
reverseState=true;
if ((act==MobsiAction::Prev && state==0) || (act==MobsiAction::Next && state==stateNum-1)) {
auto sq = npc.setAnimAngGet(Npc::Anim::InteractToStand);
if (sq==nullptr)
return;
case 4 :
if (state<1) {
reverseState=true;
return;
}
if (state>stateNum-2) {
reverseState=false;
return;
}
npc.stopAnim("");
npc.setInteraction(nullptr);
waitAnim=world.tickCount()+uint64_t(sq->totalTime());
npc.setDirectionY(0);
if (state==0) {
setState(-1);
} else {
setState(stateNum);
}
return;
}
if (!setAnim(&npc,Anim::In))
return;
if (reverseState) {
setState(std::max(0,state-1));
} else {
setState(std::min(state+1,stateNum));
}
loopState = (prev==state);
}

void Interactive::implQuitInteract(Interactive::Pos &p) {
if(p.user==nullptr)
return;
Npc& npc = *p.user;
const Animation::Sequence* sq = nullptr;
if(state==0 || (isLadder() && state==stateNum-1)) {
if(state==0) {
// S0 -> STAND
sq = npc.setAnimAngGet(Npc::Anim::InteractToStand);
if (isLadder() && state==stateNum-1) {
waitAnim=world.tickCount()+uint64_t(sq->totalTime());
setState(stateNum);
npc.setDirectionY(0);
return;
}
}
if(sq==nullptr && !(npc.isDown() || npc.setAnim(Npc::Anim::Idle)))
return;
Expand Down Expand Up @@ -939,7 +946,7 @@ const Animation::Sequence* Interactive::animNpc(const AnimationSolver &solver, A
if(t==Anim::FromStand) {
st[0] = -1;
st[1] = state<1 ? 0 : stateNum - 1;
}
}
else if(t==Anim::ToStand) {
st[0] = state<1 ? 0 : stateNum - 1;
st[1] = -1;
Expand Down
9 changes: 7 additions & 2 deletions game/world/objects/interactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class Interactive : public Vob {
FromStand = 11,
};

enum MobsiAction : uint8_t {
Next = 1,
Prev = 2,
Quit = 3,
};

Interactive(Vob* parent, World& world, const phoenix::vobs::mob& vob, Flags flags);

void load(Serialize& fin) override;
Expand Down Expand Up @@ -78,7 +84,7 @@ class Interactive : public Vob {
auto animNpc(const AnimationSolver &solver, Anim t) -> const Animation::Sequence*;
void marchInteractives(DbgPainter& p) const;

void nextState(Npc& npc, const uint8_t st);
void nextState(Npc& npc, MobsiAction act);

protected:
Tempest::Matrix4x4 nodeTranform(std::string_view nodeName) const;
Expand Down Expand Up @@ -153,7 +159,6 @@ class Interactive : public Vob {
int stepsCount = 0;

int32_t state = -1;
bool wait = false;
bool reverseState = false;
bool loopState = false;
bool isLockCracked = false;
Expand Down

0 comments on commit 03606c6

Please sign in to comment.