#ifndef ENTITY_H
#define ENTITY_H

#include "engine.h"
#include "tier1/utlvector.h"
#include "math3d.h"

class CBaseEntity;
class C_BaseEntity;

//-----------------------------------------------------------------------------
// 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;
	virtual void ReadParameter( const char *szName, const char *szValue );
	virtual void Think( float fDelta ) = 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;
};


typedef CBaseEntity*(*EntityRegistryFn)( void );
typedef C_BaseEntity*(*ClientEntityRegistryFn)( void );

interface CEntityRegistry
{
public:
	CEntityRegistry( const char *szName, const char *szClass, EntityRegistryFn pfn );

	const char *m_szName;
	const char *m_szClass;
	EntityRegistryFn m_pfn;
	ClientEntityRegistryFn m_pClientfn;
};



#define DECLARE_ENTITY(name, class) \
CBaseEntity *__entity_alloc_##name() \
{ \
	return new class; \
}; \
GLOBAL_USED CEntityRegistry __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); \


extern CUtlSelfReferencingVector<CBaseEntity*> g_entities;
extern CUtlVector<CEntityRegistry*> g_RegisteredEntities;


#endif
