lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Well, after a bit of thinking, I wrote this up in about five minutes. I'd go test this, but I have no access to a decent environment nor my code at hand, so I can't.

class ObjectManager
{
   std::map<string, object*> object_list;
public:
   bool AddObject(object*); // Returns true if it could add an object with the
                            // name. False if it couldn't (IE: name taken)
   object * GetObject(string name); // Returns the object, NULL if none
   bool RemoveObject(object*); // Removes the object
   bool RemoveObject(string name);
   void UpdateAllObject();  // Calls update of each object.
};

class object
{
   string name;         // Name identifier. Duh.
   OgreNewt::body * body; // Newton physic collision mesh. Also tied through call-
                        // backs to the graphic representation.
public:
   object(string name);
   bool AddPrimitive(string identifier, primitive prim); // Adds a uh.. primitive
                        // that gets called on each update.
   bool RemovePrimitive(string identifier); // Removes the "primitive"
   void Update();            // Prods at each primitive to get itself update.
                             // Any primitive that returns true is removed
                             // from the list.
   Vector3 GetPosition();    // returns the position
};

class primitive
{
   OgreNewt::body * body;       // Physics collision mesh, primitives probably
                              // Are going to want to update this.
public:
   virtual bool Update() = 0; // For derived functions. Returning true will
                              // result in the primitive getting removed from
                              // the list.
   primitive(OgreNewt::body * body_ptr);
};
   
class MoveToPrimitive : public primitive
{
public:
    MoveToPrimitive(Vector3 destination, float speed);
    virtual bool Update(); // Returns true when the object is at the destination
};

class RotatingPrimitive : public primitive
{
   int rotationsleft; // -1 means keep rotating forever.
   float degrees_left_on_rotation;
public:
    RotatingPrimitive(float degrees_to_rotate, int times_to_rotate, float speed, OgreNewt::body * ptr);
    // Degrees to rotate : -1 means keep rotating indefinatelly
    // Times to rotate: -1 means indefinatelly
    // Speed : Degrees per second
    bool Update(); // Returns true when times to rotate has dimished to 0.
};

/* Expose public functions to LUA.... */

Lua pseudo-script.
Function OnStart
   ObjectName = "My Unique Object 13242135"
   body_collision = ObjectManager->CreateObject( ObjectName, "object mesh.mesh");
   MoveToPrimitive( (10,10,10) , body_collision);
   Object(ObjectName, body_collision);
   Object->AddPrimitive(MoveToPrim
itive)

Function OnTick(object)
  if( object->GetPosition == 10,10,10)
      MoveToPrimitive( (0,0,0), object->getbody()); // Where does the collision come from though in this context >_<
      object->AddPrimitive(MoveToPrimitive)
  else if( object->GetPosition == 0,0,0)
      MovePrimitive( (10,10,10), object->getbody() );
      object->addprimitive(MovePrimitive)
   else
    // do nothing 0_q

// Yeah, yeah, the script code looks very odd because I haven't done any LUA
// programming whatsoever so far... but I think it's simple enough that making
// levels shouldn't be too hard. Some of this script code can be generated
// by the level editor.
// Also, I'm missing the whole "object manager" thing that is called by the onstart function -- that's basically that
//"create_platform" function you were talking about.

Well, what do you think? Is this feasible, or is there a simpler route that I can go :P?

Thanks,
Gary

>Date: Tue, 29 Aug 2006 09:15:15 +0200
>From: Andreas Stenius < kaos@explosive.se>
>Subject: Re: Fun with scripting and LUA! (Can't wrap head >around
>        concepts!)
>To: Lua list < lua@bazar2.conectiva.com.br>
>Message-ID: <44F3E983.4030504@explosive.se>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>I find this quite interesting, but it's not easy to answer any of >your
>questions without access to your code (current api) without >jumping in
>and develop the whole thing for you.
>
>I'd suggest you think over your questions and rephrase them >into a more
>thecnical nature as "how do I make my script call a function in >C"
>(after doing some research on the topic), etc.
>
>API-wise I'd recommend you expose functions to declare >platforms (with
>properties, like waypoints and what not). This way you set the >rules in
>your current application (in C++), but get all data from a script >(lua).
>
>Example exposed api (C-side left as exercise :)
>
>create_platform( input: table with options ) output: platform >object
>
>oh.. one function was all I could come up with for starters.. ;)
>
>Example usage:
>
>local a_platform = create_platform{
>        name = "My first Platform",
>       size = { 150, 400 },
>       -- optional: pos = { 200, 400, 0 },
>       -- if left out, use first waypoint
>
>        waypoints = {
>                { 200, 400, 0, speed = 25, target_waypoint = 3 },
>               { 200, 400, 100, speed = 50, target_waypoint = 1 },
>               { 200, 800, 100, speed = 25, target_waypoint = 2 }
>       }
>}
>
>-- comments:
>This way the waypoints can easily be extended with more >features, named
>and so on.. it all comes down to what you need/want to do.
>I left out table keys for x, y, and z values for brevity at the >expense
>of clarity.
>
>This is about as far as I can take you (timewise ;)..
>Be sure to read up on the Wiki, the PiL book(s) and google >around.
>There's always tons of information out there.
>
>Tip: use some of the tools to bind your c++ code to lua if you >don't
>feel comfortable with the c-to-lua api's.
>
>Cheers,
>Andreas