diff --git a/src/Events/BindMouseButton.h b/src/Events/BindMouseButton.h index 1f46647..e7fb8a4 100644 --- a/src/Events/BindMouseButton.h +++ b/src/Events/BindMouseButton.h @@ -10,6 +10,7 @@ struct BindMouseButton : Event { int Button; std::string Command; + float Value; }; } diff --git a/src/Events/LockMouse.h b/src/Events/LockMouse.h new file mode 100644 index 0000000..b045c55 --- /dev/null +++ b/src/Events/LockMouse.h @@ -0,0 +1,14 @@ +#ifndef Events_LockMouse_h__ +#define Events_LockMouse_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct LockMouse : Event { }; +struct UnlockMouse : Event { }; + +} + +#endif // Events_LockMouse_h__ \ No newline at end of file diff --git a/src/Factory.h b/src/Factory.h index adcfbbf..80649ee 100755 --- a/src/Factory.h +++ b/src/Factory.h @@ -10,12 +10,18 @@ template class Factory { public: - void Register(std::string name, std::function factoryFunction) + /*void Register(std::string name, std::function factoryFunction) { m_FactoryFunctions[name] = factoryFunction; + }*/ + + template + void Register(std::function factoryFunction) + { + m_FactoryFunctions[typeid(T2).name()] = factoryFunction; } - T Create(std::string name) + /*T Create(std::string name) { auto it = m_FactoryFunctions.find(name); if (it != m_FactoryFunctions.end()) @@ -26,6 +32,20 @@ public: { return nullptr; } + }*/ + + template + T Create() + { + auto it = m_FactoryFunctions.find(typeid(T2).name()); + if (it != m_FactoryFunctions.end()) + { + return it->second(); + } + else + { + return nullptr; + } } private: diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 1daaaa9..468d356 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -13,20 +13,20 @@ void GameWorld::Initialize() BindKey(GLFW_KEY_A, "horizontal", -1.f); BindKey(GLFW_KEY_D, "horizontal", 1.f); BindGamepadAxis(Gamepad::Axis::LeftX, "horizontal", 1.f); - BindGamepadAxis(Gamepad::Axis::RightTrigger, "vertical", 1.f); - BindGamepadAxis(Gamepad::Axis::LeftTrigger, "vertical", -1.f); - + BindGamepadAxis(Gamepad::Axis::LeftY, "vertical", 1.f); + BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); BindKey(GLFW_KEY_DOWN, "barrel_rotation", -1.f); BindKey(GLFW_KEY_LEFT, "tower_rotation", -1.f); BindKey(GLFW_KEY_RIGHT, "tower_rotation", 1.f); BindGamepadAxis(Gamepad::Axis::RightX, "tower_rotation", 1.f); BindGamepadAxis(Gamepad::Axis::RightY, "barrel_rotation", 1.f); - + BindKey(GLFW_KEY_SPACE, "handbrake", 1.f); BindGamepadButton(Gamepad::Button::A, "handbrake", 1.f); BindKey(GLFW_KEY_Z, "shoot", 1.f); + BindGamepadAxis(Gamepad::Axis::RightTrigger, "shoot", 1.f); //BindGamepadButton(Gamepad::Button::Up, "Gamepad::Button::Up", 1.f); //BindGamepadButton(Gamepad::Button::Down, "Gamepad::Button::Down", 1.f); @@ -42,46 +42,24 @@ void GameWorld::Initialize() //BindGamepadButton(Gamepad::Button::B, "Gamepad::Button::B", 1.f); //BindGamepadButton(Gamepad::Button::X, "Gamepad::Button::X", 1.f); //BindGamepadButton(Gamepad::Button::Y, "Gamepad::Button::Y", 1.f); -// -// BindKey(GLFW_KEY_UP, "vertical", -1.f); -// BindKey(GLFW_KEY_DOWN, "vertical", 1.f); -// BindKey(GLFW_KEY_LEFT, "horizontal", -1.f); -// BindKey(GLFW_KEY_RIGHT, "horizontal", 1.f); - /* - BindKey(GLFW_KEY_Q, "+tower_right"); - BindKey(GLFW_KEY_E, "+tower_left"); - - BindKey(GLFW_KEY_Q, "+up"); - BindKey(GLFW_KEY_LEFT_CONTROL, "+down"); - BindKey(GLFW_KEY_LEFT_ALT, "+slow"); - BindKey(GLFW_KEY_LEFT_SHIFT, "+fast"); - BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack"); - BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2"); - BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3"); - - - BindKey(GLFW_KEY_UP, "+cam_forward"); - BindKey(GLFW_KEY_DOWN, "+cam_backward"); - BindKey(GLFW_KEY_LEFT, "+cam_left"); - BindKey(GLFW_KEY_RIGHT, "+cam_right");*/ RegisterComponents(); auto camera = CreateEntity(); { - auto transform = AddComponent(camera, "Transform"); + auto transform = AddComponent(camera); transform->Position.z = 20.f; transform->Position.y = 20.f; //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); - auto cameraComp = AddComponent(camera, "Camera"); + auto cameraComp = AddComponent(camera); cameraComp->FarClip = 2000.f; - auto freeSteering = AddComponent(camera, "FreeSteering"); + auto freeSteering = AddComponent(camera); } CommitEntity(camera); auto viewport1 = CreateEntity(); { - auto viewport = AddComponent(viewport1, "Viewport"); + auto viewport = AddComponent(viewport1); viewport->Right = 0.5f; viewport->Camera = camera; } @@ -89,29 +67,29 @@ void GameWorld::Initialize() auto viewport2 = CreateEntity(); { - auto viewport = AddComponent(viewport2, "Viewport"); + auto viewport = AddComponent(viewport2); viewport->Left = 0.5f; } CommitEntity(viewport2); { auto ground = CreateEntity(); - auto transform = AddComponent(ground, "Transform"); + auto transform = AddComponent(ground); transform->Position = glm::vec3(0, 0, 0); //transform->Scale = glm::vec3(400.0f, 10.0f, 400.0f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(ground, "Model"); + auto model = AddComponent(ground); //model->ModelFile = "Models/TestScene/testScene.obj"; model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; - auto physics = AddComponent(ground, "Physics"); + auto physics = AddComponent(ground); physics->Mass = 10; physics->Static = true; auto groundshape = CreateEntity(ground); - auto transformshape = AddComponent(groundshape, "Transform"); - auto meshShape = AddComponent(groundshape, "MeshShape"); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; //meshShape->ResourceName = "Models/TestScene/testScene.obj"; @@ -124,23 +102,23 @@ void GameWorld::Initialize() /*{ auto jeep = CreateEntity(); - auto transform = AddComponent(jeep, "Transform"); + auto transform = AddComponent(jeep); transform->Position = glm::vec3(0, 5, 0); transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); - auto physics = AddComponent(jeep, "Physics"); + auto physics = AddComponent(jeep); physics->Mass = 1800; physics->Static = false; - auto vehicle = AddComponent(jeep, "Vehicle"); - AddComponent(jeep, "Input"); + auto vehicle = AddComponent(jeep); + AddComponent(jeep); { auto shape = CreateEntity(jeep); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; CommitEntity(shape); - // auto box = AddComponent(jeep, "Box"); + // auto box = AddComponent(jeep); // box->Width = 1.487f; // box->Height = 0.727f; // box->Depth = 2.594f; @@ -149,17 +127,17 @@ void GameWorld::Initialize() { auto chassis = CreateEntity(jeep); - auto transform = AddComponent(chassis, "Transform"); + auto transform = AddComponent(chassis); transform->Position = glm::vec3(0, 0, 0); // 0.6577f - auto model = AddComponent(chassis, "Model"); + auto model = AddComponent(chassis); model->ModelFile = "Models/Jeep/Chassi/chassi.obj"; } { auto lightentity = CreateEntity(jeep); - auto transform = AddComponent(lightentity, "Transform"); + auto transform = AddComponent(lightentity); transform->Position = glm::vec3(0, 15, 0); - auto light = AddComponent(lightentity, "PointLight"); + auto light = AddComponent(lightentity); light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); light->Specular = glm::vec3(1.f); light->constantAttenuation = 0.3f; @@ -174,12 +152,12 @@ void GameWorld::Initialize() float suspensionStrength = 35.f; { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.9f, 0.5546f - wheelOffset, -0.9242f); transform->Scale = glm::vec3(1.0f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; @@ -193,13 +171,13 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.9f, 0.5546f - wheelOffset, -0.9242f); transform->Scale = glm::vec3(1.0f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; @@ -213,11 +191,11 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(0.2726f, 0.2805f - wheelOffset, 1.9307f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 50; @@ -231,12 +209,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-0.2726f, 0.2805f - wheelOffset, 1.9307f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 50; @@ -254,25 +232,25 @@ void GameWorld::Initialize() { auto tank = CreateEntity(); - auto transform = AddComponent(tank, "Transform"); + auto transform = AddComponent(tank); transform->Position = glm::vec3(0, 5, 0); //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); - auto physics = AddComponent(tank, "Physics"); + auto physics = AddComponent(tank); physics->Mass = 45000; physics->Static = false; - auto vehicle = AddComponent(tank, "Vehicle"); + auto vehicle = AddComponent(tank); vehicle->MaxTorque = 5200.f; - AddComponent(tank, "TankSteering"); - AddComponent(tank, "Input"); + AddComponent(tank); + AddComponent(tank); { auto shape = CreateEntity(tank); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; CommitEntity(shape); - // auto box = AddComponent(jeep, "Box"); + // auto box = AddComponent(jeep); // box->Width = 1.487f; // box->Height = 0.727f; // box->Depth = 2.594f; @@ -281,48 +259,48 @@ void GameWorld::Initialize() { auto chassis = CreateEntity(tank); - auto transform = AddComponent(chassis, "Transform"); + auto transform = AddComponent(chassis); transform->Position = glm::vec3(0, 0, 0); - auto model = AddComponent(chassis, "Model"); + auto model = AddComponent(chassis); model->ModelFile = "Models/Tank/Fix/Chassi.obj"; } { auto tower = CreateEntity(tank); SetProperty(tower, "Name", "tower"); - auto transform = AddComponent(tower, "Transform"); + auto transform = AddComponent(tower); transform->Position = glm::vec3(0.f, 1.2f, 1.8f); - auto model = AddComponent(tower, "Model"); + auto model = AddComponent(tower); model->ModelFile = "Models/Tank/Fix/Top.obj"; - auto towerSteering = AddComponent(tower, "TowerSteering"); + auto towerSteering = AddComponent(tower); towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); towerSteering->TurnSpeed = glm::pi()/4.f; { auto barrel = CreateEntity(tower); - auto transform = AddComponent(barrel, "Transform"); + auto transform = AddComponent(barrel); transform->Position = glm::vec3(-0.018f, -0.2, -1.3f); - auto model = AddComponent(barrel, "Model"); + auto model = AddComponent(barrel); model->ModelFile = "Models/Tank/Fix/Barrel.obj"; - auto barrelSteering = AddComponent(barrel, "BarrelSteering"); + auto barrelSteering = AddComponent(barrel); barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); barrelSteering->TurnSpeed = glm::pi()/4.f; barrelSteering->ShotSpeed = 70.f; { auto shot = CreateEntity(barrel); - auto transform = AddComponent(shot, "Transform"); + auto transform = AddComponent(shot); transform->Position = glm::vec3(0.35f, 0.f, -2.f); transform->Orientation = glm::angleAxis(-glm::pi()/2.f, glm::vec3(1, 0, 0)); transform->Scale = glm::vec3(3.f); - AddComponent(shot, "Template"); - auto physics = AddComponent(shot, "Physics"); + AddComponent(shot); + auto physics = AddComponent(shot); physics->Mass = 10.f; physics->Static = false; - auto modelComponent = AddComponent(shot, "Model"); + auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; { auto shape = CreateEntity(shot); - auto transform = AddComponent(shape, "Transform"); - auto boxShape = AddComponent(shape, "BoxShape"); + auto transform = AddComponent(shape); + auto boxShape = AddComponent(shape); boxShape->Width = 0.5f; boxShape->Height = 0.5f; boxShape->Depth = 0.5f; @@ -336,23 +314,23 @@ void GameWorld::Initialize() auto cameraTower = CreateEntity(tower); { - auto transform = AddComponent(cameraTower, "Transform"); + auto transform = AddComponent(cameraTower); transform->Position.z = 11.f; transform->Position.y = 4.f; //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); - auto cameraComp = AddComponent(cameraTower, "Camera"); + auto cameraComp = AddComponent(cameraTower); cameraComp->FarClip = 2000.f; - auto freeSteering = AddComponent(cameraTower, "FreeSteering"); + //auto freeSteering = AddComponent(cameraTower); } CommitEntity(cameraTower); - GetComponent(viewport2, "Viewport")->Camera = cameraTower; + GetComponent(viewport2)->Camera = cameraTower; } { auto lightentity = CreateEntity(tank); - auto transform = AddComponent(lightentity, "Transform"); + auto transform = AddComponent(lightentity); transform->Position = glm::vec3(0, 0, 0); - auto light = AddComponent(lightentity, "PointLight"); + auto light = AddComponent(lightentity); //light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); //light->Specular = glm::vec3(1.f); /*light->ConstantAttenuation = 0.3f; @@ -371,12 +349,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -389,9 +367,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -401,12 +379,12 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -419,9 +397,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -432,12 +410,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -450,9 +428,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -462,12 +440,12 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -480,9 +458,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -495,11 +473,11 @@ void GameWorld::Initialize() //Back { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -512,9 +490,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -524,11 +502,11 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -541,9 +519,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -552,11 +530,11 @@ void GameWorld::Initialize() CommitEntity(wheel); auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); + auto transformComponent = AddComponent(entity); transformComponent->Position = glm::vec3(2,-1.7,2.0); transformComponent->Scale = glm::vec3(3,3,3); transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); - auto emitterComponent = AddComponent(entity, "ParticleEmitter"); + auto emitterComponent = AddComponent(entity); emitterComponent->SpawnCount = 2; emitterComponent->SpawnFrequency = 0.005; emitterComponent->SpreadAngle = glm::pi(); @@ -567,9 +545,9 @@ void GameWorld::Initialize() CommitEntity(entity); auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); + auto TEMP = AddComponent(particleEntity); TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); + auto spriteComponent = AddComponent(particleEntity); spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; emitterComponent->ParticleTemplate = particleEntity; @@ -578,12 +556,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -596,9 +574,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -608,11 +586,11 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -625,9 +603,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -636,11 +614,11 @@ void GameWorld::Initialize() CommitEntity(wheel); auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); + auto transformComponent = AddComponent(entity); transformComponent->Position = glm::vec3(-2,-1.7,2.0); transformComponent->Scale = glm::vec3(3,3,3); transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); - auto emitterComponent = AddComponent(entity, "ParticleEmitter"); + auto emitterComponent = AddComponent(entity); emitterComponent->SpawnCount = 2; emitterComponent->SpawnFrequency = 0.005; emitterComponent->SpreadAngle = glm::pi(); @@ -651,9 +629,9 @@ void GameWorld::Initialize() CommitEntity(entity); auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); + auto TEMP = AddComponent(particleEntity); TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); + auto spriteComponent = AddComponent(particleEntity); spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; emitterComponent->ParticleTemplate = particleEntity; @@ -661,19 +639,6 @@ void GameWorld::Initialize() } CommitEntity(tank); - { - auto camera = CreateEntity(tank); - auto transform = AddComponent(camera, "Transform"); - transform->Position.z = 30.f; - transform->Position.y = 5.f; - //transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); - transform->Orientation = glm::angleAxis(glm::pi() / 100, glm::vec3(1,0,0)); - auto cameraComp = AddComponent(camera, "Camera"); - cameraComp->FarClip = 2000.f; - AddComponent(camera, "Input"); - auto freeSteering = AddComponent(camera, "FreeSteering"); - CommitEntity(camera); - } } @@ -682,7 +647,7 @@ void GameWorld::Initialize() for(int i = 0; i < 10; i++) { auto entity = CreateEntity(); - auto transform = AddComponent(entity, "Transform"); + auto transform = AddComponent(entity); transform->Position = glm::vec3(30 + i*0.1f, 0 + i*0.1f, 10 + i*0.1f); transform->Scale = glm::vec3(0); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); @@ -690,13 +655,13 @@ void GameWorld::Initialize() std::stringstream ss; ss << "Models/Placeholders/ShatterTest/" << i+1 << ".obj"; - auto model = AddComponent(entity, "Model"); + auto model = AddComponent(entity); model->ModelFile = ss.str(); - auto physics = AddComponent(entity, "Physics"); + auto physics = AddComponent(entity); physics->Mass = 100; physics->Static = true; - auto meshShape = AddComponent(entity, "MeshShape"); + auto meshShape = AddComponent(entity); meshShape->ResourceName = ss.str(); CommitEntity(entity); @@ -709,22 +674,22 @@ void GameWorld::Initialize() for (int x = -5; x < 5; x++) { auto brick = CreateEntity(); - auto transform = AddComponent(brick, "Transform"); + auto transform = AddComponent(brick); transform->Position = glm::vec3(x + 0.01f, y * 0.3f + 0.01f, -20); transform->Position.x += (y % 2)*0.5f; transform->Scale = glm::vec3(1, 0.3f, 0.4f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(brick, "Model"); + auto model = AddComponent(brick); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - auto physics = AddComponent(brick, "Physics"); + auto physics = AddComponent(brick); physics->Mass = 3; auto shape = CreateEntity(brick); - auto transformshape = AddComponent(shape, "Transform"); - auto box = AddComponent(shape, "BoxShape"); + auto transformshape = AddComponent(shape); + auto box = AddComponent(shape); box->Width = 0.5f; box->Height = 0.15f; box->Depth = 0.3f; @@ -738,16 +703,16 @@ void GameWorld::Initialize() for (int y = 0; y < 5; y++) { auto cube = CreateEntity(); - auto transform = AddComponent(cube, "Transform"); + auto transform = AddComponent(cube); transform->Position = glm::vec3(3 * x + 0.1f + -20.f, 3 * y + 0.1f + 1.f, 0); transform->Scale = glm::vec3(3); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(cube, "Model"); + auto model = AddComponent(cube); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - auto physics = AddComponent(cube, "Physics"); + auto physics = AddComponent(cube); physics->Mass = 100; - auto box = AddComponent(cube, "BoxShape"); + auto box = AddComponent(cube); box->Width = 1.5f; box->Height = 1.5f; box->Depth = 1.5f; @@ -759,7 +724,7 @@ void GameWorld::Initialize() /*{ auto entity = CreateEntity(); AddComponent(entity, "Transform"); - auto emitter = AddComponent(entity, "SoundEmitter"); + auto emitter = AddComponent(entity); emitter->Path = "Sounds/korvring.wav"; emitter->Loop = true; GetSystem("SoundSystem")->PlaySound(emitter); @@ -775,40 +740,40 @@ void GameWorld::Update(double dt) void GameWorld::RegisterComponents() { - m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); }); - m_ComponentFactory.Register("Template", []() { return new Components::Template(); }); + m_ComponentFactory.Register([]() { return new Components::Transform(); }); + m_ComponentFactory.Register([]() { return new Components::Template(); }); } void GameWorld::RegisterSystems() { - m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); }); - m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker); }); - m_SystemFactory.Register("DebugSystem", [this]() { return new Systems::DebugSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); - m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); - m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); - m_SystemFactory.Register("TankSteeringSystem", [this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); - m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this, m_EventBroker); }); - m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); - m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); + m_SystemFactory.Register([this]() { return new Systems::TransformSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::LevelGenerationSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::InputSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::DebugSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::CollisionSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::PlayerSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::SoundSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); } void GameWorld::AddSystems() { - AddSystem("TransformSystem"); - //AddSystem("LevelGenerationSystem"); - AddSystem("InputSystem"); - AddSystem("DebugSystem"); - //AddSystem("CollisionSystem"); - AddSystem("ParticleSystem"); - //AddSystem("PlayerSystem"); - AddSystem("FreeSteeringSystem"); - AddSystem("TankSteeringSystem"); - AddSystem("SoundSystem"); - AddSystem("PhysicsSystem"); - AddSystem("RenderSystem"); + AddSystem(); + //AddSystem(); + AddSystem(); + AddSystem(); + //AddSystem(); + AddSystem(); + //AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); } void GameWorld::BindKey(int keyCode, std::string command, float value) @@ -820,11 +785,12 @@ void GameWorld::BindKey(int keyCode, std::string command, float value) m_EventBroker->Publish(e); } -void GameWorld::BindMouseButton(int button, std::string command) +void GameWorld::BindMouseButton(int button, std::string command, float value) { Events::BindMouseButton e; e.Button = button; e.Command = command; + e.Value = value; m_EventBroker->Publish(e); } diff --git a/src/GameWorld.h b/src/GameWorld.h index f0c83c8..2823a7f 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -58,7 +58,7 @@ private: std::shared_ptr m_Renderer; void BindKey(int keyCode, std::string command, float value); - void BindMouseButton(int button, std::string command); + void BindMouseButton(int button, std::string command, float value); void BindGamepadAxis(Gamepad::Axis axis, std::string command, float value); void BindGamepadButton(Gamepad::Button button, std::string command, float value); }; diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 00ae7be..2e89d9f 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -6,6 +6,9 @@ void InputManager::Initialize() { m_LastGamepadAxisState = std::array(); m_LastGamepadButtonState = std::array(); + + EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &InputManager::OnLockMouse); + EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &InputManager::OnUnlockMouse); } void InputManager::Update(double dt) @@ -26,13 +29,13 @@ void InputManager::Update(double dt) { Events::KeyDown e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::KeyUp e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } @@ -52,7 +55,7 @@ void InputManager::Update(double dt) e.Button = i; e.X = x; e.Y = y; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { @@ -60,7 +63,7 @@ void InputManager::Update(double dt) e.Button = i; e.X = x; e.Y = y; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } @@ -77,7 +80,7 @@ void InputManager::Update(double dt) e.Y = m_CurrentMouseY; e.DeltaX = m_CurrentMouseDeltaX; e.DeltaY = m_CurrentMouseDeltaY; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } // // Lock mouse while holding LMB @@ -169,7 +172,7 @@ void InputManager::PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis e.GamepadID = gamepadID; e.Axis = axis; e.Value = currentValue; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } @@ -184,14 +187,30 @@ void InputManager::PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button Events::GamepadButtonDown e; e.GamepadID = gamepadID; e.Button = button; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::GamepadButtonUp e; e.GamepadID = gamepadID; e.Button = button; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } + +bool InputManager::OnLockMouse(const Events::LockMouse &event) +{ + m_MouseLocked = true; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + return true; +} + +bool InputManager::OnUnlockMouse(const Events::UnlockMouse &event) +{ + m_MouseLocked = false; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + + return true; +} \ No newline at end of file diff --git a/src/InputManager.h b/src/InputManager.h index 104d2b4..6b56c92 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -9,22 +9,24 @@ #include "Events/MousePress.h" #include "Events/MouseRelease.h" #include "Events/MouseMove.h" +#include "Events/LockMouse.h" #include "Events/GamepadAxis.h" #include "Events/GamepadButton.h" class InputManager { public: - InputManager(GLFWwindow* window, std::shared_ptr eventBroker) + InputManager(GLFWwindow* window, std::shared_ptr<::EventBroker> eventBroker) : m_GLFWWindow(window) - , m_EventBroker(eventBroker) + , EventBroker(eventBroker) , m_CurrentKeyState() , m_LastKeyState() , m_CurrentMouseState() , m_LastMouseState() , m_CurrentMouseX(0), m_CurrentMouseY(0) , m_LastMouseX(0), m_LastMouseY(0) - , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) + , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) + , m_MouseLocked(false) { Initialize(); } void Initialize(); @@ -35,8 +37,13 @@ public: private: GLFWwindow* m_GLFWWindow; - std::shared_ptr m_EventBroker; - + std::shared_ptr<::EventBroker> EventBroker; + + EventRelay m_ELockMouse; + bool OnLockMouse(const Events::LockMouse &event); + EventRelay m_EUnlockMouse; + bool OnUnlockMouse(const Events::UnlockMouse &event); + std::array m_CurrentKeyState; std::array m_LastKeyState; std::array m_CurrentMouseState; @@ -51,6 +58,7 @@ private: double m_CurrentMouseX, m_CurrentMouseY; double m_LastMouseX, m_LastMouseY; double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; + bool m_MouseLocked; void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis); void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button); diff --git a/src/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index 90e65dc..7db47af 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -7,13 +7,13 @@ void VehicleSetup::buildVehicle(World *world, const hkpWorld* physicsWorld, hkpVehicleInstance& vehicle, EntityID vehicleEntity, std::vector wheelEntities) { - auto vehicleComponent = world->GetComponent(vehicleEntity, "Vehicle"); + auto vehicleComponent = world->GetComponent(vehicleEntity); WheelData wheelData; for (int i = 0; i < wheelEntities.size(); i++) { - wheelData.WheelComponent = world->GetComponent(wheelEntities[i], "Wheel"); - wheelData.TransformComponent = world->GetComponent(wheelEntities[i], "Transform"); + wheelData.WheelComponent = world->GetComponent(wheelEntities[i]); + wheelData.TransformComponent = world->GetComponent(wheelEntities[i]); m_Wheels.push_back(wheelData); } diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index d59b475..8a5d638 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -4,7 +4,7 @@ void Systems::FreeSteeringSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("FreeSteering", []() { return new Components::FreeSteering(); }); + cf->Register([]() { return new Components::FreeSteering(); }); } void Systems::FreeSteeringSystem::Initialize() @@ -19,100 +19,90 @@ void Systems::FreeSteeringSystem::Update(double dt) void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto steering = m_World->GetComponent(entity, "FreeSteering"); + auto steering = m_World->GetComponent(entity); if (steering) { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); - glm::vec3 cameraRight = glm::vec3(m_InputController->Orientation * glm::vec4(1, 0, 0, 0)); - glm::vec3 cameraForward = glm::vec3(m_InputController->Orientation * glm::vec4(0, 0, -1, 0)); + glm::vec3 cameraRight = glm::vec3(transform->Orientation * glm::vec4(1, 0, 0, 0)); + glm::vec3 cameraForward = glm::vec3(transform->Orientation * glm::vec4(0, 0, -1, 0)); glm::vec3 movement; movement += cameraRight * m_InputController->Movement.x; movement.y += m_InputController->Movement.y; movement += cameraForward * -m_InputController->Movement.z; - transform->Position += movement * steering->Speed * m_InputController->SpeedMultiplier * (float)dt; - transform->Orientation = m_InputController->Orientation; + float speedMultiplier = 1.f; + if (m_InputController->SpeedMultiplier > 0) + speedMultiplier *= 4; + else if (m_InputController->SpeedMultiplier < 0) + speedMultiplier /= 4; + + transform->Position += movement * steering->Speed * speedMultiplier * (float)dt; + + glm::quat mouseOrientationPitch = glm::quat(m_InputController->MouseOrientation * glm::vec3(1, 0, 0)); + glm::quat mouseOrientationYaw = glm::quat(m_InputController->MouseOrientation * glm::vec3(0, 1, 0)); + + glm::vec3 controllerOrientationEuler = m_InputController->ControllerOrientation * (float)dt; + glm::quat controllerOrientationPitch = glm::quat(controllerOrientationEuler * glm::vec3(1, 0, 0)); + glm::quat controllerOrientationYaw = glm::quat(controllerOrientationEuler * glm::vec3(0, 1, 0)); + + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + //--------------------------------------------------------------------- + transform->Orientation = (mouseOrientationYaw * controllerOrientationYaw) + * transform->Orientation + * (mouseOrientationPitch * controllerOrientationPitch); + //--------------------------------------------------------------------- + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS } + + m_InputController->MouseOrientation = glm::vec3(0); } bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) { // Movement - if (event.Command == "+cam_forward") + if (event.Command == "vertical") { - Movement.z += -1.f; + Movement.z = -event.Value; } - else if (event.Command == "-cam_forward") + else if (event.Command == "horizontal") { - Movement.z -= -1.f; + Movement.x = event.Value; } - else if (event.Command == "+cam_backward") + else if (event.Command == "normal") { - Movement.z += 1.f; - } - else if (event.Command == "-cam_backward") - { - Movement.z -= 1.f; - } - else if (event.Command == "+cam_right") - { - Movement.x -= 1.f; - } - else if (event.Command == "-cam_right") - { - Movement.x += 1.f; - } - else if (event.Command == "+cam_left") - { - Movement.x -= -1.f; - } - else if (event.Command == "-cam_left") - { - Movement.x += -1.f; - } - else if (event.Command == "+up") - { - Movement.y += 1.f; - } - else if (event.Command == "-up") - { - Movement.y -= 1.f; - } - else if (event.Command == "+down") - { - Movement.y += -1.f; - } - else if (event.Command == "-down") - { - Movement.y -= -1.f; + Movement.y = event.Value; } // Speed - else if (event.Command == "+fast") + else if (event.Command == "speed") { - SpeedMultiplier *= 4.f; - } - else if (event.Command == "-fast") - { - SpeedMultiplier /= 4.f; - } - else if (event.Command == "+slow") - { - SpeedMultiplier /= 4.f; - } - else if (event.Command == "-slow") - { - SpeedMultiplier *= 4.f; + SpeedMultiplier = event.Value; } // Mouse click - else if (event.Command == "+attack") + else if (event.Command == "attack") { - OrientationActive = true; + OrientationActive = event.Value > 0; + + if (OrientationActive) + { + Events::LockMouse e; + EventBroker->Publish(e); + } + else + { + Events::UnlockMouse e; + EventBroker->Publish(e); + } } - else if (event.Command == "-attack") + + else if (event.Command == "vertical2") { - OrientationActive = false; + ControllerOrientation.x = event.Value; + } + else if (event.Command == "horizontal2") + { + ControllerOrientation.y = -event.Value; } return true; @@ -122,11 +112,7 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnMouseMove(const { if (OrientationActive) { - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS - //--------------------------------------------------------------------- - Orientation = glm::angleAxis(event.DeltaX / 300.f, glm::vec3(0, -1, 0)) * Orientation * glm::angleAxis(event.DeltaY / 300.f, glm::vec3(-1, 0, 0)); - //--------------------------------------------------------------------- - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + MouseOrientation = -glm::vec3(event.DeltaY / 300.f, event.DeltaX / 300.f, 0.f); } return true; diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index 4535cbb..b94466b 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -4,6 +4,7 @@ #include "Components/Transform.h" #include "Components/FreeSteering.h" #include "InputController.h" +#include "Events/LockMouse.h" namespace Systems { @@ -31,11 +32,12 @@ class FreeSteeringSystem::FreeSteeringInputController : InputController public: FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) : InputController(eventBroker) - , SpeedMultiplier(1.f) + , SpeedMultiplier(0.f) , OrientationActive(false) { } glm::vec3 Movement; - glm::quat Orientation; + glm::vec3 MouseOrientation; + glm::vec3 ControllerOrientation; float SpeedMultiplier; bool OrientationActive; diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index 7c4cde7..dbb9ba6 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -4,7 +4,7 @@ void Systems::InputSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Input", []() { return new Components::Input(); }); + cf->Register([]() { return new Components::Input(); }); } void Systems::InputSystem::Initialize() @@ -52,8 +52,8 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event) std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] += value; - PublishCommand(0, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandKeyboardValues[command][event.KeyCode] = value; + PublishCommand(0, command, GetCommandTotalValue(command)); } return true; @@ -67,8 +67,8 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event) std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] -= value; - PublishCommand(0, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandKeyboardValues[command][event.KeyCode] = 0; + PublishCommand(0, command, GetCommandTotalValue(command));; } return true; @@ -79,7 +79,11 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandMouseButtonValues[command][event.Button] = value; + PublishCommand(0, command, GetCommandTotalValue(command)); } return true; @@ -90,7 +94,11 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandMouseButtonValues[command][event.Button] = 0; + PublishCommand(0, command, GetCommandTotalValue(command)); } return true; @@ -104,7 +112,8 @@ bool Systems::InputSystem::OnGamepadAxis(const Events::GamepadAxis &event) std::string command; float value; std::tie(command, value) = bindingIt->second; - PublishCommand(event.GamepadID + 1, command, event.Value * value); + m_CommandGamepadAxisValues[command][event.Axis] = event.Value * value; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); } return true; @@ -118,8 +127,8 @@ bool Systems::InputSystem::OnGamepadButtonDown(const Events::GamepadButtonDown & std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] += value; - PublishCommand(event.GamepadID + 1, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandGamepadButtonValues[command][event.Button] = value; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); } return true; @@ -133,8 +142,8 @@ bool Systems::InputSystem::OnGamepadButtonUp(const Events::GamepadButtonUp &even std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] -= value; - PublishCommand(event.GamepadID + 1, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandGamepadButtonValues[command][event.Button] = 0; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); } return true; @@ -164,7 +173,7 @@ bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &even } else { - m_MouseButtonBindings[event.Button] = event.Command; + m_MouseButtonBindings[event.Button] = std::make_tuple(event.Command, event.Value); LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str()); } @@ -201,6 +210,49 @@ bool Systems::InputSystem::OnBindGamepadButton(const Events::BindGamepadButton & return true; } +float Systems::InputSystem::GetCommandTotalValue(std::string command) +{ + float value = 0.f; + + auto keyboardIt = m_CommandKeyboardValues.find(command); + if (keyboardIt != m_CommandKeyboardValues.end()) + { + for (auto &key : keyboardIt->second) + { + value += key.second; + } + } + + auto mouseButtonIt = m_CommandMouseButtonValues.find(command); + if (mouseButtonIt != m_CommandMouseButtonValues.end()) + { + for (auto &button : mouseButtonIt->second) + { + value += button.second; + } + } + + auto gamepadAxisIt = m_CommandGamepadAxisValues.find(command); + if (gamepadAxisIt != m_CommandGamepadAxisValues.end()) + { + for (auto &axis : gamepadAxisIt->second) + { + value += axis.second; + } + } + + auto gamepadButtonIt = m_CommandGamepadButtonValues.find(command); + if (gamepadButtonIt != m_CommandGamepadButtonValues.end()) + { + for (auto &button : gamepadButtonIt->second) + { + value += button.second; + } + } + + return std::max(-1.f, std::min(value, 1.f)); +} + void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value) { Events::InputCommand e; @@ -211,5 +263,3 @@ void Systems::InputSystem::PublishCommand(int playerID, std::string command, flo LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID); } - - diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index ce9a7f3..d13c249 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -34,10 +34,13 @@ public: void Update(double dt) override; private: - std::unordered_map m_CommandValues; // command string -> command current value + std::unordered_map> m_CommandKeyboardValues; // command string -> keyboard key value for command + std::unordered_map> m_CommandMouseButtonValues; // command string -> mouse button value for command + std::unordered_map> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command + std::unordered_map> m_CommandGamepadButtonValues; // command string -> gamepad button value for command // Input binding tables std::unordered_map> m_KeyBindings; // GLFW_KEY... -> command string & value - std::unordered_map m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string + std::unordered_map> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string std::unordered_map> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value std::unordered_map> m_GamepadButtonBindings; // Gamepad::Button -> command string @@ -66,6 +69,7 @@ private: EventRelay m_EBindGamepadButton; bool OnBindGamepadButton(const Events::BindGamepadButton &event); + float GetCommandTotalValue(std::string command); void PublishCommand(int playerID, std::string command, float value); }; diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 0882f9d..5bcd97f 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -6,7 +6,7 @@ void Systems::ParticleSystem::Initialize() { - m_TransformSystem = m_World->GetSystem("TransformSystem"); + m_TransformSystem = m_World->GetSystem(); } void Systems::ParticleSystem::Update(double dt) @@ -16,15 +16,15 @@ void Systems::ParticleSystem::Update(double dt) void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if(!transformComponent) return; - auto emitterComponent = m_World->GetComponent(entity, "ParticleEmitter"); + auto emitterComponent = m_World->GetComponent(entity); if(emitterComponent) { emitterComponent->TimeSinceLastSpawn += dt; - auto emitterTransformComponent = m_World->GetComponent(entity, "Transform"); + auto emitterTransformComponent = m_World->GetComponent(entity); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { SpawnParticles(entity); @@ -35,8 +35,8 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { EntityID particleID = (it)->ParticleID; - auto transformComponent = m_World->GetComponent(particleID, "Transform"); - auto particleComponent = m_World->GetComponent(particleID, "Particle"); + auto transformComponent = m_World->GetComponent(particleID); + auto particleComponent = m_World->GetComponent(particleID); double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) @@ -94,15 +94,15 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); - cf->Register("Particle", []() { return new Components::Particle(); }); + cf->Register([]() { return new Components::ParticleEmitter(); }); + cf->Register([]() { return new Components::Particle(); }); } void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) { - auto emitterComponent = m_World->GetComponent(emitterID, "ParticleEmitter"); - auto emitterTransform = m_World->GetComponent(emitterID, "Transform"); + auto emitterComponent = m_World->GetComponent(emitterID); + auto emitterTransform = m_World->GetComponent(emitterID); glm::vec3 emitterPos = m_TransformSystem->AbsolutePosition(emitterID); glm::quat emitterOrientation = emitterTransform->Orientation; @@ -113,7 +113,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) { auto ent = m_World->CloneEntity(emitterComponent->ParticleTemplate); - auto particleTransform = m_World->GetComponent(ent, "Transform"); + auto particleTransform = m_World->GetComponent(ent); particleTransform->Position = emitterPos; particleTransform->Orientation = emitterOrientation; @@ -124,7 +124,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1))); - auto particle = m_World->AddComponent(ent, "Particle"); + auto particle = m_World->AddComponent(ent); particle->LifeTime = emitterComponent->LifeTime; particle->ScaleSpectrum = emitterComponent->ScaleSpectrum; particle->VelocitySpectrum.push_back(particleTransform->Velocity); diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 8efa4bb..1fd737b 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -108,14 +108,14 @@ void Systems::PhysicsSystem::Initialize() void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Physics", []() { return new Components::Physics(); }); - cf->Register("BoxShape", []() { return new Components::BoxShape(); }); - cf->Register("SphereShape", []() { return new Components::SphereShape(); }); - cf->Register("Vehicle", []() { return new Components::Vehicle(); }); - cf->Register("Wheel", []() { return new Components::Wheel(); }); - cf->Register("MeshShape", []() { return new Components::MeshShape(); }); - cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); - cf->Register("WheelPair", []() { return new Components::WheelPair(); }); + cf->Register([]() { return new Components::Physics(); }); + cf->Register([]() { return new Components::BoxShape(); }); + cf->Register([]() { return new Components::SphereShape(); }); + cf->Register([]() { return new Components::Vehicle(); }); + cf->Register([]() { return new Components::Wheel(); }); + cf->Register([]() { return new Components::MeshShape(); }); + cf->Register([]() { return new Components::HingeConstraint(); }); + cf->Register([]() { return new Components::WheelPair(); }); } void Systems::PhysicsSystem::Update(double dt) @@ -128,7 +128,7 @@ void Systems::PhysicsSystem::Update(double dt) if (m_RigidBodies.find(entity) == m_RigidBodies.end()) continue; - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) continue; @@ -139,7 +139,7 @@ void Systems::PhysicsSystem::Update(double dt) if (parent) { - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); position = ConvertPosition(absoluteTransform.Position); rotation = ConvertRotation(absoluteTransform.Orientation); } @@ -180,11 +180,11 @@ void Systems::PhysicsSystem::Update(double dt) void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) return; - auto wheelComponent = m_World->GetComponent(entity, "Wheel"); + auto wheelComponent = m_World->GetComponent(entity); if (wheelComponent) { EntityID car = m_World->GetEntityParent(entity); @@ -208,7 +208,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p } else if(m_RigidBodies.find(entity) != m_RigidBodies.end()) { - auto transformComponentParent = m_World->GetComponent(parent, "Transform"); + auto transformComponentParent = m_World->GetComponent(parent); transformComponent->Position = ConvertPosition(m_RigidBodies[entity]->getPosition()); transformComponent->Orientation = ConvertRotation(m_RigidBodies[entity]->getRotation()); @@ -227,11 +227,11 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) return; - auto wheelComponent = m_World->GetComponent(entity, "Wheel"); + auto wheelComponent = m_World->GetComponent(entity); if (wheelComponent) { wheelComponent->ID = m_Wheels.size(); @@ -241,9 +241,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) EntityID entityParent = m_World->GetEntityBaseParent(entity); - auto sphereComponent = m_World->GetComponent(entity, "SphereShape"); - auto boxComponent = m_World->GetComponent(entity, "BoxShape"); - auto meshShapeComponent = m_World->GetComponent(entity, "MeshShape"); + auto sphereComponent = m_World->GetComponent(entity); + auto boxComponent = m_World->GetComponent(entity); + auto meshShapeComponent = m_World->GetComponent(entity); if(entityParent == entity && (sphereComponent || boxComponent || meshShapeComponent)) { @@ -251,7 +251,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) return; } - auto physicsComponent = m_World->GetComponent(entity, "Physics"); + auto physicsComponent = m_World->GetComponent(entity); if (physicsComponent) { hkpShape* shape; @@ -292,7 +292,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC; - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); hkVector4 position = ConvertPosition(absoluteTransform.Position); hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); @@ -305,7 +305,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) // Create RigidBody hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); - auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); + auto vehicleComponent = m_World->GetComponent(entity); if (vehicleComponent && m_Vehicles.find(entity) == m_Vehicles.end()) { for (int i = 0; i < m_Wheels.size(); i++) @@ -357,7 +357,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) for (auto &shapeData : m_Shapes[entity]) { - auto childTransformComponent = m_World->GetComponent(shapeData.Entity, "Transform"); + auto childTransformComponent = m_World->GetComponent(shapeData.Entity); hkVector4 position = ConvertPosition(childTransformComponent->Position); hkQuaternion rotation = ConvertRotation(childTransformComponent->Orientation); @@ -378,7 +378,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); hkVector4 position = ConvertPosition(absoluteTransform.Position); hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); @@ -567,7 +567,7 @@ const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale) bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) { - auto vehicleComponent = m_World->GetComponent(event.Entity, "Vehicle"); + auto vehicleComponent = m_World->GetComponent(event.Entity); if (vehicleComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end()) { m_PhysicsWorld->markForWrite(); diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index c86849a..df48c92 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -11,26 +11,26 @@ void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Camera", []() { return new Components::Camera(); }); - cf->Register("Model", []() { return new Components::Model(); }); - cf->Register("Sprite", []() { return new Components::Sprite(); }); - cf->Register("PointLight", []() { return new Components::PointLight(); }); - cf->Register("DirectionalLight", []() { return new Components::DirectionalLight(); }); - cf->Register("Viewport", []() { return new Components::Viewport(); }); + cf->Register([]() { return new Components::Camera(); }); + cf->Register([]() { return new Components::Model(); }); + cf->Register([]() { return new Components::Sprite(); }); + cf->Register([]() { return new Components::PointLight(); }); + cf->Register([]() { return new Components::DirectionalLight(); }); + cf->Register([]() { return new Components::Viewport(); }); } void Systems::RenderSystem::OnEntityCommit(EntityID entity) { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); - auto camera = m_World->GetComponent(entity, "Camera"); + auto camera = m_World->GetComponent(entity); if (transform && camera) { m_Renderer->RegisterCamera(entity, camera->FOV, camera->NearClip, camera->FarClip); m_Renderer->UpdateCamera(entity, m_TransformSystem->AbsolutePosition(entity), m_TransformSystem->AbsoluteOrientation(entity), camera->FOV, camera->NearClip, camera->FarClip); } - auto viewport = m_World->GetComponent(entity, "Viewport"); + auto viewport = m_World->GetComponent(entity); if (viewport) { m_Renderer->RegisterViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom); @@ -43,10 +43,10 @@ void Systems::RenderSystem::OnEntityCommit(EntityID entity) void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); // Draw models - auto modelComponent = m_World->GetComponent(entity, "Model"); + auto modelComponent = m_World->GetComponent(entity); if (transformComponent&& modelComponent) { auto model = m_World->GetResourceManager()->Load("Model", modelComponent->ModelFile); @@ -60,7 +60,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } } - auto pointLightComponent = m_World->GetComponent(entity, "PointLight"); + auto pointLightComponent = m_World->GetComponent(entity); if (transformComponent && pointLightComponent) { glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); @@ -75,7 +75,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa ); } - auto cameraComponent = m_World->GetComponent(entity, "Camera"); + auto cameraComponent = m_World->GetComponent(entity); if (transformComponent && cameraComponent) { m_Renderer->UpdateCamera(entity @@ -86,7 +86,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa , cameraComponent->FarClip); } - auto viewportComponent = m_World->GetComponent(entity, "Viewport"); + auto viewportComponent = m_World->GetComponent(entity); if (viewportComponent) { if (viewportComponent->Camera != 0) @@ -95,13 +95,13 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } } - auto spriteComponent = m_World->GetComponent(entity, "Sprite"); + auto spriteComponent = m_World->GetComponent(entity); if (transformComponent && spriteComponent) { //TEMP Texture* texture = m_World->GetResourceManager()->Load("Texture", spriteComponent->SpriteFile); //glBindTexture(GL_TEXTURE_2D, texture); - auto transform = m_World->GetComponent(spriteComponent->Entity, "Transform"); + auto transform = m_World->GetComponent(spriteComponent->Entity); glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(transform->Orientation).z, glm::vec3(0, 0, -1)); m_Renderer->AddTextureToDraw(texture, transform->Position, orientation2D, transform->Scale); } @@ -109,7 +109,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa void Systems::RenderSystem::Initialize() { - m_TransformSystem = m_World->GetSystem("TransformSystem"); + m_TransformSystem = m_World->GetSystem(); m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj")); } + diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index 02a665e..b36bf47 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -29,7 +29,7 @@ void Systems::SoundSystem::Initialize() void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("SoundEmitter", []() { return new Components::SoundEmitter(); }); + cf->Register([]() { return new Components::SoundEmitter(); }); } void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm) @@ -44,7 +44,7 @@ void Systems::SoundSystem::Update(double dt) void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (transformComponent == nullptr) return; @@ -68,7 +68,7 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par alListenerfv(AL_ORIENTATION, listenerOri); } - auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); + auto soundEmitter = m_World->GetComponent(entity); if(soundEmitter != nullptr) { ALuint source = m_Sources[soundEmitter]; diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 6ecbfb9..dca3441 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -4,9 +4,9 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf ) { - cf->Register("TankSteering", []() { return new Components::TankSteering(); }); - cf->Register("TowerSteering", []() { return new Components::TowerSteering(); }); - cf->Register("BarrelSteering", []() { return new Components::BarrelSteering(); }); + cf->Register([]() { return new Components::TankSteering(); }); + cf->Register([]() { return new Components::TowerSteering(); }); + cf->Register([]() { return new Components::BarrelSteering(); }); } void Systems::TankSteeringSystem::Initialize() @@ -23,7 +23,7 @@ void Systems::TankSteeringSystem::Update(double dt) void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto tankSteeringComponent = m_World->GetComponent(entity, "TankSteering"); + auto tankSteeringComponent = m_World->GetComponent(entity); if(tankSteeringComponent) { Events::TankSteer e; @@ -34,27 +34,27 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit EventBroker->Publish(e); } - auto towerSteeringComponent = m_World->GetComponent(entity, "TowerSteering"); + auto towerSteeringComponent = m_World->GetComponent(entity); if(towerSteeringComponent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); transformComponent->Orientation *= orientation; } - auto barrelSteeringComponent = m_World->GetComponent(entity, "BarrelSteering"); + auto barrelSteeringComponent = m_World->GetComponent(entity); if(barrelSteeringComponent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto transformComponent = m_World->GetComponent(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); glm::quat orientation = glm::angleAxis(barrelSteeringComponent->TurnSpeed * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis); transformComponent->Orientation *= orientation; if(m_TowerInputController->Shoot && m_TimeSinceLastShot[entity] > 1.0) { EntityID clone = m_World->CloneEntity(barrelSteeringComponent->ShotTemplate); - auto templateAbsoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(barrelSteeringComponent->ShotTemplate); - auto cloneTransform = m_World->GetComponent(clone, "Transform"); + auto templateAbsoluteTransform = m_World->GetSystem()->AbsoluteTransform(barrelSteeringComponent->ShotTemplate); + auto cloneTransform = m_World->GetComponent(clone); cloneTransform->Position = templateAbsoluteTransform.Position; cloneTransform->Orientation = absoluteTransform.Orientation * cloneTransform->Orientation; Events::SetVelocity e; diff --git a/src/Systems/TransformSystem.cpp b/src/Systems/TransformSystem.cpp index 3eccc26..69ec6fb 100755 --- a/src/Systems/TransformSystem.cpp +++ b/src/Systems/TransformSystem.cpp @@ -7,8 +7,8 @@ // if (parent == 0) // return; // -// auto transform = m_World->GetComponent(entity, "Transform"); -// auto parentTransform = m_World->GetComponent(parent, "Transform"); +// auto transform = m_World->GetComponent(entity); +// auto parentTransform = m_World->GetComponent(parent); // // transform->Position = parentTransform->Position + transform->RelativePosition; //} @@ -20,10 +20,10 @@ glm::vec3 Systems::TransformSystem::AbsolutePosition(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); //absPosition += transform->Position; entity = m_World->GetEntityParent(entity); - auto transform2 = m_World->GetComponent(entity, "Transform"); + auto transform2 = m_World->GetComponent(entity); if (entity == 0) absPosition += transform->Position; else @@ -39,7 +39,7 @@ glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); absOrientation = transform->Orientation * absOrientation; entity = m_World->GetEntityParent(entity); } while (entity != 0); @@ -53,7 +53,7 @@ glm::vec3 Systems::TransformSystem::AbsoluteScale(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); absScale *= transform->Scale; entity = m_World->GetEntityParent(entity); } while (entity != 0); @@ -69,9 +69,9 @@ Components::Transform Systems::TransformSystem::AbsoluteTransform(EntityID entit do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); entity = m_World->GetEntityParent(entity); - auto transform2 = m_World->GetComponent(entity, "Transform"); + auto transform2 = m_World->GetComponent(entity); // Position if (entity == 0) diff --git a/src/World.cpp b/src/World.cpp index c84a2bd..2a25856 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -132,11 +132,6 @@ void World::Initialize() } } -std::shared_ptr World::AddComponent(EntityID entity, std::string componentType) -{ - return AddComponent(entity, componentType); -} - void World::CommitEntity(EntityID entity) { for (auto pair : m_Systems) @@ -158,11 +153,6 @@ void World::AddComponent(EntityID entity, std::string componentType, std::shared } } -void World::AddSystem(std::string systemType) -{ - m_Systems[systemType] = std::shared_ptr(m_SystemFactory.Create(systemType)); -} - EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) { int clone = CreateEntity(parent); @@ -170,7 +160,7 @@ EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) for (auto pair : m_EntityComponents[entity]) { auto type = pair.first; - if (type == "Template") + if (type == typeid(Components::Template).name()) continue; auto component = std::shared_ptr(pair.second->Clone()); if (component != nullptr) diff --git a/src/World.h b/src/World.h index 0c4d76e..87bfecf 100755 --- a/src/World.h +++ b/src/World.h @@ -13,6 +13,7 @@ #include "Factory.h" #include "Entity.h" #include "Component.h" +#include "Components/Template.h" #include "System.h" #include "EventBroker.h" #include "ResourceManager.h" @@ -31,10 +32,14 @@ public: virtual void AddSystems() = 0; virtual void RegisterComponents() = 0; + template + void AddSystem() + { + m_Systems[typeid(T).name()] = std::shared_ptr(m_SystemFactory.Create()); + } - void AddSystem(std::string systemType); - template - std::shared_ptr GetSystem(std::string systemType); + template + std::shared_ptr GetSystem(); EntityID CreateEntity(EntityID parent = 0); EntityID CloneEntity(EntityID entity, EntityID parent = 0); @@ -69,10 +74,9 @@ public: } template - std::shared_ptr AddComponent(EntityID entity, std::string componentType); - std::shared_ptr AddComponent(EntityID entity, std::string componentType); + std::shared_ptr AddComponent(EntityID entity); template - T* GetComponent(EntityID entity, std::string componentType); + T* GetComponent(EntityID entity); // Triggers commit events in systems void CommitEntity(EntityID entity); @@ -117,11 +121,13 @@ protected: }; template -std::shared_ptr World::GetSystem(std::string systemType) +std::shared_ptr World::GetSystem() { + const char* systemType = typeid(T).name(); + if (m_Systems.find(systemType) == m_Systems.end()) { - LOG_WARNING("Tried to get pointer to unregistered system \"%s\"!", systemType.c_str()); + LOG_WARNING("Tried to get pointer to unregistered system \"%s\"!", systemType); return nullptr; } @@ -129,12 +135,14 @@ std::shared_ptr World::GetSystem(std::string systemType) } template -std::shared_ptr World::AddComponent(EntityID entity, std::string componentType) +std::shared_ptr World::AddComponent(EntityID entity) { - std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create(componentType))); + const char* componentType = typeid(T).name(); + + std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create())); if (component == nullptr) { - LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType.c_str(), entity); + LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType, entity); return nullptr; } @@ -145,17 +153,11 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp template -T* World::GetComponent(EntityID entity, std::string componentType) +T* World::GetComponent(EntityID entity) { - - /*auto it0 = m_EntityComponents.find(entity); - - if (it0 == m_EntityComponents.end()) - return nullptr;*/ - auto components = m_EntityComponents[entity]; - auto it = components.find(componentType); + auto it = components.find(typeid(T).name()); if (it != components.end()) { return static_cast(it->second.get()); diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index f3e136e..8700594 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -162,6 +162,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 078900d..a3379e2 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -353,7 +353,10 @@ Input\Events - Rendering\Components + Physics\Components + + + Input\Events