A gray tank! :D

This commit is contained in:
2014-04-05 01:24:19 +02:00
parent ed29297d00
commit ad2862bf84
15 changed files with 289 additions and 18 deletions
+61
View File
@@ -0,0 +1,61 @@
#include "PrecompiledHeader.h"
#include "FreeSteeringSystem.h"
#include "World.h"
Systems::FreeSteeringSystem::FreeSteeringSystem(World* world) : System(world)
{
}
void Systems::FreeSteeringSystem::Update(double dt)
{
}
void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto steering = m_World->GetComponent<Components::FreeSteering>(entity, "FreeSteering");
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (steering && input) {
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation);
glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation);
float speed = steering->Speed;
if (input->KeyState[GLFW_KEY_LEFT_SHIFT]) {
speed *= 4.0f;
}
if (input->KeyState[GLFW_KEY_LEFT_ALT]) {
speed /= 4.0f;
}
if (input->KeyState[GLFW_KEY_A]) {
transform->Position -= Camera_Right * (float)dt * speed;
}
else if (input->KeyState[GLFW_KEY_D]) {
transform->Position += Camera_Right * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_W]) {
transform->Position -= Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_S]) {
transform->Position += Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_SPACE]) {
transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_LEFT_CONTROL]) {
transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT]) {
// TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
//---------------------------------------------------------------------
transform->Orientation = glm::angleAxis<float>(input->dY / 300.f, glm::vec3(1, 0, 0)) * transform->Orientation;
transform->Orientation = transform->Orientation * glm::angleAxis<float>(input->dX / 300.f, glm::vec3(0, 1, 0));
//---------------------------------------------------------------------
// TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
}
}
}
+18
View File
@@ -0,0 +1,18 @@
#include <array>
#include "System.h"
#include "Components/Transform.h"
#include "Components/Input.h"
#include "Components/FreeSteering.h"
namespace Systems
{
class FreeSteeringSystem : public System
{
public:
FreeSteeringSystem(World* world);
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
};
}
+73
View File
@@ -0,0 +1,73 @@
#include "PrecompiledHeader.h"
#include "InputSystem.h"
#include "World.h"
void Systems::InputSystem::Update(double dt)
{
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
// Keyboard input
for (int i = 0; i <= GLFW_KEY_LAST; ++i) {
m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
}
// Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) {
m_CurrentMouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i);
}
// Cursor position
double xpos, ypos;
glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos);
m_CurrentMouseDeltaX = xpos - m_LastMouseX;
m_CurrentMouseDeltaY = ypos - m_LastMouseY;
m_LastMouseX = xpos;
m_LastMouseY = ypos;
// Lock mouse while holding LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT]) {
m_LastMouseX = m_Renderer->WIDTH / 2.f; // xpos;
m_LastMouseY = m_Renderer->HEIGHT / 2.f; // ypos;
glfwSetCursorPos(m_Renderer->GetWindow(), m_LastMouseX, m_LastMouseY);
}
// Hide/show cursor with LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && !m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT]) {
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
if (!m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT]) {
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
#ifdef DEBUG
// Wireframe
if (m_CurrentKeyState[GLFW_KEY_F1] && !m_LastKeyState[GLFW_KEY_F1]) {
m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
}
// Normals
if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2]) {
m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
}
// Bounds
if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3]) {
m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
}
#endif
}
void Systems::InputSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (input == nullptr)
return;
input->KeyState = m_CurrentKeyState;
input->LastKeyState = m_LastKeyState;
input->MouseState = m_CurrentMouseState;
input->LastMouseState = m_LastMouseState;
input->dX = m_CurrentMouseDeltaX;
input->dY = m_CurrentMouseDeltaY;
}
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_CurrentKeyState;
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_LastKeyState;
+35
View File
@@ -0,0 +1,35 @@
#ifndef InputSystem_h__
#define InputSystem_h__
#include <array>
#include "System.h"
#include "Renderer.h"
#include "Components/Input.h"
namespace Systems
{
class InputSystem : public System
{
public:
InputSystem(World* world, std::shared_ptr<Renderer> renderer)
: System(world), m_Renderer(renderer) { }
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
std::shared_ptr<Renderer> m_Renderer;
static std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
static std::array<int, GLFW_KEY_LAST+1> m_LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_CurrentMouseState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_LastMouseState;
float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
float m_LastMouseX, m_LastMouseY;
};
}
#endif // InputSystem_h__
+2 -1
View File
@@ -25,7 +25,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
auto model = m_CachedModels[modelComponent->ModelFile];
glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity);
m_Renderer->AddModelToDraw(model, position, orientation, transformComponent->Scale, modelComponent->Visible, modelComponent->ShadowCaster);
glm::vec3 scale = m_TransformSystem->AbsoluteScale(entity);
m_Renderer->AddModelToDraw(model, position, orientation, scale, modelComponent->Visible, modelComponent->ShadowCaster);
}
// Debug draw bounds
+15 -1
View File
@@ -45,4 +45,18 @@ glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity)
} while (entity != 0);
return absOrientation;
}
}
glm::vec3 Systems::TransformSystem::AbsoluteScale(EntityID entity)
{
glm::vec3 absScale(1);
do
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
absScale *= transform->Scale;
entity = m_World->GetEntityParent(entity);
} while (entity != 0);
return absScale;
}
+1
View File
@@ -18,6 +18,7 @@ namespace Systems
glm::vec3 AbsolutePosition(EntityID entity);
glm::quat AbsoluteOrientation(EntityID entity);
glm::vec3 AbsoluteScale(EntityID entity);
};
}