Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

world.cc

Go to the documentation of this file.
00001 #include "world.hh"
00002 #include "vector.hh"
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <iostream>
00006 #include "loader.hh"
00007 #include "event.hh"
00008 #include "message.hh"
00009 #include "context.hh"
00010 #include "collisionevent.hh"
00011 
00012 namespace anoid {
00013     namespace main {
00014 
00015         using namespace std;
00016         using namespace simple;
00017         using namespace base;
00018         using namespace config;
00019         using namespace context;
00020 
00021         void collisionCallback(void *client_data, DtObjectRef object1, DtObjectRef object2, const DtCollData *coll_data) {
00022             if (coll_data->normal[0] || coll_data->normal[1] || coll_data->normal[2]) {
00023                 AbstractWorld *theWorld = (AbstractWorld *)client_data;
00024                 Event *e = new CollisionEvent;
00025                 e->addArgument((Object *) object1);
00026                 e->addArgument((Object *) object2);
00027                 e->addData(new Vector(coll_data->normal[0], coll_data->normal[1], coll_data->normal[2]));
00028                 e->addData(new Vector(coll_data->point1[0], coll_data->point1[1], coll_data->point1[2]));
00029                 e->addData(new Vector(coll_data->point2[0], coll_data->point2[1], coll_data->point2[2]));
00030                 theWorld->send(e);
00031             }
00032         }
00033 
00034         World::World(Configuration &c, Context *context, double g): maxid(1), time(0.0), G(g), _context(context) {
00035             world = this;
00036             init(c);
00037         }
00038 
00039         void World::init(Configuration &c) {
00040             if (c.hasElement("G"))
00041                 G = c.getDouble("G");
00042 
00043             old = SDL_GetTicks();
00044 
00045             AbstractWorld::init(c);
00046         }
00047 
00048         Event *World::loadEvent(const string &name) {
00049             Event *e = (Event *) loadObject(name);
00050             return e;
00051         }
00052 
00053         Event *World::loadEvent(const string &name, Configuration &c) {
00054             Event *e = (Event *) loadObject(name);
00055             e->init(c);
00056             return e;
00057         }
00058 
00059         Event *World::sendEvent(const string &name) {
00060             Event *e = loadEvent(name);
00061             send(e);
00062             return e;
00063         }
00064 
00065         Event *World::sendEvent(const string &name, Object *o1) {
00066             Event *e = sendEvent(name);
00067             e->addArgument(o1);
00068             return e;
00069         }
00070 
00071         Event *World::sendEvent(const string &name, Object *o1, Object *o2) {
00072             Event *e = sendEvent(name, o1);
00073             e->addArgument(o2);
00074             return e;
00075         }
00076 
00077         Event *World::sendEvent(const string &name, Object *o1, Object *o2, Object *o3) {
00078             Event *e = sendEvent(name, o1, o2);
00079             e->addArgument(o3);
00080             return e;
00081         }
00082 
00083         int World::getFreshID() {
00084             return ++maxid;
00085         }
00086 
00087         Object *World::loadObject(const string &name) {
00088             return New<Object>(name);
00089         }
00090 
00091         vector<Object*>::iterator World::begin() {
00092             return children.begin();
00093         }
00094 
00095         vector<Object*>::iterator World::end() {
00096             return children.end();
00097         }
00098 
00099         Object* World::add(Object *o) {
00100             Element *e = dynamic_cast<Element *>(o);
00101             if (e)
00102                 childrenE.push_back(e);
00103             return AbstractWorld::add(o);
00104         }
00105 
00106         void World::update() {
00107             Uint32 now = SDL_GetTicks();
00108             time = ((double) (now - old)) / 1000.0;
00109             old = now;
00110             AbstractWorld::update();
00111         }
00112 
00113         void World::redraw() {
00114             AbstractWorld::redraw();
00115             if (!dtTest())
00116                 dtProceed();
00117         }
00118 
00119         void World::send(Event *e) {
00120             events.push_back(e);
00121         }
00122 
00123         void World::listen(Object *o, const string &m) {
00124             listeners[strdup(m.c_str())].push_back(o);
00125             if (m == "CollisionMessage") {
00126                 dtSetObjectResponse(o, collisionCallback, DT_SMART_RESPONSE, this);
00127             }
00128         }
00129 
00130         void World::fireEvents() {
00131             while (!events.empty()) {
00132                 Event *e = events.front();
00133                 if (dynamic_cast<Message *>(e)) {
00134                     vector<Object *>::iterator i = listeners[e->getType().c_str()].begin();
00135                     vector<Object *>::iterator j = listeners[e->getType().c_str()].end();
00136                     while (i != j) {
00137                         e->addArgument(*i);
00138                         ++i;
00139                     }
00140                 }
00141                 e->fire();
00142                 delete e;
00143                 events.pop_front();
00144             }
00145         }
00146 
00147     };
00148 };

Anoid NG © Michael Westergaard, Martin Stig Stissing, Ronni Michael Laursen, and Kristian Bisgaard Lassen