]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Scene.cc
new level-based controllers
[chaz/yoink] / src / Moof / Scene.cc
index fe49ed276d8706edc8626b566db7db51dd108dd8..b3313124d22100bb4e59871ca8d4e3070d290987 100644 (file)
@@ -56,6 +56,13 @@ struct Meh
 
        OctreeP         octree;
 
+       enum AXIS
+       {
+               X = 0,
+               Y = 1,
+               Z = 2
+       };
+
        Meh()
        {
                octree = Octree::alloc(Aabb());
@@ -112,15 +119,9 @@ struct Meh
 
        int translate(Script& script)
        {
-               Script::Value x = script[1];
-               Script::Value y = script[2];
-               Script::Value z = script[3];
-
-               if (!x.isNumber() || !y.isNumber() || !z.isNumber())
-               {
-                       logWarning("wrong arguments to translate; ignoring...");
-                       return 0;
-               }
+               Script::Value x = script[1].requireNumber();
+               Script::Value y = script[2].requireNumber();
+               Script::Value z = script[3].requireNumber();
 
                Vector3 vec;
                x.get(vec[0]);
@@ -139,9 +140,9 @@ struct Meh
                if (script.getSize() == 3)
                {
                        Vector3 vec;
-                       script[1].get(vec[0]);
-                       script[2].get(vec[1]);
-                       script[3].get(vec[2]);
+                       script[1].requireNumber().get(vec[0]);
+                       script[2].requireNumber().get(vec[1]);
+                       script[3].requireNumber().get(vec[2]);
 
                        Matrix4 scaling;
                        cml::matrix_scale(scaling, vec);
@@ -150,16 +151,15 @@ struct Meh
                else if (script.getSize() == 1)
                {
                        Scalar value = 1.0;
-                       script[1].get(value);
+                       script[1].requireNumber().get(value);
 
                        Matrix4 scaling;
-                       cml::matrix_uniform_scale(scaling,
-                                       Scalar(value));
+                       cml::matrix_uniform_scale(scaling, value);
                        transform = scaling * transform;
                }
                else
                {
-                       logWarning("wrong arguments to scale; ignoring...");
+                       script.getTop().throwError("wrong number of arguments");
                }
 
                return 0;
@@ -167,53 +167,34 @@ struct Meh
 
        int rotate(Script& script)
        {
-               Script::Value a = script[1];
-               Script::Value d = script[2];
-
-               if (!a.isString() || !d.isNumber())
-               {
-                       logWarning("wrong arguments to rotate; ignoring...");
-                       return 0;
-               }
-
-               std::string axis;
-               a.get(axis);
+               Script::Value axis = script[1].requireString();
+               Script::Value angle = script[2].requireNumber();
 
                size_t index = 0;
-               if (axis == "x")      index = 0;
-               else if (axis == "y") index = 1;
-               else if (axis == "z") index = 2;
+               axis.get(index);
 
                Scalar value;
-               d.get(value);
+               angle.get(value);
 
-               cml::matrix_rotate_about_world_axis(transform,
-                               index, cml::rad(Scalar(value)));
+               cml::matrix_rotate_about_world_axis(transform, index, cml::rad(value));
 
                return 0;
        }
 
        int setTexture(Script& script)
        {
-               Script::Value t = script[1];
+               Script::Value name = script[1].requireString();
 
-               if (t.isString()) t.get(texture);
-               else logWarning("wrong arguments to setTexture; ignoring...");
+               name.get(texture);
 
                return 0;
        }
 
        int makeTilemap(Script& script)
        {
-               Script::Value table = script[1];
+               Script::Value table = script[1].requireTable();
                Script::Value top = script[-1];
 
-               if (!table.isTable())
-               {
-                       logWarning("wrong arguments to makeTilemap; ignoring...");
-                       return 0;
-               }
-
                long width = 1;
                long height = 1;
 
@@ -226,43 +207,37 @@ struct Meh
                Script::Value tiles = script.getTop();
                nTiles = tiles.getLength();
 
-               std::vector< std::vector<Tilemap::Index> > indices;
+               if (nTiles % width != 0) table.throwError("invalid number of tiles");
 
-               if (nTiles % width == 0)
-               {
-                       int i, w, h;
+               std::vector< std::vector<Tilemap::Index> > indices;
 
-                       height = nTiles / width;
-                       indices.resize(height);
+               int i, w, h;
 
-                       // the indices are stored upside-down in the scene file so that they
-                       // are easier to edit as text, so we'll need to load them last row
-                       // first
+               height = nTiles / width;
+               indices.resize(height);
 
-                       i = 1;
-                       for (h = height - 1; h >= 0; --h)
-                       {
-                               std::vector<Tilemap::Index> row;
+               // the indices are stored upside-down in the scene file so that they
+               // are easier to edit as text, so we'll need to load them last row
+               // first
 
-                               for (w = 0; w < width; ++w, ++i)
-                               {
-                                       script.checkStack(2);
-                                       script.push(long(i));
-                                       tiles.pushField();
+               i = 1;
+               for (h = height - 1; h >= 0; --h)
+               {
+                       std::vector<Tilemap::Index> row;
 
-                                       long index;
-                                       top.get(index);
+                       for (w = 0; w < width; ++w, ++i)
+                       {
+                               script.checkStack(2);
+                               script.push(long(i));
+                               tiles.pushField();
 
-                                       row.push_back(Tilemap::Index(index));
-                               }
+                               long index;
+                               top.get(index);
 
-                               indices[h] = row;
+                               row.push_back(Tilemap::Index(index));
                        }
-               }
-               else
-               {
-                       logError("invalid tiles in tilemap instruction");
-                       return 0;
+
+                       indices[h] = row;
                }
 
                Vector4 vertices[height+1][width+1];
@@ -315,16 +290,16 @@ struct Meh
                if (table.isTable())
                {
                        table.pushField("tile");
-                       if (top.isNumber()) top.get(index);
+                       top.get(index);
 
                        table.pushField("u_scale");
-                       if (top.isNumber()) top.get(width);
+                       top.get(width);
 
                        table.pushField("blend");
-                       if (top.isBoolean()) top.get(blending);
+                       top.get(blending);
 
                        table.pushField("fog");
-                       if (top.isBoolean()) top.get(fog);
+                       top.get(fog);
                }
 
                Vector4 vertices[2][width+1];
@@ -368,7 +343,7 @@ struct Meh
 };
 
 
-static void importScriptBindings(Script& script, Meh& scene)
+static void importSceneBindings(Script& script, Meh& scene)
 {
        script.importFunction("SetPlayfieldBounds",
                        boost::bind(&Meh::setPlayfieldBounds, &scene, _1));
@@ -388,6 +363,25 @@ static void importScriptBindings(Script& script, Meh& scene)
                        boost::bind(&Meh::makeTilemap, &scene, _1));
        script.importFunction("MakeBillboard",
                        boost::bind(&Meh::makeBillboard, &scene, _1));
+
+       long detail = 3;
+       Settings::getInstance().get("detail", detail);
+       script.push(detail);
+       script.set("detail");
+
+       script.push(Quad::LEFT);
+       script.set("LEFT");
+       script.push(Quad::RIGHT);
+       script.set("RIGHT");
+       script.push(Quad::TOP);
+       script.set("TOP");
+
+       script.push(Meh::X);
+       script.set("X");
+       script.push(Meh::Y);
+       script.set("Y");
+       script.push(Meh::Z);
+       script.set("Z");
 }
 
 
@@ -400,22 +394,14 @@ OctreeP loadScene(const std::string& name)
        Script script;
        script.importStandardLibraries();
        importLogScript(script);
-       importScriptBindings(script, cool);
-
-       long detail = 3;
-       Settings::getInstance().get("detail", detail);
-
-       script.push(detail);
-       script.set("detail");
+       importSceneBindings(script, cool);
 
-       logInfo("doing file...");
-       if (script.doFile(filePath) != 0)
+       if (script.doFile(filePath) != Script::SUCCESS)
        {
                std::string str;
                script[-1].get(str);
-               logError("lua error: %s", str.c_str());
+               logScript("%s", str.c_str());
        }
-       logInfo("done");
 
        cool.octree->sort();
        return cool.octree;
This page took 0.026123 seconds and 4 git commands to generate.