send player and other players to the lua function

This commit is contained in:
Bluub
2021-06-09 16:50:56 +02:00
parent a1ddc170dc
commit 620812107b
5 changed files with 43 additions and 46 deletions
+12 -10
View File
@@ -1,14 +1,16 @@
function Sum(a,b)
return a + b;
end
function Update(infos)
function Update(player, infos, players)
print(player.x);
print(player.y);
print(player.z);
for i, info in ipairs(infos) do
print (info.x);
print (info.y);
print (info.z);
print (info.type);
--print("x");
--print (info.x);
--print("y");
--print (info.y);
--print("z");
--print (info.z);
--print("type");
--print (info.type);
end
return 1, 1, false, false;
--return info.x, info.y, (info.z ~= 1), (info.type ~= 0);
end
+1
View File
@@ -55,4 +55,5 @@ namespace BBM
constexpr const char Breakable[] = "Breakable";
constexpr const char Hole[] = "Hole";
constexpr const char Bumper[] = "Bumper";
constexpr const char Player[] = "Player";
}
+1
View File
@@ -96,6 +96,7 @@ namespace BBM
.addComponent<ControllableComponent>()
.addComponent<AnimatorComponent>()
.addComponent<KeyboardComponent>()
.addComponent<IAControllableComponent>("./ai_scripts/john.lua")
.addComponent<TagComponent<Blowable>>()
//.addComponent<GamepadComponent>(0)
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 3)
@@ -3,6 +3,7 @@
//
#include "Map/MapInfo.hpp"
#include "Component/Tag/TagComponent.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Component/IAControllable/IAControllableComponent.hpp"
#include "System/IAControllable/IAControllableSystem.hpp"
@@ -12,46 +13,40 @@
namespace BBM
{
IAControllableSystem::IAControllableSystem(WAL::Wal &wal)
: System(wal)
: System(wal), _wal(wal)
{ }
float IAControllableSystem::getReturnNumber(lua_State *state)
{
float res = 0;
if (lua_isnil(state, -1))
return res;
if (!lua_isnumber(state, -1))
return res;
res = lua_tonumber(state, -1);
lua_pop(state, 1);
return res;
}
bool IAControllableSystem::getReturnBool(lua_State *state)
{
bool res = false;
if (lua_isnil(state, -1))
return res;
if (!lua_isboolean(state, -1))
return res;
res = lua_toboolean(state, -1);
lua_pop(state, 1);
return res;
}
void IAControllableSystem::onFixedUpdate(WAL::ViewEntity<ControllableComponent, IAControllableComponent> &entity)
void IAControllableSystem::onFixedUpdate(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent> &entity)
{
auto &ia = entity.get<IAControllableComponent>();
auto &controllable = entity.get<ControllableComponent>();
auto &pos = entity.get<PositionComponent>();
MapInfo player(pos.position, MapGenerator::NOTHING);
std::vector<MapInfo> infos;
std::vector<MapInfo> players;
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Breakable>>())
infos.push_back(MapInfo(pos.position, MapGenerator::BREAKABLE));
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Unbreakable>>())
infos.push_back(MapInfo(pos.position, MapGenerator::UNBREAKABLE));
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Bumper>>())
infos.push_back(MapInfo(pos.position, MapGenerator::BUMPER));
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Hole>>())
infos.push_back(MapInfo(pos.position, MapGenerator::HOLE));
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Player>>()) {
if (static_cast<WAL::Entity>(entity).getUid() == other.getUid())
continue;
players.push_back(MapInfo(pos.position, MapGenerator::NOTHING));
}
luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update");
if (!updateFunc.isFunction())
return;
luabridge::LuaResult res = updateFunc(infos);
luabridge::LuaResult res = updateFunc(player, infos, players);
if (res.hasFailed() || res.size() != 4)
return;
@@ -9,16 +9,14 @@
namespace BBM
{
//! @brief A system to handle keyboard entities.
class IAControllableSystem : public WAL::System<ControllableComponent, IAControllableComponent>
class IAControllableSystem : public WAL::System<PositionComponent, ControllableComponent, IAControllableComponent>
{
private:
//! @brief extract a number from the lua stack
float getReturnNumber(lua_State *state);
//! @brief extract a bool from the lua stack
bool getReturnBool(lua_State *state);
//! @brief Reference to wal to get Views
WAL::Wal &_wal;
public:
//! @inherit
void onFixedUpdate(WAL::ViewEntity<ControllableComponent, IAControllableComponent> &entity) override;
void onFixedUpdate(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent> &entity) override;
//! @brief A default constructor
IAControllableSystem(WAL::Wal &wal);