Implemented entity.GetType() in lua

This will only work for userdata decented from the Entity base class.
Using something else results in undefined behavior.
This commit is contained in:
Kayne Ruse
2015-03-09 09:04:12 +11:00
parent 501b1e9814
commit decc77e21c
8 changed files with 46 additions and 12 deletions
+8
View File
@@ -21,6 +21,10 @@
*/
#include "entity.hpp"
Entity::Entity(const char* _type): type(_type) {
//EMPTY
}
void Entity::Update() {
origin += motion;
}
@@ -55,4 +59,8 @@ Vector2 Entity::GetMotion() const {
BoundingBox Entity::GetBounds() const {
return bounds;
}
const char* Entity::GetType() const {
return type;
}
+9 -4
View File
@@ -25,6 +25,8 @@
#include "bounding_box.hpp"
#include "vector2.hpp"
#include <string>
//The base class for all objects in the world
class Entity {
public:
@@ -41,14 +43,17 @@ public:
Vector2 GetMotion() const;
BoundingBox GetBounds() const;
const char* GetType() const;
protected:
Entity() = default;
Entity(const char*);
virtual ~Entity() = default;
int roomIndex = -1;
Vector2 origin;
Vector2 motion;
BoundingBox bounds;
Vector2 origin = {0, 0};
Vector2 motion = {0, 0};
BoundingBox bounds = {0, 0, 0, 0};
const char* type;
};
#endif
+7
View File
@@ -82,6 +82,12 @@ static int getBounds(lua_State* L) {
return 4;
}
static int getType(lua_State* L) {
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 1));
lua_pushstring(L, entity->GetType());
return 1;
}
static const luaL_Reg entityLib[] = {
{"SetRoomIndex", setRoomIndex},
{"SetOrigin", setOrigin},
@@ -91,6 +97,7 @@ static const luaL_Reg entityLib[] = {
{"GetOrigin", getOrigin},
{"GetMotion", getMotion},
{"GetBounds", getBounds},
{"GetType", getType},
{nullptr, nullptr}
};