]> Dogcows Code - chaz/yoink/blobdiff - src/Animation.cc
converted tilemap scripts to lua
[chaz/yoink] / src / Animation.cc
similarity index 72%
rename from src/Moof/Animation.cc
rename to src/Animation.cc
index 7eaec1947eb0a69f6d730a986890dbf92c268412..b449a0fbd97a500e36cd10e987831117fee4bf08 100644 (file)
 #include <map>
 #include <vector>
 
-#include "Animation.hh"
-#include "Mippleton.hh"
-#include "Serializable.hh"
-
+#include <Moof/Log.hh>
+#include <Moof/Mippleton.hh>
+#include <Moof/Script.hh>
 
-namespace Mf {
+#include "Animation.hh"
 
 
 /**
@@ -56,10 +55,10 @@ class Animation::Impl
         * which wants to use these loaded sequences.
         */
 
-       class Data : public Mippleton<Data>
+       class Data : public Mf::Mippleton<Data>
        {
                friend class Impl;
-               friend class Mippleton<Data>;
+               friend class Mf::Mippleton<Data>;
 
                /**
                 * A frame of an animation sequence.  A frame is merely an index which
@@ -70,40 +69,24 @@ class Animation::Impl
                struct Frame
                {
                        unsigned        index;                                  ///< Frame index.
-                       Scalar          duration;                               ///< Frame duration.
+                       Mf::Scalar      duration;                               ///< Frame duration.
 
                        /**
                         * Construction is initialization.  The frame data is loaded from a
                         * frame map which is probably loaded within an animation file.
                         */
                
-                       Frame(SerializableP root) :
+                       Frame(Mf::Script& script, Mf::Script::Value table) :
                                index(0),
                                duration(1.0)
                        {
-                               Serializable::Map rootObj;
-
-                               if (root->get(rootObj))
-                               {
-                                       Serializable::Map::iterator it;
+                               table.pushField("index");
+                               script[-1].get(index);
+                               script.pop();
 
-                                       for (it = rootObj.begin(); it != rootObj.end(); ++it)
-                                       {
-                                               std::string key = (*it).first;
-                                               if (key == "index")
-                                               {
-                                                       long value = 0;
-                                                       (*it).second->get(value);
-                                                       index = unsigned(value);
-                                               }
-                                               else if (key == "duration")
-                                               {
-                                                       double value = 0.0;
-                                                       (*it).second->getNumber(value);
-                                                       duration = Scalar(value);
-                                               }
-                                       }
-                               }
+                               table.pushField("duration");
+                               script[-1].get(duration);
+                               script.pop();
                        }
                };
 
@@ -116,7 +99,7 @@ class Animation::Impl
                struct Sequence
                {
                        std::vector<Frame>      frames;                 ///< List of frames.
-                       Scalar                          delay;                  ///< Scale frame durations.
+                       Mf::Scalar                      delay;                  ///< Scale frame durations.
                        bool                            loop;                   ///< Does the sequence repeat?
                        std::string                     next;                   ///< Next sequence name.
 
@@ -127,53 +110,43 @@ class Animation::Impl
                         * constructor which loads each individual frame.
                         */
 
-                       Sequence(SerializableP root) :
+                       Sequence(Mf::Script& script, Mf::Script::Value table) :
                                delay(0.0),
                                loop(true)
                        {
-                               Serializable::Map rootObj;
+                               table.pushField("delay");
+                               script[-1].get(delay);
+                               script.pop();
 
-                               if (root->get(rootObj))
+                               table.pushField("loop");
+                               script[-1].get(loop);
+                               script.pop();
+
+                               table.pushField("next");
+                               script[-1].get(next);
+                               script.pop();
+
+                               // TODO - sequence class/type not yet implemented
+
+                               table.pushField("frames");
+                               Mf::Script::Value frameTable = script.getTop();
+                               if (frameTable.isTable())
                                {
-                                       Serializable::Map::iterator it;
-                                       for (it = rootObj.begin(); it != rootObj.end(); ++it)
+                                       Mf::Script::Value top = script[-1];
+                                       int index = 1;
+
+                                       for (;;)
                                        {
-                                               std::string key = (*it).first;
-
-                                               if (key == "frames")
-                                               {
-                                                       Serializable::Array framesObj;
-
-                                                       if ((*it).second->get(framesObj))
-                                                       {
-                                                               Serializable::Array::iterator jt;
-
-                                                               for (jt = framesObj.begin();
-                                                                               jt != framesObj.end(); ++jt)
-                                                               {
-                                                                       if (*jt)
-                                                                       {
-                                                                               frames.push_back(Frame(*jt));
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                               else if (key == "delay")
-                                               {
-                                                       double value;
-                                                       (*it).second->getNumber(value);
-                                                       delay = Scalar(value);
-                                               }
-                                               else if (key == "loop")
-                                               {
-                                                       (*it).second->get(loop);
-                                               }
-                                               else if (key == "next")
-                                               {
-                                                       (*it).second->get(next);
-                                               }
+                                               script.push(index);
+                                               frameTable.pushField();
+
+                                               if (top.isTable()) frames.push_back(Frame(script, top));
+                                               else               break;
+
+                                               ++index;
                                        }
                                }
+                               script.pop();
                        }
                };
 
@@ -186,36 +159,58 @@ class Animation::Impl
 
                void loadFromFile()
                {
+                       Mf::Script script;
                        std::string filePath = Animation::getPath(getName());
 
-                       Deserializer deserializer(filePath);
+                       script.importStandardLibraries();
+                       importLogScript(script);
+                       importAnimationBindings(script);
 
-                       SerializableP root = deserializer.deserialize();
-
-                       if (root)
+                       if (script.doFile(filePath) != Mf::Script::SUCCESS)
                        {
-                               Serializable::Map rootObj;
+                               std::string str;
+                               script[-1].get(str);
+                               Mf::logScript("%s", str.c_str());
+                       }
+               }
 
-                               if (root->get(rootObj))
-                               {
-                                       Serializable::Map::iterator it;
+               int defineSequence(Mf::Script& script)
+               {
+                       Mf::Script::Value name = script[1].requireString();
+                       Mf::Script::Value table = script[2].requireTable();
 
-                                       for (it = rootObj.begin(); it != rootObj.end(); ++it)
-                                       {
-                                               sequences.insert(std::pair<std::string,Sequence>((*it).first,
-                                                                       Sequence((*it).second)));
-                                       }
-                               }
-                       }
+                       std::string nameStr;
+                       name.get(nameStr);
+
+                       sequences.insert(std::pair<std::string,Sequence>(nameStr,
+                                               Sequence(script, table)));
+
+                       return 0;
                }
 
+
+               void importAnimationBindings(Mf::Script& script)
+               {
+                       script.importFunction("DefineSequence",
+                                       boost::bind(&Data::defineSequence, this, _1));
+
+                       script.push(1); script.set("ATTACK");
+                       script.push(2); script.set("CHARGE");
+                       script.push(3); script.set("FLY");
+                       script.push(4); script.set("HIT");
+                       script.push(5); script.set("JUMP");
+                       script.push(6); script.set("RUN");
+                       script.push(7); script.set("STAND");
+               }
+
+
                /**
                 * Construction is initialization.  The animation class data container
                 * registers itself as a mippleton and then loads the animation data.
                 */
 
                explicit Data(const std::string& name) :
-                       Mippleton<Data>(name)
+                       Mf::Mippleton<Data>(name)
                {
                        loadFromFile();
                }
@@ -239,8 +234,8 @@ class Animation::Impl
 
        /**
         * Sets up the animation classes to "play" a named sequence.  If another
-        * sequence was active, it will be replaced as the current sequence.  Future
-        * updates will progress the new sequence.
+        * sequence was active, it will be replaced.  Future updates will progress
+        * the new sequence.
         */
 
        void startSequence(const std::string& name)
@@ -270,7 +265,7 @@ class Animation::Impl
         * starts over again.
         */
 
-       void update(Scalar t, Scalar dt)
+       void update(Mf::Scalar t, Mf::Scalar dt)
        {
                if (currentSequence)
                {
@@ -308,8 +303,8 @@ class Animation::Impl
        Data::Sequence*                 currentSequence;        ///< Active sequence.
        unsigned                                frameCounter;           ///< Current frame.
        unsigned                                frameIndex;                     ///< Index of current frame.
-       Scalar                                  timeAccum;                      ///< Time accumulation.
-       Scalar                                  frameDuration;          ///< Scaled frame duration.
+       Mf::Scalar                              timeAccum;                      ///< Time accumulation.
+       Mf::Scalar                              frameDuration;          ///< Scaled frame duration.
 };
 
 
@@ -324,7 +319,7 @@ void Animation::startSequence(const std::string& name)
        impl_->startSequence(name);
 }
 
-void Animation::update(Scalar t, Scalar dt)
+void Animation::update(Mf::Scalar t, Mf::Scalar dt)
 {
        // pass through
        impl_->update(t, dt);
@@ -349,11 +344,9 @@ unsigned Animation::getFrame() const
 
 std::string Animation::getPath(const std::string& name)
 {
-       return Resource::getPath("animations/" + name + ".json");
+       return Mf::Resource::getPath("animations/" + name + ".lua");
 }
 
 
-} // namespace Mf
-
 /** vim: set ts=4 sw=4 tw=80: *************************************************/
 
This page took 0.02686 seconds and 4 git commands to generate.