From ca7f4a50fb771427d8abb0d3b5bbf40225a04b5c Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 18 Jan 2021 17:11:08 +0100 Subject: [PATCH] Add nativescript component, camera controller --- inferno/src/inferno/scene/components.h | 27 ++++- .../src/inferno/script/cameracontroller.cpp | 7 ++ inferno/src/inferno/script/cameracontroller.h | 109 ++++++++++++++++++ inferno/src/inferno/script/nativescript.h | 36 ++++++ 4 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 inferno/src/inferno/script/cameracontroller.cpp create mode 100644 inferno/src/inferno/script/cameracontroller.h create mode 100644 inferno/src/inferno/script/nativescript.h diff --git a/inferno/src/inferno/scene/components.h b/inferno/src/inferno/scene/components.h index 5c4bab0..a27276b 100644 --- a/inferno/src/inferno/scene/components.h +++ b/inferno/src/inferno/scene/components.h @@ -1,14 +1,17 @@ #ifndef COMPONENTS_H #define COMPONENTS_H -#include // std::shared_ptr -#include // std::string +#include // std::shared_ptr +#include // std::string +#include // std::function #include "glm/ext/matrix_float4x4.hpp" // glm::mat4 #include "glm/ext/vector_float3.hpp" // glm::vec3 +#include "inferno/assertions.h" #include "inferno/log.h" #include "inferno/render/texture.h" +#include "inferno/script/nativescript.h" namespace Inferno { @@ -51,10 +54,28 @@ namespace Inferno { }; struct SpriteComponent { - glm::vec4 color; + glm::vec4 color { 1.0f }; std::shared_ptr texture; }; + struct NativeScriptComponent { + NativeScript* instance = nullptr; + + std::function initialize = nullptr; + std::function destroy = nullptr; + + // Dont allow manually setting instance during construction + NativeScriptComponent() {} + + template + void bind() + { + ASSERT(initialize == nullptr && destroy == nullptr, "NativeScript already bound"); + initialize = [&]() { instance = static_cast(new T()); }; + destroy = [&]() { delete instance; instance = nullptr; initialize = nullptr; destroy = nullptr; }; + } + }; + // ---------------------------------------- const LogStream& operator<<(const LogStream& stream, const TransformComponent& value); diff --git a/inferno/src/inferno/script/cameracontroller.cpp b/inferno/src/inferno/script/cameracontroller.cpp new file mode 100644 index 0000000..f4fb43f --- /dev/null +++ b/inferno/src/inferno/script/cameracontroller.cpp @@ -0,0 +1,7 @@ +#include "inferno/script/cameracontroller.h" + +namespace Inferno { + + //.. + +} diff --git a/inferno/src/inferno/script/cameracontroller.h b/inferno/src/inferno/script/cameracontroller.h new file mode 100644 index 0000000..9f9372a --- /dev/null +++ b/inferno/src/inferno/script/cameracontroller.h @@ -0,0 +1,109 @@ +#ifndef CAMERA_CONTROLLER_H +#define CAMERA_CONTROLLER_H + +#define TRANSLATE_SPEED 2.5f +#define ROTATE_SPEED 90.0f +#define ZOOM_SENSITIVITY 2.5f +#define MOUSE_SENSITIVITY 0.25f +#define NEAR_PLANE 0.1f +#define FAR_PLANE 100.0f + +#include // glm::perspective, glm::ortho +#include // glm::radians, glm::lookAt + +#include "inferno/input.h" +#include "inferno/inputcodes.h" +#include "inferno/scene/components.h" +#include "inferno/script/nativescript.h" + +namespace Inferno { + + class CameraController final : public NativeScript { + public: + CameraController() {} + virtual ~CameraController() {} + + virtual void initialize() override + { + } + + virtual void update(float deltaTime) override + { + m_camera = &getComponent(); + + if (m_camera->type == CameraType::Orthographic) { + updateOrthographic(deltaTime); + } + else if (m_camera->type == CameraType::Perspective) { + updatePerspective(deltaTime); + } + } + + void updateOrthographic(float deltaTime) + { + // Update camera rotation + + float cameraRotateSpeed = ROTATE_SPEED * (1.0f/ 60.0f); + + if (Input::isKeyPressed(KeyCode("GLFW_KEY_Q"))) { + transform->rotate.z += cameraRotateSpeed; + } + if (Input::isKeyPressed(KeyCode("GLFW_KEY_E"))) { + transform->rotate.z -= cameraRotateSpeed; + } + + if (transform->rotate.z > 180.0f) { + transform->rotate.z -= 360.0f; + } + else if (transform->rotate.z <= -180.0f) { + transform->rotate.z += 360.0f; + } + + // Update camera translation + + float cameraTranslateSpeed = TRANSLATE_SPEED * deltaTime; + + // WASD movement + if (Input::isKeyPressed(KeyCode("GLFW_KEY_W"))) { + transform->translate.x += -sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + transform->translate.y += cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + } + if (Input::isKeyPressed(KeyCode("GLFW_KEY_S"))) { + transform->translate.x -= -sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + transform->translate.y -= cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + } + if (Input::isKeyPressed(KeyCode("GLFW_KEY_A"))) { + transform->translate.x -= cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + transform->translate.y -= sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + } + if (Input::isKeyPressed(KeyCode("GLFW_KEY_D"))) { + transform->translate.x += cos(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + transform->translate.y += sin(glm::radians(transform->rotate.z)) * cameraTranslateSpeed; + } + + // Update camera zoom + + float zoomSpeed = ZOOM_SENSITIVITY * (1.0f / 60.0f); + + if (Input::isKeyPressed(KeyCode("GLFW_KEY_EQUAL"))) { + m_camera->zoomLevel -= zoomSpeed; + } + if (Input::isKeyPressed(KeyCode("GLFW_KEY_MINUS"))) { + m_camera->zoomLevel += zoomSpeed; + } + m_camera->zoomLevel = std::max(m_camera->zoomLevel, 0.25f); + m_camera->zoomLevel = std::min(m_camera->zoomLevel, 10.0f); + } + + void updatePerspective(float deltaTime) + { + + } + + private: + CameraComponent* m_camera; + }; + +} + +#endif // CAMERA_CONTROLLER_H diff --git a/inferno/src/inferno/script/nativescript.h b/inferno/src/inferno/script/nativescript.h new file mode 100644 index 0000000..df08742 --- /dev/null +++ b/inferno/src/inferno/script/nativescript.h @@ -0,0 +1,36 @@ +#ifndef NATIVE_SCRIPT_H +#define NATIVE_SCRIPT_H + +#include "inferno/scene/scene.h" + +namespace Inferno { + + struct TransformComponent; + + class NativeScript { + public: + virtual ~NativeScript() {} + + protected: + virtual void initialize() {} + virtual void destroy() {} + virtual void update(float deltaTime) { (void)deltaTime; } + + template + T& getComponent() const + { + return m_scene->getComponent(m_entity); + } + + TransformComponent* transform = nullptr; + + private: + Scene* m_scene = nullptr; + uint32_t m_entity = 0; + + friend class ScriptSystem; + }; + +} + +#endif // NATIVE_SCRIPT_H