Code Search for Developers
 
 
  

EventManager.h from palisma2d at Krugle


Show EventManager.h syntax highlighted

#pragma once

#include <queue>
#include <list>
#include <map>
#include <string>



/*-----------------------------------------
    Event Types
------------------------------------------*/
enum EventType
{
    EVT_MOVE    = (1<<0),   // entity has moved
    EVT_ATTACK  = (1<<1),   // entity is trying to attack
    EVT_USE     = (1<<2),   // entity is trying to 'use'
    EVT_THROW   = (1<<3),   // entity is throwing
    EVT_FACE    = (1<<4),   // entity changed face direction

    EVT_DIALOG  = (1<<5),
    EVT_ENTITY  = (1<<6),

    EVT_ADD_MISSION = (1<<7),           // mission holder adds
    EVT_RM_MISSION = (1<<8),            // mission holder removes
    EVT_START_MISSION = (1<<9),         // missiondispatcher sends a new mission
    EVT_FAILED_MISSION = (1<<10),       // missiondispatcher sends a failed status
    EVT_COMPLETED_MISSION = (1<<11)

};

/*-----------------------------------------
    Listener Type
------------------------------------------*/
enum ListenerType
{
    LT_ENTITY              = (1<<0),    // Entity listener
    LT_DIALOG              = (1<<1),    // Dialog listener
    LT_MISSION_HOLDER      = (1<<2),    // Mission Holder
    LT_MISSION_DISPATCHER  = (1<<3),    // Mission Listener (dispatchs missions to a mission holder)
    LT_ALL    = 0xFFFFFFFF
};


/**
====================
The events Data
====================
*/
struct IEventData
{
    virtual IEventData* Copy() { return NULL; };
    virtual ~IEventData() {};
};

/**
=======================
The actual Event
=======================
*/
class IEvent
{
public:
    IEvent(EventType type, float time, IEventData* data, ListenerType listenerType) 
        :  m_type(type),
           m_time(time),
           m_data(data),
           m_Ltype(listenerType) {};

    IEvent(const IEvent &e) {
        m_type = e.m_type;
        m_time = e.m_time;
        m_data = NULL;
        m_data = e.m_data->Copy();
        m_Ltype = e.m_Ltype;
    };


    /* Get the Type of Event */
    int GetType() const { return m_type; };
    /* Get the Listener Type of Event */
    int GetListenerType() const { return m_Ltype; };
    /** Get the Time it was fired */
    float GetTimeFired() const { return m_time; };

    /** Get the data */
    template<typename T>
    T* GetData() { return reinterpret_cast<T*>(m_data); }; 

    /** Delete */
    virtual ~IEvent() { if ( m_data ) { delete m_data; m_data = NULL; } };
private:
    // listener type
    ListenerType m_Ltype;
    // Type of Event
    EventType   m_type;
    // time the event was fired
    float       m_time;
    // event data
    IEventData* m_data;

};



/**
=============================
The glue of the subsystems
Hopefully one of these days this will actually 
be true.  As of right now, I am only having player
actions recorded as events.  I am doing this so
we can have play back functionality.

Singleton
=============================
*/
class IEventListener;
class EventManager
{
public:
    /** Get the singleton */
    static EventManager* GetInstance() { return instance; };

    /** Add a Listener */
    void AddListener( IEventListener* listener );
    /** Remove a listener */
    void RemoveListener( IEventListener* listener);

    /** Trigger an event NOW */
    void TriggerEvent( IEvent &e );
    /** Update, trigger any events in the queue */
    void UpdateEventQueue( long deltaTime );

    /** Queue an Event */
    bool QueueEvent( IEvent* e );
    /** Abort an event */
    bool AbortQueuedEvent( const IEvent &e );
    /** Empty all events */
    void EmptyQueue( bool executeAllEvents = false );
    

private:
    EventManager(void);
    // our only instance
    static EventManager* instance;


    // list of listeners
    typedef std::list<IEventListener*> type_Listeners;
    // hash of listeners index
    typedef std::map<int, type_Listeners > type_ListTypes;
    type_ListTypes      m_listeners;

    // queue of events
    typedef std::queue< IEvent* > type_EventQueue;
    type_EventQueue     m_events;
public:
    // destroy
    virtual ~EventManager() { EmptyQueue( false ); };
};


