#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"
/**
* 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
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();
}
};
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.
* 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();
}
};
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();
}
/**
* 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)
* starts over again.
*/
- void update(Scalar t, Scalar dt)
+ void update(Mf::Scalar t, Mf::Scalar dt)
{
if (currentSequence)
{
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.
};
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);
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: *************************************************/