From 73c986ec873f29523c68d0222dcff2d2623775ba Mon Sep 17 00:00:00 2001 From: Paul Merrill Date: Sun, 14 Aug 2011 04:59:22 -0700 Subject: [PATCH] Entity descriptor has scripts --- TODO.txt | 2 ++ .../{postMove.script => postmove.script} | 0 src/babysfirst/{preMove.script => premove.script} | 0 src/babysfirst/vlad.entity | 7 ++++- src/dtd/entity.dtd | 7 ++++- src/entity.cpp | 32 +++++++++++++++++++--- src/entity.h | 3 ++ 7 files changed, 45 insertions(+), 6 deletions(-) rename src/babysfirst/{postMove.script => postmove.script} (100%) rename src/babysfirst/{preMove.script => premove.script} (100%) diff --git a/TODO.txt b/TODO.txt index b5655d6..870bae1 100644 --- a/TODO.txt +++ b/TODO.txt @@ -34,3 +34,5 @@ ITEMS * Can we create a metatable for a userdata? Is that what liolib.c does? Right now we use a table object with userdata set at table.object. +* Unify how tile scripts and entity scripts are stored. + diff --git a/src/babysfirst/postMove.script b/src/babysfirst/postmove.script similarity index 100% rename from src/babysfirst/postMove.script rename to src/babysfirst/postmove.script diff --git a/src/babysfirst/preMove.script b/src/babysfirst/premove.script similarity index 100% rename from src/babysfirst/preMove.script rename to src/babysfirst/premove.script diff --git a/src/babysfirst/vlad.entity b/src/babysfirst/vlad.entity index 0daf937..0cf944f 100644 --- a/src/babysfirst/vlad.entity +++ b/src/babysfirst/vlad.entity @@ -1,6 +1,6 @@ - 240 + 240 vlad.sheet @@ -106,5 +106,10 @@ step.sound + + + + + diff --git a/src/dtd/entity.dtd b/src/dtd/entity.dtd index 0042da7..60fdd45 100644 --- a/src/dtd/entity.dtd +++ b/src/dtd/entity.dtd @@ -1,4 +1,4 @@ - + @@ -24,3 +24,8 @@ + + + + + diff --git a/src/entity.cpp b/src/entity.cpp index ed2bf54..7228755 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -297,10 +297,11 @@ void Entity::preMove(coord_t delta) void Entity::preMoveLua() { - if (rc->resourceExists("preMove.script")) { + const std::string& name = scripts["premove"]; + if (name.size() && rc->resourceExists(name)) { Script s(rc); bindEntity(s, this, "entity"); - s.run(rc, "preMove.script"); + s.run(rc, name); } } @@ -332,10 +333,11 @@ void Entity::postMove() void Entity::postMoveLua() { - if (rc->resourceExists("postMove.script")) { + const std::string& name = scripts["postmove"]; + if (rc->resourceExists(name)) { Script s(rc); bindEntity(s, this, "entity"); - s.run(rc, "postMove.script"); + s.run(rc, name); } } @@ -379,6 +381,9 @@ bool Entity::processDescriptor() if (!processSounds(node)) return false; } + else if (!xmlStrncmp(node->name, BAD_CAST("scripts"), 8)) { + processScripts(node); + } } return true; } @@ -497,3 +502,22 @@ bool Entity::processSound(xmlNode* sound) return s; } +void Entity::processScripts(const xmlNode* scripts) +{ + for (xmlNode* script = scripts->xmlChildrenNode; script != NULL; + script = script->next) + if (!xmlStrncmp(script->name, BAD_CAST("script"), 7)) // needed? + processScript(script); +} + +void Entity::processScript(xmlNode* script) +{ + const std::string trigger = readXmlAttribute(script, "trigger"); + const std::string filename = readXmlElement(script); + // FIXME: check name, filename for 0 length + + // Don't verify for script exists here. This happens when it's + // triggered. + scripts[trigger] = filename; +} + diff --git a/src/entity.h b/src/entity.h index 71cace0..a323f88 100644 --- a/src/entity.h +++ b/src/entity.h @@ -109,6 +109,8 @@ protected: const TiledImage& tiles); bool processSounds(const xmlNode* sounds); bool processSound(xmlNode* sound); + void processScripts(const xmlNode* scripts); + void processScript(xmlNode* script); Resourcer* rc; @@ -119,6 +121,7 @@ protected: std::string facing; boost::unordered_map sounds; + boost::unordered_map scripts; bool moving; coord_t dest; -- 2.11.4.GIT