moved event handling into Modules instead of having seperate event listener objects
This commit is contained in:
+41
-38
@@ -6,7 +6,7 @@
|
||||
#include "Engine.h"
|
||||
#include "Physics.h"
|
||||
#include "Model.h"
|
||||
#include "EventsBase.h"
|
||||
#include "EventBase.h"
|
||||
|
||||
#include "ShipEntity.h"
|
||||
#include "AsteroidEntity.h"
|
||||
@@ -78,61 +78,64 @@ int View::OnInit (int argc, char* argv[]) {
|
||||
mShipPartsSprite.LoadFromPNG ("./data/textures/ship_parts.png");
|
||||
mShipPartsSprite.SetSubSpriteCount (10);
|
||||
|
||||
mAccelerateEventHandler = new AccelerateEventHandler (this);
|
||||
Engine::RegisterListener (mAccelerateEventHandler, EventAccelerateStart);
|
||||
Engine::RegisterListener (mAccelerateEventHandler, EventAccelerateStop);
|
||||
|
||||
mShipExplodeEventHandler = new ShipExplodeEventHandler (this);
|
||||
Engine::RegisterListener (mShipExplodeEventHandler, EventShipExplode);
|
||||
Engine::RegisterListener (this, EventAccelerateStart);
|
||||
Engine::RegisterListener (this, EventAccelerateStop);
|
||||
Engine::RegisterListener (this, EventShipExplode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void View::OnDestroy() {
|
||||
delete mAccelerateEventHandler;
|
||||
delete mShipExplodeEventHandler;
|
||||
|
||||
mBackgroundStars.clear();
|
||||
mShipPartsEntityIds.clear();
|
||||
|
||||
Engine::ViewBase::OnDestroy();
|
||||
}
|
||||
|
||||
/*
|
||||
* Event Handlers
|
||||
*/
|
||||
bool View::AccelerateEventHandler::HandleEvent (const Engine::EventBasePtr &event) const {
|
||||
if (event->mEventType == EventAccelerateStart)
|
||||
mView->mShipThrustSprite.ResetAnimation();
|
||||
bool View::OnReceiveEvent (const Engine::EventBasePtr &event) {
|
||||
switch (event->mEventType) {
|
||||
case EventAccelerateStart:
|
||||
case EventAccelerateStop:
|
||||
if (event->mEventType == EventAccelerateStart)
|
||||
mShipThrustSprite.ResetAnimation();
|
||||
|
||||
Engine::LogDebug ("Received Acceleration Event: %d", event->mEventType);
|
||||
return true;
|
||||
}
|
||||
Engine::LogDebug ("Received Acceleration Event: %d", event->mEventType);
|
||||
return true;
|
||||
|
||||
bool View::ShipExplodeEventHandler::HandleEvent (const Engine::EventBasePtr &event) const {
|
||||
if (event->mEventType == EventShipExplode) {
|
||||
Engine::EntityBase *ship_entity = Engine::GetEntity (event->mEventUnsignedInt);
|
||||
vector3d position = ship_entity->mPhysicState->mPosition;
|
||||
vector3d orientation = ship_entity->mPhysicState->mOrientation;
|
||||
vector3d velocity = ship_entity->mPhysicState->mVelocity;
|
||||
break;
|
||||
case EventShipExplode:
|
||||
if (event->mEventType == EventShipExplode) {
|
||||
Engine::EntityBase *ship_entity = Engine::GetEntity (event->mEventUnsignedInt);
|
||||
vector3d position = ship_entity->mPhysicState->mPosition;
|
||||
vector3d orientation = ship_entity->mPhysicState->mOrientation;
|
||||
vector3d velocity = ship_entity->mPhysicState->mVelocity;
|
||||
|
||||
unsigned int i;
|
||||
mView->mShipPartsEntityIds.clear();
|
||||
unsigned int i;
|
||||
mShipPartsEntityIds.clear();
|
||||
|
||||
for (i = 0; i < mView->mShipPartsSprite.GetSubSpriteCount(); i++) {
|
||||
Engine::EntityBase* part_sprite_particle = Engine::CreateEntity (GameEntityTypeShipPart);
|
||||
part_sprite_particle->mPhysicState->mPosition = position;
|
||||
part_sprite_particle->mPhysicState->mOrientation = orientation;
|
||||
part_sprite_particle->mPhysicState->mVelocity = velocity;
|
||||
part_sprite_particle->mPhysicState->mVelocity = vector3d (velocity[0] * (rand()/float(RAND_MAX)) * 1.7, 0., velocity[2] * (rand()/float(RAND_MAX)) * 1.5);
|
||||
part_sprite_particle->mPhysicState->mAngleVelocity = (rand()/float(RAND_MAX) - 0.5 ) * 100.;
|
||||
for (i = 0; i < mShipPartsSprite.GetSubSpriteCount(); i++) {
|
||||
Engine::EntityBase* part_sprite_particle = Engine::CreateEntity (GameEntityTypeShipPart);
|
||||
part_sprite_particle->mPhysicState->mPosition = position;
|
||||
part_sprite_particle->mPhysicState->mOrientation = orientation;
|
||||
part_sprite_particle->mPhysicState->mVelocity = velocity;
|
||||
part_sprite_particle->mPhysicState->mVelocity = vector3d (velocity[0] * (rand()/float(RAND_MAX)) * 1.7, 0., velocity[2] * (rand()/float(RAND_MAX)) * 1.5);
|
||||
part_sprite_particle->mPhysicState->mAngleVelocity = (rand()/float(RAND_MAX) - 0.5 ) * 100.;
|
||||
|
||||
mView->mShipPartsEntityIds.push_back(part_sprite_particle->mId);
|
||||
}
|
||||
mShipPartsEntityIds.push_back(part_sprite_particle->mId);
|
||||
}
|
||||
}
|
||||
|
||||
Engine::LogDebug ("Received Ship Explode Event: %d", event->mEventType);
|
||||
return true;
|
||||
|
||||
|
||||
break;
|
||||
|
||||
default: Engine::LogWarning ("Received Event with type %d but don't know what to do with it!", event->mEventType);
|
||||
break;
|
||||
}
|
||||
|
||||
Engine::LogDebug ("Received Ship Explode Event: %d", event->mEventType);
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user