DarkflameServer/dLua/lEntity.h
wincent 5f689104af Initial work on LUA scripting.
This LUA scripting implementation uses the sol header only C++ wrapper for the native LUA libraries.

The API does not follow the original LU scripting. Each instance of a script is in its own LUA-State, and has 'self' as a global. There are hooks to all the implemented CppScript methods, as well as a subset of the entity functionallity. Has to be expanded upon.

This is local work which has been sitting for awhile; thought someone might like to take a look at it.
2022-04-13 19:51:14 +02:00

123 lines
2.5 KiB
C++

#pragma once
#include "dLua.h"
#include "Entity.h"
class LuaScript;
/**
* A struct for wrapping an Entity in Lua.
*/
struct lEntity {
static void RegisterClass(LuaScript* script);
LWOOBJID id;
lEntity() : id(LWOOBJID_EMPTY) {}
lEntity(LWOOBJID id) : id(id) {}
lEntity(Entity* entity);
/**
* Checks if this entity is valid.
*/
bool IsValid() const;
/**
* Get the entity's ID.
*/
LWOOBJID GetID() const;
/**
* Get the entity's lot.
*/
LOT GetLOT() const;
/**
* Get the entity's position.
*/
NiPoint3 GetPosition() const;
/**
* Get the entity's rotation.
*/
NiQuaternion GetRotation() const;
/**
* Set the entit's position.
*/
void SetPosition(const NiPoint3& pos);
/**
* Set the entity's rotation.
*/
void SetRotation(const NiQuaternion& rot);
/**
* Set a variable in the entity's data table.
*/
void SetVar(const std::string& key, sol::object value);
/**
* Get a variable from the entity's data table.
*/
sol::object GetVar(LuaScript* script, const std::string& key);
/**
* Smash this entity.
*/
void Smash(sol::object smasher);
/**
* Add a timer.
*/
void AddTimer(const std::string& name, float seconds);
/**
* Load a LUA script and attach it to this entity.
*/
void LoadScript(const std::string& name);
/**
* Serialize this entity.
*/
void Serialize();
Entity* GetEntity() const;
Entity* GetEntityOrThrow() const;
Entity* operator->() const;
/**
* Assets that this entity has a specific component.
*/
template <typename T>
void AssertComponent() {
if (!GetEntity()->HasComponent(T::ComponentType)) {
throw std::runtime_error("Entity does not have component " + std::to_string(T::ComponentType));
}
}
/**
* Assets that an entity has a specific component.
*/
template <typename T>
static inline void AssertComponent(lEntity entity) {
if (!entity->HasComponent(T::ComponentType)) {
throw std::runtime_error("Entity does not have component " + std::to_string(T::ComponentType));
}
}
/**
* Utility for getting a component, asserting it exists
*/
template <typename T>
static inline T* GetComponent(lEntity entity) {
entity.AssertComponent<T>();
return entity->GetComponent<T>();
}
};