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"
|
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>
|
2014-05-31 21:19:15 +02:00
|
|
|
#include <render/Model.hpp>
|
2014-06-29 23:14:46 +02:00
|
|
|
#include <items/WeaponItem.hpp>
|
2014-12-15 00:21:30 +01:00
|
|
|
#include <boost/concept_check.hpp>
|
|
|
|
|
|
|
|
#define AUTOLOOK_TIME 2.f
|
2014-05-26 06:34:49 +02:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
IngameState::IngameState(RWGame* game, bool test)
|
2014-12-15 00:21:30 +01:00
|
|
|
: State(game), autolookTimer(0.f)
|
2014-07-24 00:57:21 +02:00
|
|
|
{
|
|
|
|
if( test ) {
|
|
|
|
startTest();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
getWorld()->runScript("data/main.scm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IngameState::startTest()
|
2014-05-26 06:34:49 +02:00
|
|
|
{
|
2014-08-12 22:15:26 +02:00
|
|
|
auto playerChar = getWorld()->createPedestrian(1, {-1000.f, -990.f, 13.f});
|
|
|
|
auto player = new PlayerController(playerChar);
|
2014-05-31 09:18:50 +02:00
|
|
|
|
2014-08-12 22:15:26 +02:00
|
|
|
getWorld()->state.player = player;
|
2014-06-07 05:11:20 +02:00
|
|
|
|
2014-07-04 22:06:56 +02:00
|
|
|
/*auto bat = new WeaponItem(getWorld()->gameData.weaponData["ak47"]);
|
2014-06-29 23:14:46 +02:00
|
|
|
_playerCharacter->addToInventory(bat);
|
2014-07-04 22:06:56 +02:00
|
|
|
_playerCharacter->setActiveItem(bat->getInventorySlot());*/
|
|
|
|
|
2014-07-09 23:28:25 +02:00
|
|
|
glm::vec3 itemspawn(-1000.f, -980.f, 11.0f);
|
|
|
|
for( auto& w : getWorld()->gameData.weaponData ) {
|
|
|
|
if( w.first == "unarmed" ) continue;
|
|
|
|
getWorld()->objects.insert(new ItemPickup(getWorld(), itemspawn,
|
|
|
|
w.second));
|
|
|
|
itemspawn.x += 2.5f;
|
|
|
|
}
|
2014-06-29 23:14:46 +02:00
|
|
|
|
2014-06-19 00:05:59 +02:00
|
|
|
auto carPos = glm::vec3( -1000.f, -1000.f, 12.f );
|
2014-06-17 01:48:19 +02:00
|
|
|
auto boatPos = glm::vec3( -1000.f, -1040.f, 5.f );
|
2014-09-17 04:13:02 +02:00
|
|
|
for( auto& vi : getWorld()->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") )
|
|
|
|
{
|
|
|
|
auto vehicle = std::static_pointer_cast<VehicleData>(vi.second);
|
|
|
|
|
|
|
|
auto sp = carPos;
|
|
|
|
if( vehicle->type == VehicleData::BOAT ) {
|
|
|
|
sp = boatPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto v = getWorld()->createVehicle(vi.first, sp, glm::quat());
|
|
|
|
|
|
|
|
if( vehicle->type == VehicleData::BOAT ) {
|
|
|
|
boatPos -= glm::vec3( 2.f + v->info->handling.dimensions.x, 0.f, 0.f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
carPos -= 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
|
|
|
}
|
|
|
|
|
|
|
|
void IngameState::spawnPlayerVehicle()
|
|
|
|
{
|
2014-09-17 04:13:02 +02:00
|
|
|
#if 0
|
2014-08-12 22:15:26 +02:00
|
|
|
if(! getWorld()->state.player ) return;
|
2014-05-26 06:34:49 +02:00
|
|
|
glm::vec3 hit, normal;
|
2014-09-16 20:22:43 +02:00
|
|
|
if(game->hitWorldRay(hit, normal)) {
|
2014-05-26 06:34:49 +02:00
|
|
|
|
|
|
|
// Pick random vehicle.
|
|
|
|
auto it = getWorld()->vehicleTypes.begin();
|
|
|
|
std::uniform_int_distribution<int> uniform(0, 9);
|
|
|
|
for(size_t i = 0, n = uniform(getWorld()->randomEngine); i != n; i++) {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto spawnpos = hit + normal;
|
|
|
|
auto vehicle = getWorld()->createVehicle(it->first, spawnpos,
|
|
|
|
glm::quat(glm::vec3(0.f, 0.f, -_lookAngles.x * PiOver180)));
|
2014-08-12 22:15:26 +02:00
|
|
|
|
|
|
|
getWorld()->state.player->getCharacter()->enterVehicle(vehicle, 0);
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
2014-09-17 04:13:02 +02:00
|
|
|
#endif
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
|
2014-08-12 22:15:26 +02:00
|
|
|
PlayerController *IngameState::getPlayer()
|
|
|
|
{
|
|
|
|
return getWorld()->state.player;
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
|
2014-07-24 00:57:21 +02:00
|
|
|
void IngameState::enter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IngameState::exit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IngameState::tick(float dt)
|
|
|
|
{
|
2014-12-15 00:21:30 +01:00
|
|
|
autolookTimer = std::max(autolookTimer - dt, 0.f);
|
|
|
|
|
2014-08-12 22:15:26 +02:00
|
|
|
auto player = getPlayer();
|
2015-01-27 16:25:45 +01:00
|
|
|
if( player && player->isInputEnabled() )
|
2014-08-12 22:15:26 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2014-08-12 22:15:26 +02:00
|
|
|
|
|
|
|
_lookAngles.x += deltaMouse.x / 100.0;
|
|
|
|
_lookAngles.y += deltaMouse.y / 100.0;
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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-08-12 22:15:26 +02:00
|
|
|
|
2014-12-17 01:39:52 +01:00
|
|
|
auto position = target->getPosition();
|
2014-08-12 22:15:26 +02:00
|
|
|
|
2014-12-15 00:21:30 +01:00
|
|
|
float viewDistance = 4.f;
|
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;
|
|
|
|
for(auto& g : model->model->geometries) {
|
|
|
|
viewDistance = std::max(
|
2014-12-15 00:21:30 +01:00
|
|
|
(glm::length(g->geometryBounds.center) + g->geometryBounds.radius) * 4.0f,
|
2014-08-12 22:15:26 +02:00
|
|
|
viewDistance);
|
|
|
|
}
|
2014-12-15 00:21:30 +01:00
|
|
|
position = vehicle->getPosition();
|
|
|
|
position.z += (vehicle->info->handling.dimensions.z / 2.f) * 2.5f;
|
|
|
|
|
|
|
|
float speed = vehicle->physVehicle->getCurrentSpeedKmHour();
|
|
|
|
if( autolookTimer <= 0.f && std::abs(speed) > 1.f )
|
|
|
|
{
|
|
|
|
float d = glm::sign(speed) > 0.f ? 0.f : glm::radians(180.f);
|
|
|
|
auto atrophy = std::min(1.f * glm::sign(_lookAngles.x - d) * dt, _lookAngles.x - d);
|
|
|
|
_lookAngles.x -= atrophy;
|
|
|
|
}
|
|
|
|
angle *= glm::angleAxis(glm::roll(vehicle->getRotation()) + glm::radians(90.f), glm::vec3(0.f, 0.f, 1.f));
|
2014-08-12 22:15:26 +02:00
|
|
|
}
|
2014-07-24 00:57:21 +02:00
|
|
|
|
2014-08-12 22:15:26 +02:00
|
|
|
// Move back from the character
|
|
|
|
position += angle * glm::vec3(-viewDistance, 0.f, 1.f);
|
|
|
|
|
|
|
|
// 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));
|
2014-08-12 22:15:26 +02:00
|
|
|
|
|
|
|
_look.position = position;
|
|
|
|
_look.rotation = angle;
|
2014-07-24 00:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 18:54:31 +01:00
|
|
|
void IngameState::draw(sf::RenderWindow& w)
|
|
|
|
{
|
2015-01-26 02:32:21 +01:00
|
|
|
if( !getWorld()->state.isCinematic && getWorld()->isCutsceneDone() )
|
2015-01-25 18:54:31 +01:00
|
|
|
{
|
2015-01-25 19:42:29 +01:00
|
|
|
drawHUD(w);
|
2015-01-25 18:54:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
State::draw(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-26 06:34:49 +02:00
|
|
|
void IngameState::handleEvent(const sf::Event &event)
|
|
|
|
{
|
2014-08-12 22:15:26 +02:00
|
|
|
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:
|
2014-09-16 20:22:43 +02:00
|
|
|
StateManager::get().enter(new PauseState(game));
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
2014-05-31 09:18:50 +02:00
|
|
|
case sf::Keyboard::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;
|
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:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.x = 1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::S:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.x =-1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::A:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.y = 1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::D:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.y =-1.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::LShift:
|
2014-08-12 22:15:26 +02:00
|
|
|
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() ) {
|
2014-08-12 22:15:26 +02:00
|
|
|
if( player->getCharacter()->getCurrentVehicle()) {
|
|
|
|
player->exitVehicle();
|
2014-05-26 06:34:49 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-08-12 22:15:26 +02:00
|
|
|
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:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.x = 0.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::A:
|
|
|
|
case sf::Keyboard::D:
|
2014-08-12 22:15:26 +02:00
|
|
|
_movement.y = 0.f;
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::LShift:
|
2014-08-12 22:15:26 +02:00
|
|
|
player->setRunning(false);
|
2014-05-26 06:34:49 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2014-06-29 23:14:46 +02:00
|
|
|
case sf::Event::MouseButtonPressed:
|
|
|
|
switch(event.mouseButton.button) {
|
|
|
|
case sf::Mouse::Left:
|
2014-08-12 22:15:26 +02:00
|
|
|
player->useItem(true, true);
|
2014-06-29 23:14:46 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case sf::Event::MouseButtonReleased:
|
|
|
|
switch(event.mouseButton.button) {
|
|
|
|
case sf::Mouse::Left:
|
2014-08-12 22:15:26 +02:00
|
|
|
player->useItem(false, true);
|
2014-06-29 23:14:46 +02:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2014-07-04 22:06:56 +02:00
|
|
|
case sf::Event::MouseWheelMoved:
|
2014-08-12 22:15:26 +02:00
|
|
|
player->getCharacter()->cycleInventory(event.mouseWheel.delta > 0);
|
2014-07-04 22:06:56 +02:00
|
|
|
break;
|
2014-05-26 06:34:49 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
State::handleEvent(event);
|
|
|
|
}
|
2014-08-12 22:15:26 +02:00
|
|
|
|
|
|
|
const ViewCamera &IngameState::getCamera()
|
|
|
|
{
|
|
|
|
return _look;
|
|
|
|
}
|
2015-01-25 19:42:29 +01:00
|
|
|
|
|
|
|
void IngameState::drawHUD(const sf::RenderWindow& w)
|
|
|
|
{
|
|
|
|
MapRenderer::MapInfo map;
|
|
|
|
map.scale = 0.5f;
|
|
|
|
map.viewport = glm::vec2(w.getSize().x, w.getSize().y);
|
|
|
|
|
|
|
|
map.mapPos = glm::vec2(map.viewport.x * 0.2f, map.viewport.y * 0.2f);
|
|
|
|
float aspect = map.viewport.x / map.viewport.y;
|
|
|
|
map.mapSize = map.viewport * 0.3f;
|
|
|
|
map.mapPos.x /= aspect;
|
|
|
|
map.mapSize.x /= aspect;
|
|
|
|
|
|
|
|
if( getWorld()->state.player )
|
|
|
|
{
|
|
|
|
map.center = glm::vec2(getWorld()->state.player->getCharacter()->getPosition());
|
|
|
|
}
|
|
|
|
getWorld()->renderer.map.draw(map);
|
|
|
|
}
|
|
|
|
|