/**
=============================
Event Listener-
Implement this interface, and
register with the EventManager
to receive events
=============================
*/
class IEventListener
{
public:
    IEventListener() { };//EventManager::GetInstance()->AddListener(this); };
    /* Get the Name of the Listener */
    virtual std::string GetName()=0;
    /** Get Listener Type - Events this listener listens too */
    virtual int GetListenerType()=0;

    /** Handle an event */
    virtual bool HandleEvent( IEvent* e )=0;

    virtual ~IEventListener() { };//EventManager::GetInstance()->RemoveListener(this); };
};







See more files for this project here

palisma2d

The University of Wisconsin-Parkside Developers Union first product. More info to come. Code name Palisma.

Project homepage: http://code.google.com/p/palisma2d/
Programming language(s): C,C++
License: gpl2

  Data/
    entities/
      link/
        finallink.tga
      objects/
        pot.tga
        torchpit.bmp
      soldier/
        soldier_01.png
        soldier_01.tga
        soldier_011.tga
      sryion.tga
    maps/
      dialogs/
        test.dialog
      tiles/
        layer2/
          fence.bmp
          fence.tga
          stump.tga
        grass_01.bmp
        street_01.bmp
        street_02.bmp
        street_03.bmp
        tile_a.bmp
        tile_b.bmp
        tile_c.bmp
        tile_d.bmp
        tile_e.bmp
        tile_f.bmp
        tile_g.bmp
        tile_h.bmp
        tile_i.bmp
      map01.dfn
      map01.ents
      map01.evt
      map01.ly1
      map01.ly2
      test.dfn
      test.ly1
      test.ly1.big
      test.ly2
    misc/
      rain1.tga
      rain2.tga
      rain3.tga
      rain4.tga
    scripts/
      hitman_mission/
        hitman.dialog
        hitman.lua
        hitman.msn
        hitman_inprogress.dialog
        hitman_reward.dialog
        hitman_reward.lua
        hitman_setup.lua
      test_mission/
        HitPotSetup.lua
        m_hitPot.dialog
        m_hitPot.lua
        m_hitPot.msn
        m_hitPot_inprogress.dialog
        m_hitPot_reward.lua
        reward.dialog
      mission_check.lua
    sounds/
      weather/
      hurt.wav
      lift.wav
      stroke.wav
      theme.wav
      throw.wav
    weapons/
      shotgun/
  Lib/
    LUA/
    OpenAL/
    SDL/
    Zlib/
  game/
    DialogModel.cpp
    DialogModel.h
    DialogState.cpp
    DialogState.h
    Enemy.cpp
    Enemy.h
    EntityController.cpp
    EntityController.h
    EntityEvents.cpp
    EntityEvents.h
    EntityFactory.cpp
    EntityFactory.h
    EntityManager.cpp
    EntityManager.h
    EntityStates.cpp
    EntityStates.h
    Event.h
    HUD.cpp
    HUD.h
    IEntity.cpp
    IEntity.h
    IWeapon.cpp
    IWeapon.h
    InGameState.cpp
    InGameState.h
    Inventory.cpp
    Inventory.h
    MissionHolder.cpp
    MissionHolder.h
    Player.cpp
    Player.h
    PlayerConfig.cpp
    PlayerConfig.h
    PlayerController.cpp
    PlayerController.h
    QuestImporter.cpp
    QuestImporter.h
    ReadMe.txt
    Scene.cpp
    Scene.h
    Shotgun.cpp
    Shotgun.h
    State.h
    StateFactory.cpp
    StateFactory.h
  gui/
  input/
  render/
  script/
  shared/
  sound/
  Console.cpp
  Console.h
  ConsoleFunctions.h
  Cvars.cpp
  Cvars.h
  EventManager.cpp
  EventManager.h
  Exec_f.cpp
  GameManager.cpp
  GameManager.h
  IConsole.h
  IGameState.h
  IProcess.h
  Kernel.cpp
  Kernel.h
  Log.cpp
  Log.h
  Myriad.cpp
  Myriad.h
  ReadMe.txt
  Recorder.cpp
  Recorder.h
  Resource.cpp
  Resource.h
  Timer.cpp
  Timer.h
  client.log
  game01.zip
  mmanager.cpp
  mmanager.h
  stdafx.cpp
  stdafx.h