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

pad.cc

Go to the documentation of this file.
00001 #include "pad.hh"
00002 #include "keyboardmessage.hh"
00003 #include "pointermessage.hh"
00004 #include "velocity.hh"
00005 #include "spinvelocity.hh"
00006 #include "register.hh"
00007 #include "dynamics.hh"
00008 #include <iostream>
00009 
00010 namespace anoid {
00011     namespace plugin {
00012         namespace anoid {
00013 
00014             using namespace simple;
00015             using namespace base;
00016             using namespace config;
00017 
00018             void Pad::init(Configuration &c) {
00019                 listen("KeyboardMessage");
00020                 listen("PointerMessage");
00021                 Element::init(c);
00022             }
00023 
00024             void Pad::receive(Message *mes) {
00025                 signed int dx = 0, dy = 0;
00026                 signed int rx = 0, ry = 0;
00027                 KeyboardMessage *k = dynamic_cast<KeyboardMessage *>(mes);
00028                 PointerMessage *p = dynamic_cast<PointerMessage *>(mes);
00029 
00030                 Velocity *v = dynamic_cast<Velocity *>(lookupAbility("Velocity"));
00031                 Move *m = dynamic_cast<Move *>(lookupAbility("Move"));
00032                 SpinVelocity *s = dynamic_cast<SpinVelocity *>(lookupAbility("SpinVelocity"));
00033                 Spin *r = dynamic_cast<Spin *>(lookupAbility("Spin"));
00034 
00035                 if (p) {
00036                     dx = -p->getdx();
00037                     dy = -p->getdy();
00038                 } else if(k) {
00039                     SDL_keysym sym = k->getKey();       
00040                     switch (sym.sym) {
00041                         case SDLK_UP:
00042                             dy = 1;
00043                             break;
00044                         case SDLK_DOWN:
00045                             dy = -1;
00046                             break;
00047                         case SDLK_LEFT:
00048                             dx = 1;
00049                             break;
00050                         case SDLK_RIGHT:
00051                             dx = -1;
00052                             break;
00053                         case SDLK_d:
00054                             rx = 1;
00055                             break;
00056                         case SDLK_s:
00057                             rx = -1;
00058                             break;
00059                         case SDLK_a:
00060                             ry = -1;
00061                             break;
00062                         case SDLK_f:
00063                             ry = 1;
00064                             break;
00065                         default:
00066                             break;
00067                     }
00068                     if (k->getUp()) {
00069                         if(v) { //a => v, v abbreviates (a || v)
00070                             dx *= -1;
00071                             dy *= -1;
00072                             rx *= -1;
00073                             ry *= -1;
00074                         } else {
00075                             dx = dy = rx = ry = 0; 
00076                         }
00077                     }
00078                     dx *= 25;
00079                     dy *= 25;
00080                     rx *= 25;
00081                     ry *= 25;
00082                 } else
00083                     Element::receive(mes);
00084 
00085                 if (v) {
00086                     Vector vel = v->getVelocity();
00087                     if (dy)
00088                         vel += dy * Vector(0.0, 60.0, 0.0);
00089                     if (dx)
00090                         vel += dx * Vector(-60.0, 0.0, 0.0);
00091                     v->setVelocity(vel);
00092                 } else if (m) { // Check for movement without acceleration
00093                     Vector pos = m->getPosition();
00094                     if (dy)
00095                         pos += dy * Vector(0.0, 10.0, 0.0);
00096                     if (dx)
00097                         pos += dx * Vector(-10.0, 0.0, 0.0);
00098                     m->setPosition(pos);
00099                 }
00100 
00101                 if (s) { // Check for rotation with acceleration
00102                     Vector vel = s->getSpinVelocity();
00103                     if (ry)
00104                         vel += ry * Vector(0.0, 10000.0, 0.0);
00105                     if (rx)
00106                         vel += rx * Vector(-10000.0, 0.0, 0.0);
00107                     s->setSpinVelocity(vel);
00108                 } else if (r) { // Check for rotation without acceleration
00109                     /*      Vector rot = r->getRotation();
00110                             if (ry)
00111                             rot += ry * Vector(0.0, 2.4, 0.0);
00112                             if (rx)
00113                             rot += rx * Vector(-2.4, 0.0, 0.0);
00114                             r->setRotation(rot);*/
00115                 }
00116             }
00117 
00118             Register(Pad)
00119 
00120         };
00121     };
00122 };

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