00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_engine2_entity.hpp"
00012
00013 #include "xrb_engine2_compound.hpp"
00014 #include "xrb_engine2_events.hpp"
00015 #include "xrb_engine2_objectlayer.hpp"
00016 #include "xrb_engine2_physicshandler.hpp"
00017 #include "xrb_engine2_quadtree.hpp"
00018 #include "xrb_engine2_sprite.hpp"
00019 #include "xrb_engine2_world.hpp"
00020 #include "xrb_render.hpp"
00021 #include "xrb_serializer.hpp"
00022
00023 namespace Xrb
00024 {
00025
00026 Engine2::Sprite *Engine2::Entity::OwnerSprite () const
00027 {
00028 if (m_owner_object == NULL)
00029 return NULL;
00030
00031 ASSERT1(m_owner_object->GetObjectType() == OT_SPRITE);
00032 return static_cast<Sprite *>(m_owner_object);
00033 }
00034
00035 Engine2::Compound *Engine2::Entity::OwnerCompound () const
00036 {
00037 if (m_owner_object == NULL)
00038 return NULL;
00039
00040 ASSERT1(m_owner_object->GetObjectType() == OT_COMPOUND);
00041 return static_cast<Compound *>(m_owner_object);
00042 }
00043
00044 void Engine2::Entity::SetWrappedOffset (FloatVector2 const &wrapped_offset)
00045 {
00046 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(wrapped_offset[Dim::X]));
00047 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(wrapped_offset[Dim::Y]));
00048 m_wrapped_offset = wrapped_offset;
00049 }
00050
00051 void Engine2::Entity::AccumulateWrappedOffset (FloatVector2 const &wrapped_offset_delta)
00052 {
00053 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(wrapped_offset_delta[Dim::X]));
00054 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(wrapped_offset_delta[Dim::Y]));
00055 m_wrapped_offset += wrapped_offset_delta;
00056 }
00057
00058 void Engine2::Entity::RemoveFromWorld ()
00059 {
00060 ASSERT1(IsInWorld());
00061 ASSERT1(OwnerObject()->GetWorld() != NULL);
00062 ASSERT1(GetObjectLayer() != NULL);
00063 OwnerObject()->GetWorld()->RemoveDynamicObject(OwnerObject());
00064 }
00065
00066 void Engine2::Entity::AddBackIntoWorld ()
00067 {
00068 ASSERT1(!IsInWorld());
00069 ASSERT1(OwnerObject()->GetWorld() != NULL);
00070 ASSERT1(GetObjectLayer() != NULL);
00071 OwnerObject()->GetWorld()->AddDynamicObject(OwnerObject(), GetObjectLayer());
00072 }
00073
00074 void Engine2::Entity::ReAddToQuadTree (QuadTreeType const quad_tree_type)
00075 {
00076 ASSERT1(m_owner_object != NULL);
00077 if (OwnerQuadTree(quad_tree_type) != NULL)
00078 {
00079 GetObjectLayer()->HandleContainmentOrWrapping(m_owner_object);
00080 OwnerQuadTree(quad_tree_type)->ReAddObject(m_owner_object);
00081 }
00082 }
00083
00084 void Engine2::Entity::ScheduleForDeletion (Float time_delay)
00085 {
00086 ASSERT1(IsInWorld());
00087
00088 if (time_delay < 0.0f)
00089 time_delay = 0.0f;
00090
00091 OwnerObject()->GetWorld()->EnqueueEvent(
00092 new EventEntity(
00093 this,
00094 OwnerObject()->GetWorld()->MostRecentFrameTime() + time_delay,
00095 Event::ENGINE2_DELETE_ENTITY));
00096 }
00097
00098 void Engine2::Entity::ScheduleForRemovalFromWorld (Float time_delay)
00099 {
00100 ASSERT1(IsInWorld());
00101
00102 if (time_delay < 0.0f)
00103 time_delay = 0.0f;
00104
00105 OwnerObject()->GetWorld()->EnqueueEvent(
00106 new EventEntity(
00107 this,
00108 OwnerObject()->GetWorld()->MostRecentFrameTime() + time_delay,
00109 Event::ENGINE2_REMOVE_ENTITY_FROM_WORLD));
00110 }
00111
00112 void Engine2::Entity::CloneProperties (Engine2::Entity const *const entity)
00113 {
00114 ASSERT1(entity != NULL);
00115 m_wrapped_offset = entity->m_wrapped_offset;
00116 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(m_wrapped_offset[Dim::X]));
00117 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(m_wrapped_offset[Dim::Y]));
00118 }
00119
00120 void Engine2::Entity::ReadClassSpecific (Serializer &serializer)
00121 {
00122 serializer.ReadFloatVector2(&m_wrapped_offset);
00123 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(m_wrapped_offset[Dim::X]));
00124 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(m_wrapped_offset[Dim::Y]));
00125 }
00126
00127 void Engine2::Entity::WriteClassSpecific (Serializer &serializer) const
00128 {
00129 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(m_wrapped_offset[Dim::X]));
00130 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(m_wrapped_offset[Dim::Y]));
00131 serializer.WriteFloatVector2(m_wrapped_offset);
00132 }
00133
00134 }