diff --git a/src/dawnshared/util/random.hpp b/src/dawn/util/random.hpp similarity index 96% rename from src/dawnshared/util/random.hpp rename to src/dawn/util/random.hpp index aa065531..fc55f99b 100644 --- a/src/dawnshared/util/random.hpp +++ b/src/dawn/util/random.hpp @@ -4,7 +4,8 @@ // https://opensource.org/licenses/MIT #pragma once -#include "mathutils.hpp" +#include "dawnlibs.hpp" +#include "util/mathutils.hpp" namespace Dawn { /** diff --git a/src/dawnrose/scene/components/CMakeLists.txt b/src/dawnrose/scene/components/CMakeLists.txt index ea78b6e8..0211def1 100644 --- a/src/dawnrose/scene/components/CMakeLists.txt +++ b/src/dawnrose/scene/components/CMakeLists.txt @@ -8,7 +8,8 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE GameCamera.cpp PlayerController.cpp - EntityHealth.cpp HurtHazard.cpp - EntityRandomWalk.cpp -) \ No newline at end of file +) + +# Subdirs +add_subdirectory(entity) \ No newline at end of file diff --git a/src/dawnrose/scene/components/PlayerController.cpp b/src/dawnrose/scene/components/PlayerController.cpp index 1d593894..a1fb4d05 100644 --- a/src/dawnrose/scene/components/PlayerController.cpp +++ b/src/dawnrose/scene/components/PlayerController.cpp @@ -15,32 +15,22 @@ PlayerController::PlayerController(SceneItem *item) : SceneItemComponent(item) { std::vector PlayerController::getDependencies() { return { (this->characterController = item->getComponent()), - (this->entityHealth = item->getComponent()) + (this->entityHealth = item->getComponent()), + (this->entityMove = item->getComponent()) }; } void PlayerController::onStart() { assertNotNull(this->characterController); + assertNotNull(this->entityMove); assertNotNull(this->entityHealth); useEvent([&](float_t delta){ - // Don't move if stunned. - if(entityHealth->isStunned()) return; - // Movement - auto inputMove = getGame()->inputManager.getAxis2D( + this->entityMove->direction = getGame()->inputManager.getAxis2D( INPUT_BIND_NEGATIVE_X, INPUT_BIND_POSITIVE_X, INPUT_BIND_NEGATIVE_Y, INPUT_BIND_POSITIVE_Y ); - - if(inputMove.x != 0 || inputMove.y != 0) { - float_t angle = atan2(inputMove.y, inputMove.x); - glm::vec2 movement(cos(angle), sin(angle)); - - transform->setLocalRotation(glm::quat(glm::vec3(0, -angle + 1.5708f, 0))); - - characterController->velocity += movement * delta * moveSpeed; - } }, getScene()->eventSceneUpdate); // Hurt Hazard Processing diff --git a/src/dawnrose/scene/components/PlayerController.hpp b/src/dawnrose/scene/components/PlayerController.hpp index 735e7ea5..ab7aff6c 100644 --- a/src/dawnrose/scene/components/PlayerController.hpp +++ b/src/dawnrose/scene/components/PlayerController.hpp @@ -6,17 +6,17 @@ #pragma once #include "scene/components/physics/2d/CharacterController2D.hpp" #include "scene/components/HurtHazard.hpp" -#include "scene/components/EntityHealth.hpp" +#include "scene/components/entity/EntityHealth.hpp" +#include "scene/components/entity/EntityMove.hpp" namespace Dawn { class PlayerController : public SceneItemComponent { protected: CharacterController2D *characterController; EntityHealth *entityHealth; + EntityMove *entityMove; public: - // @optional - float_t moveSpeed = 60.0f; // @optional float_t hitKnockback = 20.0f; diff --git a/src/dawnrose/scene/components/entity/CMakeLists.txt b/src/dawnrose/scene/components/entity/CMakeLists.txt new file mode 100644 index 00000000..8c97609a --- /dev/null +++ b/src/dawnrose/scene/components/entity/CMakeLists.txt @@ -0,0 +1,13 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + EntityChargePlayer.cpp + EntityHealth.cpp + EntityRandomWalk.cpp + EntityMove.cpp +) \ No newline at end of file diff --git a/src/dawnrose/scene/components/entity/EntityChargePlayer.cpp b/src/dawnrose/scene/components/entity/EntityChargePlayer.cpp new file mode 100644 index 00000000..050b0436 --- /dev/null +++ b/src/dawnrose/scene/components/entity/EntityChargePlayer.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "EntityChargePlayer.hpp" + +using namespace Dawn; + +EntityChargePlayer::EntityChargePlayer(SceneItem* item) : SceneItemComponent(item) { + +} + +std::vector EntityChargePlayer::getDependencies() { + return { + (characterController = item->getComponent()) + }; +} + +void EntityChargePlayer::onStart() { + assertNotNull(characterController); + + useEvent([&](float_t delta) { + // Get the direction to move in + PlayerController * player = getScene()->findComponent(); + if(player == nullptr) return; + + // Get the direction to move in + glm::vec2 pos = glm::vec2( + player->transform->getLocalPosition().x, + player->transform->getLocalPosition().z + ) - glm::vec2( + transform->getLocalPosition().x, + transform->getLocalPosition().z + ); + + // Conver to an angle and then convert back into a 2D vector. This mimics a + // user essentially "moving the analogue stick in this direction" + + if(mathAbs(pos.x) > 1.0f || mathAbs(pos.y) > 1.0f) { + float_t angle = atan2(pos.y, pos.x); + glm::vec2 movement(cos(angle), sin(angle)); + transform->setLocalRotation(glm::quat(glm::vec3(0, -angle + 1.5708f, 0))); + characterController->velocity += movement * delta * moveSpeed; + } + }, getScene()->eventSceneUpdate); +} \ No newline at end of file diff --git a/src/dawnrose/scene/components/entity/EntityChargePlayer.hpp b/src/dawnrose/scene/components/entity/EntityChargePlayer.hpp new file mode 100644 index 00000000..c89c9bfe --- /dev/null +++ b/src/dawnrose/scene/components/entity/EntityChargePlayer.hpp @@ -0,0 +1,24 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneItemComponent.hpp" +#include "scene/components/physics/2d/CharacterController2D.hpp" +#include "scene/components/PlayerController.hpp" + +namespace Dawn { + class EntityChargePlayer : public SceneItemComponent { + protected: + CharacterController2D *characterController = nullptr; + + public: + // @optional + float_t moveSpeed = 30.0f; + + EntityChargePlayer(SceneItem* item); + std::vector getDependencies() override; + void onStart() override; + }; +} \ No newline at end of file diff --git a/src/dawnrose/scene/components/EntityHealth.cpp b/src/dawnrose/scene/components/entity/EntityHealth.cpp similarity index 100% rename from src/dawnrose/scene/components/EntityHealth.cpp rename to src/dawnrose/scene/components/entity/EntityHealth.cpp diff --git a/src/dawnrose/scene/components/EntityHealth.hpp b/src/dawnrose/scene/components/entity/EntityHealth.hpp similarity index 100% rename from src/dawnrose/scene/components/EntityHealth.hpp rename to src/dawnrose/scene/components/entity/EntityHealth.hpp diff --git a/src/dawnrose/scene/components/entity/EntityMove.cpp b/src/dawnrose/scene/components/entity/EntityMove.cpp new file mode 100644 index 00000000..129a1f22 --- /dev/null +++ b/src/dawnrose/scene/components/entity/EntityMove.cpp @@ -0,0 +1,38 @@ +// 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 EntityMove::getDependencies() { + return { + (characterController = item->getComponent()), + (this->entityHealth = item->getComponent()) + }; +} + +void EntityMove::onStart() { + assertNotNull(this->characterController); + assertNotNull(this->entityHealth); + + useEvent([&](float_t delta) { + // Don't move if stunned. + 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(cos(angle), sin(angle)); + transform->setLocalRotation(glm::quat(glm::vec3(0, -angle + 1.5708f, 0))); + characterController->velocity += movement * delta * moveSpeed; + }, getScene()->eventSceneUpdate); +} \ No newline at end of file diff --git a/src/dawnrose/scene/components/entity/EntityMove.hpp b/src/dawnrose/scene/components/entity/EntityMove.hpp new file mode 100644 index 00000000..9b754841 --- /dev/null +++ b/src/dawnrose/scene/components/entity/EntityMove.hpp @@ -0,0 +1,27 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneItemComponent.hpp" +#include "scene/components/physics/2d/CharacterController2D.hpp" +#include "scene/components/entity/EntityHealth.hpp" + +namespace Dawn { + class EntityMove : public SceneItemComponent { + protected: + CharacterController2D *characterController = nullptr; + EntityHealth *entityHealth = nullptr; + + public: + // @optional + float_t moveSpeed = 30.0f; + // @optional + glm::vec2 direction = glm::vec2(0, 0); + + EntityMove(SceneItem* item); + std::vector getDependencies() override; + void onStart() override; + }; +} \ No newline at end of file diff --git a/src/dawnrose/scene/components/EntityRandomWalk.cpp b/src/dawnrose/scene/components/entity/EntityRandomWalk.cpp similarity index 70% rename from src/dawnrose/scene/components/EntityRandomWalk.cpp rename to src/dawnrose/scene/components/entity/EntityRandomWalk.cpp index 468ffb8f..940df824 100644 --- a/src/dawnrose/scene/components/EntityRandomWalk.cpp +++ b/src/dawnrose/scene/components/entity/EntityRandomWalk.cpp @@ -13,7 +13,8 @@ EntityRandomWalk::EntityRandomWalk(SceneItem* item) : SceneItemComponent(item) { std::vector EntityRandomWalk::getDependencies() { return { - (characterController = item->getComponent()) + (characterController = item->getComponent()), + (this->entityMove = item->getComponent()) }; } @@ -34,20 +35,14 @@ void EntityRandomWalk::onStart() { } // Get the direction to move in - glm::vec2 pos = destination - glm::vec2( + glm::vec2 diff = destination - glm::vec2( transform->getLocalPosition().x, transform->getLocalPosition().z ); - - // Conver to an angle and then convert back into a 2D vector. This mimics a - // user essentially "moving the analogue stick in this direction" - - if(mathAbs(pos.x) > 1.0f || mathAbs(pos.y) > 1.0f) { - float_t angle = atan2(pos.y, pos.x); - glm::vec2 movement(cos(angle), sin(angle)); - transform->setLocalRotation(glm::quat(glm::vec3(0, -angle + 1.5708f, 0))); - characterController->velocity += movement * delta * moveSpeed; + if(glm::distance(glm::vec2(), diff) <= 1.0f) { + this->entityMove->direction = glm::vec2(0, 0); + } else { + this->entityMove->direction = diff; } - }, getScene()->eventSceneUpdate); } \ No newline at end of file diff --git a/src/dawnrose/scene/components/EntityRandomWalk.hpp b/src/dawnrose/scene/components/entity/EntityRandomWalk.hpp similarity index 89% rename from src/dawnrose/scene/components/EntityRandomWalk.hpp rename to src/dawnrose/scene/components/entity/EntityRandomWalk.hpp index 90644efb..ba673a56 100644 --- a/src/dawnrose/scene/components/EntityRandomWalk.hpp +++ b/src/dawnrose/scene/components/entity/EntityRandomWalk.hpp @@ -4,21 +4,20 @@ // https://opensource.org/licenses/MIT #pragma once -#include "scene/SceneItemComponent.hpp" #include "util/random.hpp" #include "scene/components/physics/2d/CharacterController2D.hpp" +#include "scene/components/entity/EntityMove.hpp" namespace Dawn { class EntityRandomWalk : public SceneItemComponent { protected: CharacterController2D *characterController = nullptr; + EntityMove *entityMove = nullptr; public: // @optional float_t timeLeftUntilNextDecision = 0.0f; // @optional - float_t moveSpeed = 30.0f; - // @optional glm::vec2 destination = glm::vec2(0, 0); // @optional glm::vec2 walkRange = glm::vec2(12.0f, 12.0f);