diff options
Diffstat (limited to 'src/core/entity.cc')
-rw-r--r-- | src/core/entity.cc | 106 |
1 files changed, 104 insertions, 2 deletions
diff --git a/src/core/entity.cc b/src/core/entity.cc index 7ff9d7b..a1db040 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -222,9 +222,14 @@ Entity::~Entity() if (entity_inventory) delete entity_inventory; - if (entity_zone) + if (entity_zone) { entity_zone->remove(this); + if (body() && entity_zone->physics()) { + entity_zone->physics()->removeRigidBody(body()); + } + } + if (entity_motionstate) delete entity_motionstate; @@ -278,9 +283,14 @@ void Entity::set_zone(Zone *zone) if (entity_zone == zone) return; - if (entity_zone) + if (entity_zone) { entity_zone->remove(this); + if (body() && entity_zone->physics()) { + entity_zone->physics()->removeRigidBody(body()); + } + } + // oldzone is cleared after every game frame if (!entity_oldzone) entity_oldzone = entity_zone; @@ -289,6 +299,10 @@ void Entity::set_zone(Zone *zone) if (entity_zone) { entity_zone->add(this); + if (body() && entity_zone->physics()) { + entity_zone->physics()->addRigidBody(body()); + reset(); + } } } @@ -432,6 +446,7 @@ void Entity::receive_server_create(std::istream &is) if (inventory()) { inventory()->receive_server_update(is); } + entity_dirty = false; } @@ -672,6 +687,8 @@ EntityControlable::EntityControlable() : EntityDynamic() target_afterburner = 0.0f; entity_owner = 0; + + entity_vehicle = 0; } EntityControlable::EntityControlable(std::istream & is) : @@ -688,6 +705,8 @@ EntityControlable::EntityControlable(std::istream & is) : target_afterburner = 0.0f; entity_owner = 0; + + entity_vehicle = 0; } @@ -866,6 +885,89 @@ void EntityControlable::set_afterburner(float afterburner) } } +void EntityControlable::set_zone(Zone *zone) +{ + if (entity_zone == zone) + return; + + if (entity_zone) { + entity_zone->remove(this); + if (vehicle() && entity_zone->physics()) { + entity_zone->physics()->removeAction(entity_vehicle); + entity_zone->physics()->removeRigidBody(body()); + delete entity_vehicle; + entity_vehicle = 0; + } + } + + // oldzone is cleared after every game frame + if (!entity_oldzone) + entity_oldzone = entity_zone; + + entity_zone = zone; + set_dirty(); + + if (entity_zone) { + entity_zone->add(this); + if (body() && entity_zone->physics()) { + entity_vehicle = new btRaycastVehicle(entity_vehicletuning, body(), entity_zone->raycaster()); + entity_zone->physics()->addRigidBody(body()); + entity_zone->physics()->addAction(entity_vehicle); + reset(); + } + } +} + +void EntityControlable::reset() +{ + // location and orientation + btTransform t; + t.setIdentity(); + t.setOrigin(to_btVector3(location())); + t.setBasis(to_btMatrix3x3(axis())); + + // construct physics body if necessary + if (!entity_body) { + // create collision shape + if (model()) { + entity_collision_shape = new btBoxShape(to_btVector3(model()->maxbbox())); + } else { + entity_collision_shape = new btSphereShape(radius()); + } + btVector3 inertia(0, 0, 0); + if (entity_mass) + entity_collision_shape->calculateLocalInertia(entity_mass, inertia); + + // create motion state + entity_motionstate = new btDefaultMotionState(t); + + // create physics body + entity_body_info = new btRigidBody::btRigidBodyConstructionInfo(entity_mass, entity_motionstate, entity_collision_shape, inertia); + entity_body = new btRigidBody(*entity_body_info); + entity_body->setActivationState(DISABLE_DEACTIVATION); + } + + entity_body->setWorldTransform(t); + if (zone()) { + if (!entity_vehicle) { + entity_vehicle = new btRaycastVehicle(entity_vehicletuning, body(), entity_zone->raycaster()); + entity_zone->physics()->addRigidBody(body()); + entity_zone->physics()->addAction(entity_vehicle); + } + + // transfer entity location to motion state + zone()->physics()->synchronizeSingleMotionState(entity_body); + } + + if ((entity_state == Docked) || (entity_state == Destroyed) || (!visible())){ + entity_body->activate(false); + } else { + entity_body->activate(true); + } + + set_dirty(); +} + /*----- EntityGlobe ------------------------------------------------ */ EntityGlobe::EntityGlobe() : Entity() |