38 lines
1.1 KiB
C++
38 lines
1.1 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "EntityMove.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
EntityMove::EntityMove(SceneItem* item) : SceneItemComponent(item) {
|
|
|
|
}
|
|
|
|
std::vector<SceneItemComponent*> EntityMove::getDependencies() {
|
|
return {
|
|
(characterController = item->getComponent<CharacterController2D>()),
|
|
(this->entityHealth = item->getComponent<EntityHealth>()),
|
|
};
|
|
}
|
|
|
|
void EntityMove::onStart() {
|
|
assertNotNull(this->characterController);
|
|
assertNotNull(this->entityHealth);
|
|
|
|
useEvent([&](float_t delta) {
|
|
// Don't move if stunned or attacking.
|
|
if(entityHealth->isStunned()) return;
|
|
|
|
// Don't move if no move.
|
|
if(direction.x == 0 && direction.y == 0) return;
|
|
|
|
// Move
|
|
float_t angle = atan2(direction.y, direction.x);
|
|
glm::vec2 movement(cosf(angle), sinf(angle));
|
|
transform->setLocalRotation(glm::quat(glm::vec3(0, -angle + 1.5708f, 0)));
|
|
characterController->velocity += movement * delta * moveSpeed;
|
|
}, getScene()->eventSceneUpdate);
|
|
} |