mirror of
https://github.com/rwengine/openrw.git
synced 2024-09-18 16:32:32 +02:00
Add object lifetime values, clean up spawned traffic
This commit is contained in:
parent
9407ee3135
commit
fe4926e5d6
@ -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();
|
||||
@ -157,6 +158,23 @@ public:
|
||||
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__
|
||||
|
@ -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
|
||||
|
@ -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 );
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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
27
tests/test_lifetime.cpp
Normal 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()
|
Loading…
Reference in New Issue
Block a user