Move keyboard input to DefaultPlayerController

This commit is contained in:
MrLetsplay 2022-11-16 18:38:37 +01:00
parent 64745491aa
commit 3a38190290
6 changed files with 55 additions and 69 deletions

View File

@ -1,6 +1,7 @@
#include "input.h"
#include "ui.h"
#include "uielements.h"
#include "defaultplayercontroller.h"
#include "internal.h"
#include "internal/ui.h"
#include "internal/physics.h"
@ -8,12 +9,12 @@
namespace kek::Defaults {
static KeyBinding
keyForward,
/*keyForward,
keyBackward,
keyLeft,
keyRight,
keyUp,
keyDown,
keyDown,*/
keyOptions,
keyToggleCursorMode,
keyToggleNoclip,
@ -25,53 +26,7 @@ static void defaultInput(GLFWwindow *window, void *data) {
if(Input::isKeyboardCaptured()) return;
// 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();
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) {
@ -157,12 +112,13 @@ static void defaultMouseButtonCallback(GLFWwindow* window, int button, int actio
}
void init() {
keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W);
keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S);
keyLeft = Input::createKeyBinding("Left", GLFW_KEY_A);
keyRight = Input::createKeyBinding("Right", GLFW_KEY_D);
keyUp = Input::createKeyBinding("Up", GLFW_KEY_SPACE);
keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL);
DefaultPlayerController *controller = new DefaultPlayerController();
controller->keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W);
controller->keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S);
controller->keyLeft = Input::createKeyBinding("Left", GLFW_KEY_A);
controller->keyRight = Input::createKeyBinding("Right", GLFW_KEY_D);
controller->keyJump = Input::createKeyBinding("Up", GLFW_KEY_SPACE);
//keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL);
keyOptions = Input::createKeyBinding("Options", GLFW_KEY_Q);
keyToggleCursorMode = Input::createKeyBinding("Toggle Cursor Mode", GLFW_KEY_TAB);
keyToggleNoclip = Input::createKeyBinding("Toggle Noclip", GLFW_KEY_N);
@ -201,8 +157,8 @@ void init() {
kekData.player->physics = new PlayerPhysicsObjectData();
kekData.player->physics->body = body;
//kekData.player->physics->jumpCollider = jumpCollider;
kekData.player->moveTo(glm::vec3(5,10,0));
kekData.player->controller = controller;
}
void destroy() {

View File

@ -86,21 +86,47 @@ glm::vec3 DefaultPlayerController::move(glm::vec3 movement) {
return totalMovement;
}
void DefaultPlayerController::jump() {
velocity += glm::vec3(0, jumpVelocity, 0);
}
void DefaultPlayerController::update() {
bool onGround = checkPlayerGrounded();
kekData.player->onGround = onGround;
move(velocity * kekData.lastFrameTime);
if(!onGround) {
velocity += gravity * kekData.lastFrameTime;
}else{
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));
}
}

View File

@ -6,9 +6,7 @@
namespace kek {
Player::Player(): RotateableObject() {
this->noclip = false;
this->onGround = false;
this->controller = new DefaultPlayerController();
}
void Player::rotateTo(glm::quat rotation) {

View File

@ -1,6 +1,7 @@
#pragma once
#include "playercontroller.h"
#include "input.h"
namespace kek {
@ -13,8 +14,15 @@ public:
glm::vec3 gravity = glm::vec3(0,-9.81,0);
float minSlideAngle = 5;
float maxWalkAngle = 60;
float jumpVelocity = 2.5;
float jumpVelocity = 3;
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);
@ -22,7 +30,6 @@ public:
virtual ~DefaultPlayerController();
virtual glm::vec3 move(glm::vec3 movement);
virtual void jump();
virtual void update();
};

View File

@ -10,8 +10,8 @@ class Player: public RotateableObject {
public:
PlayerPhysicsObjectData *physics;
PlayerController *controller;
bool noclip;
bool onGround;
bool noclip = false;
bool onGround = false;
Player();

View File

@ -10,7 +10,6 @@ public:
virtual ~PlayerController() = 0;
virtual glm::vec3 move(glm::vec3 movement) = 0;
virtual void jump() = 0;
virtual void update() = 0;
};