Files
qt5-legatus-legacy/Lts/Plugins/BuildingViewer/Common/BgViewer/viewerCtrl/playercomponent.h
T
2026-08-13 18:33:10 +02:00

111 lines
3.4 KiB
C++
Executable File

#pragma once
#include <dtEntity/component.h>
#include <dtEntity/entity.h>
#include <dtEntity/defaultentitysystem.h>
#include <dtEntity/dynamicproperty.h>
#include <dtEntity/scriptaccessor.h>
#include <osg/Referenced>
#include <osg/PositionAttitudeTransform>
#include <dtEntityOSG/layercomponent.h>
#include <dtEntityOSG/osgdebugdrawinterface.h>
#include "buildingmanager.h"
#include <set>
namespace dtEntityOSG
{
class TransformComponent;
}
namespace VCtrl
{
////////////////////////////////////////////////////////////////////////////////
class PlayerComponent final : public dtEntity::Component
{
public:
static const dtEntity::StringId TYPE;
PlayerComponent() = default;
virtual dtEntity::ComponentType GetType() const override { return TYPE; }
virtual bool IsInstanceOf(dtEntity::ComponentType id) const
{
return ((id == TYPE));
}
void OnAddedToEntity(dtEntity::Entity &entity) override;
void OnRemovedFromEntity(dtEntity::Entity &entity) override;
dtEntity::Entity *GetEntity() const { return mEntity; }
void PlaceAt(const osg::Vec3d& _goal, const VCtrl::OBB& obb, int roomNumber, const std::string& roomname);
struct Command : public osg::Referenced
{
double mStartTime = 0;
Command(double startTime) : mStartTime(startTime) {}
virtual ~Command() = default;
virtual void Execute(PlayerComponent *component) = 0;
};
void AddCommand(Command *c) { mCommands.insert(c); }
size_t GetNumCommands() const { return mCommands.size(); }
void ExecuteCommands(double timenow);
void DeleteQueuedCommands();
dtEntityOSG::TransformComponent *GetTransform();
osg::Vec4 GetColor() const;
void SetColor(const osg::Vec4 &);
dtEntity::StringId GetCurrentCellGroup() const { return mCurrentCellGroup; }
dtEntity::StringId GetCurrentRoom() const { return mCurrentRoom; }
dtEntity::StringId GetPreviousRoom() const { return mPreviousRoom; }
private:
void PositionOnMesh(const VCtrl::OBB&, osg::Vec3d& pos, const std::string& roomname);
dtEntity::Entity *mEntity = nullptr;
using Commands = std::set<osg::ref_ptr<Command>>;
Commands mCommands;
dtEntityOSG::TransformComponent *mTransform = nullptr;
osg::Vec4f mColor;
dtEntity::StringId mCurrentCellGroup = dtEntity::StringId();
dtEntity::StringId mCurrentRoom = dtEntity::StringId();
dtEntity::StringId mPreviousRoom = dtEntity::StringId();
};
////////////////////////////////////////////////////////////////////////////////
class PlayerSystem final
: public dtEntity::DefaultEntitySystem<PlayerComponent>
, public dtEntity::ScriptAccessor
{
public:
static const dtEntity::StringId TYPE;
explicit PlayerSystem(dtEntity::EntityManager &em);
void OnTick(const dtEntity::Message &m);
void ShowHidePlayer(dtEntity::EntityId eid, bool show, double when);
void TeleportToTarget(dtEntity::EntityId id, double when, const osg::Vec3d &goal, const VCtrl::OBB &obb, int roomNumber, const std::string &roomname);
void PlayerShoot(int32_t id, dtEntity::EntityId shooterid, dtEntity::EntityId targetid,
const Vec3d &shooterPos, const Vec3d &targetPos,
int buildingIdSrc, int roomNumberSrc, int buildingIdDst, int roomNumberDst, int lineType,
double when, double duration);
void RemovePlayerShoot(int32_t id, double when);
// delete commands from all players
void DeleteQueuedCommands();
private:
dtEntity::MessageFunctor mTickFunctor;
};
}