-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptableBehavior.cpp
76 lines (62 loc) · 1.59 KB
/
ScriptableBehavior.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* The ScriptableBehavior class implements IBehavior mechanics using a Lua script.
*
* Author: Skylar Payne
* Date: 8/5/2013
* File: ScriptableBehavior.cpp
**/
#include "ScriptableBehavior.h"
#include <lua.hpp>
#include "Entity.h"
#include "Logger.h"
lua_State* ScriptableBehavior::s_L;
/**
* @brief ScriptableBehavior::LoadFile loads a file, and parent ID into lua's global table
*/
void ScriptableBehavior::LoadFile()
{
if(luaL_dofile(s_L, _Script.c_str()))
{
g_Logger << lua_tostring(s_L, -1) << "\n";
lua_pop(s_L, 1);
}
lua_pushnumber(s_L, this->GetParent()->GetID());
lua_setglobal(s_L, "my_ID");
}
/**
* @brief ScriptableBehavior::Update Executes an update function from a lua script
*/
void ScriptableBehavior::Update()
{
this->LoadFile();
lua_getglobal(s_L, "Update");
if(lua_pcall(s_L, 0, 0, 0))
{
g_Logger << lua_tostring(s_L, -1) << "\n";
lua_pop(s_L, 1);
}
lua_settop(s_L, 0);
}
/**
* @brief ScriptableBehavior::OnCollide executes a scripted trigger on collision.
* @param ID the entity collided with.
* @param norm the collision surface/penetration depth
*/
void ScriptableBehavior::OnCollide(unsigned int ID, sf::Vector2f& norm)
{
this->LoadFile();
lua_getglobal(s_L, "OnCollide");
if(lua_isfunction(s_L, -1) == 0)
{
return;
}
lua_pushnumber(s_L, ID);
lua_pushnumber(s_L, norm.x);
lua_pushnumber(s_L, norm.y);
if(lua_pcall(s_L, 3, 0, 0))
{
g_Logger << lua_tostring(s_L, -1) << "\n";
lua_pop(s_L, 1);
}
lua_settop(s_L, 0);
}