1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 10:22:52 +01:00

Player vehicle enter and exit

This commit is contained in:
Daniel Evans 2013-12-13 18:19:03 +00:00
parent ceae78d893
commit 8b22761ad2
4 changed files with 78 additions and 4 deletions

View File

@ -24,6 +24,10 @@ public:
void updateMovementDirection(const glm::vec3& pos);
void exitVehicle();
void enterNearestVehicle();
virtual void update(float dt);
virtual glm::vec3 getTargetPosition();

View File

@ -1,6 +1,8 @@
#include "renderwure/ai/GTAPlayerAIController.hpp"
#include <renderwure/objects/GTACharacter.hpp>
#include <renderwure/objects/GTAVehicle.hpp>
#include <renderwure/engine/GTAEngine.hpp>
#include <glm/gtc/matrix_transform.hpp>
GTAPlayerAIController::GTAPlayerAIController(GTACharacter* character)
: GTAAIController(character), lastRotation(glm::vec3(0.f, 0.f, 0.f)), running(false)
@ -23,6 +25,41 @@ void GTAPlayerAIController::updateMovementDirection(const glm::vec3& dir)
direction = dir;
}
void GTAPlayerAIController::exitVehicle()
{
if(character->getCurrentVehicle()) {
// Determine the seat location and teleport to outside the vehicle.
auto vehicle = character->getCurrentVehicle();
auto seatPos = vehicle->info.seats[character->getCurrentSeat()].offset;
seatPos.y += vehicle->info.handling.seatOffset;
glm::mat4 vehicleMatrix;
vehicleMatrix = glm::translate(vehicleMatrix, vehicle->getPosition());
vehicleMatrix = vehicleMatrix * glm::mat4_cast(vehicle->getRotation());
glm::vec3 worldp(vehicleMatrix * glm::vec4(seatPos.x, seatPos.y, seatPos.z, 1.f));
character->enterVehicle(nullptr, 0);
character->setPosition(worldp);
}
}
void GTAPlayerAIController::enterNearestVehicle()
{
if(! character->getCurrentVehicle()) {
auto world = character->engine;
GTAVehicle* nearest = nullptr; float d = 10.f;
for(auto it = world->vehicleInstances.begin(); it != world->vehicleInstances.end(); ++it) {
float vd = glm::length( character->getPosition() - (*it)->getPosition());
if( vd < d ) {
d = vd;
nearest = *it;
}
}
if( nearest ) {
character->enterVehicle(nearest, 0);
}
}
}
void GTAPlayerAIController::update(float dt)
{
if( glm::length(direction) > 0.001f ) {

View File

@ -200,8 +200,9 @@ bool GTACharacter::enterVehicle(GTAVehicle* vehicle, size_t seat)
}
else {
if(currentVehicle) {
vehicle->setOccupant(seat, nullptr);
setPosition(currentVehicle->getPosition());
currentVehicle->setOccupant(seat, nullptr);
// Disabled due to crashing.
//setPosition(currentVehicle->getPosition());
setCurrentVehicle(nullptr, 0);
return true;
}

View File

@ -95,8 +95,10 @@ std::map<std::string, std::function<void (std::string)>> Commands = {
[&](std::string) {
glm::vec3 hit, normal;
if(hitWorldRay(hit, normal)) {
playerCharacter = gta->createPedestrian(1, plyPos+glm::vec3(0.f,10.f,0.f));
player = new GTAPlayerAIController(playerCharacter);
if(! playerCharacter) {
playerCharacter = gta->createPedestrian(1, plyPos+glm::vec3(0.f,10.f,0.f));
player = new GTAPlayerAIController(playerCharacter);
}
// Pick random vehicle.
auto it = gta->vehicleTypes.begin();
@ -111,6 +113,22 @@ std::map<std::string, std::function<void (std::string)>> Commands = {
}
}
},
{"empty-vehicle",
[&](std::string) {
glm::vec3 hit, normal;
if(hitWorldRay(hit, normal)) {
// Pick random vehicle.
auto it = gta->vehicleTypes.begin();
std::uniform_int_distribution<int> uniform(0, 9);
for(size_t i = 0, n = uniform(gta->randomEngine); i != n; i++) {
it++;
}
auto spawnpos = hit + normal;
auto vehicle = gta->createVehicle(it->first, spawnpos, glm::quat(glm::vec3(0.f, 0.f, -plyLook.x * PiOver180)));
}
}
},
{"player",
[&](std::string) {
playerCharacter = gta->createPedestrian(1, plyPos);
@ -311,6 +329,16 @@ void handleInputEvent(sf::Event &event)
case sf::Keyboard::D:
movement.x = 0;
break;
case sf::Keyboard::F:
if(playerCharacter) {
if(playerCharacter->getCurrentVehicle()) {
player->exitVehicle();
}
else {
player->enterNearestVehicle();
}
}
break;
default: break;
}
break;
@ -332,6 +360,9 @@ void handleCommandEvent(sf::Event &event)
case sf::Keyboard::F3:
command("player-vehicle");
break;
case sf::Keyboard::F4:
command("empty-vehicle");
break;
case sf::Keyboard::F6:
command("vehicle-test");
break;
@ -527,6 +558,7 @@ void render()
ss << "F1 - Toggle Help" << std::endl;
ss << "F2 - Create Vehicle (with driver)" << std::endl;
ss << "F3 - Create Vehicle (with player)" << std::endl;
ss << "F4 - Create Vehicle (empty)" << std::endl;
ss << "F6 - Create all Vehicles" << std::endl;
ss << "F7 - Create all Pedestrians" << std::endl;
ss << "F9 - Display Object Information" << std::endl;