2016-07-02 20:49:31 +02:00
|
|
|
#include "IngameState.hpp"
|
|
|
|
#include "DebugState.hpp"
|
2015-02-15 13:41:51 +01:00
|
|
|
#include "DrawUI.hpp"
|
2016-09-09 22:13:20 +02:00
|
|
|
#include "PauseState.hpp"
|
|
|
|
#include "RWGame.hpp"
|
2014-08-12 22:15:26 +02:00
|
|
|
|
|
|
|
#include <ai/PlayerController.hpp>
|
2017-01-03 15:18:06 +01:00
|
|
|
#include <data/Clump.hpp>
|
2018-05-18 10:49:02 +02:00
|
|
|
#include <data/CutsceneData.hpp>
|
2016-10-19 03:22:33 +02:00
|
|
|
#include <data/WeaponData.hpp>
|
2016-09-09 22:13:20 +02:00
|
|
|
#include <dynamics/CollisionInstance.hpp>
|
|
|
|
#include <dynamics/RaycastCallbacks.hpp>
|
2015-04-27 16:31:39 +02:00
|
|
|
#include <engine/GameState.hpp>
|
2016-09-09 22:13:20 +02:00
|
|
|
#include <engine/GameWorld.hpp>
|
|
|
|
#include <objects/CharacterObject.hpp>
|
2018-05-18 10:49:02 +02:00
|
|
|
#include <objects/PickupObject.hpp>
|
2016-09-09 22:13:20 +02:00
|
|
|
#include <objects/VehicleObject.hpp>
|
2015-04-04 04:12:28 +02:00
|
|
|
#include <script/ScriptMachine.hpp>
|
2014-12-15 00:21:30 +01:00
|
|
|
|
2016-08-16 18:53:35 +02:00
|
|
|
#include <glm/gtc/constants.hpp>
|
2017-09-12 02:02:05 +02:00
|
|
|
#include <glm/gtx/matrix_major_storage.hpp>
|
2018-05-18 10:49:02 +02:00
|
|
|
#include <glm/gtx/norm.hpp>
|
2016-06-29 00:33:36 +02:00
|
|
|
|
2017-11-01 03:13:07 +01:00
|
|
|
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
|
|
|
|
|
2016-05-21 20:06:37 +02:00
|
|
|
constexpr float kAutoLookTime = 2.f;
|
|
|
|
constexpr float kAutolookMinVelocity = 0.2f;
|
2018-02-18 01:47:44 +01:00
|
|
|
constexpr float kInGameFOV = glm::half_pi<float>();
|
|
|
|
constexpr float kMaxRotationRate = glm::quarter_pi<float>();
|
|
|
|
constexpr float kCameraPitchLimit = glm::quarter_pi<float>() * 0.5f;
|
|
|
|
constexpr float kVehicleCameraPitch =
|
2016-09-09 22:13:20 +02:00
|
|
|
glm::half_pi<float>() - glm::quarter_pi<float>() * 0.25f;
|
2014-05-26 06:34:49 +02:00
|
|
|
|
2015-07-03 03:52:43 +02:00
|
|
|
IngameState::IngameState(RWGame* game, bool newgame, const std::string& save)
|
2016-09-09 22:13:20 +02:00
|
|
|
: State(game)
|
|
|
|
, save(save)
|
|
|
|
, newgame(newgame)
|
2018-05-19 23:55:27 +02:00
|
|
|
, m_invertedY(game->getConfig().getInputInvertY()) {
|
2014-07-24 00:57:21 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::startTest() {
|
|
|
|
auto playerChar = getWorld()->createPlayer({270.f, -605.f, 40.f});
|
|
|
|
|
|
|
|
getWorld()->state->playerObject = playerChar->getGameObjectID();
|
|
|
|
|
|
|
|
glm::vec3 itemspawn(276.5f, -609.f, 36.5f);
|
2016-11-30 00:28:53 +01:00
|
|
|
for (unsigned int i = 1; i < getWorld()->data->weaponData.size(); ++i) {
|
2016-10-19 03:22:33 +02:00
|
|
|
auto& item = getWorld()->data->weaponData[i];
|
|
|
|
getWorld()->createPickup(itemspawn, item->modelID,
|
2016-09-09 22:13:20 +02:00
|
|
|
PickupObject::OnStreet);
|
|
|
|
itemspawn.x += 2.5f;
|
|
|
|
}
|
|
|
|
|
2018-05-18 10:49:02 +02:00
|
|
|
PickupObject* moneyObj =
|
|
|
|
getWorld()->createPickup(glm::vec3(276.5f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("Money"),
|
|
|
|
PickupObject::PickupType::Money);
|
|
|
|
static_cast<MoneyPickup*>(moneyObj)->setMoney(100000);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(279.f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("package1"),
|
|
|
|
PickupObject::PickupType::Collectable);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(281.5f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("health"),
|
|
|
|
PickupObject::PickupType::OnStreetSlow);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(284.f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("bodyarmour"),
|
|
|
|
PickupObject::PickupType::OnStreetSlow);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(286.5f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("adrenaline"),
|
|
|
|
PickupObject::PickupType::OnStreetSlow);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(289.f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("killfrenzy"),
|
|
|
|
PickupObject::PickupType::Once);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(291.5f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("info"),
|
|
|
|
PickupObject::PickupType::Once);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(294.f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("briefcase"),
|
|
|
|
PickupObject::PickupType::Once);
|
|
|
|
|
|
|
|
getWorld()->createPickup(glm::vec3(296.5f, -604.f, 36.5f),
|
|
|
|
getWorld()->data->findModelObject("floatpackge1"),
|
|
|
|
PickupObject::PickupType::FloatingPackage);
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
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));
|
2016-10-05 23:33:58 +02:00
|
|
|
// Landstalker, Stinger, Linerunner, Trash, Bobcat
|
|
|
|
const std::vector<int> kTestVehicles = {90, 92, 93, 98, 111};
|
|
|
|
for (auto id : kTestVehicles) {
|
|
|
|
getWorld()->createVehicle(id, carPos, carRot);
|
|
|
|
carPos += carRot * glm::vec3(5.f, 0.f, 0.f);
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::startGame() {
|
|
|
|
game->startScript("data/main.scm");
|
2016-10-21 00:10:52 +02:00
|
|
|
game->getScriptVM()->startThread(0);
|
2018-06-21 17:43:38 +02:00
|
|
|
auto audioCityPath = getWorld()->data->index.findFilePath("audio/City.wav");
|
|
|
|
getWorld()->sound.playBackground(audioCityPath.string()); //FIXME: playBackground should just accept "audio/City.wav"
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::enter() {
|
|
|
|
if (!started) {
|
|
|
|
if (newgame) {
|
|
|
|
if (save.empty()) {
|
2015-07-03 03:52:43 +02:00
|
|
|
startGame();
|
2016-09-09 22:13:20 +02:00
|
|
|
} else if (save == "test") {
|
|
|
|
startTest();
|
|
|
|
} else {
|
|
|
|
game->loadGame(save);
|
2015-07-03 03:52:43 +02:00
|
|
|
}
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
|
|
|
started = true;
|
|
|
|
}
|
2014-07-24 00:57:21 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
getWindow().hideCursor();
|
|
|
|
}
|
2014-07-24 00:57:21 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::exit() {
|
2014-07-24 00:57:21 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::tick(float dt) {
|
|
|
|
auto world = getWorld();
|
|
|
|
autolookTimer = std::max(autolookTimer - dt, 0.f);
|
|
|
|
|
|
|
|
// Update displayed money value
|
|
|
|
// @todo the game uses another algorithm which is non-linear
|
|
|
|
{
|
|
|
|
float moneyFrequency = 1.0f / 30.0f;
|
|
|
|
moneyTimer += dt;
|
|
|
|
while (moneyTimer >= moneyFrequency) {
|
|
|
|
int32_t difference = world->state->playerInfo.money -
|
|
|
|
world->state->playerInfo.displayedMoney;
|
|
|
|
|
|
|
|
// Generates 0, 1 (difference < 100), 12 (difference < 1000), 123
|
|
|
|
// (difference < 10000), .. etc.
|
|
|
|
// Negative input will result in negative output
|
|
|
|
auto GetIncrement = [](int32_t difference) -> int32_t {
|
|
|
|
// @todo is this correct for difference >= 1000000000 ?
|
|
|
|
int32_t r = 1;
|
|
|
|
int32_t i = 2;
|
|
|
|
if (difference == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (std::abs(difference) >= 100) {
|
|
|
|
difference /= 10;
|
|
|
|
r = r * 10 + i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (difference < 0) ? -r : r;
|
|
|
|
};
|
|
|
|
world->state->playerInfo.displayedMoney += GetIncrement(difference);
|
|
|
|
|
|
|
|
moneyTimer -= moneyFrequency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 18:13:32 +02:00
|
|
|
auto player = game->getWorld()->getPlayer();
|
2016-11-10 23:00:02 +01:00
|
|
|
|
2016-11-16 21:23:05 +01:00
|
|
|
if (player) {
|
2016-11-24 22:29:25 +01:00
|
|
|
// Force all input to 0 if player input is disabled
|
|
|
|
/// @todo verify 0ing input is the correct behaviour
|
|
|
|
const auto inputEnabled = player->isInputEnabled();
|
|
|
|
|
|
|
|
auto input = [&](GameInputState::Control c) {
|
|
|
|
return inputEnabled ? world->state->input[0][c] : 0.f;
|
|
|
|
};
|
|
|
|
auto pressed = [&](GameInputState::Control c) {
|
|
|
|
return inputEnabled && world->state->input[0].pressed(c) &&
|
|
|
|
!world->state->input[1].pressed(c);
|
|
|
|
};
|
|
|
|
auto held = [&](GameInputState::Control c) {
|
|
|
|
return inputEnabled && world->state->input[0].pressed(c);
|
|
|
|
};
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
auto target = world->pedestrianPool.find(world->state->cameraTarget);
|
|
|
|
|
|
|
|
if (target == nullptr) {
|
|
|
|
target = player->getCharacter();
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::vec3 targetPosition = target->getPosition();
|
|
|
|
glm::vec3 lookTargetPosition = targetPosition;
|
|
|
|
targetPosition += glm::vec3(0.f, 0.f, 1.f);
|
|
|
|
lookTargetPosition += glm::vec3(0.f, 0.f, 0.5f);
|
|
|
|
|
2016-12-06 23:40:42 +01:00
|
|
|
auto look = player->getCharacter()->getLook();
|
|
|
|
look += cameradelta_;
|
|
|
|
cameradelta_ = glm::vec2();
|
|
|
|
look.y = glm::clamp(look.y, kCameraPitchLimit,
|
|
|
|
glm::pi<float>() - kCameraPitchLimit);
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
auto vehicle =
|
|
|
|
(target->type() == GameObject::Character)
|
|
|
|
? static_cast<CharacterObject*>(target)->getCurrentVehicle()
|
|
|
|
: nullptr;
|
|
|
|
if (vehicle) {
|
|
|
|
float velocity = vehicle->getVelocity();
|
2016-12-06 23:40:42 +01:00
|
|
|
|
|
|
|
if (glm::abs(velocity) > kAutolookMinVelocity &&
|
|
|
|
autolookTimer <= 0.f) {
|
|
|
|
glm::vec2 ideal(glm::roll(vehicle->getRotation()),
|
|
|
|
kVehicleCameraPitch);
|
2016-09-09 22:13:20 +02:00
|
|
|
if (velocity < 0.f) {
|
2016-12-06 23:40:42 +01:00
|
|
|
ideal.x = glm::mod(ideal.x - glm::pi<float>(),
|
|
|
|
glm::pi<float>() * 2.f);
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2016-12-06 23:40:42 +01:00
|
|
|
glm::vec2 current(glm::mod(look.x, glm::pi<float>() * 2),
|
|
|
|
look.y);
|
|
|
|
auto delta = ideal - current;
|
|
|
|
if (glm::abs(delta.x) > glm::pi<float>()) {
|
|
|
|
delta.x -= glm::sign(delta.x) * glm::pi<float>() * 2.f;
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2016-12-06 23:40:42 +01:00
|
|
|
look += glm::clamp(delta, -kMaxRotationRate * dt,
|
|
|
|
kMaxRotationRate * dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_vehicleFreeLook) {
|
|
|
|
look.y = kVehicleCameraPitch;
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 20:47:31 +01:00
|
|
|
glm::vec3 movement{};
|
2016-11-10 23:00:02 +01:00
|
|
|
movement.x = input(GameInputState::GoForward) -
|
2016-12-06 23:40:42 +01:00
|
|
|
input(GameInputState::GoBackwards),
|
2016-09-09 22:13:20 +02:00
|
|
|
movement.y =
|
2016-11-10 23:00:02 +01:00
|
|
|
input(GameInputState::GoLeft) - input(GameInputState::GoRight);
|
2016-09-09 22:13:20 +02:00
|
|
|
/// @todo replace with correct sprint behaviour
|
2016-11-10 23:00:02 +01:00
|
|
|
float speed = held(GameInputState::Sprint) ? 2.f : 1.f;
|
2016-09-09 22:13:20 +02:00
|
|
|
|
2016-11-16 21:23:05 +01:00
|
|
|
player->setRunning(!held(GameInputState::Walk));
|
|
|
|
/// @todo find the correct behaviour for entering & exiting
|
|
|
|
if (pressed(GameInputState::EnterExitVehicle)) {
|
|
|
|
/// @todo move me
|
|
|
|
if (player->getCharacter()->getCurrentVehicle()) {
|
|
|
|
player->exitVehicle();
|
|
|
|
} else if (!player->isCurrentActivity(
|
|
|
|
Activities::EnterVehicle::ActivityName)) {
|
|
|
|
player->enterNearestVehicle();
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2016-11-16 21:23:05 +01:00
|
|
|
} else if (glm::length2(movement) > 0.001f) {
|
|
|
|
if (player->isCurrentActivity(
|
|
|
|
Activities::EnterVehicle::ActivityName)) {
|
|
|
|
// Give up entering a vehicle if we're alreadying doing so
|
|
|
|
player->skipActivity();
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 22:13:20 +02:00
|
|
|
|
2016-11-16 21:23:05 +01:00
|
|
|
if (player->getCharacter()->getCurrentVehicle()) {
|
|
|
|
auto vehicle = player->getCharacter()->getCurrentVehicle();
|
|
|
|
vehicle->setHandbraking(held(GameInputState::Handbrake));
|
|
|
|
player->setMoveDirection(movement);
|
|
|
|
} else {
|
|
|
|
if (pressed(GameInputState::Jump)) {
|
|
|
|
player->jump();
|
|
|
|
}
|
2016-09-09 22:13:20 +02:00
|
|
|
|
2016-11-16 21:23:05 +01:00
|
|
|
float length = glm::length(movement);
|
|
|
|
if (length > 0.1f) {
|
2016-11-30 00:28:53 +01:00
|
|
|
auto move = speed * glm::normalize(movement);
|
|
|
|
player->setMoveDirection(glm::vec3(move.x, 0.f, move.y));
|
2016-11-16 21:23:05 +01:00
|
|
|
} else {
|
|
|
|
player->setMoveDirection(glm::vec3(0.f));
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 23:40:42 +01:00
|
|
|
player->setLookDirection(look);
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2014-07-24 00:57:21 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::draw(GameRenderer* r) {
|
|
|
|
if (!getWorld()->state->isCinematic && getWorld()->isCutsceneDone()) {
|
2018-05-26 18:13:32 +02:00
|
|
|
drawHUD(_look, game->getWorld()->getPlayer(), getWorld(), r);
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
State::draw(r);
|
2015-01-25 18:54:31 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::handleEvent(const SDL_Event& event) {
|
2018-05-26 18:13:32 +02:00
|
|
|
auto player = game->getWorld()->getPlayer();
|
2016-09-09 22:13:20 +02:00
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_ESCAPE:
|
2016-10-17 00:56:51 +02:00
|
|
|
StateManager::get().enter<PauseState>(game);
|
2016-09-09 22:13:20 +02:00
|
|
|
break;
|
|
|
|
case SDLK_m:
|
2016-10-17 00:56:51 +02:00
|
|
|
StateManager::get().enter<DebugState>(game, _look.position,
|
|
|
|
_look.rotation);
|
2016-09-09 22:13:20 +02:00
|
|
|
break;
|
|
|
|
case SDLK_SPACE:
|
|
|
|
if (getWorld()->state->currentCutscene) {
|
|
|
|
getWorld()->state->skipCutscene = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDLK_c:
|
|
|
|
camMode =
|
|
|
|
CameraMode((camMode + (CameraMode)1) % CAMERA_MAX);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (player && player->isInputEnabled()) {
|
|
|
|
handlePlayerInput(event);
|
|
|
|
}
|
|
|
|
State::handleEvent(event);
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void IngameState::handlePlayerInput(const SDL_Event& event) {
|
2018-05-26 18:13:32 +02:00
|
|
|
auto player = game->getWorld()->getPlayer();
|
2016-09-09 22:13:20 +02:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
switch (event.button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
player->getCharacter()->useItem(true, true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
switch (event.button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
player->getCharacter()->useItem(false, true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEWHEEL:
|
2017-02-19 02:17:05 +01:00
|
|
|
if (player->getCharacter()->getCurrentVehicle() == nullptr &&
|
|
|
|
player->getCharacter()->isAlive()) {
|
2017-02-17 23:40:33 +01:00
|
|
|
player->getCharacter()->cycleInventory(event.wheel.y > 0);
|
|
|
|
}
|
2016-09-09 22:13:20 +02:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
if (game->hasFocus()) {
|
|
|
|
glm::ivec2 screenSize = getWindow().getSize();
|
|
|
|
glm::vec2 mouseMove(
|
|
|
|
event.motion.xrel / static_cast<float>(screenSize.x),
|
|
|
|
event.motion.yrel / static_cast<float>(screenSize.y));
|
|
|
|
autolookTimer = kAutoLookTime;
|
2016-12-06 23:40:42 +01:00
|
|
|
if (m_invertedY) {
|
2016-09-09 22:13:20 +02:00
|
|
|
mouseMove.y = -mouseMove.y;
|
|
|
|
}
|
2016-12-06 23:40:42 +01:00
|
|
|
cameradelta_ -= mouseMove;
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-06-29 00:58:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
bool IngameState::shouldWorldUpdate() {
|
2015-04-03 16:38:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-30 00:28:53 +01:00
|
|
|
const ViewCamera& IngameState::getCamera(float alpha) {
|
2016-12-04 02:33:26 +01:00
|
|
|
auto state = game->getState();
|
2018-05-26 18:13:32 +02:00
|
|
|
auto player = game->getWorld()->getPlayer();
|
2016-11-30 00:28:53 +01:00
|
|
|
auto world = getWorld();
|
|
|
|
|
2016-12-04 02:33:26 +01:00
|
|
|
if (state->currentCutscene && state->cutsceneStartTime >= 0.f) {
|
|
|
|
auto cutscene = state->currentCutscene;
|
|
|
|
float cutsceneTime =
|
|
|
|
std::min(world->getGameTime() - state->cutsceneStartTime,
|
|
|
|
cutscene->tracks.duration);
|
|
|
|
cutsceneTime += GAME_TIMESTEP * alpha;
|
|
|
|
glm::vec3 cameraPos = cutscene->tracks.getPositionAt(cutsceneTime),
|
|
|
|
targetPos = cutscene->tracks.getTargetAt(cutsceneTime);
|
|
|
|
float zoom = cutscene->tracks.getZoomAt(cutsceneTime);
|
|
|
|
_look.frustum.fov = glm::radians(zoom);
|
|
|
|
float tilt = cutscene->tracks.getRotationAt(cutsceneTime);
|
|
|
|
|
|
|
|
auto direction = glm::normalize(targetPos - cameraPos);
|
|
|
|
auto right =
|
|
|
|
glm::normalize(glm::cross(glm::vec3(0.f, 0.f, 1.f), direction));
|
|
|
|
auto up = glm::normalize(glm::cross(direction, right));
|
|
|
|
|
2017-09-12 02:02:05 +02:00
|
|
|
glm::mat3 m = glm::rowMajor3(direction, right, up);
|
2016-12-04 02:33:26 +01:00
|
|
|
|
|
|
|
auto qtilt = glm::angleAxis(glm::radians(tilt), direction);
|
|
|
|
|
|
|
|
cameraPos += cutscene->meta.sceneOffset;
|
|
|
|
targetPos += cutscene->meta.sceneOffset;
|
|
|
|
|
|
|
|
_look.position = cameraPos;
|
|
|
|
_look.rotation = glm::inverse(glm::quat_cast(m)) * qtilt;
|
|
|
|
return _look;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->cameraFixed) {
|
|
|
|
_look.position = state->cameraPosition;
|
|
|
|
_look.rotation = state->cameraRotation;
|
|
|
|
return _look;
|
|
|
|
}
|
|
|
|
|
|
|
|
_look.frustum.fov = kInGameFOV;
|
|
|
|
|
2016-11-30 00:28:53 +01:00
|
|
|
if (!player) {
|
|
|
|
return _look;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force all input to 0 if player input is disabled
|
|
|
|
/// @todo verify 0ing input is the correct behaviour
|
|
|
|
const auto inputEnabled = player->isInputEnabled();
|
|
|
|
|
|
|
|
auto held = [&](GameInputState::Control c) {
|
|
|
|
return inputEnabled && world->state->input[0].pressed(c);
|
|
|
|
};
|
|
|
|
|
|
|
|
float viewDistance = getViewDistance();
|
|
|
|
auto target = getCameraTarget();
|
|
|
|
bool lookleft = held(GameInputState::LookLeft);
|
|
|
|
bool lookright = held(GameInputState::LookRight);
|
|
|
|
btCollisionObject* physTarget = player->getCharacter()->physObject;
|
|
|
|
|
|
|
|
auto targetTransform = target->getTimeAdjustedTransform(alpha);
|
|
|
|
|
|
|
|
glm::vec3 targetPosition(targetTransform[3]);
|
|
|
|
glm::vec3 lookTargetPosition(targetPosition);
|
|
|
|
targetPosition += glm::vec3(0.f, 0.f, 1.f);
|
|
|
|
lookTargetPosition += glm::vec3(0.f, 0.f, 0.5f);
|
|
|
|
|
|
|
|
if (target->type() == GameObject::Vehicle) {
|
|
|
|
auto vehicle = (VehicleObject*)target;
|
|
|
|
auto model = vehicle->getModel();
|
|
|
|
auto maxDist = model->getBoundingRadius() * 2.f;
|
|
|
|
viewDistance = viewDistance + maxDist;
|
|
|
|
lookTargetPosition.z += (vehicle->info->handling.dimensions.z * 0.5f);
|
|
|
|
targetPosition.z += (vehicle->info->handling.dimensions.z * 0.5f);
|
|
|
|
physTarget = vehicle->collision->getBulletBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle top-down camera
|
|
|
|
if (camMode == CAMERA_TOPDOWN) {
|
|
|
|
cameraPosition = targetPosition + glm::vec3(0.f, 0.f, viewDistance);
|
|
|
|
_look.rotation =
|
|
|
|
glm::angleAxis(glm::half_pi<float>(), glm::vec3(0.f, 1.f, 0.f));
|
|
|
|
} else if ((lookleft || lookright) &&
|
|
|
|
target->type() == GameObject::Vehicle) {
|
|
|
|
auto rotation = target->getRotation();
|
|
|
|
if (!lookright) {
|
|
|
|
rotation *= glm::angleAxis(glm::half_pi<float>(),
|
|
|
|
glm::vec3(0.f, 0.f, -1.f));
|
|
|
|
} else if (!lookleft) {
|
|
|
|
rotation *=
|
|
|
|
glm::angleAxis(glm::half_pi<float>(), glm::vec3(0.f, 0.f, 1.f));
|
|
|
|
}
|
|
|
|
cameraPosition =
|
|
|
|
targetPosition + rotation * glm::vec3(0.f, viewDistance, 0.f);
|
|
|
|
} else {
|
2016-12-06 23:40:42 +01:00
|
|
|
auto look = player->getCharacter()->getLook();
|
2016-11-30 00:28:53 +01:00
|
|
|
// Determine the "ideal" camera position for the current view angles
|
2016-12-06 23:40:42 +01:00
|
|
|
auto yaw = glm::angleAxis(look.x - glm::half_pi<float>(),
|
2016-11-30 00:28:53 +01:00
|
|
|
glm::vec3(0.f, 0.f, 1.f));
|
2016-12-06 23:40:42 +01:00
|
|
|
auto pitch = glm::angleAxis(look.y, glm::vec3(0.f, 1.f, 0.f));
|
2016-11-30 00:28:53 +01:00
|
|
|
auto cameraOffset = yaw * pitch * glm::vec3(0.f, 0.f, viewDistance);
|
|
|
|
cameraPosition = targetPosition + cameraOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lookdir = glm::normalize(lookTargetPosition - cameraPosition);
|
|
|
|
// Calculate the angles to look at the target position
|
|
|
|
float len2d = glm::length(glm::vec2(lookdir));
|
|
|
|
float anglePitch = glm::atan(lookdir.z, len2d);
|
|
|
|
float angleYaw = glm::atan(lookdir.y, lookdir.x);
|
|
|
|
glm::quat angle(glm::vec3(0.f, -anglePitch, angleYaw));
|
|
|
|
|
|
|
|
// Ensure the target position is actually visible
|
|
|
|
auto rayEnd = cameraPosition;
|
|
|
|
auto rayStart = targetPosition;
|
|
|
|
auto to = btVector3(rayEnd.x, rayEnd.y, rayEnd.z);
|
|
|
|
auto from = btVector3(rayStart.x, rayStart.y, rayStart.z);
|
|
|
|
ClosestNotMeRayResultCallback ray(physTarget, from, to);
|
|
|
|
|
|
|
|
world->dynamicsWorld->rayTest(from, to, ray);
|
|
|
|
if (ray.hasHit() && ray.m_closestHitFraction < 1.f) {
|
|
|
|
cameraPosition =
|
|
|
|
glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
|
|
|
|
ray.m_hitPointWorld.z());
|
|
|
|
cameraPosition +=
|
|
|
|
glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
|
|
|
|
ray.m_hitNormalWorld.z()) *
|
|
|
|
0.1f;
|
|
|
|
}
|
|
|
|
_look.position = cameraPosition;
|
|
|
|
_look.rotation = angle;
|
2016-09-09 22:13:20 +02:00
|
|
|
return _look;
|
2014-08-12 22:15:26 +02:00
|
|
|
}
|
2016-11-30 00:28:53 +01:00
|
|
|
|
|
|
|
GameObject* IngameState::getCameraTarget() const {
|
|
|
|
auto target =
|
|
|
|
getWorld()->pedestrianPool.find(game->getState()->cameraTarget);
|
|
|
|
|
2018-05-26 18:13:32 +02:00
|
|
|
if (target == nullptr && game->getWorld()->getPlayer()) {
|
|
|
|
target = game->getWorld()->getPlayer()->getCharacter();
|
2016-11-30 00:28:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the target is a character in a vehicle, make the vehicle the target
|
|
|
|
if (target && target->type() == GameObject::Character) {
|
|
|
|
auto vehicle = ((CharacterObject*)target)->getCurrentVehicle();
|
|
|
|
if (vehicle) {
|
|
|
|
target = vehicle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
float IngameState::getViewDistance() const {
|
|
|
|
float viewDistance = 4.f;
|
|
|
|
switch (camMode) {
|
|
|
|
case IngameState::CAMERA_CLOSE:
|
|
|
|
viewDistance = 2.f;
|
|
|
|
break;
|
|
|
|
case IngameState::CAMERA_NORMAL:
|
|
|
|
viewDistance = 4.0f;
|
|
|
|
break;
|
|
|
|
case IngameState::CAMERA_FAR:
|
|
|
|
viewDistance = 6.f;
|
|
|
|
break;
|
|
|
|
case IngameState::CAMERA_TOPDOWN:
|
|
|
|
viewDistance = 15.f;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
viewDistance = 4.f;
|
|
|
|
}
|
|
|
|
return viewDistance;
|
|
|
|
}
|