Move keyboard input to DefaultPlayerController
This commit is contained in:
parent
64745491aa
commit
3a38190290
@ -1,6 +1,7 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "uielements.h"
|
#include "uielements.h"
|
||||||
|
#include "defaultplayercontroller.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include "internal/ui.h"
|
#include "internal/ui.h"
|
||||||
#include "internal/physics.h"
|
#include "internal/physics.h"
|
||||||
@ -8,12 +9,12 @@
|
|||||||
namespace kek::Defaults {
|
namespace kek::Defaults {
|
||||||
|
|
||||||
static KeyBinding
|
static KeyBinding
|
||||||
keyForward,
|
/*keyForward,
|
||||||
keyBackward,
|
keyBackward,
|
||||||
keyLeft,
|
keyLeft,
|
||||||
keyRight,
|
keyRight,
|
||||||
keyUp,
|
keyUp,
|
||||||
keyDown,
|
keyDown,*/
|
||||||
keyOptions,
|
keyOptions,
|
||||||
keyToggleCursorMode,
|
keyToggleCursorMode,
|
||||||
keyToggleNoclip,
|
keyToggleNoclip,
|
||||||
@ -25,53 +26,7 @@ static void defaultInput(GLFWwindow *window, void *data) {
|
|||||||
if(Input::isKeyboardCaptured()) return;
|
if(Input::isKeyboardCaptured()) return;
|
||||||
|
|
||||||
// TODO: move input handling to controller class, add NoclipController
|
// TODO: move input handling to controller class, add NoclipController
|
||||||
|
|
||||||
glm::vec3 direction = glm::vec3(0);
|
|
||||||
|
|
||||||
if(Input::getKeyState(keyForward) == GLFW_PRESS) {
|
|
||||||
direction += kekData.activeCamera->direction;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Input::getKeyState(keyBackward) == GLFW_PRESS) {
|
|
||||||
direction += -kekData.activeCamera->direction;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Input::getKeyState(keyLeft) == GLFW_PRESS) {
|
|
||||||
direction += -glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Input::getKeyState(keyRight) == GLFW_PRESS) {
|
|
||||||
direction += glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool jump = false;
|
|
||||||
if(Input::getKeyState(keyUp) == GLFW_PRESS) {
|
|
||||||
direction += glm::vec3(0,1,0);
|
|
||||||
jump = kekData.player->onGround;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Input::getKeyState(keyDown) == GLFW_PRESS) {
|
|
||||||
direction += glm::vec3(0,-1,0);
|
|
||||||
}
|
|
||||||
|
|
||||||
kekData.player->controller->update();
|
kekData.player->controller->update();
|
||||||
|
|
||||||
direction = glm::normalize(direction);
|
|
||||||
if(glm::length2(direction) > 0) {
|
|
||||||
if(kekData.player->noclip) {
|
|
||||||
kekData.activeCamera->translate(direction * KEK_NOCLIP_SPEED * kekData.lastFrameTime);
|
|
||||||
}else {
|
|
||||||
//kekData.player->physics->body->applyCentralImpulse(Physics::fromGLM(glm::normalize(direction) * (kekData.lastFrameTime * 20)));
|
|
||||||
/*btVector3 vel = kekData.player->physics->body->getLinearVelocity();
|
|
||||||
glm::vec3 newVel = glm::normalize(direction) * KEK_PLAYER_WALK_VELOCITY;
|
|
||||||
newVel.y = vel.y();
|
|
||||||
if(jump) newVel.y = KEK_PLAYER_JUMP_VELOCITY;
|
|
||||||
kekData.player->physics->body->setLinearVelocity(Physics::fromGLM(newVel));*/
|
|
||||||
glm::vec3 xz = glm::vec3(direction.x, 0, direction.z);
|
|
||||||
kekData.player->controller->move(xz * (kekData.lastFrameTime * 10));
|
|
||||||
if(jump) kekData.player->controller->jump();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void defaultKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data) {
|
static void defaultKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data) {
|
||||||
@ -157,12 +112,13 @@ static void defaultMouseButtonCallback(GLFWwindow* window, int button, int actio
|
|||||||
}
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W);
|
DefaultPlayerController *controller = new DefaultPlayerController();
|
||||||
keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S);
|
controller->keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W);
|
||||||
keyLeft = Input::createKeyBinding("Left", GLFW_KEY_A);
|
controller->keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S);
|
||||||
keyRight = Input::createKeyBinding("Right", GLFW_KEY_D);
|
controller->keyLeft = Input::createKeyBinding("Left", GLFW_KEY_A);
|
||||||
keyUp = Input::createKeyBinding("Up", GLFW_KEY_SPACE);
|
controller->keyRight = Input::createKeyBinding("Right", GLFW_KEY_D);
|
||||||
keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL);
|
controller->keyJump = Input::createKeyBinding("Up", GLFW_KEY_SPACE);
|
||||||
|
//keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL);
|
||||||
keyOptions = Input::createKeyBinding("Options", GLFW_KEY_Q);
|
keyOptions = Input::createKeyBinding("Options", GLFW_KEY_Q);
|
||||||
keyToggleCursorMode = Input::createKeyBinding("Toggle Cursor Mode", GLFW_KEY_TAB);
|
keyToggleCursorMode = Input::createKeyBinding("Toggle Cursor Mode", GLFW_KEY_TAB);
|
||||||
keyToggleNoclip = Input::createKeyBinding("Toggle Noclip", GLFW_KEY_N);
|
keyToggleNoclip = Input::createKeyBinding("Toggle Noclip", GLFW_KEY_N);
|
||||||
@ -201,8 +157,8 @@ void init() {
|
|||||||
|
|
||||||
kekData.player->physics = new PlayerPhysicsObjectData();
|
kekData.player->physics = new PlayerPhysicsObjectData();
|
||||||
kekData.player->physics->body = body;
|
kekData.player->physics->body = body;
|
||||||
//kekData.player->physics->jumpCollider = jumpCollider;
|
|
||||||
kekData.player->moveTo(glm::vec3(5,10,0));
|
kekData.player->moveTo(glm::vec3(5,10,0));
|
||||||
|
kekData.player->controller = controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy() {
|
void destroy() {
|
||||||
|
@ -86,21 +86,47 @@ glm::vec3 DefaultPlayerController::move(glm::vec3 movement) {
|
|||||||
return totalMovement;
|
return totalMovement;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultPlayerController::jump() {
|
|
||||||
velocity += glm::vec3(0, jumpVelocity, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DefaultPlayerController::update() {
|
void DefaultPlayerController::update() {
|
||||||
bool onGround = checkPlayerGrounded();
|
bool onGround = checkPlayerGrounded();
|
||||||
kekData.player->onGround = onGround;
|
kekData.player->onGround = onGround;
|
||||||
|
|
||||||
move(velocity * kekData.lastFrameTime);
|
|
||||||
|
|
||||||
if(!onGround) {
|
if(!onGround) {
|
||||||
velocity += gravity * kekData.lastFrameTime;
|
velocity += gravity * kekData.lastFrameTime;
|
||||||
}else{
|
}else{
|
||||||
velocity = glm::vec3(0);
|
velocity = glm::vec3(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::vec3 direction = glm::vec3(0);
|
||||||
|
|
||||||
|
if(Input::getKeyState(keyForward) == GLFW_PRESS) {
|
||||||
|
direction += kekData.activeCamera->direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Input::getKeyState(keyBackward) == GLFW_PRESS) {
|
||||||
|
direction += -kekData.activeCamera->direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Input::getKeyState(keyLeft) == GLFW_PRESS) {
|
||||||
|
direction += -glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Input::getKeyState(keyRight) == GLFW_PRESS) {
|
||||||
|
direction += glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Input::getKeyState(keyJump) == GLFW_PRESS && kekData.player->onGround) {
|
||||||
|
velocity += glm::vec3(0, jumpVelocity, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
direction = glm::vec3(direction.x, 0, direction.z);
|
||||||
|
|
||||||
|
glm::vec3 move = velocity;
|
||||||
|
if(glm::length2(direction) > 0) {
|
||||||
|
direction = glm::normalize(direction) * walkSpeed;
|
||||||
|
move += direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
kekData.player->controller->move(move * (kekData.lastFrameTime));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,7 @@
|
|||||||
namespace kek {
|
namespace kek {
|
||||||
|
|
||||||
Player::Player(): RotateableObject() {
|
Player::Player(): RotateableObject() {
|
||||||
this->noclip = false;
|
|
||||||
this->onGround = false;
|
|
||||||
this->controller = new DefaultPlayerController();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::rotateTo(glm::quat rotation) {
|
void Player::rotateTo(glm::quat rotation) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "playercontroller.h"
|
#include "playercontroller.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
namespace kek {
|
namespace kek {
|
||||||
|
|
||||||
@ -13,8 +14,15 @@ public:
|
|||||||
glm::vec3 gravity = glm::vec3(0,-9.81,0);
|
glm::vec3 gravity = glm::vec3(0,-9.81,0);
|
||||||
float minSlideAngle = 5;
|
float minSlideAngle = 5;
|
||||||
float maxWalkAngle = 60;
|
float maxWalkAngle = 60;
|
||||||
float jumpVelocity = 2.5;
|
float jumpVelocity = 3;
|
||||||
float groundDistance = 0.01;
|
float groundDistance = 0.01;
|
||||||
|
float walkSpeed = 6;
|
||||||
|
|
||||||
|
KeyBinding keyForward = KEK_INVALID_ID;
|
||||||
|
KeyBinding keyBackward = KEK_INVALID_ID;
|
||||||
|
KeyBinding keyLeft = KEK_INVALID_ID;
|
||||||
|
KeyBinding keyRight = KEK_INVALID_ID;
|
||||||
|
KeyBinding keyJump = KEK_INVALID_ID;
|
||||||
|
|
||||||
glm::vec3 velocity = glm::vec3(0);
|
glm::vec3 velocity = glm::vec3(0);
|
||||||
|
|
||||||
@ -22,7 +30,6 @@ public:
|
|||||||
virtual ~DefaultPlayerController();
|
virtual ~DefaultPlayerController();
|
||||||
|
|
||||||
virtual glm::vec3 move(glm::vec3 movement);
|
virtual glm::vec3 move(glm::vec3 movement);
|
||||||
virtual void jump();
|
|
||||||
virtual void update();
|
virtual void update();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -10,8 +10,8 @@ class Player: public RotateableObject {
|
|||||||
public:
|
public:
|
||||||
PlayerPhysicsObjectData *physics;
|
PlayerPhysicsObjectData *physics;
|
||||||
PlayerController *controller;
|
PlayerController *controller;
|
||||||
bool noclip;
|
bool noclip = false;
|
||||||
bool onGround;
|
bool onGround = false;
|
||||||
|
|
||||||
Player();
|
Player();
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ public:
|
|||||||
virtual ~PlayerController() = 0;
|
virtual ~PlayerController() = 0;
|
||||||
|
|
||||||
virtual glm::vec3 move(glm::vec3 movement) = 0;
|
virtual glm::vec3 move(glm::vec3 movement) = 0;
|
||||||
virtual void jump() = 0;
|
|
||||||
virtual void update() = 0;
|
virtual void update() = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user