Vehicle spawning

This commit is contained in:
2014-06-03 00:51:21 +02:00
parent 46f377c94c
commit 4596fffb91
11 changed files with 461 additions and 313 deletions
+16
View File
@@ -0,0 +1,16 @@
#ifndef Components_VehicleSpawn_h__
#define Components_VehicleSpawn_h__
#include "Component.h"
namespace Components
{
struct SpawnPoint : Component
{
virtual SpawnPoint* Clone() const override { return new SpawnPoint(*this); }
};
}
#endif // Components_VehicleSpawn_h__
+1 -9
View File
@@ -8,16 +8,8 @@ namespace Events
{
struct SpawnVehicle : Event
{
enum class VehicleType
{
Tank = 0,
Helicopter,
HRSV,
Jeep
};
int PlayerID;
VehicleType Vehicle;
std::string VehicleType;
};
}
+2 -1
View File
@@ -27,11 +27,12 @@ public:
vp1->Width = this->Width / 2.f;
//new PlayerHUD(vp1, "PlayerHUD", m_World, 1);
new VehicleSelection(vp1, "VehicleSelection", m_World, 1);
vp2 = new Viewport(worldFrame, "Viewport2", m_World);
vp2->X = vp1->Right();
vp2->Width = this->Width / 2.f;
new PlayerHUD(vp2, "PlayerHUD", m_World, 2);
//new PlayerHUD(vp2, "PlayerHUD", m_World, 2);
m_FreeCamViewport = new Viewport(worldFrame, "ViewportFreeCam", m_World);
m_FreeCamViewport->Hide();
+24 -6
View File
@@ -3,6 +3,7 @@
#include "GUI/Frame.h"
#include "GUI/HealthOverlay.h"
#include "Events/SpawnVehicle.h"
namespace GUI
{
@@ -37,14 +38,8 @@ namespace GUI
m_Background->SetTexture("Textures/GUI/VehicleSelection/1.png");
}
void Update(double dt) override
{
glm::vec2 posDiff = m_TargetCoordinate - m_CurrentCoordinate;
m_CurrentCoordinate += posDiff * 2.f * (float)dt;
@@ -76,6 +71,12 @@ namespace GUI
bool OnCommand(const Events::InputCommand &event) override
{
if (Hidden())
return false;
if (event.PlayerID != m_PlayerID)
return false;
// Menu movement
if (event.Command == "interface_horizontal" || event.Command == "interface_vertical")
{
@@ -119,6 +120,23 @@ namespace GUI
m_TargetSize = size * scale;
}
// Vehicle select
if (event.Command == "interface_confirm" && event.Value > 0)
{
Events::SpawnVehicle e;
e.PlayerID = m_PlayerID;
if (m_CurrentSelection == 0)
e.VehicleType = "Tank"; // HACK: Base on selected vehicle ID
else if (m_CurrentSelection == 1)
e.VehicleType = "Helicopter";
else if (m_CurrentSelection == 2)
e.VehicleType = "HRSV";
else if (m_CurrentSelection == 3)
e.VehicleType = "Jeep";
EventBroker->Publish(e);
Hide();
}
return true;
}
};
+31 -289
View File
@@ -19,8 +19,8 @@ void GameWorld::Initialize()
BindGamepadAxis(Gamepad::Axis::LeftY, "interface_vertical", 1.f);
BindGamepadButton(Gamepad::Button::Up, "interface_vertical", 1.f);
BindGamepadButton(Gamepad::Button::Down, "interface_vertical", -1.f);
BindKey(GLFW_KEY_SPACE, "interface_accept", 1.f);
BindGamepadButton(Gamepad::Button::A, "interface_accept", 1.f);
BindKey(GLFW_KEY_SPACE, "interface_confirm", 1.f);
BindGamepadButton(Gamepad::Button::A, "interface_confirm", 1.f);
@@ -44,6 +44,9 @@ void GameWorld::Initialize()
BindKey(GLFW_KEY_Z, "shoot", 1.f);
BindGamepadAxis(Gamepad::Axis::RightTrigger, "shoot", 1.f);
BindKey(GLFW_KEY_X, "use", 1.f);
BindGamepadButton(Gamepad::Button::X, "use", 1.f);
BindMouseButton(GLFW_MOUSE_BUTTON_1, "cam_lock", 1.f);
BindKey(GLFW_KEY_Y, "cam_vertical", 1.f);
BindKey(GLFW_KEY_H, "cam_vertical", -1.f);
@@ -114,17 +117,17 @@ void GameWorld::Initialize()
CommitEntity(ground);
}
EntityID tank1 = CreateTank(1);
/*EntityID tank1 = CreateTank(1);
{
auto transform = GetComponent<Components::Transform>(tank1);
transform->Position.z = -125.f;
transform->Position.y = -20.f;
transform->Orientation = glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0));
Events::SetViewportCamera e;
e.CameraEntity = GetProperty<EntityID>(tank1, "Camera");
e.ViewportFrame = "Viewport1";
EventBroker->Publish(e);
}
auto transform = GetComponent<Components::Transform>(tank1);
transform->Position.z = -125.f;
transform->Position.y = -20.f;
transform->Orientation = glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0));
Events::SetViewportCamera e;
e.CameraEntity = GetProperty<EntityID>(tank1, "Camera");
e.ViewportFrame = "Viewport1";
EventBroker->Publish(e);
}*/
/*EntityID tank2 = CreateTank(2);
{
@@ -176,6 +179,10 @@ void GameWorld::Initialize()
auto garage = CreateEntity();
auto transform = AddComponent<Components::Transform>(garage);
transform->Position = Position;
auto player = AddComponent<Components::Player>(garage);
player->ID = 1;
auto ElevatorBase = CreateEntity(garage);
{
@@ -282,6 +289,14 @@ void GameWorld::Initialize()
box->Depth = 6.86f;
CommitEntity(shape);
}
auto spawnPoint = CreateEntity(elevator);
{
auto transform = AddComponent<Components::Transform>(spawnPoint);
transform->Position.y = 1.f;
auto spawnPointComponent = AddComponent<Components::SpawnPoint>(spawnPoint);
}
CommitEntity(spawnPoint);
}
CommitEntity(elevator);
@@ -314,16 +329,16 @@ void GameWorld::Initialize()
auto trigger = CreateEntity(garage);
SetProperty(trigger, "Name", "ElevatorShaftTrigger");
auto transform = AddComponent<Components::Transform>(trigger);
transform->Position = glm::vec3(0.69385f, -6.7997f, 2.39938f);
transform->Position = glm::vec3(0.69385f, -6.7997f, -2.0f);
auto triggerComponent = AddComponent<Components::Trigger>(trigger);
{
auto shape = CreateEntity(trigger);
auto transform = AddComponent<Components::Transform>(shape);
transform->Position = glm::vec3(0, 0, 0);
auto box = AddComponent<Components::BoxShape>(shape);
box->Width = 1.0f;
box->Height = 10.f;
box->Depth = 1.0f;
box->Width = 2.0f;
box->Height = 12.f;
box->Depth = 2.0f;
CommitEntity(shape);
}
CommitEntity(trigger);
@@ -564,279 +579,6 @@ void GameWorld::CreateGate(glm::vec3 Position)
}
}
EntityID GameWorld::CreateTank(int playerID)
{
auto tank = CreateEntity();
auto transform = AddComponent<Components::Transform>(tank);
transform->Position = glm::vec3(0, 5, 0);
//transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0));
auto physics = AddComponent<Components::Physics>(tank);
physics->Mass = 63000 - 16000;
physics->Static = false;
auto vehicle = AddComponent<Components::Vehicle>(tank);
vehicle->MaxTorque = 8000.f;
vehicle->MaxSteeringAngle = 90.f;
vehicle->MaxSpeedFullSteeringAngle = 4.f;
auto player = AddComponent<Components::Player>(tank);
player->ID = playerID;
auto tankSteering = AddComponent<Components::TankSteering>(tank);
AddComponent<Components::Input>(tank);
auto health = AddComponent<Components::Health>(tank);
health->Amount = 100.f;
{
auto shape = CreateEntity(tank);
auto transform = AddComponent<Components::Transform>(shape);
auto meshShape = AddComponent<Components::MeshShape>(shape);
meshShape->ResourceName = "Models/Tank/TankCollisionShape.obj";
CommitEntity(shape);
}
{
auto shapeTower = CreateEntity(tank);
auto transform = AddComponent<Components::Transform>(shapeTower);
transform->Position = glm::vec3(0, 1.10633f, 1.03024f);
auto box = AddComponent<Components::BoxShape>(shapeTower);
box->Width = 1.298f;
box->Height = 0.502f;
box->Depth = 1.211f;
CommitEntity(shapeTower);
}
{
auto chassis = CreateEntity(tank);
auto transform = AddComponent<Components::Transform>(chassis);
transform->Position = glm::vec3(0, 0, 0);
auto model = AddComponent<Components::Model>(chassis);
model->ModelFile = "Models/Tank/tankBody.obj";
}
{
auto tower = CreateEntity(tank);
SetProperty(tower, "Name", "tower");
auto transform = AddComponent<Components::Transform>(tower);
transform->Position = glm::vec3(0.f, 0.68f, 0.9f);
auto model = AddComponent<Components::Model>(tower);
model->ModelFile = "Models/Tank/tankTop.obj";
auto towerSteering = AddComponent<Components::TowerSteering>(tower);
towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f);
towerSteering->TurnSpeed = glm::pi<float>() / 4.f;
{
auto barrel = CreateEntity(tower);
auto transform = AddComponent<Components::Transform>(barrel);
transform->Position = glm::vec3(-0.012f, 0.3f, -0.95);
auto model = AddComponent<Components::Model>(barrel);
model->ModelFile = "Models/Tank/tankBarrel.obj";
auto barrelSteering = AddComponent<Components::BarrelSteering>(barrel);
barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f);
barrelSteering->TurnSpeed = glm::pi<float>() / 4.f;
barrelSteering->ShotSpeed = 70.f;
barrelSteering->LowerRotationLimit = glm::radians(-10.f);
barrelSteering->UpperRotationLimit = glm::radians(40.f);
{
auto shot = CreateEntity(barrel);
auto transform = AddComponent<Components::Transform>(shot);
transform->Position = glm::vec3(0.35f, 0.f, -2.f);
transform->Orientation = glm::angleAxis(-glm::pi<float>() / 2.f, glm::vec3(1, 0, 0));
transform->Scale = glm::vec3(3.f);
AddComponent<Components::Template>(shot);
auto physics = AddComponent<Components::Physics>(shot);
physics->Mass = 25.f;
physics->Static = false;
physics->CollisionEvent = true;
auto modelComponent = AddComponent<Components::Model>(shot);
modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj";
auto tankShellComponent = AddComponent<Components::TankShell>(shot);
tankShellComponent->Damage = 20.f;
tankShellComponent->ExplosionRadius = 30.f;
tankShellComponent->ExplosionStrength = 300000.f;
{
auto shape = CreateEntity(shot);
auto transform = AddComponent<Components::Transform>(shape);
auto boxShape = AddComponent<Components::BoxShape>(shape);
boxShape->Width = 0.5f;
boxShape->Height = 0.5f;
boxShape->Depth = 0.5f;
CommitEntity(shape);
}
CommitEntity(shot);
barrelSteering->ShotTemplate = shot;
auto cameraTower = CreateEntity(barrel);
{
auto transform = AddComponent<Components::Transform>(cameraTower);
transform->Position.z = 16.f;
transform->Position.y = 4.f;
transform->Orientation = glm::quat(glm::vec3(-glm::radians(5.f), 0.f, 0.f));
auto cameraComp = AddComponent<Components::Camera>(cameraTower);
cameraComp->FarClip = 2000.f;
/*auto follow = AddComponent<Components::Follow>(cameraTower);
follow->Entity = barrel;
follow->Distance = 15.f;
follow->FollowAxis = glm::vec3(1, 1, 1);*/
}
SetProperty(tank, "Camera", cameraTower);
}
CommitEntity(barrel);
tankSteering->Barrel = barrel;
}
CommitEntity(tower);
tankSteering->Turret = tower;
}
//{
// auto lightentity = CreateEntity(tank);
// auto transform = AddComponent<Components::Transform>(lightentity);
// transform->Position = glm::vec3(0, 0, 0);
// auto light = AddComponent<Components::PointLight>(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;
// light->LinearAttenuation = 0.003f;
// light->QuadraticAttenuation = 0.002f;*/
//}
// auto wheelpair = CreateEntity(tank);
// SetProperty(wheelpair, "Name", "WheelPair");
// AddComponent(wheelpair, "WheelPairThingy");
#pragma region Wheels
//Create wheels
float wheelOffset = -0.83f;
const float suspensionStrength = 15.f;
const float springLength = 0.3f;
AddTankWheelPair(tank, glm::vec3(1.68f, wheelOffset, -1.715f), 0, true);
AddTankWheelPair(tank, glm::vec3(-1.68f, wheelOffset, -1.715f), 0, true);
AddTankWheelPair(tank, glm::vec3(1.68f, wheelOffset, 2.375), 1, false);
AddTankWheelPair(tank, glm::vec3(-1.68f, wheelOffset, 2.375), 1, false);
#pragma endregion
{
auto entity = CreateEntity(tank);
auto transformComponent = AddComponent<Components::Transform>(entity);
transformComponent->Position = glm::vec3(-2, -1.7, 2.0);
transformComponent->Scale = glm::vec3(3, 3, 3);
transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0));
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
emitterComponent->SpawnCount = 2;
emitterComponent->SpawnFrequency = 0.005;
emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
CommitEntity(entity);
auto particleEntity = CreateEntity(entity);
auto TEMP = AddComponent<Components::Transform>(particleEntity);
TEMP->Scale = glm::vec3(0);
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
emitterComponent->ParticleTemplate = particleEntity;
CommitEntity(particleEntity);
}
CommitEntity(tank);
return tank;
}
void GameWorld::AddTankWheelPair(EntityID tankEntity, glm::vec3 position, int axleID, bool front)
{
const float separation = 1.77f;
const float suspensionStrength = 15.f;
const float springLength = 0.3f;
bool steering = front;
auto wheelFront = CreateEntity(tankEntity);
{
auto transform = AddComponent<Components::Transform>(wheelFront);
transform->Position = position - glm::vec3(0, 0, separation / 2.f); // glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f);
//transform->Orientation = glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0));
#ifdef DEBUG
auto model = AddComponent<Components::Model>(wheelFront);
model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj";
#endif
auto Wheel = AddComponent<Components::Wheel>(wheelFront);
Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f);
Wheel->AxleID = axleID;
Wheel->Mass = 2000;
Wheel->Radius = 0.6f;
Wheel->Steering = steering;
Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f;
Wheel->Width = 0.6f;
CommitEntity(wheelFront);
}
auto wheelBack = CreateEntity(tankEntity);
{
auto transform = AddComponent<Components::Transform>(wheelBack);
transform->Position = position + glm::vec3(0, 0, separation / 2.f); // glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f);
//transform->Orientation = glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0));
#ifdef DEBUG
auto model = AddComponent<Components::Model>(wheelBack);
model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj";
#endif
auto Wheel = AddComponent<Components::Wheel>(wheelBack);
Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f);
Wheel->AxleID = axleID;
Wheel->Mass = 2000;
Wheel->Radius = 0.6f;
Wheel->Steering = steering;
Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f;
Wheel->Width = 0.6f;
CommitEntity(wheelBack);
}
auto wheelPair = CreateEntity(tankEntity);
{
{
auto transform = AddComponent<Components::Transform>(wheelPair);
transform->Position = position;
//transform->Orientation = glm::quat(glm::vec3(0, glm::pi<float>() / 4.f, 0));
auto pair = AddComponent<Components::WheelPair>(wheelPair);
pair->FakeWheelFront = wheelFront;
pair->FakeWheelBack = wheelBack;
}
auto modelEntity = CreateEntity(wheelPair);
{
auto transform = AddComponent<Components::Transform>(modelEntity);
//transform->Position = glm::vec3(0.f, 0.35f, 0.f);
if (front)
{
transform->Position = glm::vec3(0.f, 0.03f, 0.f);
transform->Orientation = glm::quat(glm::vec3(0, glm::pi<float>(), 0));
}
else
{
transform->Position = glm::vec3(0.f, 0.17f, 0.f);
transform->Scale = glm::vec3(1.f, 1.2f, 1.2f);
}
auto model = AddComponent<Components::Model>(modelEntity);
model->ModelFile = "Models/Tank/tankWheel.obj";
}
CommitEntity(modelEntity);
}
CommitEntity(wheelPair);
}
EntityID GameWorld::CreateJeep(int playerID)
{
auto jeep = CreateEntity();
+39 -1
View File
@@ -5,12 +5,14 @@
void Systems::GarageSystem::RegisterComponents( ComponentFactory* cf )
{
cf->Register<Components::Garage>([]() { return new Components::Garage(); });
cf->Register<Components::SpawnPoint>([]() { return new Components::SpawnPoint(); });
}
void Systems::GarageSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EEnterTrigger, &Systems::GarageSystem::OnEnterTrigger);
EVENT_SUBSCRIBE_MEMBER(m_ELeaveTrigger, &Systems::GarageSystem::OnLeaveTrigger);
EVENT_SUBSCRIBE_MEMBER(m_ECommand, &Systems::GarageSystem::OnCommand);
}
void Systems::GarageSystem::Update(double dt)
@@ -34,11 +36,13 @@ bool Systems::GarageSystem::OnEnterTrigger(const Events::EnterTrigger &event)
std::string name = m_World->GetProperty<std::string>(event.Trigger, "Name");
if (name == "BoundsTrigger" || name == "ElevatorShaftTrigger")
if (name == "BoundsTrigger")
{
ToggleGarage(garageComponent);
}
m_EntitiesInTrigger[event.Trigger].insert(event.Entity);
return true;
}
@@ -59,6 +63,40 @@ bool Systems::GarageSystem::OnLeaveTrigger(const Events::LeaveTrigger &event)
ToggleGarage(garageComponent);
}
m_EntitiesInTrigger[event.Trigger].erase(event.Entity);
return true;
}
bool Systems::GarageSystem::OnCommand(const Events::InputCommand &event)
{
if (event.Command == "use" && event.Value > 0)
{
for (auto &pair : m_EntitiesInTrigger)
{
EntityID trigger = pair.first;
auto entities = pair.second;
std::string name = m_World->GetProperty<std::string>(trigger, "Name");
if (name != "ElevatorShaftTrigger")
continue;
auto triggerBaseParent = m_World->GetEntityBaseParent(trigger);
auto triggerPlayerComponent = m_World->GetComponent<Components::Player>(triggerBaseParent);
for (EntityID entity : entities)
{
auto entityPlayerComponent = m_World->GetComponent<Components::Player>(triggerBaseParent);
if (triggerPlayerComponent == entityPlayerComponent)
{
EntityID garage = m_World->GetEntityParent(trigger);
auto garageComponent = m_World->GetComponent<Components::Garage>(garage);
ToggleGarage(garageComponent);
}
}
}
}
return true;
}
+9
View File
@@ -1,13 +1,17 @@
#ifndef GarageSystem_h__
#define GarageSystem_h__
#include <set>
#include "System.h"
#include "Components/Transform.h"
#include "Components/Trigger.h"
#include "Components/Garage.h"
#include "Components/SpawnPoint.h"
#include "Events/EnterTrigger.h"
#include "Events/LeaveTrigger.h"
#include "Events/SpawnVehicle.h"
#include "Events/InputCommand.h"
#include "Components/Player.h"
#include "Events/Move.h"
@@ -30,12 +34,17 @@ namespace Systems
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
std::map<EntityID, std::set<EntityID>> m_EntitiesInTrigger;
EventRelay<GarageSystem, Events::EnterTrigger> m_EEnterTrigger;
bool OnEnterTrigger(const Events::EnterTrigger &event);
EventRelay<GarageSystem, Events::LeaveTrigger> m_ELeaveTrigger;
bool OnLeaveTrigger(const Events::LeaveTrigger &event);
EventRelay<GarageSystem, Events::InputCommand> m_ECommand;
bool OnCommand(const Events::InputCommand &event);
void ToggleGarage(Components::Garage* garageComponent);
};
}
+312 -4
View File
@@ -2,7 +2,7 @@
#include "TankSteeringSystem.h"
#include "World.h"
void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf )
void Systems::TankSteeringSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register<Components::TankSteering>([]() { return new Components::TankSteering(); });
cf->Register<Components::TowerSteering>([]() { return new Components::TowerSteering(); });
@@ -12,6 +12,7 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf )
void Systems::TankSteeringSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_ECollision, &Systems::TankSteeringSystem::OnCollision);
EVENT_SUBSCRIBE_MEMBER(m_ESpawnVehicle, &Systems::TankSteeringSystem::OnSpawnVehicle);
for (int i = 0; i < 4; i++)
{
@@ -110,7 +111,7 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit
}
}
bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
bool Systems::TankSteeringSystem::OnCollision(const Events::Collision &e)
{
if(m_World->ValidEntity(e.Entity1) && m_World->ValidEntity(e.Entity2))
{
@@ -251,6 +252,315 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
return true;
}
bool Systems::TankSteeringSystem::OnSpawnVehicle(const Events::SpawnVehicle &event)
{
if (event.VehicleType != "Tank")
return false;
auto spawnPointComponents = m_World->GetComponentsOfType<Components::SpawnPoint>();
for (auto &spawnPointComponent : *spawnPointComponents)
{
auto spawnPoint = spawnPointComponent->Entity;
auto spawnPointBaseParent = m_World->GetEntityBaseParent(spawnPoint);
auto playerComponent = m_World->GetComponent<Components::Player>(spawnPointBaseParent);
if (!playerComponent || playerComponent->ID != event.PlayerID)
continue;
Components::Transform absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(spawnPoint);
// Create a tank
EntityID tank = CreateTank(event.PlayerID);
auto tankTransform = m_World->GetComponent<Components::Transform>(tank);
tankTransform->Position = absoluteTransform.Position;
tankTransform->Orientation = absoluteTransform.Orientation;
// Set the viewport correctly
Events::SetViewportCamera e;
e.CameraEntity = m_World->GetProperty<EntityID>(tank, "Camera");
if (event.PlayerID == 1)
e.ViewportFrame = "Viewport1";
else if (event.PlayerID == 2)
e.ViewportFrame = "Viewport2";
EventBroker->Publish(e);
e.ViewportFrame = "Viewport2";
EventBroker->Publish(e);
}
return true;
}
EntityID Systems::TankSteeringSystem::CreateTank(int playerID)
{
auto tank = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(tank);
transform->Position = glm::vec3(0, 5, 0);
//transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0));
auto physics = m_World->AddComponent<Components::Physics>(tank);
physics->Mass = 63000 - 16000;
physics->Static = false;
auto vehicle = m_World->AddComponent<Components::Vehicle>(tank);
vehicle->MaxTorque = 8000.f;
vehicle->MaxSteeringAngle = 90.f;
vehicle->MaxSpeedFullSteeringAngle = 4.f;
auto player = m_World->AddComponent<Components::Player>(tank);
player->ID = playerID;
auto tankSteering = m_World->AddComponent<Components::TankSteering>(tank);
m_World->AddComponent<Components::Input>(tank);
auto health = m_World->AddComponent<Components::Health>(tank);
health->Amount = 100.f;
{
auto shape = m_World->CreateEntity(tank);
auto transform = m_World->AddComponent<Components::Transform>(shape);
auto meshShape = m_World->AddComponent<Components::MeshShape>(shape);
meshShape->ResourceName = "Models/Tank/TankCollisionShape.obj";
m_World->CommitEntity(shape);
}
{
auto shapeTower = m_World->CreateEntity(tank);
auto transform = m_World->AddComponent<Components::Transform>(shapeTower);
transform->Position = glm::vec3(0, 1.10633f, 1.03024f);
auto box = m_World->AddComponent<Components::BoxShape>(shapeTower);
box->Width = 1.298f;
box->Height = 0.502f;
box->Depth = 1.211f;
m_World->CommitEntity(shapeTower);
}
{
auto chassis = m_World->CreateEntity(tank);
auto transform = m_World->AddComponent<Components::Transform>(chassis);
transform->Position = glm::vec3(0, 0, 0);
auto model = m_World->AddComponent<Components::Model>(chassis);
model->ModelFile = "Models/Tank/tankBody.obj";
}
{
auto tower = m_World->CreateEntity(tank);
m_World->SetProperty(tower, "Name", "tower");
auto transform = m_World->AddComponent<Components::Transform>(tower);
transform->Position = glm::vec3(0.f, 0.68f, 0.9f);
auto model = m_World->AddComponent<Components::Model>(tower);
model->ModelFile = "Models/Tank/tankTop.obj";
auto towerSteering = m_World->AddComponent<Components::TowerSteering>(tower);
towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f);
towerSteering->TurnSpeed = glm::pi<float>() / 4.f;
{
auto barrel = m_World->CreateEntity(tower);
auto transform = m_World->AddComponent<Components::Transform>(barrel);
transform->Position = glm::vec3(-0.012f, 0.3f, -0.95);
auto model = m_World->AddComponent<Components::Model>(barrel);
model->ModelFile = "Models/Tank/tankBarrel.obj";
auto barrelSteering = m_World->AddComponent<Components::BarrelSteering>(barrel);
barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f);
barrelSteering->TurnSpeed = glm::pi<float>() / 4.f;
barrelSteering->ShotSpeed = 70.f;
barrelSteering->LowerRotationLimit = glm::radians(-10.f);
barrelSteering->UpperRotationLimit = glm::radians(40.f);
{
auto shot = m_World->CreateEntity(barrel);
auto transform = m_World->AddComponent<Components::Transform>(shot);
transform->Position = glm::vec3(0.35f, 0.f, -2.f);
transform->Orientation = glm::angleAxis(-glm::pi<float>() / 2.f, glm::vec3(1, 0, 0));
transform->Scale = glm::vec3(3.f);
m_World->AddComponent<Components::Template>(shot);
auto physics = m_World->AddComponent<Components::Physics>(shot);
physics->Mass = 25.f;
physics->Static = false;
physics->CollisionEvent = true;
auto modelComponent = m_World->AddComponent<Components::Model>(shot);
modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj";
auto tankShellComponent = m_World->AddComponent<Components::TankShell>(shot);
tankShellComponent->Damage = 20.f;
tankShellComponent->ExplosionRadius = 30.f;
tankShellComponent->ExplosionStrength = 300000.f;
{
auto shape = m_World->CreateEntity(shot);
auto transform = m_World->AddComponent<Components::Transform>(shape);
auto boxShape = m_World->AddComponent<Components::BoxShape>(shape);
boxShape->Width = 0.5f;
boxShape->Height = 0.5f;
boxShape->Depth = 0.5f;
m_World->CommitEntity(shape);
}
m_World->CommitEntity(shot);
barrelSteering->ShotTemplate = shot;
auto cameraTower = m_World->CreateEntity(barrel);
{
auto transform = m_World->AddComponent<Components::Transform>(cameraTower);
transform->Position.z = 16.f;
transform->Position.y = 4.f;
transform->Orientation = glm::quat(glm::vec3(-glm::radians(5.f), 0.f, 0.f));
auto cameraComp = m_World->AddComponent<Components::Camera>(cameraTower);
cameraComp->FarClip = 2000.f;
/*auto follow = m_World->AddComponent<Components::Follow>(cameraTower);
follow->Entity = barrel;
follow->Distance = 15.f;
follow->FollowAxis = glm::vec3(1, 1, 1);*/
}
m_World->SetProperty(tank, "Camera", cameraTower);
}
m_World->CommitEntity(barrel);
tankSteering->Barrel = barrel;
}
m_World->CommitEntity(tower);
tankSteering->Turret = tower;
}
//{
// auto lightentity = m_World->CreateEntity(tank);
// auto transform = m_World->AddComponent<Components::Transform>(lightentity);
// transform->Position = glm::vec3(0, 0, 0);
// auto light = m_World->AddComponent<Components::PointLight>(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;
// light->LinearAttenuation = 0.003f;
// light->QuadraticAttenuation = 0.002f;*/
//}
// auto wheelpair = m_World->CreateEntity(tank);
// SetProperty(wheelpair, "Name", "WheelPair");
// AddComponent(wheelpair, "WheelPairThingy");
#pragma region Wheels
//Create wheels
float wheelOffset = -0.83f;
const float suspensionStrength = 15.f;
const float springLength = 0.3f;
AddTankWheelPair(tank, glm::vec3(1.68f, wheelOffset, -1.715f), 0, true);
AddTankWheelPair(tank, glm::vec3(-1.68f, wheelOffset, -1.715f), 0, true);
AddTankWheelPair(tank, glm::vec3(1.68f, wheelOffset, 2.375), 1, false);
AddTankWheelPair(tank, glm::vec3(-1.68f, wheelOffset, 2.375), 1, false);
#pragma endregion
{
auto entity = m_World->CreateEntity(tank);
auto transformComponent = m_World->AddComponent<Components::Transform>(entity);
transformComponent->Position = glm::vec3(-2, -1.7, 2.0);
transformComponent->Scale = glm::vec3(3, 3, 3);
transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0));
auto emitterComponent = m_World->AddComponent<Components::ParticleEmitter>(entity);
emitterComponent->SpawnCount = 2;
emitterComponent->SpawnFrequency = 0.005;
emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
m_World->CommitEntity(entity);
auto particleEntity = m_World->CreateEntity(entity);
auto TEMP = m_World->AddComponent<Components::Transform>(particleEntity);
TEMP->Scale = glm::vec3(0);
auto spriteComponent = m_World->AddComponent<Components::Sprite>(particleEntity);
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
emitterComponent->ParticleTemplate = particleEntity;
m_World->CommitEntity(particleEntity);
}
m_World->CommitEntity(tank);
return tank;
}
void Systems::TankSteeringSystem::AddTankWheelPair(EntityID tankEntity, glm::vec3 position, int axleID, bool front)
{
const float separation = 1.77f;
const float suspensionStrength = 15.f;
const float springLength = 0.3f;
bool steering = front;
auto wheelFront = m_World->CreateEntity(tankEntity);
{
auto transform = m_World->AddComponent<Components::Transform>(wheelFront);
transform->Position = position - glm::vec3(0, 0, separation / 2.f); // glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f);
//transform->Orientation = glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0));
#ifdef DEBUG
auto model = m_World->AddComponent<Components::Model>(wheelFront);
model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj";
#endif
auto Wheel = m_World->AddComponent<Components::Wheel>(wheelFront);
Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f);
Wheel->AxleID = axleID;
Wheel->Mass = 2000;
Wheel->Radius = 0.6f;
Wheel->Steering = steering;
Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f;
Wheel->Width = 0.6f;
m_World->CommitEntity(wheelFront);
}
auto wheelBack = m_World->CreateEntity(tankEntity);
{
auto transform = m_World->AddComponent<Components::Transform>(wheelBack);
transform->Position = position + glm::vec3(0, 0, separation / 2.f); // glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f);
//transform->Orientation = glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0));
#ifdef DEBUG
auto model = m_World->AddComponent<Components::Model>(wheelBack);
model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj";
#endif
auto Wheel = m_World->AddComponent<Components::Wheel>(wheelBack);
Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f);
Wheel->AxleID = axleID;
Wheel->Mass = 2000;
Wheel->Radius = 0.6f;
Wheel->Steering = steering;
Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f;
Wheel->Width = 0.6f;
m_World->CommitEntity(wheelBack);
}
auto wheelPair = m_World->CreateEntity(tankEntity);
{
{
auto transform = m_World->AddComponent<Components::Transform>(wheelPair);
transform->Position = position;
//transform->Orientation = glm::quat(glm::vec3(0, glm::pi<float>() / 4.f, 0));
auto pair = m_World->AddComponent<Components::WheelPair>(wheelPair);
pair->FakeWheelFront = wheelFront;
pair->FakeWheelBack = wheelBack;
}
auto modelEntity = m_World->CreateEntity(wheelPair);
{
auto transform = m_World->AddComponent<Components::Transform>(modelEntity);
//transform->Position = glm::vec3(0.f, 0.35f, 0.f);
if (front)
{
transform->Position = glm::vec3(0.f, 0.03f, 0.f);
transform->Orientation = glm::quat(glm::vec3(0, glm::pi<float>(), 0));
}
else
{
transform->Position = glm::vec3(0.f, 0.17f, 0.f);
transform->Scale = glm::vec3(1.f, 1.2f, 1.2f);
}
auto model = m_World->AddComponent<Components::Model>(modelEntity);
model->ModelFile = "Models/Tank/tankWheel.obj";
}
m_World->CommitEntity(modelEntity);
}
m_World->CommitEntity(wheelPair);
}
void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt )
{
PositionX = m_Horizontal;
@@ -315,5 +625,3 @@ bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const E
return true;
}
+18 -1
View File
@@ -32,6 +32,18 @@
#include "Components/Vehicle.h"
#include "Components/Player.h"
#include "Events/SpawnVehicle.h"
#include "Components/SpawnPoint.h"
#include "Components/Wheel.h"
#include "Components/MeshShape.h"
#include "Components/Camera.h"
#include "Components/BoxShape.h"
#include "Components/WheelPair.h"
#include "Components/Input.h"
#include "Events/SetViewportCamera.h"
namespace Systems
{
@@ -49,13 +61,18 @@ namespace Systems
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
std::map<EntityID, double> m_TimeSinceLastShot;
EventRelay<TankSteeringSystem, Events::Collision> m_ECollision;
bool OnCollision(const Events::Collision &e);
EventRelay<TankSteeringSystem, Events::SpawnVehicle> m_ESpawnVehicle;
bool OnSpawnVehicle(const Events::SpawnVehicle &e);
class TankSteeringInputController;
std::array<std::shared_ptr<TankSteeringInputController>, 4> m_TankInputControllers;
std::map<EntityID, double> m_TimeSinceLastShot;
EntityID CreateTank(int playerID);
void AddTankWheelPair(EntityID tankEntity, glm::vec3 position, int axleID, bool front);
};
class TankSteeringSystem::TankSteeringInputController : InputController<TankSteeringSystem>
+2
View File
@@ -170,6 +170,7 @@
<ClInclude Include="..\..\src\Components\Trigger.h" />
<ClInclude Include="..\..\src\Components\TriggerRotate.h" />
<ClInclude Include="..\..\src\Components\Vehicle.h" />
<ClInclude Include="..\..\src\Components\SpawnPoint.h" />
<ClInclude Include="..\..\src\Components\Viewport.h" />
<ClInclude Include="..\..\src\Components\Wheel.h" />
<ClInclude Include="..\..\src\Components\WheelPair.h" />
@@ -203,6 +204,7 @@
<ClInclude Include="..\..\src\Events\Rotate.h" />
<ClInclude Include="..\..\src\Events\SetVelocity.h" />
<ClInclude Include="..\..\src\Events\SetViewportCamera.h" />
<ClInclude Include="..\..\src\Events\SpawnVehicle.h" />
<ClInclude Include="..\..\src\Events\StopSound.h" />
<ClInclude Include="..\..\src\Events\TankSteer.h" />
<ClInclude Include="..\..\src\Events\EnterTrigger.h" />
@@ -184,8 +184,7 @@
</Filter>
<Filter Include="Game\Systems">
<UniqueIdentifier>{3c2ea0e5-41a1-4b11-a891-1d59ead7223c}</UniqueIdentifier>
</Filter>
</Filter>
<Filter Include="Base\Events">
<UniqueIdentifier>{3cbe68d9-2446-45ee-8637-ffb805997dcd}</UniqueIdentifier>
</Filter>
@@ -545,6 +544,12 @@
<ClInclude Include="..\..\src\GUI\VehicleSelection.h">
<Filter>GUI</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\SpawnVehicle.h">
<Filter>Game\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\SpawnPoint.h">
<Filter>Game\Components</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\Fragment2.glsl">