Merge branch 'master' into Respawning

# Conflicts:
#	resources/DefaultConfig.ini
#	resources/Schema/Entities/MovementTest.xml
#	src/Game/Systems/SoundSystem.cpp
This commit is contained in:
William Moberg
2016-02-12 00:29:51 +01:00
62 changed files with 7752 additions and 858 deletions
@@ -16,6 +16,9 @@ void CapturePointHUDSystem::Update(double dt)
auto CapturePointHUDElements = m_World->GetComponents("CapturePointHUD");
auto CapturePoints = m_World->GetComponents("CapturePoint");
if (CapturePointHUDElements == nullptr) {
return;
}
for (auto& cCapturePointHUD : *CapturePointHUDElements) {
int HUD_ID = cCapturePointHUD["CapturePointNumber"];
+10 -17
View File
@@ -32,6 +32,7 @@ void CapturePointSystem::UpdateComponent(EntityWrapper& capturePointEntity, Comp
const int redTeam = (int)teamComponent["Team"].Enum("Red");
const int blueTeam = (int)teamComponent["Team"].Enum("Blue");
const int spectatorTeam = (int)teamComponent["Team"].Enum("Spectator");
const double captureTimeToTakeOver = (double)cCapturePoint["CapturePointMaxTimer"];
int homePointForTeam = (int)cCapturePoint["HomePointForTeam"];
if (m_NumberOfCapturePoints == 0 && capturePointNumber != 0 && (homePointForTeam == redTeam || homePointForTeam == blueTeam)) {
@@ -57,7 +58,7 @@ void CapturePointSystem::UpdateComponent(EntityWrapper& capturePointEntity, Comp
int blueTeamPlayersStandingInside = 0;
if (capturePointEntity.HasComponent("Model")) {
//Now sets team color to the capturepoint, or white if it is uncaptured.
capturePointEntity["Model"]["Color"] = ownedBy == blueTeam ? glm::vec4(0, 0.2f, 1, 0.3) : ownedBy == redTeam ? glm::vec4(1, 0.2f, 0, 0.3) : glm::vec4(1, 1, 1, 0.3);
capturePointEntity["Model"]["Color"] = ownedBy == blueTeam ? glm::vec4(0, 0.0f, 1, 0.3) : ownedBy == redTeam ? glm::vec4(1, 0.0f, 0, 0.3) : glm::vec4(1, 1, 1, 0.3);
}
//calculate next possible capturePoint for both teams
@@ -98,20 +99,16 @@ void CapturePointSystem::UpdateComponent(EntityWrapper& capturePointEntity, Comp
ComponentWrapper& capturePoint = m_CapturePointNumberToEntityMap[i]["CapturePoint"];
if ((int)capturePoint["CapturePointNumber"] != nextPossibleCapturePoint["Red"] &&
(int)capturePoint["CapturePointNumber"] != nextPossibleCapturePoint["Blue"]) {
capturePoint["CaptureTimer"] = 0.0;
//RED = +, BLUE = -, NONE
auto teamOwners = (int)m_CapturePointNumberToEntityMap[i]["Team"]["Team"];
if (teamOwners == redTeam || teamOwners == blueTeam) {
capturePoint["CaptureTimer"] = teamOwners == blueTeam ? -captureTimeToTakeOver : captureTimeToTakeOver;
}
}
}
m_ResetTimers = false;
}
//colorize next possible capturepoint
if (nextPossibleCapturePoint["Red"] == capturePointNumber) {
capturePointEntity["Model"]["Color"] = glm::vec4(1, 1, 0, 0.3);
}
if (nextPossibleCapturePoint["Blue"] == capturePointNumber) {
capturePointEntity["Model"]["Color"] = glm::vec4(0, 1, 1, 0.3);
}
//check how many players are standing inside and are healthy
for (size_t i = m_ETriggerTouchVector.size(); i > 0; i--)
{
@@ -170,9 +167,6 @@ void CapturePointSystem::UpdateComponent(EntityWrapper& capturePointEntity, Comp
//B. at most one of the teams have players inside
//if capturePoint is not owned by the take-over team, just modify the CaptureTimer accordingly
if (ownedBy != currentTeam && canCapture) {
if (abs((double)cCapturePoint["CaptureTimer"]) < 0.001f) {
LOG_DEBUG("Point is being captured by team %i", currentTeam); //Remove when we tested sufficiently.
}
cCapturePoint["CaptureTimer"] = (double)cCapturePoint["CaptureTimer"] + timerDeltaChange;
}
//if capturePoint is owned by the team, and the other team has been trying to take it, then increase/decrease the timer towards 0.0
@@ -180,12 +174,11 @@ void CapturePointSystem::UpdateComponent(EntityWrapper& capturePointEntity, Comp
(ownedBy == currentTeam && currentTeam == blueTeam && (double)cCapturePoint["CaptureTimer"] > 0.0)) {
cCapturePoint["CaptureTimer"] = (double)cCapturePoint["CaptureTimer"] + timerDeltaChange;
}
//check if captureTimer > m_CaptureTimeToTakeOver and if so change owner and publish the eCaptured event
if (abs((double)cCapturePoint["CaptureTimer"]) > abs(m_CaptureTimeToTakeOver) && canCapture) {
//check if captureTimer > captureTimeToTakeOver and if so change owner and publish the eCaptured event
if (abs((double)cCapturePoint["CaptureTimer"]) > captureTimeToTakeOver && canCapture) {
teamComponent["Team"] = currentTeam;
cCapturePoint["CaptureTimer"] = 0.0;
cCapturePoint["CaptureTimer"] = glm::sign((double)cCapturePoint["CaptureTimer"])*captureTimeToTakeOver;
//publish Captured event
LOG_DEBUG("Point is captured by team %i!", currentTeam); //Remove when we tested sufficiently.
Events::Captured e;
e.CapturePointID = cCapturePoint.EntityID;
e.TeamNumberThatCapturedCapturePoint = currentTeam;
@@ -0,0 +1,69 @@
#include "Systems/DamageIndicatorSystem.h"
DamageIndicatorSystem::DamageIndicatorSystem(SystemParams params)
: System(params)
{
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &DamageIndicatorSystem::OnPlayerDamage);
//current camera
EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &DamageIndicatorSystem::OnSetCamera);
//load texture to cache
auto texture = CommonFunctions::LoadTexture("Textures/DamageIndicator.png", false);
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/DamageIndicator.xml");
}
bool DamageIndicatorSystem::OnPlayerDamage(Events::PlayerDamage& e)
{
if (m_CurrentCamera == EntityID_Invalid) {
return false;
}
if (e.Victim != LocalPlayer) {
return false;
}
//grab players direction
auto playerOrientation = glm::quat((glm::vec3)e.Victim["Transform"]["Orientation"]);
//get the position vectors, but ignore the y-height
auto enemyPosition = (glm::vec3)e.Inflictor["Transform"]["Position"];
auto playerPosition = (glm::vec3)e.Victim["Transform"]["Position"];
enemyPosition.y = 0.0f;
playerPosition.y = 0.0f;
//calculate the enemy to player vector
auto enemyPlayerVector = glm::normalize(playerPosition - enemyPosition);
//get angle from players current rotation, this angle is how much you rotate around the y-axis
auto playerAngle = glm::angle(playerOrientation);
auto playerRotationVector = glm::normalize(glm::rotateY(glm::vec3(0, 0, 1), playerAngle));
//dot product of players direction-vector and enemys-to-playervector will give the cos of the angle between the vectors
auto playerRotationDot = glm::dot(playerRotationVector, enemyPlayerVector);
//to get the angle between the vectors just do cos-inverse
auto angleBetweenVectors = glm::acos(playerRotationDot);
//rotate the direction-vector 90 degrees to get the players side-vector
auto playerSideVector = glm::normalize(glm::rotateY(glm::vec3(0, 0, 1), playerAngle + 1.57f));
//dot of sidevector positive = enemy is on the right side, dot sidevector negative = left side
auto playerSideVectorDot = glm::dot(playerSideVector, enemyPlayerVector);
if (playerSideVectorDot < 0) {
angleBetweenVectors = -angleBetweenVectors;
}
//load & set the "2d" sprite
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/DamageIndicator.xml");
EntityFileParser parser(entityFile);
EntityID spriteID = parser.MergeEntities(m_World);
m_World->SetParent(spriteID, m_CurrentCamera);
auto spriteWrapper = EntityWrapper(m_World, spriteID);
//simply set the rotation z-wise to the angleBetweenVectors
spriteWrapper["Transform"]["Orientation"] = glm::vec3(0, 0, angleBetweenVectors);
return true;
}
bool DamageIndicatorSystem::OnSetCamera(const Events::SetCamera& e) {
m_CurrentCamera = e.CameraEntity.ID;
return true;
}
+2 -2
View File
@@ -15,13 +15,13 @@ void HealthSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& comp
bool HealthSystem::OnPlayerDamaged(Events::PlayerDamage& e)
{
ComponentWrapper cHealth = e.Player["Health"];
ComponentWrapper cHealth = e.Victim["Health"];
double& health = cHealth["Health"];
health -= e.Damage;
if (health <= 0.0) {
Events::PlayerDeath ePlayerDeath;
ePlayerDeath.Player = e.Player;
ePlayerDeath.Player = e.Victim;
m_EventBroker->Publish(ePlayerDeath);
//Note: we will delete the entity in PlayerDeathSystem
}
+6 -4
View File
@@ -59,8 +59,10 @@ void PlayerDeathSystem::createDeathEffect(EntityWrapper player)
//deathEffectEW["ExplosionEffect"]["ExplosionOrigin"] = glm::vec3(0, 0, 0);
//camera (with lifetime) behind the player
auto cam = deathEffectEW.FirstChildByName("Camera");
Events::SetCamera eSetCamera;
eSetCamera.CameraEntity = cam;
m_EventBroker->Publish(eSetCamera);
if (player == LocalPlayer) {
auto cam = deathEffectEW.FirstChildByName("Camera");
Events::SetCamera eSetCamera;
eSetCamera.CameraEntity = cam;
m_EventBroker->Publish(eSetCamera);
}
}
+4 -2
View File
@@ -51,6 +51,7 @@ void PlayerMovementSystem::updateMovementControllers(double dt)
float playerMovementSpeed = player["Player"]["MovementSpeed"];
float playerCrouchSpeed = player["Player"]["CrouchSpeed"];
glm::vec3& wishDirection = player["Player"]["CurrentWishDirection"];
if (player.HasComponent("Physics")) {
ComponentWrapper cPhysics = player["Physics"];
@@ -58,7 +59,7 @@ void PlayerMovementSystem::updateMovementControllers(double dt)
if (player.HasComponent("DashAbility")) {
controller->AssaultDashCheck(dt, ((glm::vec3)cPhysics["Velocity"]).y != 0.0f, player["DashAbility"]["CoolDownMaxTimer"]);
}
glm::vec3 wishDirection = controller->Movement() * glm::inverse(glm::quat(ori));
wishDirection = controller->Movement() * glm::inverse(glm::quat(ori));
//this makes sure you can only dash in the 4 directions: forw,backw,left,right
if (controller->AssaultDashDoubleTapped() && controller->Movement().z != 0 && controller->Movement().x != 0) {
wishDirection = glm::vec3(controller->Movement().x, 0, 0)* glm::inverse(glm::quat(ori));
@@ -233,7 +234,8 @@ void PlayerMovementSystem::updateVelocity(double dt)
float speed = glm::length(velocity);
static float groundFriction = 7.f;
ImGui::InputFloat("groundFriction", &groundFriction);
static float airFriction = 0.f;
static float airFriction = 2.f;
ImGui::InputFloat("airFriction", &airFriction);
float friction = isOnGround ? groundFriction : airFriction;
if (speed > 0) {
+2 -1
View File
@@ -133,7 +133,8 @@ bool PlayerSpawnSystem::OnPlayerSpawned(Events::PlayerSpawned& e)
// Set the camera to the correct entity
EntityWrapper cameraEntity = e.Player.FirstChildByName("Camera");
if (cameraEntity.Valid()) {
bool outOfBodyExperience = ResourceManager::Load<ConfigFile>("Config.ini")->Get<bool>("Debug.OutOfBodyExperience", false);
if (cameraEntity.Valid() && !outOfBodyExperience) {
Events::SetCamera e;
e.CameraEntity = cameraEntity;
m_EventBroker->Publish(e);
+1 -1
View File
@@ -54,7 +54,7 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e)
}
if (e.Command == "TakeDamage" && e.Value > 0 && LocalPlayer.Valid()) {
Events::PlayerDamage ev;
ev.Player = LocalPlayer;
ev.Victim = LocalPlayer;
ev.Damage = e.Value;
m_EventBroker->Publish(ev);
}
+6 -4
View File
@@ -48,10 +48,12 @@ EntityWrapper SpawnerSystem::Spawn(EntityWrapper spawner, EntityWrapper parent /
EntityFileParser parser(entityFile);
EntityWrapper spawnedEntity(world, parser.MergeEntities(world, parent.ID));
// Set its position and orientation to that of the SpawnPoint
spawnedEntity["Transform"]["Position"] = Transform::AbsolutePosition(spawnPoint.World, spawnPoint.ID);
// TODO: Quaternions, bitch
spawnedEntity["Transform"]["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(spawnPoint));
if (spawnPoint != parent) {
// Set its position and orientation to that of the SpawnPoint
spawnedEntity["Transform"]["Position"] = Transform::AbsolutePosition(spawnPoint.World, spawnPoint.ID);
// TODO: Quaternions, bitch
spawnedEntity["Transform"]["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(spawnPoint));
}
return spawnedEntity;
}
@@ -0,0 +1,341 @@
#include "Systems/Weapon/AssaultWeaponBehaviour.h"
AssaultWeaponBehaviour::AssaultWeaponBehaviour(SystemParams systemParams, IRenderer* renderer, Octree<EntityAABB>* collisionOctree, EntityWrapper player)
: WeaponBehaviour(systemParams, renderer, collisionOctree, player)
{
m_FirstPersonModel = m_Player.FirstChildByName("Hands");
EVENT_SUBSCRIBE_MEMBER(m_EAnimationComplete, &AssaultWeaponBehaviour::OnAnimationComplete);
}
void AssaultWeaponBehaviour::Fire()
{
m_TimeSinceLastFire = 0.0;
m_Firing = true;
fireRound();
}
void AssaultWeaponBehaviour::CeaseFire()
{
m_Firing = false;
}
void AssaultWeaponBehaviour::Reload()
{
if (m_Reloading) {
return;
}
ComponentWrapper& cAssaultWeapon = m_Player["AssaultWeapon"];
int magAmmo = cAssaultWeapon["MagazineAmmo"];
int magSize = cAssaultWeapon["MagazineSize"];
int ammo = cAssaultWeapon["Ammo"];
// Don't reload if we're already fully loaded
if (magAmmo == magSize) {
return;
}
// Don't reload if we're completly out of ammo
if (ammo == 0) {
return;
}
m_Reloading = true;
m_ReloadTimer = cAssaultWeapon["ReloadTime"];
playReloadAnimation();
}
void AssaultWeaponBehaviour::Update(double dt)
{
if (m_Reloading) {
m_ReloadTimer -= dt;
// Re-enable glow on reload impersonator half-way through the animation
if (m_ReloadTimer <= (double)m_Player["AssaultWeapon"]["ReloadTime"] / 2.0) {
if (m_ReloadImpersonator.Valid()) {
m_ReloadImpersonator["Model"]["GlowMap"] = true;
}
}
if (m_ReloadTimer <= 0) {
finishReload();
}
}
if (m_Firing && !m_Reloading) {
m_TimeSinceLastFire += dt;
ComponentWrapper& cAssaultWeapon = m_Player["AssaultWeapon"];
if (m_TimeSinceLastFire >= 1.0 / ((double)cAssaultWeapon["RPM"] / 60.0)) {
fireRound();
}
}
if (!m_Firing && !m_Reloading) {
playIdleAnimation();
}
// Disable glow map on weapon if it's out of ammo
// Make real first person weapon model visible again
EntityWrapper firstPersonWeaponModel = m_Player.FirstChildByName("WeaponModel");
if (firstPersonWeaponModel.Valid()) {
firstPersonWeaponModel["Model"]["GlowMap"] = hasAmmo();
}
}
bool AssaultWeaponBehaviour::OnAnimationComplete(Events::AnimationComplete& e)
{
if (e.Entity != m_FirstPersonModel) {
return false;
}
//if (e.Name == "ShootRifle") {
// if (!m_Firing) {
// playIdleAnimation();
// }
//}
return true;
}
bool AssaultWeaponBehaviour::hasAmmo()
{
ComponentWrapper& cAssaultWeapon = m_Player["AssaultWeapon"];
int& magAmmo = cAssaultWeapon["MagazineAmmo"];
return magAmmo > 0;
}
void AssaultWeaponBehaviour::fireRound()
{
if (m_Reloading) {
return;
}
ComponentWrapper& cAssaultWeapon = m_Player["AssaultWeapon"];
int& magAmmo = cAssaultWeapon["MagazineAmmo"];
int ammo = cAssaultWeapon["Ammo"];
// Reload if our magazine is empty
if (magAmmo <= 0) {
Reload();
return;
}
// Fire
magAmmo -= 1;
spawnTracer();
playSound();
viewPunch();
playShootAnimation();
bool hit = shoot(cAssaultWeapon["BaseDamage"]);
if (hit) {
showHitMarker();
}
m_TimeSinceLastFire = 0.0;
}
void AssaultWeaponBehaviour::spawnTracer()
{
if (!IsClient) {
return;
}
EntityWrapper spawner;
if (m_Player == LocalPlayer) {
spawner = m_Player.FirstChildByName("WeaponMuzzle");
} else {
spawner = m_Player.FirstChildByName("ThirdPersonWeaponMuzzle");
}
if (!spawner.Valid()) {
return;
}
float distance = traceRayDistance(Transform::AbsolutePosition(spawner), Transform::AbsoluteOrientation(spawner) * glm::vec3(0, 0, -1));
EntityWrapper ray = SpawnerSystem::Spawn(spawner);
((glm::vec3&)ray["Transform"]["Scale"]).z = (distance / 100.f);
}
float AssaultWeaponBehaviour::traceRayDistance(glm::vec3 origin, glm::vec3 direction)
{
// TODO: Cast a ray and size tracer appropriately
float distance;
glm::vec3 pos;
auto entity = Collision::EntityFirstHitByRay(Ray(origin, direction), m_CollisionOctree, distance, pos);
if (entity) {
return distance;
} else {
return 100.f;
}
}
void AssaultWeaponBehaviour::playSound()
{
if (!IsClient) {
return;
}
Events::PlaySoundOnEntity e;
e.EmitterID = m_Player.ID;
e.FilePath = "Audio/laser/laser1.wav";
m_EventBroker->Publish(e);
}
void AssaultWeaponBehaviour::viewPunch()
{
EntityWrapper playerCamera = m_Player.FirstChildByName("Camera");
if (!playerCamera.Valid()) {
return;
}
float viewPunch = m_Player["AssaultWeapon"]["ViewPunch"];
ComponentWrapper cTransform = playerCamera["Transform"];
glm::vec3& orientation = cTransform["Orientation"];
orientation.x += viewPunch;
}
void AssaultWeaponBehaviour::finishReload()
{
ComponentWrapper& cAssaultWeapon = m_Player["AssaultWeapon"];
int& magAmmo = cAssaultWeapon["MagazineAmmo"];
int magSize = cAssaultWeapon["MagazineSize"];
int& ammo = cAssaultWeapon["Ammo"];
// Throw away rounds in magazine to incentivise ammo sharing
int toLoad = glm::min(magSize, ammo);
magAmmo = toLoad;
ammo -= toLoad;
// Make real first person weapon model visible again
EntityWrapper firstPersonWeaponModel = m_Player.FirstChildByName("WeaponModel");
firstPersonWeaponModel["Model"]["Visible"] = true;
m_Reloading = false;
}
void AssaultWeaponBehaviour::playShootAnimation()
{
ComponentWrapper cAnimation = m_FirstPersonModel["Animation"];
if (cAnimation["AnimationName1"] != "ShootRifle") {
cAnimation["AnimationName1"] = "ShootRifle";
cAnimation["Weight1"] = 1.0;
cAnimation["Time1"] = 0.0;
cAnimation["Speed1"] = 1.0;
cAnimation["Loop1"] = true;
}
}
void AssaultWeaponBehaviour::playIdleAnimation()
{
if (!m_FirstPersonModel.Valid()) {
return;
}
ComponentWrapper cAnimation = m_FirstPersonModel["Animation"];
std::string& animationName1 = cAnimation["AnimationName1"];
double& animationSpeed1 = cAnimation["Speed1"];
std::string animationToPlay = "Idle";
double speedToSet = 1.0;
ComponentWrapper cPlayer = m_Player["Player"];
glm::vec3 movementDirection = cPlayer["CurrentWishDirection"];
if (glm::length2(movementDirection) > 0) {
animationToPlay = "Run";
ComponentWrapper cPhysics = m_Player["Physics"];
speedToSet = glm::length((glm::vec3)cPhysics["Velocity"]) / (float)cPlayer["MovementSpeed"];
}
if (animationName1 != animationToPlay) {
cAnimation["AnimationName1"] = animationToPlay;
cAnimation["Weight1"] = 1.0;
cAnimation["Time1"] = 0.0;
cAnimation["Loop1"] = true;
}
if (animationSpeed1 != speedToSet) {
cAnimation["Speed1"] = speedToSet;
}
}
void AssaultWeaponBehaviour::playReloadAnimation()
{
// Play animation
ComponentWrapper cAnimation = m_FirstPersonModel["Animation"];
cAnimation["AnimationName1"] = "ReloadSwitch";
cAnimation["Weight1"] = 1.0;
cAnimation["Time1"] = 0.0;
cAnimation["Speed1"] = 0.5;
cAnimation["Loop1"] = true;
// Hide weapon model and spawn the exploding version
EntityWrapper firstPersonWeaponModel = m_Player.FirstChildByName("WeaponModel");
EntityWrapper reloadSpawner = m_Player.FirstChildByName("FirstPersonReloadSpawner");
m_ReloadImpersonator = SpawnerSystem::Spawn(reloadSpawner, reloadSpawner);
firstPersonWeaponModel["Model"].Copy(m_ReloadImpersonator["Model"]);
firstPersonWeaponModel["Model"]["Visible"] = false;
}
bool AssaultWeaponBehaviour::shoot(double damage)
{
// Only do shooting clientside
if (!IsClient) {
return false;
}
// Only handle shooting for the local player
if (m_Player != LocalPlayer) {
return false;
}
// Make sure the player isn't shooting from the grave
if (!m_Player.Valid()) {
return false;
}
// Screen center, based on current resolution!
Rectangle screenResolution = m_Renderer->GetViewportSize();
glm::vec2 centerScreen = glm::vec2(screenResolution.Width / 2, screenResolution.Height / 2);
// Pick middle of screen
PickData pickData = m_Renderer->Pick(centerScreen);
if (pickData.Entity == EntityID_Invalid) {
return false;
}
EntityWrapper victim(m_World, pickData.Entity);
// Don't let us shoot ourselves in the foot
if (victim == LocalPlayer) {
return false;
}
// Only care about players being hit
if (!victim.HasComponent("Player")) {
victim = victim.FirstParentWithComponent("Player");
}
if (!victim.Valid()) {
return false;
}
// Check for friendly fire
if ((ComponentInfo::EnumType)victim["Team"]["Team"] == (ComponentInfo::EnumType)m_Player["Team"]["Team"]) {
return false;
}
// Deal damage!
Events::PlayerDamage ePlayerDamage;
ePlayerDamage.Inflictor = m_Player;
ePlayerDamage.Victim = victim;
ePlayerDamage.Damage = damage;
m_EventBroker->Publish(ePlayerDamage);
return true;
}
void AssaultWeaponBehaviour::showHitMarker()
{
// Show hit marker
EntityWrapper hitMarkerSpawner = m_Player.FirstChildByName("HitMarkerSpawner");
if (hitMarkerSpawner.Valid()) {
SpawnerSystem::Spawn(hitMarkerSpawner, hitMarkerSpawner);
}
}
@@ -1,4 +1,4 @@
#include "Systems/WeaponSystem.h"
#include "Systems/Weapon/WeaponSystem.h"
WeaponSystem::WeaponSystem(SystemParams params, IRenderer* renderer, Octree<EntityAABB>* collisionOctree)
: System(params)
@@ -8,7 +8,6 @@ WeaponSystem::WeaponSystem(SystemParams params, IRenderer* renderer, Octree<Enti
, m_CollisionOctree(collisionOctree)
{
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &WeaponSystem::OnInputCommand);
EVENT_SUBSCRIBE_MEMBER(m_EShoot, &WeaponSystem::OnShoot);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &WeaponSystem::OnPlayerSpawned);
}
@@ -24,7 +23,8 @@ void WeaponSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cPla
if (it == m_ActiveWeapons.end()) {
selectWeapon(entity, 1);
}
m_EventBroker->Process<WeaponBehaviour>();
m_ActiveWeapons.at(entity)->Update(dt);
}
@@ -59,6 +59,14 @@ bool WeaponSystem::OnInputCommand(Events::InputCommand& e)
}
}
// Reload
if (e.Command == "Reload" && e.Value != 0) {
if (m_ActiveWeapons.find(player) != m_ActiveWeapons.end()) {
auto weapon = m_ActiveWeapons.at(player);
weapon->Reload();
}
}
return true;
}
@@ -68,7 +76,7 @@ void WeaponSystem::selectWeapon(EntityWrapper player, ComponentInfo::EnumType sl
if (slot == 1) {
// TODO: if class...
if (m_ActiveWeapons.count(player) == 0) {
m_ActiveWeapons.insert(std::make_pair(player, std::make_shared<AssaultWeaponBehaviour>(m_SystemParams, m_CollisionOctree, player)));
m_ActiveWeapons.insert(std::make_pair(player, std::make_shared<AssaultWeaponBehaviour>(m_SystemParams, m_Renderer, m_CollisionOctree, player)));
} else {
//m_ActiveWeapons.erase(player);
}
@@ -84,54 +92,5 @@ bool WeaponSystem::OnPlayerSpawned(Events::PlayerSpawned& e)
{
// Select primary weapon on player spawn
// TODO: Select the active one specified by player component
return true;
}
bool WeaponSystem::OnShoot(Events::Shoot& eShoot)
{
if (!eShoot.Player.Valid()) {
return false;
}
// Only run further picking code for the local player!
if (eShoot.Player != LocalPlayer) {
return false;
}
// Screen center, based on current resolution!
// TODO: check if player has enough ammo and if weapon has a cooldown or not
Rectangle screenResolution = m_Renderer->GetViewportSize();
glm::vec2 centerScreen = glm::vec2(screenResolution.Width / 2, screenResolution.Height / 2);
// TODO: check if player has enough ammo and if weapon has a cooldown or not
// Pick middle of screen
PickData pickData = m_Renderer->Pick(centerScreen);
if (pickData.Entity == EntityID_Invalid) {
return false;
}
EntityWrapper player(m_World, pickData.Entity);
// Only care about players being hit
if (!player.HasComponent("Player")) {
player = player.FirstParentWithComponent("Player");
}
if (!player.Valid()) {
return false;
}
// Check for friendly fire
EntityWrapper shooter = eShoot.Player;
if ((ComponentInfo::EnumType)player["Team"]["Team"] == (ComponentInfo::EnumType)shooter["Team"]["Team"]) {
return false;
}
// TODO: Weapon damage calculations etc
Events::PlayerDamage ePlayerDamage;
ePlayerDamage.Player = player;
ePlayerDamage.Damage = 100;
m_EventBroker->Publish(ePlayerDamage);
return true;
}