1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02:00

Add object lifetime values, clean up spawned traffic

This commit is contained in:
Daniel Evans 2015-02-18 16:00:55 +00:00
parent 9407ee3135
commit fe4926e5d6
6 changed files with 67 additions and 3 deletions

View File

@ -58,7 +58,8 @@ public:
GameObject(GameWorld* engine, const glm::vec3& pos, const glm::quat& rot, ModelHandle* model)
: _lastPosition(pos), _lastRotation(rot), position(pos), rotation(rot),
model(model), engine(engine), animator(nullptr), skeleton(nullptr), mHealth(0.f),
_inWater(false), _lastHeight(std::numeric_limits<float>::max()), visible(true)
_inWater(false), _lastHeight(std::numeric_limits<float>::max()), visible(true),
lifetime(GameObject::UnknownLifetime)
{}
virtual ~GameObject();
@ -156,7 +157,24 @@ public:
t = t * glm::mat4_cast(glm::slerp(_lastRotation, getRotation(), alpha));
return t;
}
enum ObjectLifetime
{
/// lifetime has not been set
UnknownLifetime,
/// Generic background pedestrians
TrafficLifetime,
/// Part of a mission
MissionLifetime,
/// Is owned by the player (or is the player)
PlayerLifetime
};
void setLifetime(ObjectLifetime ol) { lifetime = ol; }
ObjectLifetime getLifetime() const { return lifetime; }
private:
ObjectLifetime lifetime;
};
#endif // __GAMEOBJECTS_HPP__

View File

@ -108,6 +108,7 @@ public:
bool placeItems(const std::string& name);
void createTraffic(const glm::vec3& near);
void cleanupTraffic(const glm::vec3& focus);
/**
* Creates an instance

View File

@ -88,6 +88,7 @@ std::vector<GameObject*> TrafficDirector::populateNearby(const glm::vec3& center
// spawn a cop
auto cop = world->createPedestrian(1, spawn->position + glm::vec3( 0.f, 0.f, 1.f ) );
cop->setLifetime(GameObject::TrafficLifetime);
created.push_back( cop );
}

View File

@ -296,10 +296,26 @@ void GameWorld::createTraffic(const glm::vec3& near)
{
TrafficDirector director(&aigraph, this);
director.populateNearby( near, 100, 5 );
}
void GameWorld::cleanupTraffic(const glm::vec3& focus)
{
for ( GameObject* object : objects )
{
if ( object->getLifetime() != GameObject::TrafficLifetime )
{
continue;
}
if ( glm::distance( focus, object->getPosition() ) >= 100.f )
{
destroyObjectQueued( object );
}
}
destroyQueuedObjects();
}
#include <strings.h>
uint16_t GameWorld::findModelDefinition(const std::string model)
{

View File

@ -208,6 +208,7 @@ void RWGame::tick(float dt)
{
// Use the current camera position to spawn pedestrians.
auto p = nextCam.position;
engine->cleanupTraffic(p);
engine->createTraffic(p);
}
}

27
tests/test_lifetime.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <boost/test/unit_test.hpp>
#include <glm/gtx/string_cast.hpp>
#include <objects/InstanceObject.hpp>
#include "test_globals.hpp"
BOOST_AUTO_TEST_SUITE(LifetimeTests)
BOOST_AUTO_TEST_CASE(test_cleanup)
{
GameObject* f = Global::get().e->createInstance(1337, glm::vec3(0.f, 0.f, 1000.f));
f->setLifetime(GameObject::TrafficLifetime);
{
auto search = Global::get().e->objects.find( f );
BOOST_CHECK( search != Global::get().e->objects.end() );
}
Global::get().e->cleanupTraffic(glm::vec3(0.f, 0.f, 0.f));
{
auto search = Global::get().e->objects.find( f );
BOOST_CHECK( search == Global::get().e->objects.end() );
}
}
BOOST_AUTO_TEST_SUITE_END()