WIP shoot
This commit is contained in:
@@ -2,15 +2,15 @@
|
|||||||
#define EPlayerDamage_h__
|
#define EPlayerDamage_h__
|
||||||
|
|
||||||
#include "EventBroker.h"
|
#include "EventBroker.h"
|
||||||
#include "../Core/Entity.h"
|
#include "../Core/EntityWrapper.h"
|
||||||
|
|
||||||
namespace Events
|
namespace Events
|
||||||
{
|
{
|
||||||
|
|
||||||
struct PlayerDamage : Event
|
struct PlayerDamage : Event
|
||||||
{
|
{
|
||||||
double DamageAmount;
|
EntityWrapper Player;
|
||||||
EntityID PlayerDamagedID;
|
double Damage;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,14 @@
|
|||||||
#define EShoot_h__
|
#define EShoot_h__
|
||||||
|
|
||||||
#include "EventBroker.h"
|
#include "EventBroker.h"
|
||||||
#include "../Core/Entity.h"
|
#include "../Core/EntityWrapper.h"
|
||||||
#include "Engine/GLM.h"
|
|
||||||
|
|
||||||
namespace Events
|
namespace Events
|
||||||
{
|
{
|
||||||
|
|
||||||
struct Shoot : Event
|
struct Shoot : Event
|
||||||
{
|
{
|
||||||
//ID for who made the shot
|
EntityWrapper Player;
|
||||||
EntityID shooter;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ struct EntityWrapper
|
|||||||
|
|
||||||
static const EntityWrapper Invalid;
|
static const EntityWrapper Invalid;
|
||||||
|
|
||||||
bool HasComponent(const std::string& componentName);
|
bool HasComponent(const std::string& componentType);
|
||||||
EntityWrapper Parent();
|
EntityWrapper Parent();
|
||||||
EntityWrapper FirstChildByName(const std::string& name);
|
EntityWrapper FirstChildByName(const std::string& name);
|
||||||
|
EntityWrapper FirstParentWithComponent(const std::string& componentType);
|
||||||
bool IsChildOf(EntityWrapper potentialParent);
|
bool IsChildOf(EntityWrapper potentialParent);
|
||||||
bool Valid();
|
bool Valid();
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "Core/System.h"
|
#include "Core/System.h"
|
||||||
#include "Core/EPlayerDamage.h"
|
#include "Core/EPlayerDamage.h"
|
||||||
#include "Core/EShoot.h"
|
#include "Core/EShoot.h"
|
||||||
|
#include "Core/EPlayerSpawned.h"
|
||||||
#include "Input/EInputCommand.h"
|
#include "Input/EInputCommand.h"
|
||||||
|
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
@@ -23,16 +24,18 @@ public:
|
|||||||
virtual void Update(double dt) override;
|
virtual void Update(double dt) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//methods which will take care of specific events
|
|
||||||
EventRelay<WeaponSystem, Events::Shoot> m_EShoot;
|
|
||||||
bool WeaponSystem::OnShoot(const Events::Shoot& e);
|
|
||||||
|
|
||||||
EventRelay<WeaponSystem, Events::InputCommand> m_EInputCommand;
|
|
||||||
bool WeaponSystem::OnInputCommand(const Events::InputCommand& e);
|
|
||||||
|
|
||||||
IRenderer* m_Renderer;
|
IRenderer* m_Renderer;
|
||||||
|
|
||||||
std::vector<std::tuple<EntityID, glm::vec2>> m_EShootVector;
|
// State
|
||||||
|
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
|
||||||
|
|
||||||
|
// Events
|
||||||
|
EventRelay<WeaponSystem, Events::PlayerSpawned> m_EPlayerSpawned;
|
||||||
|
bool WeaponSystem::OnPlayerSpawned(const Events::PlayerSpawned& e);
|
||||||
|
EventRelay<WeaponSystem, Events::Shoot> m_EShoot;
|
||||||
|
bool WeaponSystem::OnShoot(const Events::Shoot& e);
|
||||||
|
EventRelay<WeaponSystem, Events::InputCommand> m_EInputCommand;
|
||||||
|
bool WeaponSystem::OnInputCommand(const Events::InputCommand& e);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
<c:Collidable/>
|
<c:Collidable/>
|
||||||
<c:Model/>
|
<c:Model/>
|
||||||
<c:Physics>
|
<c:Physics>
|
||||||
<Velocity X="8.33307467e-09" Y="0" Z="9.82836834e-09"/>
|
<Gravity>false</Gravity>
|
||||||
</c:Physics>
|
</c:Physics>
|
||||||
<c:Player>
|
<c:Player>
|
||||||
<MovementSpeed>5</MovementSpeed>
|
<MovementSpeed>5</MovementSpeed>
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
</Team>
|
</Team>
|
||||||
</c:Team>
|
</c:Team>
|
||||||
<c:Transform>
|
<c:Transform>
|
||||||
<Position X="2.84928203" Y="0.0264999866" Z="7.31610918"/>
|
<Position X="-0.550999999" Y="0.0264999866" Z="3.01600003"/>
|
||||||
<Orientation X="0" Y="2.14675093" Z="0"/>
|
<Orientation X="0" Y="2.14675093" Z="0"/>
|
||||||
</c:Transform>
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
@@ -82,6 +82,7 @@
|
|||||||
<Components>
|
<Components>
|
||||||
<c:Model>
|
<c:Model>
|
||||||
<Resource>Models/AssaultHeadless.obj</Resource>
|
<Resource>Models/AssaultHeadless.obj</Resource>
|
||||||
|
<Color A="1" B="0" G="0" R="1"/>
|
||||||
</c:Model>
|
</c:Model>
|
||||||
<c:Transform>
|
<c:Transform>
|
||||||
<Orientation X="0" Y="3.14159274" Z="0"/>
|
<Orientation X="0" Y="3.14159274" Z="0"/>
|
||||||
|
|||||||
@@ -36,6 +36,18 @@ EntityWrapper EntityWrapper::FirstChildByName(const std::string& name)
|
|||||||
return EntityWrapper::Invalid;
|
return EntityWrapper::Invalid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EntityWrapper EntityWrapper::FirstParentWithComponent(const std::string& componentType)
|
||||||
|
{
|
||||||
|
EntityWrapper entity = *this;
|
||||||
|
while (entity.Parent().Valid()) {
|
||||||
|
entity = entity.Parent();
|
||||||
|
if (entity.HasComponent(componentType)) {
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return EntityWrapper::Invalid;
|
||||||
|
}
|
||||||
|
|
||||||
bool EntityWrapper::IsChildOf(EntityWrapper potentialParent)
|
bool EntityWrapper::IsChildOf(EntityWrapper potentialParent)
|
||||||
{
|
{
|
||||||
EntityWrapper entity = *this;
|
EntityWrapper entity = *this;
|
||||||
|
|||||||
@@ -327,8 +327,8 @@ bool Client::OnInputCommand(const Events::InputCommand & e)
|
|||||||
bool Client::OnPlayerDamage(const Events::PlayerDamage & e)
|
bool Client::OnPlayerDamage(const Events::PlayerDamage & e)
|
||||||
{
|
{
|
||||||
Packet packet(MessageType::OnInputCommand, m_SendPacketID);
|
Packet packet(MessageType::OnInputCommand, m_SendPacketID);
|
||||||
packet.WritePrimitive(e.DamageAmount);
|
packet.WritePrimitive(e.Damage);
|
||||||
packet.WritePrimitive(e.PlayerDamagedID);
|
packet.WritePrimitive(e.Player.ID);
|
||||||
send(packet);
|
send(packet);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -286,8 +286,8 @@ void Server::parseOnInputCommand(Packet& packet)
|
|||||||
void Server::parseOnPlayerDamage(Packet & packet)
|
void Server::parseOnPlayerDamage(Packet & packet)
|
||||||
{
|
{
|
||||||
Events::PlayerDamage e;
|
Events::PlayerDamage e;
|
||||||
e.DamageAmount = packet.ReadPrimitive<double>();
|
e.Damage = packet.ReadPrimitive<double>();
|
||||||
e.PlayerDamagedID = packet.ReadPrimitive<EntityID>();
|
e.Player = EntityWrapper(m_World, packet.ReadPrimitive<EntityID>());
|
||||||
m_EventBroker->Publish(e);
|
m_EventBroker->Publish(e);
|
||||||
//LOG_DEBUG("Server::parseOnPlayerDamage: Command is %s. Value is %f. PlayerID is %i.", e.DamageAmount, e.PlayerDamagedID, e.TypeOfDamage.c_str());
|
//LOG_DEBUG("Server::parseOnPlayerDamage: Command is %s. Value is %f. PlayerID is %i.", e.DamageAmount, e.PlayerDamagedID, e.TypeOfDamage.c_str());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ void HealthSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& comp
|
|||||||
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e)
|
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e)
|
||||||
{
|
{
|
||||||
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
||||||
m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerDamagedID, -e.DamageAmount));
|
//m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerDamagedID, -e.DamageAmount));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,49 +5,79 @@ WeaponSystem::WeaponSystem(World* world, EventBroker* eventBroker, IRenderer* re
|
|||||||
, ImpureSystem()
|
, ImpureSystem()
|
||||||
, m_Renderer(renderer)
|
, m_Renderer(renderer)
|
||||||
{
|
{
|
||||||
|
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &WeaponSystem::OnPlayerSpawned);
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &WeaponSystem::OnInputCommand);
|
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &WeaponSystem::OnInputCommand);
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EShoot, &WeaponSystem::OnShoot);
|
EVENT_SUBSCRIBE_MEMBER(m_EShoot, &WeaponSystem::OnShoot);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeaponSystem::Update(double dt)
|
void WeaponSystem::Update(double dt)
|
||||||
{
|
{
|
||||||
for (int i = m_EShootVector.size(); i > 0; i--)
|
|
||||||
{
|
|
||||||
//TODO: check if player has enough ammo and if weapon has a cooldown or not
|
|
||||||
|
|
||||||
//pick the object
|
}
|
||||||
PickData pickDataFromShot = m_Renderer->Pick(std::get<1>(m_EShootVector[i - 1]));
|
|
||||||
if (pickDataFromShot.Entity == EntityID_Invalid) {
|
bool WeaponSystem::OnPlayerSpawned(const Events::PlayerSpawned& e)
|
||||||
m_EShootVector.erase(m_EShootVector.begin() + i - 1);
|
{
|
||||||
continue;
|
if (e.PlayerID == -1) {
|
||||||
}
|
m_LocalPlayer = e.Player;
|
||||||
//if its a player, do PlayerDamage event
|
|
||||||
const bool hasPlayerComponent = m_World->HasComponent(pickDataFromShot.Entity, "Player");
|
|
||||||
if (hasPlayerComponent) {
|
|
||||||
Events::PlayerDamage ePlayerDamage;
|
|
||||||
//TODO: damage based on weapontype/class?
|
|
||||||
//TODO: multiple shots at the same time? (shotgunner)
|
|
||||||
ePlayerDamage.DamageAmount = 25;
|
|
||||||
ePlayerDamage.PlayerDamagedID = pickDataFromShot.Entity;
|
|
||||||
m_EventBroker->Publish(ePlayerDamage);
|
|
||||||
}
|
|
||||||
m_EShootVector.erase(m_EShootVector.begin() + i - 1);
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WeaponSystem::OnInputCommand(const Events::InputCommand& e)
|
bool WeaponSystem::OnInputCommand(const Events::InputCommand& e)
|
||||||
{
|
{
|
||||||
|
// Only shoot client-side!
|
||||||
|
if (e.PlayerID != -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only shoot if the player is alive
|
||||||
|
if (!m_LocalPlayer.Valid()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (e.Command == "PrimaryFire" && e.Value > 0) {
|
if (e.Command == "PrimaryFire" && e.Value > 0) {
|
||||||
Events::Shoot eShoot;
|
Events::Shoot eShoot;
|
||||||
eShoot.shooter = e.PlayerID;
|
eShoot.Player = m_LocalPlayer;
|
||||||
m_EventBroker->Publish(eShoot);
|
m_EventBroker->Publish(eShoot);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool WeaponSystem::OnShoot(const Events::Shoot& e) {
|
|
||||||
//screen center, based on current resolution!
|
bool WeaponSystem::OnShoot(const Events::Shoot& eShoot) {
|
||||||
|
// Screen center, based on current resolution!
|
||||||
Rectangle screenResolution = m_Renderer->Resolution();
|
Rectangle screenResolution = m_Renderer->Resolution();
|
||||||
glm::vec2 centerScreen = glm::vec2(screenResolution.Width / 2, screenResolution.Height / 2);
|
glm::vec2 centerScreen = glm::vec2(screenResolution.Width / 2, screenResolution.Height / 2);
|
||||||
m_EShootVector.push_back(std::make_pair(e.shooter, centerScreen));
|
|
||||||
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user