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

364 lines
8.7 KiB
C++
Raw Normal View History

2014-05-26 06:34:49 +02:00
#include "ingamestate.hpp"
#include "RWGame.hpp"
2014-05-26 06:34:49 +02:00
#include "pausestate.hpp"
#include "debugstate.hpp"
#include "DrawUI.hpp"
#include <ai/PlayerController.hpp>
2014-06-06 16:22:26 +02:00
#include <objects/CharacterObject.hpp>
#include <objects/VehicleObject.hpp>
#include <objects/ItemPickup.hpp>
2014-05-31 21:19:15 +02:00
#include <render/Model.hpp>
#include <items/WeaponItem.hpp>
2015-02-04 18:16:46 +01:00
#include <engine/GameWorld.hpp>
#include <script/ScriptMachine.hpp>
2014-12-15 00:21:30 +01:00
#define AUTOLOOK_TIME 2.f
2014-05-26 06:34:49 +02:00
/**
* This should be kept in rwengine/physics
*/
class ClosestNotMeRayResultCallback : public btCollisionWorld::ClosestRayResultCallback
{
btCollisionObject* _self;
public:
ClosestNotMeRayResultCallback( btCollisionObject* self, const btVector3& from, const btVector3& to )
: ClosestRayResultCallback( from, to ), _self( self ) {}
virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace)
{
if( rayResult.m_collisionObject == _self ) {
return 1.0;
}
return ClosestRayResultCallback::addSingleResult( rayResult, normalInWorldSpace );
}
};
IngameState::IngameState(RWGame* game, bool test)
: State(game), started(false), test(test), autolookTimer(0.f)
{
}
void IngameState::startTest()
2014-05-26 06:34:49 +02:00
{
auto playerChar = getWorld()->createPedestrian(1, {270.f, -605.f, 40.f});
auto player = new PlayerController(playerChar);
getWorld()->state.player = player;
2014-06-07 05:11:20 +02:00
/*auto bat = new WeaponItem(getWorld()->gameData.weaponData["ak47"]);
_playerCharacter->addToInventory(bat);
_playerCharacter->setActiveItem(bat->getInventorySlot());*/
glm::vec3 itemspawn( 276.5f, -609.f, 36.5f);
2014-07-09 23:28:25 +02:00
for( auto& w : getWorld()->gameData.weaponData ) {
if( w.first == "unarmed" ) continue;
getWorld()->objects.insert(new ItemPickup(getWorld(), itemspawn,
w.second));
itemspawn.x += 2.5f;
}
auto carPos = glm::vec3( 286.f, -591.f, 37.f );
auto carRot = glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 0.f, 1.f));
//auto boatPos = glm::vec3( -1000.f, -1040.f, 5.f );
int i = 0;
for( auto& vi : getWorld()->objectTypes ) {
switch( vi.first ) {
case 140: continue;
case 141: continue;
}
if( vi.second->class_type == ObjectInformation::_class("CARS") )
{
if ( i++ > 20 ) break;
auto vehicle = std::static_pointer_cast<VehicleData>(vi.second);
auto& sp = carPos;
auto& sr = carRot;
auto v = getWorld()->createVehicle(vi.first, sp, sr);
sp += sr * glm::vec3( 2.f + v->info->handling.dimensions.x, 0.f, 0.f);
}
2014-06-07 05:11:20 +02:00
}
2014-05-26 06:34:49 +02:00
}
void IngameState::startGame()
2014-05-26 06:34:49 +02:00
{
game->startScript("data/main.scm");
getWorld()->sound.playBackground( getWorld()->gameData.getDataPath() + "/audio/City.wav" );
2014-05-26 06:34:49 +02:00
}
PlayerController *IngameState::getPlayer()
{
return getWorld()->state.player;
2014-05-26 06:34:49 +02:00
}
void IngameState::enter()
{
if( ! started )
{
if( test ) {
startTest();
}
else {
startGame();
}
started = true;
}
}
void IngameState::exit()
{
}
void IngameState::tick(float dt)
{
2014-12-15 00:21:30 +01:00
autolookTimer = std::max(autolookTimer - dt, 0.f);
auto player = getPlayer();
2015-01-27 16:25:45 +01:00
if( player && player->isInputEnabled() )
{
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());
2014-12-15 00:21:30 +01:00
if(deltaMouse.x != 0 || deltaMouse.y != 0)
{
autolookTimer = AUTOLOOK_TIME;
}
_lookAngles.x += deltaMouse.x / 100.0;
_lookAngles.y += deltaMouse.y / 100.0;
2015-04-01 06:05:21 +02:00
while(_lookAngles.x > glm::pi<float>())
{
_lookAngles.x -= 2.f * glm::pi<float>();
}
while(_lookAngles.x < -glm::pi<float>())
{
_lookAngles.x += 2.f * glm::pi<float>();
}
if (_lookAngles.y > qpi)
_lookAngles.y = qpi;
else if (_lookAngles.y < -qpi)
_lookAngles.y = -qpi;
auto angle = glm::angleAxis(-_lookAngles.x, glm::vec3(0.f, 0.f, 1.f));
angle *= glm::angleAxis(_lookAngles.y, glm::vec3(0.f, 1.f, 0.f));
player->updateMovementDirection(angle * _movement, _movement);
2014-12-17 01:39:52 +01:00
auto target = getWorld()->state.cameraTarget;
if( target == nullptr )
{
target = player->getCharacter();
}
2014-12-17 01:39:52 +01:00
auto position = target->getPosition();
2014-12-15 00:21:30 +01:00
float viewDistance = 4.f;
btCollisionObject* physTarget = player->getCharacter()->physObject;
2014-12-17 01:39:52 +01:00
auto vehicle = ( target->type() == GameObject::Character ) ? static_cast<CharacterObject*>(target)->getCurrentVehicle() : nullptr;
if( vehicle ) {
auto model = vehicle->model;
for(auto& g : model->resource->geometries) {
viewDistance = std::max(
2014-12-15 00:21:30 +01:00
(glm::length(g->geometryBounds.center) + g->geometryBounds.radius) * 4.0f,
viewDistance);
}
2014-12-15 00:21:30 +01:00
position = vehicle->getPosition();
position.z += (vehicle->info->handling.dimensions.z / 2.f) * 2.5f;
physTarget = vehicle->physBody;
2014-12-15 00:21:30 +01:00
float speed = vehicle->physVehicle->getCurrentSpeedKmHour();
if( autolookTimer <= 0.f && std::abs(speed) > 1.f )
{
2015-04-01 06:05:21 +02:00
float b = glm::roll(vehicle->getRotation()) + glm::half_pi<float>();
while( b > glm::pi<float>() )
{
b -= 2.f * glm::pi<float>();
}
while( b < -glm::pi<float>() )
{
b += 2.f * glm::pi<float>();
}
if( speed < 0.f )
{
if( _lookAngles.x < 0.f )
{
b -= glm::pi<float>();
}
else
{
b += glm::pi<float>();
}
}
float aD = b - _lookAngles.x;
const float rotateSpeed = 1.f;
if( std::abs(aD) <= rotateSpeed * dt )
{
_lookAngles.x = b;
}
else
{
_lookAngles.x += glm::sign(aD) * rotateSpeed * dt;
}
angle = glm::angleAxis(_lookAngles.x, glm::vec3(0.f, 0.f, 1.f));
2014-12-15 00:21:30 +01:00
}
}
auto rayEnd = position + angle * glm::vec3(-viewDistance, 0.f, 1.f);
auto rayStart = position + glm::vec3(0.f, 0.f, 1.f);
auto to = btVector3(rayEnd.x, rayEnd.y, rayEnd.z);
auto from = btVector3(rayStart.x, rayStart.y, rayStart.z);
ClosestNotMeRayResultCallback ray(physTarget, from, to);
getWorld()->dynamicsWorld->rayTest(from, to, ray);
if( ray.hasHit() && ray.m_closestHitFraction < 1.f )
{
position = glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
ray.m_hitPointWorld.z());
position += glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
ray.m_hitNormalWorld.z()) * 0.1f;
}
else
{
position = rayEnd;
}
// Move back from the character
// Tilt the final look angle down a tad.
2014-12-15 00:21:30 +01:00
angle *= glm::angleAxis(glm::radians(10.f), glm::vec3(0.f, 1.f, 0.f));
_look.position = position;
_look.rotation = angle;
}
}
2015-02-07 23:55:06 +01:00
void IngameState::draw(GameRenderer* r)
{
if( !getWorld()->state.isCinematic && getWorld()->isCutsceneDone() )
{
drawHUD(getPlayer(), getWorld(), r);
}
2015-02-07 23:55:06 +01:00
State::draw(r);
}
2014-05-26 06:34:49 +02:00
void IngameState::handleEvent(const sf::Event &event)
{
auto player = getPlayer();
2014-05-26 06:34:49 +02:00
switch(event.type) {
case sf::Event::KeyPressed:
switch(event.key.code) {
case sf::Keyboard::Escape:
StateManager::get().enter(new PauseState(game));
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::M:
StateManager::get().enter(new DebugState(game, _look.position, _look.rotation));
break;
2014-05-26 06:34:49 +02:00
case sf::Keyboard::Space:
2014-12-17 01:39:52 +01:00
if( getWorld()->state.currentCutscene )
{
getWorld()->state.skipCutscene = true;
}
2015-01-27 16:25:45 +01:00
else if( player && player->isInputEnabled() ) {
2014-12-16 20:17:22 +01:00
if( player->getCharacter()->getCurrentVehicle() ) {
player->getCharacter()->getCurrentVehicle()->setHandbraking(true);
}
2015-01-27 16:25:45 +01:00
else
2014-12-16 20:17:22 +01:00
{
player->jump();
}
2014-05-26 06:34:49 +02:00
}
break;
case sf::Keyboard::W:
_movement.x = 1.f;
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::S:
_movement.x =-1.f;
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::A:
_movement.y = 1.f;
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::D:
_movement.y =-1.f;
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::LShift:
player->setRunning(true);
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::F:
2015-01-27 16:25:45 +01:00
if( player && player->isInputEnabled() ) {
if( player->getCharacter()->getCurrentVehicle()) {
player->exitVehicle();
2014-05-26 06:34:49 +02:00
}
else {
player->enterNearestVehicle();
2014-05-26 06:34:49 +02:00
}
}
break;
default: break;
}
break;
case sf::Event::KeyReleased:
switch(event.key.code) {
case sf::Keyboard::W:
case sf::Keyboard::S:
_movement.x = 0.f;
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::A:
case sf::Keyboard::D:
_movement.y = 0.f;
2014-05-26 06:34:49 +02:00
break;
case sf::Keyboard::LShift:
player->setRunning(false);
2014-05-26 06:34:49 +02:00
break;
default: break;
}
break;
case sf::Event::MouseButtonPressed:
switch(event.mouseButton.button) {
case sf::Mouse::Left:
player->useItem(true, true);
break;
default: break;
}
break;
case sf::Event::MouseButtonReleased:
switch(event.mouseButton.button) {
case sf::Mouse::Left:
player->useItem(false, true);
break;
default: break;
}
break;
case sf::Event::MouseWheelMoved:
player->getCharacter()->cycleInventory(event.mouseWheel.delta > 0);
break;
2014-05-26 06:34:49 +02:00
default: break;
}
State::handleEvent(event);
}
bool IngameState::shouldWorldUpdate()
{
return true;
}
const ViewCamera &IngameState::getCamera()
{
return _look;
}
2015-01-25 19:42:29 +01:00