* Causes the entity to get mad at the player, charge and fight.
*/
struct Aggro : public Component {
- Aggro(const std::string& a)
- : arena(a) {}
+ Aggro(bool y = false)
+ : yes(y) {}
Aggro(XMLElement* imp, XMLElement* def) {
fromXML(imp, def);
}
- std::string arena;
+ bool yes;
void fromXML(XMLElement* imp, XMLElement* def) final {
- (void)imp;
- // TODO null check..?, imp given
- arena = def->StrAttribute("arena");
+ (void)imp, (void)def;
+ // TODO
}
};
#include <vector>
using LuaVariable = std::tuple<std::string, float&>;
+using LuaList = std::vector<LuaVariable>;
+using LuaRetList = std::vector<double>;
class LuaScript {
private:
std::string script;
void setGlobal(const LuaVariable&) const;
- void getReturns(std::vector<double>& rets) const;
+ void getReturns(LuaRetList& rets) const;
static void replace(std::string& s, const std::string& rid, const std::string& put) {
for (unsigned int i = 0; i < s.size(); i++) {
}
void operator()(const std::string& func = "update") const;
- void operator()(const std::string& func, std::vector<LuaVariable> vars) const;
- void operator()(const std::string& func, std::vector<double>& rets, std::vector<LuaVariable> vars) const;
- void operator()(std::vector<LuaVariable> vars) const;
- void operator()(std::vector<double>& rets, std::vector<LuaVariable> vars) const;
+ void operator()(const std::string& func, LuaList vars) const;
+ void operator()(const std::string& func, LuaRetList& rets, LuaList vars) const;
+ void operator()(LuaList vars) const;
+ void operator()(LuaRetList& rets, LuaList vars) const;
};
class LuaSystem {
lua_setglobal(state, std::get<std::string>(nv).c_str());
}
-void LuaScript::getReturns(std::vector<double>& rets) const
+void LuaScript::getReturns(LuaRetList& rets) const
{
int count = lua_gettop(state);
for (int i = 1; i <= count; i++)
lua_pop(state, count);
}
-void LuaScript::operator()(const std::string& func, std::vector<LuaVariable> vars) const
+void LuaScript::operator()(const std::string& func, LuaList vars) const
{
for (auto& v : vars)
setGlobal(v);
}
}
-void LuaScript::operator()(const std::string& func, std::vector<double>& rets,
- std::vector<LuaVariable> vars) const
+void LuaScript::operator()(const std::string& func, LuaRetList& rets,
+ LuaList vars) const
{
for (auto& v : vars)
setGlobal(v);
}
}
-void LuaScript::operator()(std::vector<LuaVariable> vars) const
+void LuaScript::operator()(LuaList vars) const
{
for (auto& v : vars)
setGlobal(v);
}
}
-void LuaScript::operator()(std::vector<double>& rets, std::vector<LuaVariable> vars) const
+void LuaScript::operator()(LuaRetList& rets, LuaList vars) const
{
for (auto& v : vars)
setGlobal(v);
auto ppos = PlayerSystem::getPosition();
if (ppos.x > position.x && ppos.x < position.x + entity.component<Solid>()->width) {
- if (entity.has_component<Aggro>()) {
+ /*if (entity.has_component<Aggro>()) {
//auto dim = entity.component<Solid>();
//ev.emit<AttackEvent>(vec2(position.x + dim->width, position.y + dim->height), ATTACKKKKKK, false);
- /*auto& h = entity.component<Health>()->health;
+ auto& h = entity.component<Health>()->health;
if (h > 0) {
fight = true;
toFight = entity;
h = 0;
- }*/
- } else if (entity.has_component<Trigger>()) {
+ }
+ } else*/ if (entity.has_component<Trigger>()) {
static bool triggering = false;
if (!triggering) {
triggering = true;
}
}
+ static auto doAttack = [](lua_State* s) -> int {
+ vec2 pos (lua_tonumber(s, 1), lua_tonumber(s, 2));
+ LuaScript script ("effect = function()\nflash(255,0,0)\ndamage(1)\nend\n\
+ hit = function()\nxrange = 5\nend");
+ AttackSystem::initLua(script);
+ Attack attack = {vec2(), vec2(5, 5), vec2(), vec2(),
+ script, TextureIterator()};
+ game::events.emit<AttackEvent>(AttackEvent(pos,
+ attack, false));
+ return 0;
+ };
+
// make the entity wander
// TODO initialX and range?
if (entity.has_component<Wander>()) {
- entity.component<Wander>()->script({LuaVariable("vely", direction.y),
- LuaVariable("velx", direction.x)});
+ float aggro = 0;
+ LuaList vars = {
+ LuaVariable("vely", direction.y),
+ LuaVariable("velx", direction.x),
+ LuaVariable("playerx", ppos.x),
+ LuaVariable("playery", ppos.y),
+ LuaVariable("selfx", position.x),
+ LuaVariable("selfy", position.y),
+ LuaVariable("aggro", aggro)
+ };
+
+ bool hasAggro = entity.has_component<Aggro>();
+ if (hasAggro)
+ aggro = entity.component<Aggro>()->yes ? 1 : 0;
+ if (aggro)
+ entity.component<Wander>()->script.addFunction("attack", doAttack);
+ entity.component<Wander>()->script(aggro ? "hostile" : "update", vars);
+ if (hasAggro)
+ entity.component<Aggro>()->yes = aggro > 0 ? 1 : 0;
}
}
});
void WorldSystem::fight(entityx::Entity entity)
{
+ UserError("fights unimplemented?");
std::string exit = currentXMLFile;
savedEntities.emplace_back(entity.id());
- load(entity.component<Aggro>()->arena);
+ //load(entity.component<Aggro>()->arena);
savedEntities.clear();
entity.component<Health>()->health = entity.component<Health>()->maxHealth;
<structure type="1" position="300.0,100.0"/>
<structure inside="bobshouse.xml" type="1" position="10.0,100.0"/>
<skirl />
- <birb />
+ <birb position="-300.0,100.0" />
</World>
<Dialog name="Bob">
end
countdown = countdown - 1
end
+
+ hostile = function()
+
+ end
</Wander>
- <!--<Aggro arena="arena.xml" />
- <Trigger />-->
+ <Aggro/>
+ <!--<Trigger />-->
<Drop>
<item name="Arrow" min="3" max="5" />
</Drop>
<Name value="birb" />
<Wander>
countdown = 0
+ onetime = 0
update = function()
+ if (onetime == 0) then
+ onetime = 1
+ selfy = playery + 300
+ end
+
+ if (math.abs(selfx - playerx) <= 150) then
+ aggro = 1
+ end
+
if (countdown == 0) then
countdown = math.random(3000, 5000)
if (velx >= 0) then
end
countdown = countdown - 1
end
+
+ dipping = 0
+ oldy = 0
+
+ hostile = function()
+ if (dipping == 1) then
+ if (selfy >= oldy) then
+ dipping = 0
+ aggro = 0
+ vely = 0
+ onetime = 0
+ else
+ vely = vely + 0.0006
+ if (math.abs(vely) < 0.01) then
+ attack(selfx, selfy)
+ end
+ end
+ else
+ if (selfy - playery > 200) then
+ if (playerx >= selfx) then
+ velx = 0.01
+ else
+ velx = -0.01
+ end
+ dipping = 1
+ oldy = selfy
+ vely = -.2
+ end
+ end
+ end
</Wander>
+ <Aggro/>
</birb>
<structure>