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:
@@ -43,13 +43,17 @@ function createTrigger(handle, room, x, y, script)
|
|||||||
end
|
end
|
||||||
|
|
||||||
characterAPI = require("character")
|
characterAPI = require("character")
|
||||||
|
entityAPI = require("entity")
|
||||||
networkAPI = require("network")
|
networkAPI = require("network")
|
||||||
|
|
||||||
--simple teleporter
|
--simple teleporter
|
||||||
createTrigger("trigger 1", overworld, 0, 0, function(character)
|
createTrigger("trigger 1", overworld, 0, 0, function(entity)
|
||||||
local x, y = characterAPI.GetOrigin(character)
|
if entityAPI.GetType(entity) ~= "character" then
|
||||||
characterAPI.SetOrigin(character, x, y + 128)
|
return
|
||||||
networkAPI.PumpCharacterUpdate(character)
|
end
|
||||||
|
local x, y = characterAPI.GetOrigin(entity)
|
||||||
|
characterAPI.SetOrigin(entity, x, y + 128)
|
||||||
|
networkAPI.PumpCharacterUpdate(entity)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
print("Finished the lua script")
|
print("Finished the lua script")
|
||||||
|
|||||||
@@ -21,6 +21,10 @@
|
|||||||
*/
|
*/
|
||||||
#include "character_data.hpp"
|
#include "character_data.hpp"
|
||||||
|
|
||||||
|
CharacterData::CharacterData(): Entity("character") {
|
||||||
|
//EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
int CharacterData::GetOwner() {
|
int CharacterData::GetOwner() {
|
||||||
return owner;
|
return owner;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
class CharacterData: public Entity {
|
class CharacterData: public Entity {
|
||||||
public:
|
public:
|
||||||
CharacterData() = default;
|
CharacterData();
|
||||||
~CharacterData() = default;
|
~CharacterData() = default;
|
||||||
|
|
||||||
//database stuff
|
//database stuff
|
||||||
@@ -43,7 +43,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
friend class CharacterManager;
|
friend class CharacterManager;
|
||||||
|
|
||||||
int owner;
|
int owner = -1;
|
||||||
std::string handle;
|
std::string handle;
|
||||||
std::string avatar;
|
std::string avatar;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,6 +21,10 @@
|
|||||||
*/
|
*/
|
||||||
#include "entity.hpp"
|
#include "entity.hpp"
|
||||||
|
|
||||||
|
Entity::Entity(const char* _type): type(_type) {
|
||||||
|
//EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
void Entity::Update() {
|
void Entity::Update() {
|
||||||
origin += motion;
|
origin += motion;
|
||||||
}
|
}
|
||||||
@@ -55,4 +59,8 @@ Vector2 Entity::GetMotion() const {
|
|||||||
|
|
||||||
BoundingBox Entity::GetBounds() const {
|
BoundingBox Entity::GetBounds() const {
|
||||||
return bounds;
|
return bounds;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* Entity::GetType() const {
|
||||||
|
return type;
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,8 @@
|
|||||||
#include "bounding_box.hpp"
|
#include "bounding_box.hpp"
|
||||||
#include "vector2.hpp"
|
#include "vector2.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
//The base class for all objects in the world
|
//The base class for all objects in the world
|
||||||
class Entity {
|
class Entity {
|
||||||
public:
|
public:
|
||||||
@@ -41,14 +43,17 @@ public:
|
|||||||
Vector2 GetMotion() const;
|
Vector2 GetMotion() const;
|
||||||
BoundingBox GetBounds() const;
|
BoundingBox GetBounds() const;
|
||||||
|
|
||||||
|
const char* GetType() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Entity() = default;
|
Entity(const char*);
|
||||||
virtual ~Entity() = default;
|
virtual ~Entity() = default;
|
||||||
|
|
||||||
int roomIndex = -1;
|
int roomIndex = -1;
|
||||||
Vector2 origin;
|
Vector2 origin = {0, 0};
|
||||||
Vector2 motion;
|
Vector2 motion = {0, 0};
|
||||||
BoundingBox bounds;
|
BoundingBox bounds = {0, 0, 0, 0};
|
||||||
|
const char* type;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -82,6 +82,12 @@ static int getBounds(lua_State* L) {
|
|||||||
return 4;
|
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[] = {
|
static const luaL_Reg entityLib[] = {
|
||||||
{"SetRoomIndex", setRoomIndex},
|
{"SetRoomIndex", setRoomIndex},
|
||||||
{"SetOrigin", setOrigin},
|
{"SetOrigin", setOrigin},
|
||||||
@@ -91,6 +97,7 @@ static const luaL_Reg entityLib[] = {
|
|||||||
{"GetOrigin", getOrigin},
|
{"GetOrigin", getOrigin},
|
||||||
{"GetMotion", getMotion},
|
{"GetMotion", getMotion},
|
||||||
{"GetBounds", getBounds},
|
{"GetBounds", getBounds},
|
||||||
|
{"GetType", getType},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,10 @@
|
|||||||
*/
|
*/
|
||||||
#include "monster_data.hpp"
|
#include "monster_data.hpp"
|
||||||
|
|
||||||
|
MonsterData::MonsterData(): Entity("monster") {
|
||||||
|
//EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
std::string MonsterData::SetAvatar(std::string s) {
|
std::string MonsterData::SetAvatar(std::string s) {
|
||||||
return avatar = s;
|
return avatar = s;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,11 +24,13 @@
|
|||||||
|
|
||||||
#include "entity.hpp"
|
#include "entity.hpp"
|
||||||
|
|
||||||
|
#include "lua.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class MonsterData: public Entity {
|
class MonsterData: public Entity {
|
||||||
public:
|
public:
|
||||||
MonsterData() = default;
|
MonsterData();
|
||||||
~MonsterData() = default;
|
~MonsterData() = default;
|
||||||
|
|
||||||
std::string SetAvatar(std::string);
|
std::string SetAvatar(std::string);
|
||||||
@@ -41,7 +43,7 @@ private:
|
|||||||
friend class MonsterManager;
|
friend class MonsterManager;
|
||||||
|
|
||||||
std::string avatar;
|
std::string avatar;
|
||||||
int scriptRef;
|
int scriptRef = LUA_NOREF;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user