fysxasteroids/engine/ModelBase.cc

411 lines
9.8 KiB
C++
Raw Permalink Normal View History

2010-04-05 23:38:59 +02:00
#include "ModelBase.h"
#include "PhysicsBase.h"
#include "EntityBase.h"
#include "EntityFactoryBase.h"
#include <coll2d.h>
#include <assert.h>
namespace Engine {
static ModelBase* ModelInstance = NULL;
/*
* Inherited Module functions
*/
int ModelBase::OnInit (int argc, char* argv[]) {
LogDebug ("Model Init");
ModelInstance = this;
mKilledEntities.clear ();
mEntityIdCounter = 0;
mDeltaSec = 0.;
mPlayerEntityId = NullEntityId;
2010-04-05 23:38:59 +02:00
return 0;
}
void ModelBase::OnDestroy () {
LogDebug ("Model Destroy");
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.begin ();
std::map<unsigned int, EntityBase*>::iterator next = iter;
// Since DestroyEntity () also erases the entry in mEntities iter gets
// invalid. Therefore we have to store the iterator that follows iter
while (iter != mEntities.end ()) {
next = iter;
next ++;
DestroyEntity (iter->first);
iter = next;
}
ModelInstance = NULL;
}
/*
* Model specific functions
*/
void ModelBase::Process () {
// Process the controllers and state of all entities
EntityIter entity_iter = mEntities.begin();
2010-04-05 23:38:59 +02:00
do {
if (entity_iter->second == NULL) {
LogError ("Entity with id %d does not exist!", entity_iter->first);
}
entity_iter->second->Update(mDeltaSec);
entity_iter++;
} while (entity_iter != mEntities.end());
/*
// Update the timers
for (TimerIter timer_iter = mTimers.begin(); timer_iter != mTimers.end(); timer_iter++) {
timer_iter->second.Update(mDeltaSec);
timer_iter++;
}
*/
2010-04-05 23:38:59 +02:00
// simulate the world
mPhysics->Simulate (mDeltaSec, this);
assert (mKilledEntities.size() <= mEntities.size());
2010-04-05 23:38:59 +02:00
// remove killed entities
if (mKilledEntities.size() > 0) {
unsigned int i;
LogDebug ("There are %d entities to kill", mKilledEntities.size());
for (i = 0; i < mKilledEntities.size(); i++)
UnregisterEntity (mKilledEntities[i]);
2010-04-05 23:38:59 +02:00
mKilledEntities.clear();
}
2010-04-05 23:38:59 +02:00
}
EntityBase* ModelBase::CreateEntity (int type) {
// Create a new entity id
unsigned int entity_id = CreateEntityId();
// Call the event
OnCreateEntity (type, entity_id);
// Create the entity
EntityBase* result = mEntityFactory->CreateEntity (type);
result->SetId (entity_id);
// And register it
RegisterEntity (result);
return result;
}
void ModelBase::RegisterEntity (EntityBase* entity) {
unsigned int id = entity->mId;
LogDebug ("Registering Entity with id '%u' and type '%u'", id, entity->mType);
2010-04-05 23:38:59 +02:00
if (mEntities.find(id) != mEntities.end ()) {
LogError ("Replacing Entity with id '%u' and type '%u'", id, entity->mType);
2010-04-05 23:38:59 +02:00
}
if (entity->mPhysicState)
mPhysics->RegisterEntity (entity->mPhysicState);
mEntities[id] = entity;
}
void ModelBase::KillEntity (const unsigned int id) {
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.find (id);
LogDebug ("Killing entity with id '%u'", id);
2010-04-05 23:38:59 +02:00
if (iter == mEntities.end ()) {
LogError ("Could not kill Entity with id '%u': Entity not found!", id);
2010-04-05 23:38:59 +02:00
return;
} else {
EntityBase *entity = iter->second;
LogDebug ("Entity pointer = 0x%u", entity);
2010-04-05 23:38:59 +02:00
// call the event handler
OnKillEntity (entity);
LogDebug ("Entity pointer (after kill) = 0x%u", entity);
2010-04-05 23:38:59 +02:00
if (entity->mPhysicState) {
entity->mPhysicState->mAlive = false;
}
mKilledEntities.push_back (iter->first);
}
}
void ModelBase::UnregisterEntity (const unsigned int id) {
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.find (id);
if (iter == mEntities.end ()) {
LogDebug ("iter id=%u entitypointer=0x%x type=%d", iter->first, iter->second, iter->second->mType);
2010-04-05 23:38:59 +02:00
LogError ("Could not unregister Entity with id '%d': Entity not found!", id);
return;
} else {
EntityBase *entity = iter->second;
if (entity->mPhysicState) {
mPhysics->UnregisterEntity (id);
entity->mPhysicState = NULL;
}
if (entity->mControllerState) {
delete entity->mControllerState;
entity->mControllerState = NULL;
}
2010-04-05 23:38:59 +02:00
delete entity;
mEntities.erase (iter);
}
}
EntityBase* ModelBase::GetEntity (const unsigned int id) {
if (id == NullEntityId)
return NULL;
2010-04-05 23:38:59 +02:00
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.find (id);
if (iter != mEntities.end ()) {
return iter->second;
}
return NULL;
}
2011-01-02 18:25:20 +01:00
EntityBase* ModelBase::GetEntityAt (const vector3d &pos) {
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.begin();
while (iter != mEntities.end ()) {
EntityPhysicState* entity = iter->second->mPhysicState;
/// \todo fix const casting!
vector3d temp_pos (pos);
if ( (temp_pos - entity->GetPosition()).length() < entity->mRadius)
return iter->second;
iter++;
}
return NULL;
}
2010-04-05 23:38:59 +02:00
unsigned int ModelBase::CreateEntityId () {
if (mEntityIdCounter == NullEntityId - 1)
LogError ("Could not create valid entity id, reached maximum value of %u", mEntityIdCounter);
2010-04-05 23:38:59 +02:00
return ++mEntityIdCounter;
}
void ModelBase::ClearEntities () {
LogDebug ("Clearing all %d entities.", mEntities.size());
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.begin ();
std::map<unsigned int, EntityBase*>::iterator next = iter;
// Since DestroyEntity () also erases the entry in mEntities iter gets
// invalid. Therefore we have to store the iterator that follows iter
while (iter != mEntities.end ()) {
next = iter;
next ++;
DestroyEntity (iter->first);
iter = next;
}
mEntityIdCounter = 0;
// we alsohave to clear the vector of killed entities!
mKilledEntities.clear();
2010-04-05 23:38:59 +02:00
}
unsigned int ModelBase::GetPlayerEntityId () {
return mPlayerEntityId;
}
void ModelBase::SetPlayerEntityId(unsigned int entity_id) {
mPlayerEntityId = entity_id;
}
2010-04-05 23:38:59 +02:00
/**
* \param collision_time The time when the collision occured relative to the start of the simulation frame
*/
void ModelBase::SendEntityCollisionEvent (const unsigned int reference_entity_id,
const unsigned int incidence_entity_id, float collision_time, vector3d point, vector3d normal) {
2010-04-05 23:38:59 +02:00
// Check whether we report the same contact over and over
static unsigned int last_reference_entity_id = 0;
static unsigned int last_incidence_entity_id = 0;
static float last_collision_time = -1.;
if (!reference_entity_id || !incidence_entity_id)
return;
if (last_reference_entity_id != 0) {
if (last_reference_entity_id == reference_entity_id
&& last_incidence_entity_id == incidence_entity_id
&& last_collision_time == collision_time) {
return;
}
}
// update the last_* entries
last_reference_entity_id = reference_entity_id;
last_incidence_entity_id = incidence_entity_id;
last_collision_time = collision_time;
// now we need to get the Entities and check whether we should
// call EntityBase::CollisionEvent()
EntityBase* reference_entity = GetEntity (reference_entity_id);
EntityBase* incidence_entity = GetEntity (incidence_entity_id);
LogDebug ("Received collision type ref = %d type inc = %d",
reference_entity->mType,
incidence_entity->mType);
// If the incidence entity accepted the collision we can return, otherwise
// we perform the symmetric collision event.
if (incidence_entity->CollisionEvent (reference_entity, point, normal))
2010-04-05 23:38:59 +02:00
return;
reference_entity->CollisionEvent (incidence_entity, point, normal * -1.);
2010-04-05 23:38:59 +02:00
}
/*
* Global functions
*/
unsigned int GetPlayerEntityId () {
if (!ModelInstance) {
LogError ("Couldn't get Player id: Model not initialized!");
2010-04-05 23:38:59 +02:00
}
return ModelInstance->GetPlayerEntityId ();
}
void SetPlayerEntityId(unsigned int entity_id) {
if (!ModelInstance) {
LogError ("Couldn't set Player id: Model not initialized!");
}
ModelInstance->SetPlayerEntityId (entity_id);
}
2010-04-05 23:38:59 +02:00
float GetFrameDuration () {
if (!ModelInstance) {
2011-03-15 10:21:26 +01:00
LogError ("Couldn't get frame duration: Model not initialized!");
2010-04-05 23:38:59 +02:00
}
return ModelInstance->GetFrameDuration ();
}
2011-03-15 10:21:26 +01:00
float GetDurationApplicationStart () {
if (!ModelInstance) {
LogError ("Couldn't get application runtime: Model not initialized!");
}
return ModelInstance->GetDurationApplicationStart ();
}
2010-04-05 23:38:59 +02:00
EntityBase * CreateEntity (int type) {
if (!ModelInstance) {
LogError ("Couldn't create Entity: Model not initialized!");
}
EntityBase *result = ModelInstance->CreateEntity (type);
return result;
}
void DestroyEntity (unsigned int id) {
if (!ModelInstance) {
LogError ("Couldn't destroy Entity: Model not initialized!");
}
ModelInstance->UnregisterEntity (id);
}
void KillEntity (unsigned int id) {
if (!ModelInstance) {
LogError ("Couldn't kill Entity: Model not initialized!");
}
ModelInstance->KillEntity (id);
}
2011-01-02 18:25:20 +01:00
EntityBase* GetEntity (unsigned int id) {
2010-04-05 23:38:59 +02:00
if (!ModelInstance) {
LogError ("Couldn't execute GetEntity(): Model not initialized!");
}
return ModelInstance->GetEntity (id);
}
2011-01-02 18:25:20 +01:00
EntityBase* GetEntityAt(const vector3d &pos) {
if (!ModelInstance) {
LogError ("Couldn't execute GetEntityAt(): Model not initialized!");
}
return ModelInstance->GetEntityAt (pos);
}
2010-04-05 23:38:59 +02:00
EntityPhysicState * GetEntityPhysicState (unsigned int id) {
EntityBase *entity = GetEntity (id);
if (entity)
return entity->mPhysicState;
return NULL;
}
/*
void StartTimer(const std::string &id, float sec) {
if (!ModelInstance) {
LogError ("Couldn't execute GetEntity(): Model not initialized!");
}
ModelInstance->StartTimer(id, sec);
}
bool CheckTimer(const std::string &id) {
if (!ModelInstance) {
LogError ("Couldn't execute GetEntity(): Model not initialized!");
}
return ModelInstance->CheckTimer(id);
}
*/
2011-03-22 23:01:44 +01:00
// /** \brief Starts a timer with the given id that expires after sec seconds */
void StartTimer(const std::string &id, float sec) {
if (!ModelInstance) {
LogError ("Couldn't query Timer: Model not initialized!");
}
ModelInstance->StartTimer(id, sec);
}
// /** \brief Checks whether a timer expired */
bool CheckTimer(const std::string &id) {
if (!ModelInstance) {
LogError ("Couldn't query Timer: Model not initialized!");
}
return ModelInstance->CheckTimer(id);
}
// /** \brief Checks whether a timer expired */
float GetTimer(const std::string &id) {
if (!ModelInstance) {
LogError ("Couldn't query Timer: Model not initialized!");
}
return ModelInstance->GetTimer(id);
}
2010-04-05 23:38:59 +02:00
}