Files
funnygame/public/baseentity.h
2025-07-20 00:45:31 +03:00

141 lines
4.0 KiB
C++

#ifndef ENTITY_H
#define ENTITY_H
#include "engine.h"
#include "interface.h"
#include "tier1/utlvector.h"
#include "math3d.h"
class CBaseEntity;
class C_BaseEntity;
enum EPredictionMode {
PREDICTION_MODE_CREATED,
PREDICTION_MODE_DESTROYED,
PREDICTION_MODE_NONE,
};
//-----------------------------------------------------------------------------
// Base server entity class.
// It is updated every 1/tickrate (64) of a second. Does not require special
// classes to exist.
//-----------------------------------------------------------------------------
class CBaseEntity
{
public:
CBaseEntity();
virtual void Precache() = 0;
virtual void Spawn( void ) = 0;
//---------------------------------------------------------------------
//
//---------------------------------------------------------------------
virtual void Destroy( void ) = 0;
//---------------------------------------------------------------------
// Reads the parameter from the level file. szName indicates of the
// parameter that is sent to be deserialized. szValue is value encoded
// as a string.
//---------------------------------------------------------------------
virtual void ReadParameter( const char *szName, const char *szValue );
//---------------------------------------------------------------------
// Update at constant rate (+-tickrate times a second).
//---------------------------------------------------------------------
virtual void Think( float fDelta ) = 0;
//---------------------------------------------------------------------
// Used to send data from an actual server.
//---------------------------------------------------------------------
virtual void SendToServer() = 0;
//---------------------------------------------------------------------
// Used to recieve data from an actual server.
//---------------------------------------------------------------------
virtual void RecieveFromServer( void *pData, uint32_t nDataSize ) = 0;
void SetPosition( vec3 position );
void SetRotationEuler( vec3 euler );
void SetRotationQuat( vec4 quaternion );
void SetRotationMatrix( mat3 matrix );
void SetScale( vec3 scale );
C_BaseEntity *pClientEntity = NULL;
mat3 m_matrix;
vec3 m_position;
vec3 m_scale;
uint64_t m_id;
EPredictionMode m_prediction;
};
typedef CBaseEntity*(*EntityRegistryFn)( void );
typedef C_BaseEntity*(*ClientEntityRegistryFn)( void );
class CEntityRegistryObject
{
public:
CEntityRegistryObject( const char *szName, const char *szClass, EntityRegistryFn pfn );
const char *m_szName;
const char *m_szClass;
EntityRegistryFn m_pfn;
ClientEntityRegistryFn m_pClientfn = 0;
};
interface IEntityManager: public IInterface
{
public:
CUtlVector<CBaseEntity*> m_entities;
CUtlVector<CEntityRegistryObject*> m_RegisteredEntities;
};
IEntityManager *EntityManager();
#define DECLARE_ENTITY(name, class) \
CBaseEntity *__entity_alloc_##name() \
{ \
return new class; \
}; \
CEntityRegistryObject __entity_##name##_registry(#name, #class, __entity_alloc_##name); \
//-----------------------------------------------------------------------------
// Base client entity class.
// It uses server data directly.
//-----------------------------------------------------------------------------
class C_BaseEntity
{
public:
CBaseEntity *pEntity;
virtual void Precache() = 0;
virtual void Spawn( void ) = 0;
virtual void Destroy( void ) = 0;
virtual void Think( float fDelta ) = 0;
private:
};
interface C_EntityRegistry
{
public:
C_EntityRegistry( const char *pName, ClientEntityRegistryFn pfn );
};
//-----------------------------------------------------------------------------
// Server-Client sync for entities. When new server entity is created, client
// entity gets created as well.
//-----------------------------------------------------------------------------
#define LINK_CLIENT_ENTITY(client, server) \
C_BaseEntity *__c_entity_alloc_##server() \
{ \
return new client; \
}; \
C_EntityRegistry __c_entity_##server##_registry(#server, __c_entity_alloc_##server); \
#endif