2014-05-26 06:34:49 +02:00
|
|
|
#include "ingamestate.hpp"
|
2014-09-16 20:22:43 +02:00
|
|
|
#include "RWGame.hpp"
|
2014-05-26 06:34:49 +02:00
|
|
|
#include "pausestate.hpp"
|
2014-05-31 09:18:50 +02:00
|
|
|
#include "debugstate.hpp"
|
2015-02-15 13:41:51 +01:00
|
|
|
#include "DrawUI.hpp"
|
2014-08-12 22:15:26 +02:00
|
|
|
|
|
|
|
#include <ai/PlayerController.hpp>
|
2014-06-06 16:22:26 +02:00
|
|
|
#include <objects/CharacterObject.hpp>
|
|
|
|
#include <objects/VehicleObject.hpp>
|
2014-07-04 22:06:56 +02:00
|
|
|
#include <objects/ItemPickup.hpp>
|
2016-04-07 02:13:46 +02:00
|
|
|
#include <data/Model.hpp>
|
2014-06-29 23:14:46 +02:00
|
|
|
#include <items/WeaponItem.hpp>
|
2015-02-04 18:16:46 +01:00
|
|
|
#include <engine/GameWorld.hpp>
|
2015-04-27 16:31:39 +02:00
|
|
|
#include <engine/GameState.hpp>
|
2015-04-04 04:12:28 +02:00
|
|
|
#include <script/ScriptMachine.hpp>
|
2015-04-04 21:00:59 +02:00
|
|
|
#include <dynamics/RaycastCallbacks.hpp>
|
2014-12-15 00:21:30 +01:00
|
|
|
|
2016-05-21 20:06:37 +02:00
|
|
|
constexpr float kAutoLookTime = 2.f;
|
|
|
|
constexpr float kAutolookMinVelocity = 0.2f;
|
|
|
|
const float kMaxRotationRate = glm::half_pi<float>();
|
|
|
|
const float kCameraPitchLimit = glm::quarter_pi<float>() * 0.5f;
|
2016-05-22 16:58:36 +02:00
|
|
|
const float kVehicleCameraPitch = 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-05-21 20:06:37 +02:00
|
|
|
: State(game)
|
|
|
|
, started(false)
|
|
|
|
, save(save)
|
|
|
|
, newgame(newgame)
|
|
|
|
, autolookTimer(0.f)
|
|
|
|
, camMode(IngameState::CAMERA_NORMAL)
|
|
|
|
, m_cameraAngles { 0.f, glm::half_pi<float>() }
|
2016-05-22 16:58:36 +02:00
|
|
|
, m_invertedY(game->getConfig().getInputInvertY())
|
|
|
|
, m_vehicleFreeLook(true)
|
2014-07-24 00:57:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void IngameState::startTest()
|
2014-05-26 06:34:49 +02:00
|
|
|
{
|
2015-05-03 06:22:03 +02:00
|
|
|
auto playerChar = getWorld()->createPlayer({270.f, -605.f, 40.f});
|
2014-08-12 22:15:26 +02:00
|
|
|
auto player = new PlayerController(playerChar);
|
2014-05-31 09:18:50 +02:00
|
|
|
|
2015-05-03 06:22:03 +02:00
|
|
|
getWorld()->state->playerObject = playerChar->getGameObjectID();
|
2014-06-07 05:11:20 +02:00
|
|
|
|
2016-06-19 02:24:17 +02:00
|
|
|
glm::vec3 itemspawn(276.5f, -609.f, 36.5f);
|
|
|
|
for (int i = 1; i < maxInventorySlots; ++i) {
|
|
|
|
auto item = getWorld()->getInventoryItem(i);
|
|
|
|
getWorld()->createPickup(itemspawn, item->getModelID(),
|
|
|
|
PickupObject::OnStreet);
|
2014-07-09 23:28:25 +02:00
|
|
|
itemspawn.x += 2.5f;
|
|
|
|
}
|
2014-06-29 23:14:46 +02:00
|
|
|
|
2015-04-04 18:48:36 +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));
|
|
|
|
//auto boatPos = glm::vec3( -1000.f, -1040.f, 5.f );
|
|
|
|
int i = 0;
|
2015-04-24 19:09:21 +02:00
|
|
|
for( auto& vi : getWorld()->data->objectTypes ) {
|
2014-06-17 01:48:19 +02:00
|
|
|
switch( vi.first ) {
|
|
|
|
case 140: continue;
|
|
|
|
case 141: continue;
|
|
|
|
}
|
2014-09-17 04:13:02 +02:00
|
|
|
if( vi.second->class_type == ObjectInformation::_class("CARS") )
|
|
|
|
{
|
2015-04-04 18:48:36 +02:00
|
|
|
if ( i++ > 20 ) break;
|
2014-09-17 04:13:02 +02:00
|
|
|
auto vehicle = std::static_pointer_cast<VehicleData>(vi.second);
|
|
|
|
|
2015-04-04 18:48:36 +02:00
|
|
|
auto& sp = carPos;
|
|
|
|
auto& sr = carRot;
|
|
|
|
auto v = getWorld()->createVehicle(vi.first, sp, sr);
|
2014-09-17 04:13:02 +02:00
|
|
|
|
2015-04-04 18:48:36 +02:00
|
|
|
sp += sr * glm::vec3( 2.f + v->info->handling.dimensions.x, 0.f, 0.f);
|
2014-06-17 01:48:19 +02:00
|
|
|
}
|
2014-06-07 05:11:20 +02:00
|
|
|
}
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 02:26:20 +02:00
|
|
|
void IngameState::startGame()
|
2014-05-26 06:34:49 +02:00
|
|
|
{
|
2016-06-11 17:07:30 +02:00
|
|
|
game->startScript("main.scm");
|
2015-05-03 06:22:03 +02:00
|
|
|
game->getScript()->startThread(0);
|
2015-04-18 02:11:17 +02:00
|
|
|
getWorld()->sound.playBackground( getWorld()->data->getDataPath() + "/audio/City.wav" );
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-24 00:57:21 +02:00
|
|
|
void IngameState::enter()
|
|
|
|
{
|
2015-04-03 02:26:20 +02:00
|
|
|
if( ! started )
|
|
|
|
{
|
2015-05-03 06:22:03 +02:00
|
|
|
if( newgame )
|
|
|
|
{
|
2015-07-03 03:52:43 +02:00
|
|
|
if( save.empty() )
|
|
|
|
{
|
|
|
|
startGame();
|
|
|
|
}
|
|
|
|
else if( save == "test" )
|
|
|
|
{
|
2015-05-03 06:22:03 +02:00
|
|
|
startTest();
|
|
|
|
}
|
|
|
|
else {
|
2015-07-03 03:52:43 +02:00
|
|
|
game->loadGame( save );
|
2015-05-03 06:22:03 +02:00
|
|
|
}
|
2015-04-03 02:26:20 +02:00
|
|
|
}
|
|
|
|
started = true;
|
|
|
|
}
|
2016-05-23 18:44:38 +02:00
|
|
|
|
2016-06-22 12:29:39 +02:00
|
|
|
getWindow().hideCursor();
|
2014-07-24 00:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IngameState::exit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IngameState::tick(float dt)
|
|
|
|
{
|
2014-12-15 00:21:30 +01:00
|
|
|
autolookTimer = std::max(autolookTimer - dt, 0.f);
|
2015-05-04 00:31:45 +02:00
|
|
|
|
2015-05-03 06:22:03 +02:00
|
|
|
auto player = game->getPlayer();
|
2016-05-21 20:06:37 +02:00
|
|
|
if( player && player->isInputEnabled() )
|
2014-08-12 22:15:26 +02:00
|
|
|
{
|
2015-05-04 05:35:22 +02:00
|
|
|
float viewDistance = 4.f;
|
2015-05-09 00:53:56 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-24 02:48:26 +02:00
|
|
|
auto target = getWorld()->pedestrianPool.find(getWorld()->state->cameraTarget);
|
2015-05-04 05:35:22 +02:00
|
|
|
|
2014-12-17 01:39:52 +01:00
|
|
|
if( target == nullptr )
|
|
|
|
{
|
|
|
|
target = player->getCharacter();
|
|
|
|
}
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2015-05-04 05:35:22 +02:00
|
|
|
glm::vec3 targetPosition = target->getPosition();
|
2015-05-09 00:53:56 +02:00
|
|
|
glm::vec3 lookTargetPosition = targetPosition;
|
2015-05-04 05:35:22 +02:00
|
|
|
targetPosition += glm::vec3(0.f, 0.f, 1.f);
|
2015-05-09 00:53:56 +02:00
|
|
|
lookTargetPosition += glm::vec3(0.f, 0.f, 0.5f);
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2015-04-04 19:34:12 +02:00
|
|
|
btCollisionObject* physTarget = player->getCharacter()->physObject;
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2014-12-17 01:39:52 +01:00
|
|
|
auto vehicle = ( target->type() == GameObject::Character ) ? static_cast<CharacterObject*>(target)->getCurrentVehicle() : nullptr;
|
2014-08-12 22:15:26 +02:00
|
|
|
if( vehicle ) {
|
|
|
|
auto model = vehicle->model;
|
2015-05-09 00:53:56 +02:00
|
|
|
float maxDist = 0.f;
|
2015-03-06 05:38:22 +01:00
|
|
|
for(auto& g : model->resource->geometries) {
|
2015-05-09 00:53:56 +02:00
|
|
|
float partSize = glm::length(g->geometryBounds.center) + g->geometryBounds.radius;
|
|
|
|
maxDist = std::max(partSize, maxDist);
|
2014-08-12 22:15:26 +02:00
|
|
|
}
|
2015-05-09 00:53:56 +02:00
|
|
|
viewDistance = viewDistance + maxDist;
|
2015-05-04 05:35:22 +02:00
|
|
|
targetPosition = vehicle->getPosition();
|
2015-05-09 00:53:56 +02:00
|
|
|
lookTargetPosition = targetPosition;
|
2016-06-19 14:25:51 +02:00
|
|
|
lookTargetPosition.z += (vehicle->info->handling.dimensions.z * 0.5f);
|
|
|
|
targetPosition.z += (vehicle->info->handling.dimensions.z * 0.5f);
|
2015-04-04 19:34:12 +02:00
|
|
|
physTarget = vehicle->physBody;
|
2016-05-22 16:58:36 +02:00
|
|
|
|
|
|
|
if (!m_vehicleFreeLook)
|
|
|
|
{
|
|
|
|
m_cameraAngles.y = kVehicleCameraPitch;
|
|
|
|
}
|
2016-05-21 20:06:37 +02:00
|
|
|
|
|
|
|
// Rotate the camera to the ideal angle if the player isn't moving it
|
|
|
|
float velocity = vehicle->getVelocity();
|
|
|
|
if (autolookTimer <= 0.f && glm::abs(velocity) > kAutolookMinVelocity)
|
|
|
|
{
|
2016-05-22 16:58:36 +02:00
|
|
|
auto idealYaw = -glm::roll(vehicle->getRotation()) + glm::half_pi<float>();
|
|
|
|
const auto idealPitch = kVehicleCameraPitch;
|
2016-05-21 20:06:37 +02:00
|
|
|
if (velocity < 0.f) {
|
2016-05-22 16:58:36 +02:00
|
|
|
idealYaw = glm::mod(idealYaw - glm::pi<float>(), glm::pi<float>() * 2.f);
|
2016-05-21 20:06:37 +02:00
|
|
|
}
|
2016-05-22 16:58:36 +02:00
|
|
|
float currentYaw = glm::mod(m_cameraAngles.x, glm::pi<float>()*2);
|
|
|
|
float currentPitch = m_cameraAngles.y;
|
|
|
|
float deltaYaw = idealYaw - currentYaw;
|
|
|
|
float deltaPitch = idealPitch - currentPitch;
|
|
|
|
if (glm::abs(deltaYaw) > glm::pi<float>()) {
|
|
|
|
deltaYaw -= glm::sign(deltaYaw) * glm::pi<float>()*2.f;
|
2016-05-21 20:06:37 +02:00
|
|
|
}
|
2016-05-22 16:58:36 +02:00
|
|
|
m_cameraAngles.x += glm::sign(deltaYaw) * std::min(kMaxRotationRate * dt, glm::abs(deltaYaw));
|
|
|
|
m_cameraAngles.y += glm::sign(deltaPitch) * std::min(kMaxRotationRate * dt, glm::abs(deltaPitch));
|
2016-05-21 20:06:37 +02:00
|
|
|
}
|
2014-08-12 22:15:26 +02:00
|
|
|
}
|
2014-07-24 00:57:21 +02:00
|
|
|
|
2015-05-09 00:53:56 +02:00
|
|
|
// Non-topdown camera can orbit
|
|
|
|
if( camMode != IngameState::CAMERA_TOPDOWN )
|
2015-05-04 05:35:22 +02:00
|
|
|
{
|
2016-05-21 20:06:37 +02:00
|
|
|
// Determine the "ideal" camera position for the current view angles
|
|
|
|
auto yaw = glm::angleAxis(m_cameraAngles.x, glm::vec3(0.f, 0.f,-1.f));
|
2016-05-22 16:58:36 +02:00
|
|
|
auto pitch = glm::angleAxis(m_cameraAngles.y, glm::vec3(0.f, 1.f, 0.f));
|
2016-05-21 20:06:37 +02:00
|
|
|
auto cameraOffset =
|
|
|
|
yaw * pitch * glm::vec3(0.f, 0.f, viewDistance);
|
|
|
|
cameraPosition = targetPosition + cameraOffset;
|
2015-05-04 05:35:22 +02:00
|
|
|
}
|
2015-05-09 00:53:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cameraPosition = targetPosition + glm::vec3(0.f, 0.f, viewDistance);
|
|
|
|
}
|
2015-05-04 05:35:22 +02:00
|
|
|
|
|
|
|
glm::quat angle;
|
|
|
|
|
|
|
|
auto camtotarget = targetPosition - cameraPosition;
|
|
|
|
auto dir = glm::normalize(camtotarget);
|
|
|
|
float correction = glm::length(camtotarget) - viewDistance;
|
|
|
|
if( correction < 0.f )
|
|
|
|
{
|
2015-05-09 00:53:56 +02:00
|
|
|
float innerDist = viewDistance * 0.1f;
|
2015-05-04 05:35:22 +02:00
|
|
|
correction = glm::min(0.f, correction + innerDist);
|
|
|
|
}
|
|
|
|
cameraPosition += dir * 10.f * correction * dt;
|
|
|
|
|
2015-05-09 00:53:56 +02:00
|
|
|
auto lookdir = glm::normalize(lookTargetPosition - cameraPosition);
|
2015-05-04 05:35:22 +02:00
|
|
|
// Calculate the yaw to look at the target.
|
2015-05-09 00:53:56 +02:00
|
|
|
float angleYaw = glm::atan(lookdir.y, lookdir.x);
|
2015-05-04 05:35:22 +02:00
|
|
|
angle = glm::quat( glm::vec3(0.f, 0.f, angleYaw) );
|
|
|
|
|
|
|
|
// Update player with camera yaw
|
2015-05-18 01:33:06 +02:00
|
|
|
if( player->isInputEnabled() )
|
|
|
|
{
|
2016-04-17 05:54:19 +02:00
|
|
|
if (player->getCharacter()->getCurrentVehicle())
|
|
|
|
{
|
|
|
|
player->setMoveDirection(_movement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-18 01:10:44 +02:00
|
|
|
float length = glm::length(_movement);
|
2016-04-17 05:54:19 +02:00
|
|
|
float movementAngle = angleYaw - M_PI/2.f;
|
|
|
|
if (length > 0.1f)
|
|
|
|
{
|
2016-04-18 01:10:44 +02:00
|
|
|
glm::vec3 direction = glm::normalize(_movement);
|
2016-04-17 05:54:19 +02:00
|
|
|
movementAngle += atan2(direction.y, direction.x);
|
2016-04-18 01:10:44 +02:00
|
|
|
player->setMoveDirection(glm::vec3(1.f, 0.f, 0.f));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
player->setMoveDirection(glm::vec3(0.f));
|
2016-04-17 05:54:19 +02:00
|
|
|
}
|
2016-05-23 22:35:49 +02:00
|
|
|
player->setLookDirection({movementAngle, 0.f});
|
2016-04-17 05:54:19 +02:00
|
|
|
}
|
2015-05-18 01:33:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-17 05:54:19 +02:00
|
|
|
player->setMoveDirection(glm::vec3(0.f));
|
2015-05-18 01:33:06 +02:00
|
|
|
}
|
2015-05-04 05:35:22 +02:00
|
|
|
|
2015-05-09 00:53:56 +02:00
|
|
|
float len2d = glm::length(glm::vec2(lookdir));
|
|
|
|
float anglePitch = glm::atan(lookdir.z, len2d);
|
2015-05-04 05:35:22 +02:00
|
|
|
angle *= glm::quat( glm::vec3(0.f, -anglePitch, 0.f) );
|
|
|
|
|
|
|
|
// Use rays to ensure target is visible from cameraPosition
|
|
|
|
auto rayEnd = cameraPosition;
|
|
|
|
auto rayStart = targetPosition;
|
2015-04-04 19:34:12 +02:00
|
|
|
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 )
|
|
|
|
{
|
2015-05-04 05:35:22 +02:00
|
|
|
cameraPosition = glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
|
2015-04-04 19:34:12 +02:00
|
|
|
ray.m_hitPointWorld.z());
|
2015-05-04 05:35:22 +02:00
|
|
|
cameraPosition += glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
|
2015-04-04 19:34:12 +02:00
|
|
|
ray.m_hitNormalWorld.z()) * 0.1f;
|
|
|
|
}
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2015-05-04 05:35:22 +02:00
|
|
|
_look.position = cameraPosition;
|
2014-08-12 22:15:26 +02:00
|
|
|
_look.rotation = angle;
|
2014-07-24 00:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
void IngameState::draw(GameRenderer* r)
|
2015-01-25 18:54:31 +01:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
if( !getWorld()->state->isCinematic && getWorld()->isCutsceneDone() )
|
2015-01-25 18:54:31 +01:00
|
|
|
{
|
2016-04-12 02:02:09 +02:00
|
|
|
drawHUD(_look, game->getPlayer(), getWorld(), r);
|
2015-01-25 18:54:31 +01:00
|
|
|
}
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
State::draw(r);
|
2015-01-25 18:54:31 +01:00
|
|
|
}
|
|
|
|
|
2016-06-22 12:29:39 +02:00
|
|
|
void IngameState::handleEvent(const SDL_Event& event)
|
2014-05-26 06:34:49 +02:00
|
|
|
{
|
2015-05-03 06:22:03 +02:00
|
|
|
auto player = game->getPlayer();
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2014-05-26 06:34:49 +02:00
|
|
|
switch(event.type) {
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
switch(event.key.keysym.sym) {
|
|
|
|
case SDLK_ESCAPE:
|
2014-09-16 20:22:43 +02:00
|
|
|
StateManager::get().enter(new PauseState(game));
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_m:
|
2014-09-16 20:22:43 +02:00
|
|
|
StateManager::get().enter(new DebugState(game, _look.position, _look.rotation));
|
2014-05-31 09:18:50 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_SPACE:
|
2015-04-27 16:31:39 +02:00
|
|
|
if( getWorld()->state->currentCutscene )
|
2014-12-17 01:39:52 +01:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
getWorld()->state->skipCutscene = true;
|
2014-12-17 01:39:52 +01:00
|
|
|
}
|
2015-05-18 01:33:06 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_c:
|
2015-05-18 01:33:06 +02:00
|
|
|
camMode = CameraMode((camMode+(CameraMode)1)%CAMERA_MAX);
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_w:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.x = 1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_s:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.x =-1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_a:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.y = 1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_d:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.y =-1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
|
|
|
|
case SDL_KEYUP:
|
|
|
|
switch(event.key.keysym.sym) {
|
|
|
|
case SDLK_w:
|
|
|
|
case SDLK_s:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.x = 0.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_a:
|
|
|
|
case SDLK_d:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.y = 0.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2015-05-18 01:33:06 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( player && player->isInputEnabled() )
|
|
|
|
{
|
|
|
|
handlePlayerInput(event);
|
|
|
|
}
|
|
|
|
State::handleEvent(event);
|
|
|
|
}
|
|
|
|
|
2016-06-22 12:29:39 +02:00
|
|
|
void IngameState::handlePlayerInput(const SDL_Event& event)
|
2015-05-18 01:33:06 +02:00
|
|
|
{
|
|
|
|
auto player = game->getPlayer();
|
|
|
|
switch(event.type) {
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
switch(event.key.keysym.sym) {
|
|
|
|
case SDLK_SPACE:
|
2015-05-18 01:33:06 +02:00
|
|
|
if( player->getCharacter()->getCurrentVehicle() ) {
|
|
|
|
player->getCharacter()->getCurrentVehicle()->setHandbraking(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
player->jump();
|
|
|
|
}
|
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_f:
|
2015-05-18 01:33:06 +02:00
|
|
|
if( player->getCharacter()->getCurrentVehicle()) {
|
|
|
|
player->exitVehicle();
|
|
|
|
}
|
2016-05-25 00:49:01 +02:00
|
|
|
else
|
|
|
|
if (player->isCurrentActivity(
|
|
|
|
Activities::EnterVehicle::ActivityName))
|
|
|
|
{
|
|
|
|
// Give up entering a vehicle if we're alreadying doing so
|
|
|
|
player->skipActivity();
|
|
|
|
}
|
2015-05-18 01:33:06 +02:00
|
|
|
else {
|
|
|
|
player->enterNearestVehicle();
|
|
|
|
}
|
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDLK_LSHIFT:
|
|
|
|
player->setRunning(true);
|
|
|
|
break;
|
2015-05-18 01:33:06 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-06-22 12:29:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_KEYUP:
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_LSHIFT:
|
2014-08-12 22:15:26 +02:00
|
|
|
player->setRunning(false);
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2016-06-22 12:29:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
switch(event.button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
2016-04-13 22:16:27 +02:00
|
|
|
player->getCharacter()->useItem(true, true);
|
2014-06-29 23:14:46 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
switch(event.button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
2016-04-13 22:16:27 +02:00
|
|
|
player->getCharacter()->useItem(false, true);
|
2014-06-29 23:14:46 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2016-06-22 12:29:39 +02:00
|
|
|
case SDL_MOUSEWHEEL:
|
|
|
|
player->getCharacter()->cycleInventory(event.wheel.y > 0);
|
|
|
|
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;
|
|
|
|
if (!m_invertedY) {
|
|
|
|
mouseMove.y = -mouseMove.y;
|
|
|
|
}
|
|
|
|
m_cameraAngles += glm::vec2(mouseMove.x, mouseMove.y);
|
|
|
|
m_cameraAngles.y = glm::clamp(m_cameraAngles.y, kCameraPitchLimit, glm::pi<float>() - kCameraPitchLimit);
|
|
|
|
}
|
2014-07-04 22:06:56 +02:00
|
|
|
break;
|
2015-05-18 01:33:06 +02:00
|
|
|
default:
|
|
|
|
break;
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2015-05-18 01:33:06 +02:00
|
|
|
|
2015-04-03 16:38:24 +02:00
|
|
|
bool IngameState::shouldWorldUpdate()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-12 22:15:26 +02:00
|
|
|
const ViewCamera &IngameState::getCamera()
|
|
|
|
{
|
|
|
|
return _look;
|
|
|
|
}
|
2015-01-25 19:42:29 +01:00
|
|
|
|
|
|
|
|