diff --git a/src/dawn/display/Transform.hpp b/src/dawn/display/Transform.hpp index ab73b8fe..e3708b8a 100644 --- a/src/dawn/display/Transform.hpp +++ b/src/dawn/display/Transform.hpp @@ -8,6 +8,7 @@ #include "assert/assert.hpp" #include "util/flag.hpp" #include "event/Event.hpp" +#include "state/StateEvent.hpp" namespace Dawn { class SceneItem; @@ -40,7 +41,8 @@ namespace Dawn { public: // I have no idea if I'm keeping this - Event<> eventTransformUpdated; + // Event<> eventTransformUpdated; + StateEvent<> eventTransformUpdated; SceneItem *item; /** diff --git a/src/dawn/prefab/UIPrefab.hpp b/src/dawn/prefab/UIPrefab.hpp deleted file mode 100644 index 002bb82e..00000000 --- a/src/dawn/prefab/UIPrefab.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "Prefab.hpp" -#include "ui/UIComponent.hpp" - -namespace Dawn { - template - class UIPrefab : - public Prefab - { - public: - static void apply(T *item) { - assertNotNull(item); - P::prefabApply(&item->getGame()->assetManager , item); - } - - static T * prefabCreate(UICanvas *canvas) { - T * item = canvas->addElement(); - P::apply(item); - return item; - } - }; -} \ No newline at end of file diff --git a/src/dawn/scene/components/physics/2d/CharacterController2D.cpp b/src/dawn/scene/components/physics/2d/CharacterController2D.cpp index fc252e30..fb2a924b 100644 --- a/src/dawn/scene/components/physics/2d/CharacterController2D.cpp +++ b/src/dawn/scene/components/physics/2d/CharacterController2D.cpp @@ -59,6 +59,8 @@ void CharacterController2D::onStart() { } } + if(mathAbs(moveAmount.x) <= 0.001f && mathAbs(moveAmount.y) <= 0.001f) return; + transform->setLocalPosition( transform->getLocalPosition() + (glm::vec3(moveAmount.x, 0, moveAmount.y) * delta) ); diff --git a/src/dawnrose/prefabs/PlayerPrefab.hpp b/src/dawnrose/prefabs/PlayerPrefab.hpp new file mode 100644 index 00000000..088d1f71 --- /dev/null +++ b/src/dawnrose/prefabs/PlayerPrefab.hpp @@ -0,0 +1,52 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "prefab/SceneItemPrefab.hpp" +#include "scene/components/display/MeshHost.hpp" +#include "scene/components/display/MeshRenderer.hpp" +#include "scene/components/display/material/SimpleTexturedMaterial.hpp" +#include "scene/components/physics/2d/CharacterController2D.hpp" +#include "scene/components/physics/2d/BoxCollider.hpp" +#include "scene/components/PlayerController.hpp" +#include "display/mesh/CubeMesh.hpp" +#include "display/mesh/CapsuleMesh.hpp" + +namespace Dawn { + class PlayerPrefab : public SceneItemPrefab, public StateOwner { + public: + static std::vector prefabAssets(AssetManager *man) { + return {}; + } + + PlayerController *player; + + PlayerPrefab(Scene *s, sceneitemid_t i) : SceneItemPrefab(s, i) {} + + void prefabInit(AssetManager *man) override { + auto characterController = this->addComponent(); + auto hitbox = this->addComponent(); + auto meshHost = this->addComponent(); + auto meshRenderer = this->addComponent(); + auto material = this->addComponent(); + + CapsuleMesh::create(&meshHost->mesh, 0.5f, 1.5f); + + auto nose = scene->createSceneItem(); + auto noseMeshHost = nose->addComponent(); + auto noseMeshRenderer = nose->addComponent(); + auto noseMaterial = nose->addComponent(); + noseMeshHost->mesh.createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); + glm::vec3 noseSize = glm::vec3(0.25f, 0.25f, 0.25f); + CubeMesh::buffer(&noseMeshHost->mesh, -(noseSize * 0.5f), noseSize, 0, 0); + noseMaterial->color = COLOR_RED; + nose->transform.setParent(&this->transform); + nose->transform.setLocalPosition(glm::vec3(0, 0, 0.5f)); + + + player = this->addComponent(); + } + }; +} \ No newline at end of file diff --git a/src/dawnrose/scene/components/GameCamera.cpp b/src/dawnrose/scene/components/GameCamera.cpp index 7c82e9ac..13666464 100644 --- a/src/dawnrose/scene/components/GameCamera.cpp +++ b/src/dawnrose/scene/components/GameCamera.cpp @@ -4,6 +4,7 @@ // https://opensource.org/licenses/MIT #include "GameCamera.hpp" +#include "game/DawnGame.hpp" using namespace Dawn; @@ -19,11 +20,24 @@ void GameCamera::onStart() { assertNotNull(this->camera); assertNotNull(this->player); - useEvent([&](float_t delta) { - glm::vec3 cameraTarget = player->transform->getLocalPosition(); - camera->transform->lookAt( - cameraTarget + glm::vec3(0, 10.0f, 1.5f), - cameraTarget - ); + useEvent([&]{ + glm::vec3 lookOffset = player->transform->getLocalRotation() * glm::vec3(0, 0, 1.0f); + glm::vec2 target = glm::vec2(lookOffset.x, lookOffset.z) * lookOffsetScale; + slowTarget += (target - slowTarget) * getGame()->timeManager.delta * movementScrollSpeed; + }, this->player->transform->eventTransformUpdated); + + useEvent([&](float_t delta){ + if(current != slowTarget) { + float_t m = 6.0f; + float_t s = delta * 3.0f; + current += glm::vec2( + mathClamp((slowTarget.x - current.x) * s, -m, m), + mathClamp((slowTarget.y - current.y) * s, -m, m) + ); + } + + glm::vec3 current3 = glm::vec3(current.x, 0, current.y); + glm::vec3 t0 = player->transform->getLocalPosition(); + camera->transform->lookAt(t0 + current3 + zoomOffset, t0 + current3); }, getScene()->eventSceneUpdate); } \ No newline at end of file diff --git a/src/dawnrose/scene/components/GameCamera.hpp b/src/dawnrose/scene/components/GameCamera.hpp index d756fb73..25a793c6 100644 --- a/src/dawnrose/scene/components/GameCamera.hpp +++ b/src/dawnrose/scene/components/GameCamera.hpp @@ -11,8 +11,14 @@ namespace Dawn { class GameCamera : public SceneItemComponent { protected: Camera *camera = nullptr; + glm::vec2 slowTarget = glm::vec2(0, 0); + glm::vec2 current = glm::vec2(0, 0); public: + glm::vec2 lookOffsetScale = glm::vec2(6.0f, 3.0f); + float_t movementScrollSpeed = 0.5f; + glm::vec3 zoomOffset = glm::vec3(0, 30.0f, 7.5f); + PlayerController *player = nullptr; GameCamera(SceneItem *item); diff --git a/src/dawnrose/scene/components/PlayerController.cpp b/src/dawnrose/scene/components/PlayerController.cpp index 6bff107c..8c0ab88b 100644 --- a/src/dawnrose/scene/components/PlayerController.cpp +++ b/src/dawnrose/scene/components/PlayerController.cpp @@ -22,9 +22,6 @@ void PlayerController::onStart() { assertNotNull(this->characterController); useEvent([&](float_t delta){ - // auto camera = getScene()->findComponent(); - // assertNotNull(camera); - // Movement auto inputMove = getGame()->inputManager.getAxis2D( INPUT_BIND_NEGATIVE_X, INPUT_BIND_POSITIVE_X, @@ -34,6 +31,9 @@ void PlayerController::onStart() { 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); diff --git a/src/dawnrose/scene/components/PlayerController.hpp b/src/dawnrose/scene/components/PlayerController.hpp index 1050bb52..661d59fc 100644 --- a/src/dawnrose/scene/components/PlayerController.hpp +++ b/src/dawnrose/scene/components/PlayerController.hpp @@ -12,7 +12,7 @@ namespace Dawn { CharacterController2D *characterController; public: - float_t moveSpeed = 40.0f; + float_t moveSpeed = 80.0f; PlayerController(SceneItem *item); diff --git a/src/dawnrose/scenes/HelloWorldScene.hpp b/src/dawnrose/scenes/HelloWorldScene.hpp index 91e8b4e2..aacab6ff 100644 --- a/src/dawnrose/scenes/HelloWorldScene.hpp +++ b/src/dawnrose/scenes/HelloWorldScene.hpp @@ -5,16 +5,8 @@ #pragma once #include "scene/Scene.hpp" -#include "scene/components/PlayerController.hpp" #include "scene/components/GameCamera.hpp" -#include "scene/components/GameCamera.hpp" -#include "scene/components/display/MeshRenderer.hpp" -#include "scene/components/display/MeshHost.hpp" -#include "scene/components/display/material/SimpleTexturedMaterial.hpp" -#include "scene/components/example/ExampleSpin.hpp" -#include "scene/components/physics/2d/BoxCollider.hpp" -#include "scene/components/physics/2d/CharacterController2D.hpp" -#include "display/mesh/CubeMesh.hpp" +#include "prefabs/PlayerPrefab.hpp" namespace Dawn { class HelloWorldScene : public Scene { @@ -22,10 +14,7 @@ namespace Dawn { Camera *camera; void stage() override { - auto playerItem = this->createSceneItem(); - auto player = playerItem->addComponent(); - auto hitbox = playerItem->addComponent(); - playerItem->addComponent(); + auto player = PlayerPrefab::create(this); auto wallBox = this->createSceneItem()->addComponent(); wallBox->min = glm::vec2(-4, -3); @@ -36,9 +25,10 @@ namespace Dawn { wallBox2->max = glm::vec2(3, -3); camera = Camera::create(this); + camera->fov = 0.436332f; camera->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0)); - // auto gameCamera = camera->item->addComponent(); - // gameCamera->player = player; + auto gameCamera = camera->item->addComponent(); + gameCamera->player = player->player; } std::vector getRequiredAssets() override {