refactor: Add Area function to move creature in direction

This commit is contained in:
Vsevolod Kremianskii 2021-01-08 23:51:10 +07:00
parent 097c518d72
commit bb3e7dee2a
3 changed files with 14 additions and 10 deletions

View file

@ -581,16 +581,14 @@ void Area::update(float dt) {
updateHeartbeat(dt);
}
bool Area::moveCreatureTowards(const shared_ptr<Creature> &creature, const glm::vec2 &dest, bool run, float dt) {
glm::vec3 position(creature->position());
glm::vec2 delta(dest - glm::vec2(position));
glm::vec2 dir(glm::normalize(delta));
bool Area::moveCreature(const shared_ptr<Creature> &creature, const glm::vec2 &dir, bool run, float dt) {
float facing = -glm::atan(dir.x, dir.y);
creature->setFacing(facing);
float speed = run ? creature->runSpeed() : creature->walkSpeed();
float speedDt = speed * dt;
glm::vec3 position(creature->position());
position.x += dir.x * speedDt;
position.y += dir.y * speedDt;
@ -613,6 +611,12 @@ bool Area::moveCreatureTowards(const shared_ptr<Creature> &creature, const glm::
return false;
}
bool Area::moveCreatureTowards(const shared_ptr<Creature> &creature, const glm::vec2 &dest, bool run, float dt) {
glm::vec2 delta(dest - glm::vec2(creature->position()));
glm::vec2 dir(glm::normalize(delta));
return moveCreature(creature, dir, run, dt);
}
void Area::runSpawnScripts() {
for (auto &creature : _objectsByType[ObjectType::Creature]) {
static_cast<Creature &>(*creature).runSpawnScript();

View file

@ -74,12 +74,14 @@ public:
void destroyObject(const SpatialObject &object);
void fill(scene::SceneGraph &sceneGraph);
void initCameras(const glm::vec3 &entryPosition, float entryFacing);
bool moveCreatureTowards(const std::shared_ptr<Creature> &creature, const glm::vec2 &dest, bool run, float dt);
void onPartyLeaderMoved();
void startDialog(const std::shared_ptr<SpatialObject> &object, const std::string &resRef);
void update3rdPersonCameraFacing();
void update3rdPersonCameraTarget();
bool moveCreature(const std::shared_ptr<Creature> &creature, const glm::vec2 &dir, bool run, float dt);
bool moveCreatureTowards(const std::shared_ptr<Creature> &creature, const glm::vec2 &dest, bool run, float dt);
bool isUnescapable() const;
std::shared_ptr<SpatialObject> getObjectAt(int x, int y) const;

View file

@ -132,11 +132,9 @@ void Player::update(float dt) {
if (movement) {
actions.clear();
glm::vec2 dest(partyLeader->position());
dest.x -= 100.0f * glm::sin(facing);
dest.y += 100.0f * glm::cos(facing);
glm::vec2 dir(glm::normalize(glm::vec2(-glm::sin(facing), glm::cos(facing))));
if (_area->moveCreatureTowards(partyLeader, dest, true, dt)) {
if (_area->moveCreature(partyLeader, dir, true, dt)) {
partyLeader->setMovementType(Creature::MovementType::Run);
}
} else if (actions.empty()) {