Add nativescript component, camera controller
This commit is contained in:
@@ -1,14 +1,17 @@
|
|||||||
#ifndef COMPONENTS_H
|
#ifndef COMPONENTS_H
|
||||||
#define COMPONENTS_H
|
#define COMPONENTS_H
|
||||||
|
|
||||||
#include <memory> // std::shared_ptr
|
#include <memory> // std::shared_ptr
|
||||||
#include <string> // std::string
|
#include <string> // std::string
|
||||||
|
#include <functional> // std::function
|
||||||
|
|
||||||
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
|
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
|
||||||
#include "glm/ext/vector_float3.hpp" // glm::vec3
|
#include "glm/ext/vector_float3.hpp" // glm::vec3
|
||||||
|
|
||||||
|
#include "inferno/assertions.h"
|
||||||
#include "inferno/log.h"
|
#include "inferno/log.h"
|
||||||
#include "inferno/render/texture.h"
|
#include "inferno/render/texture.h"
|
||||||
|
#include "inferno/script/nativescript.h"
|
||||||
|
|
||||||
namespace Inferno {
|
namespace Inferno {
|
||||||
|
|
||||||
@@ -51,10 +54,28 @@ namespace Inferno {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct SpriteComponent {
|
struct SpriteComponent {
|
||||||
glm::vec4 color;
|
glm::vec4 color { 1.0f };
|
||||||
std::shared_ptr<Texture> texture;
|
std::shared_ptr<Texture> texture;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct NativeScriptComponent {
|
||||||
|
NativeScript* instance = nullptr;
|
||||||
|
|
||||||
|
std::function<void()> initialize = nullptr;
|
||||||
|
std::function<void()> destroy = nullptr;
|
||||||
|
|
||||||
|
// Dont allow manually setting instance during construction
|
||||||
|
NativeScriptComponent() {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void bind()
|
||||||
|
{
|
||||||
|
ASSERT(initialize == nullptr && destroy == nullptr, "NativeScript already bound");
|
||||||
|
initialize = [&]() { instance = static_cast<NativeScript*>(new T()); };
|
||||||
|
destroy = [&]() { delete instance; instance = nullptr; initialize = nullptr; destroy = nullptr; };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
|
|
||||||
const LogStream& operator<<(const LogStream& stream, const TransformComponent& value);
|
const LogStream& operator<<(const LogStream& stream, const TransformComponent& value);
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#include "inferno/script/cameracontroller.h"
|
||||||
|
|
||||||
|
namespace Inferno {
|
||||||
|
|
||||||
|
//..
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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/ext/matrix_clip_space.hpp> // glm::perspective, glm::ortho
|
||||||
|
#include <glm/ext/matrix_transform.hpp> // 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<CameraComponent>();
|
||||||
|
|
||||||
|
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
|
||||||
@@ -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<typename T>
|
||||||
|
T& getComponent() const
|
||||||
|
{
|
||||||
|
return m_scene->getComponent<T>(m_entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
TransformComponent* transform = nullptr;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Scene* m_scene = nullptr;
|
||||||
|
uint32_t m_entity = 0;
|
||||||
|
|
||||||
|
friend class ScriptSystem;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // NATIVE_SCRIPT_H
|
||||||
Reference in New Issue
Block a user