ControllerPC.h

00001 // ----------------------------------------------------------------------------
00002 //
00003 // tagGame - Example code from the book:
00004 //
00005 //           Artficial Intelligence for Computer Games: An Introduction
00006 //           by John David Funge
00007 //
00008 //           www.ai4games.org
00009 //
00010 // Source code distributed under the Copyright (c) 2003-2007, John David Funge
00011 // Original author: John David Funge (www.jfunge.com)
00012 //
00013 // Licensed under the Academic Free License version 3.0 
00014 // (for details see LICENSE.txt in this directory).
00015 //
00016 // ----------------------------------------------------------------------------
00017 
00018 #ifndef TG_CONTROLLERPC_H
00019 #define TG_CONTROLLERPC_H
00020 
00021 #include "Controller.h"
00022 #include "Util2D.h"
00023 
00024 namespace tagGame
00025 {
00027    template<class T>
00028    class ControllerPC : public Controller
00029    {
00030    public:
00031       ControllerPC(PerceptionPtr perception);
00032       virtual ~ControllerPC();
00033 
00034       bool init();
00035 
00036       virtual void calcAction();
00037 
00038    protected:
00039    private:
00040       T joystick;
00041    }; // ControllerPC
00042 
00043    template<class T>
00044    ControllerPC<T>::ControllerPC(PerceptionPtr perception) :
00045       Controller(perception)
00046    {
00047    }
00048    
00049    template<class T>
00050    ControllerPC<T>::~ControllerPC()
00051    {
00052    }
00053    
00054    template<class T>
00055    bool ControllerPC<T>::init()
00056    {
00057       return joystick.init();
00058    }
00059    
00060    template<class T>
00061    void ControllerPC<T>::calcAction()
00062    {
00063       RealVec v(Util2D::dim);
00064       v[0] = joystick.getX();
00065       v[1] = joystick.getY();
00066    
00067       if (v.isAlmostZero())
00068       {
00069          // direction unchanged
00070          action.setDesiredSpeed(Real(0));
00071       }
00072       else
00073       {
00074          action.setDesiredSpeed(std::min(1.0, v.length()));
00075          v.normalize();
00076          action.setDesiredDirection(v);
00077       }
00078    }
00079 }
00080 
00081 #endif

Generated on Sat Mar 31 22:30:54 2007 for tagGame by  doxygen 1.5.1