1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 11:22:45 +01:00
openrw/rwgame/debugstate.cpp

190 lines
4.7 KiB
C++
Raw Normal View History

#include "debugstate.hpp"
#include "RWGame.hpp"
#include <ai/PlayerController.hpp>
2014-06-06 16:22:26 +02:00
#include <objects/CharacterObject.hpp>
2014-06-20 01:47:13 +02:00
#include <objects/VehicleObject.hpp>
DebugState::DebugState(RWGame* game, const glm::vec3& vp, const glm::quat& vd)
: State(game), _freeLook( false ), _sonicMode( false )
{
Menu *m = new Menu(game->getFont());
m->offset = glm::vec2(50.f, 100.f);
2014-06-01 19:26:53 +02:00
float entryHeight = 24.f;
#if 0
2014-06-01 19:26:53 +02:00
m->addEntry(Menu::lambda("Random Vehicle", [this] {
2014-06-01 14:17:35 +02:00
auto it = getWorld()->vehicleTypes.begin();
std::uniform_int_distribution<int> uniform(0, 3);
for(size_t i = 0, n = uniform(getWorld()->randomEngine); i != n; i++) {
it++;
}
2014-06-01 14:17:35 +02:00
spawnVehicle(it->first);
2014-06-01 19:26:53 +02:00
}, entryHeight));
m->addEntry(Menu::lambda("Open All Doors/Flaps", [=] {
auto pc = getWorld()->state.player->getCharacter();
2014-06-20 01:47:13 +02:00
auto pv = pc->getCurrentVehicle();
if( pv ) {
for(auto& it : pv->_hingedObjects) {
pv->setHingeLocked(it.first, false);
}
}
}, entryHeight));
m->addEntry(Menu::lambda("Spawn Pedestrians", [=] {
glm::vec3 hit, normal;
if(game->hitWorldRay(hit, normal)) {
glm::vec3 spawnPos = hit + glm::vec3(-5, 0.f, 0.0) + normal;
size_t k = 1;
// Spawn every pedestrian.
for(auto it = getWorld()->pedestrianTypes.begin();
it != getWorld()->pedestrianTypes.end(); ++it) {
getWorld()->createPedestrian(it->first, spawnPos);
spawnPos += glm::vec3(2.5, 0, 0);
if((k++ % 6) == 0) { spawnPos += glm::vec3(-15, -2.5, 0); }
}
}
}, entryHeight));
2014-06-01 19:26:53 +02:00
int vehiclesMax = 16, i = 0;
2014-06-01 14:17:35 +02:00
for( auto& v : getWorld()->vehicleTypes ) {
if( (i++) > vehiclesMax ) break;
m->addEntry(Menu::lambda(v.second->handlingID, [=] {
spawnVehicle(v.first);
2014-06-01 19:26:53 +02:00
}, entryHeight));
2014-06-01 14:17:35 +02:00
}
#endif
this->enterMenu(m);
_debugCam.position = vp;
_debugCam.rotation = vd;
}
void DebugState::enter()
{
}
void DebugState::exit()
{
}
void DebugState::tick(float dt)
{
/*if(debugObject) {
auto p = debugObject->getPosition();
ss << "Position: " << p.x << " " << p.y << " " << p.z << std::endl;
ss << "Health: " << debugObject->mHealth << std::endl;
if(debugObject->model) {
auto m = debugObject->model;
ss << "Textures: " << std::endl;
for(auto it = m->geometries.begin(); it != m->geometries.end();
++it )
{
auto g = *it;
for(auto itt = g->materials.begin(); itt != g->materials.end();
++itt)
{
for(auto tit = itt->textures.begin(); tit != itt->textures.end();
++tit)
{
ss << " " << tit->name << std::endl;
}
}
}
}
if(debugObject->type() == GameObject::Vehicle) {
GTAVehicle* vehicle = static_cast<GTAVehicle*>(debugObject);
ss << "ID: " << vehicle->info->handling.ID << std::endl;
}
}*/
2014-06-08 23:18:03 +02:00
if( _freeLook ) {
float qpi = glm::half_pi<float>();
sf::Vector2i screenCenter{sf::Vector2i{getWindow().getSize()} / 2};
sf::Vector2i mousePos = sf::Mouse::getPosition(getWindow());
sf::Vector2i deltaMouse = mousePos - screenCenter;
sf::Mouse::setPosition(screenCenter, getWindow());
_debugLook.x -= deltaMouse.x / 100.0f;
_debugLook.y += deltaMouse.y / 100.0f;
2014-06-08 23:18:03 +02:00
if (_debugLook.y > qpi)
_debugLook.y = qpi;
else if (_debugLook.y < -qpi)
_debugLook.y = -qpi;
2014-06-08 23:18:03 +02:00
_debugCam.rotation = glm::angleAxis(_debugLook.x, glm::vec3(0.f, 0.f, 1.f))
* glm::angleAxis(_debugLook.y, glm::vec3(0.f, 1.f, 0.f));
2014-06-08 23:18:03 +02:00
_debugCam.position += _debugCam.rotation * _movement * dt * (_sonicMode ? 100.f : 10.f);
2014-06-08 23:18:03 +02:00
}
}
void DebugState::handleEvent(const sf::Event &e)
{
switch(e.type) {
2014-06-08 23:18:03 +02:00
case sf::Event::KeyPressed:
switch(e.key.code) {
default: break;
case sf::Keyboard::Escape:
StateManager::get().exit();
break;
case sf::Keyboard::W:
_movement.x = 1.f;
2014-06-08 23:18:03 +02:00
break;
case sf::Keyboard::S:
_movement.x =-1.f;
2014-06-08 23:18:03 +02:00
break;
case sf::Keyboard::A:
_movement.y = 1.f;
2014-06-08 23:18:03 +02:00
break;
case sf::Keyboard::D:
_movement.y =-1.f;
2014-06-08 23:18:03 +02:00
break;
case sf::Keyboard::F:
_freeLook = !_freeLook;
break;
case sf::Keyboard::LShift:
_sonicMode = true;
break;
}
break;
case sf::Event::KeyReleased:
switch(e.key.code) {
case sf::Keyboard::W:
case sf::Keyboard::S:
_movement.x = 0.f;
2014-06-08 23:18:03 +02:00
break;
case sf::Keyboard::A:
case sf::Keyboard::D:
_movement.y = 0.f;
2014-06-08 23:18:03 +02:00
break;
case sf::Keyboard::LShift:
_sonicMode = false;
break;
default: break;
2014-06-08 23:18:03 +02:00
}
default: break;
}
State::handleEvent(e);
}
2014-06-01 14:17:35 +02:00
void DebugState::spawnVehicle(unsigned int id)
{
auto ch = getWorld()->state.player->getCharacter();
2014-06-01 14:17:35 +02:00
if(! ch) return;
2014-06-10 18:08:29 +02:00
glm::vec3 fwd = ch->rotation * glm::vec3(0.f, 1.f, 0.f);
2014-06-01 14:17:35 +02:00
glm::vec3 hit, normal;
if(game->hitWorldRay(ch->position + (fwd * 5.f), {0.f, 0.f, -2.f}, hit, normal)) {
2014-06-01 14:17:35 +02:00
auto spawnpos = hit + normal;
getWorld()->createVehicle(id, spawnpos, glm::quat());
}
}
const ViewCamera &DebugState::getCamera()
{
return _debugCam;
}