-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KOTORBASE: Extract PC movement logic to a separate class
- Loading branch information
1 parent
8c48295
commit 7584037
Showing
5 changed files
with
199 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* xoreos - A reimplementation of BioWare's Aurora engine | ||
* | ||
* xoreos is the legal property of its developers, whose names | ||
* can be found in the AUTHORS file distributed with this source | ||
* distribution. | ||
* | ||
* xoreos is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 3 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* xoreos is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with xoreos. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @file | ||
* Handles player character movement in KotOR games. | ||
*/ | ||
|
||
#include "src/common/maths.h" | ||
|
||
#include "src/engines/aurora/satellitecamera.h" | ||
|
||
#include "src/engines/kotorbase/playercontroller.h" | ||
#include "src/engines/kotorbase/module.h" | ||
#include "src/engines/kotorbase/creature.h" | ||
#include "src/engines/kotorbase/area.h" | ||
|
||
namespace Engines { | ||
|
||
namespace KotORBase { | ||
|
||
PlayerController::PlayerController(Module *module) : | ||
_module(module), | ||
_pc(0), | ||
_forwardMovementWanted(false), | ||
_backwardMovementWanted(false), | ||
_moving(false) { | ||
} | ||
|
||
void PlayerController::setPC(Creature *pc) { | ||
_pc = pc; | ||
} | ||
|
||
void PlayerController::stopMovement() { | ||
_forwardMovementWanted = false; | ||
_backwardMovementWanted = false; | ||
_pc->playDefaultAnimation(); | ||
_moving = false; | ||
} | ||
|
||
bool PlayerController::handleEvent(const Events::Event &e) { | ||
switch (e.type) { | ||
case Events::kEventKeyDown: | ||
case Events::kEventKeyUp: | ||
if (e.key.keysym.scancode == SDL_SCANCODE_W) { | ||
_forwardMovementWanted = (e.type == Events::kEventKeyDown); | ||
return true; | ||
} else if (e.key.keysym.scancode == SDL_SCANCODE_S) { | ||
_backwardMovementWanted = (e.type == Events::kEventKeyDown); | ||
return true; | ||
} | ||
return false; | ||
|
||
case Events::kEventControllerAxisMotion: | ||
if (e.caxis.axis == Events::kControllerAxisLeftY) { | ||
_forwardMovementWanted = (e.caxis.value < -10000); | ||
_backwardMovementWanted = (e.caxis.value > 10000); | ||
return true; | ||
} | ||
return false; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
bool PlayerController::processMovement(float frameTime) { | ||
bool moveForwards = _forwardMovementWanted && !_backwardMovementWanted; | ||
bool moveBackwards = !_forwardMovementWanted && _backwardMovementWanted; | ||
|
||
if (!moveForwards && !moveBackwards) { | ||
if (_moving) { | ||
_pc->playDefaultAnimation(); | ||
_moving = false; | ||
} | ||
return false; | ||
} | ||
|
||
float x, y, _; | ||
_pc->getPosition(x, y, _); | ||
float yaw = SatelliteCam.getYaw(); | ||
float newX, newY; | ||
float moveRate = _pc->getRunRate(); | ||
|
||
if (moveForwards) { | ||
_pc->setOrientation(0.0f, 0.0f, 1.0f, Common::rad2deg(yaw)); | ||
newX = x - moveRate * sin(yaw) * frameTime; | ||
newY = y + moveRate * cos(yaw) * frameTime; | ||
} else { | ||
_pc->setOrientation(0.0f, 0.0f, 1.0f, 180 + Common::rad2deg(yaw)); | ||
newX = x + moveRate * sin(yaw) * frameTime; | ||
newY = y - moveRate * cos(yaw) * frameTime; | ||
} | ||
|
||
float z = _module->getCurrentArea()->evaluateElevation(newX, newY); | ||
if (z != FLT_MIN) { | ||
if (_module->getCurrentArea()->walkable(glm::vec3(x, y, z + 0.1f), | ||
glm::vec3(newX, newY, z + 0.1f))) | ||
_module->movePC(newX, newY, z); | ||
} | ||
|
||
if (!_moving) { | ||
_pc->playAnimation(Common::UString("run"), false, -1.0f); | ||
_moving = true; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // End of namespace KotORBase | ||
|
||
} // End of namespace Engines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* xoreos - A reimplementation of BioWare's Aurora engine | ||
* | ||
* xoreos is the legal property of its developers, whose names | ||
* can be found in the AUTHORS file distributed with this source | ||
* distribution. | ||
* | ||
* xoreos is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 3 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* xoreos is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with xoreos. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @file | ||
* Handles player character movement in KotOR games. | ||
*/ | ||
|
||
#ifndef ENGINES_KOTORBASE_PLAYERCONTROLLER_H | ||
#define ENGINES_KOTORBASE_PLAYERCONTROLLER_H | ||
|
||
#include "src/events/events.h" | ||
|
||
namespace Engines { | ||
|
||
namespace KotORBase { | ||
|
||
class Module; | ||
class Creature; | ||
|
||
class PlayerController { | ||
public: | ||
PlayerController(Module *module); | ||
|
||
void setPC(Creature *pc); | ||
|
||
void stopMovement(); | ||
bool handleEvent(const Events::Event &e); | ||
bool processMovement(float frameTime); | ||
|
||
private: | ||
Module *_module; | ||
Creature *_pc; | ||
bool _forwardMovementWanted; | ||
bool _backwardMovementWanted; | ||
bool _moving; | ||
}; | ||
|
||
} // End of namespace KotORBase | ||
|
||
} // End of namespace Engines | ||
|
||
#endif // ENGINES_KOTORBASE_PLAYERCONTROLLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters