Added primitive character controller - wooohooo!

master
Martin Felis 2016-11-07 21:34:18 +01:00
parent 466e94a939
commit 05df324bab
4 changed files with 139 additions and 52 deletions

View File

@ -29,7 +29,7 @@ struct module_api {
* Called at a regular interval by the main program. * Called at a regular interval by the main program.
* @return true if the program should continue * @return true if the program should continue
*/ */
bool (*step)(struct module_state *state); bool (*step)(struct module_state *state, float dt);
}; };
extern "C" { extern "C" {

View File

@ -85,7 +85,7 @@ void RuntimeModuleManager::Update(float dt) {
for (int i = 0; i < mModules.size(); i++) { for (int i = 0; i < mModules.size(); i++) {
LoadModule(mModules[i]); LoadModule(mModules[i]);
if (mModules[i]->handle) { if (mModules[i]->handle) {
mModules[i]->api.step(mModules[i]->state); mModules[i]->api.step(mModules[i]->state, dt);
} }
} }
} }

View File

@ -83,7 +83,7 @@ static void module_unload(struct module_state *state) {
std::cout << "RenderModule unload called" << std::endl; std::cout << "RenderModule unload called" << std::endl;
} }
static bool module_step(struct module_state *state) { static bool module_step(struct module_state *state, float dt) {
float deltaTime = 0.3; float deltaTime = 0.3;
std::ostringstream s; std::ostringstream s;
s << "RenderModule: 2 Runtime Object 4 " << deltaTime << " update called!"; s << "RenderModule: 2 Runtime Object 4 " << deltaTime << " update called!";

View File

@ -29,10 +29,60 @@ bool fps_camera = true;
// Boilerplate for the module reload stuff // Boilerplate for the module reload stuff
struct CharacterController {
enum ControllerState {
ControlForward = 1,
ControlLeft,
ControlRight,
ControlBack,
ControlJump,
ControlStateLast
};
bool state[ControlStateLast];
void reset() {
for (int i = 0; i < ControlStateLast; i++) {
state[i] = false;
}
}
CharacterController() {
reset();
};
};
struct CharacterEntity { struct CharacterEntity {
/// Render entity /// Render entity
Entity *entity; Entity *entity;
Vector3f position; Vector3f position;
CharacterController controller;
void update(float dt) {
Vector3f local_velocity (Vector3f::Zero());
if (controller.state[CharacterController::ControlForward]) {
local_velocity += Vector3f (1.f, 0.f, 0.f);
}
if (controller.state[CharacterController::ControlBack]) {
local_velocity -= Vector3f (1.f, 0.f, 0.f);
}
if (controller.state[CharacterController::ControlRight]) {
local_velocity += Vector3f (0.f, 0.f, 1.f);
}
if (controller.state[CharacterController::ControlLeft]) {
local_velocity -= Vector3f (0.f, 0.f, 1.f);
}
// integrate position
position += local_velocity * 5 * dt;
// apply transformation
bx::mtxTranslate(entity->transform, position[0], position[1], position[2]);
}
}; };
struct module_state { struct module_state {
@ -103,7 +153,19 @@ void handle_mouse (struct module_state *state) {
active_camera->updateMatrices(); active_camera->updateMatrices();
} }
void handle_keyboard (struct module_state *state) { void handle_keyboard (struct module_state *state, float dt) {
if (!glfwGetWindowAttrib(gWindow, GLFW_FOCUSED)) {
return;
}
if (glfwGetMouseButton(gWindow, 1)) {
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} else {
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
if (glfwGetMouseButton(gWindow, 1)) {
// Right mouse button pressed, move the camera
Camera *active_camera = &gRenderer->cameras[gRenderer->activeCameraIndex]; Camera *active_camera = &gRenderer->cameras[gRenderer->activeCameraIndex];
assert (active_camera != nullptr); assert (active_camera != nullptr);
Matrix44f camera_view_matrix = SimpleMath::Map<Matrix44f>(active_camera->mtxView, 4, 4); Matrix44f camera_view_matrix = SimpleMath::Map<Matrix44f>(active_camera->mtxView, 4, 4);
@ -141,12 +203,44 @@ void handle_keyboard (struct module_state *state) {
direction += Vector3f (0.f, -1.f, 0.f); direction += Vector3f (0.f, -1.f, 0.f);
} }
float step = 0.1f; eye += direction * 5.f * dt;
eye += direction * step; poi += direction * 5.f * dt;
poi += direction * step;
memcpy (active_camera->eye, eye.data(), sizeof(float) * 3); memcpy (active_camera->eye, eye.data(), sizeof(float) * 3);
memcpy (active_camera->poi, poi.data(), sizeof(float) * 3); memcpy (active_camera->poi, poi.data(), sizeof(float) * 3);
} else if (state->character != nullptr) {
// Movement of the character
CharacterController& controller = state->character->controller;
controller.reset();
// Reset the character control state:
if (glfwGetKey(gWindow, GLFW_KEY_W) == GLFW_PRESS) {
controller.state[CharacterController::ControlForward] = true;
}
if (glfwGetKey(gWindow, GLFW_KEY_S) == GLFW_PRESS) {
controller.state[CharacterController::ControlBack] = true;
}
if (glfwGetKey(gWindow, GLFW_KEY_D) == GLFW_PRESS) {
controller.state[CharacterController::ControlRight] = true;
}
if (glfwGetKey(gWindow, GLFW_KEY_A) == GLFW_PRESS) {
controller.state[CharacterController::ControlLeft] = true;
}
if (glfwGetKey(gWindow, GLFW_KEY_SPACE) == GLFW_PRESS) {
controller.state[CharacterController::ControlJump] = true;
}
}
}
void update_character(module_state* state, float dt) {
if (state->character != nullptr) {
state->character->update(dt);
}
} }
static struct module_state *module_init() { static struct module_state *module_init() {
@ -175,6 +269,7 @@ static void module_reload(struct module_state *state) {
cout << "Creating render entity ..." << endl; cout << "Creating render entity ..." << endl;
state->character->entity = gRenderer->createEntity(); state->character->entity = gRenderer->createEntity();
state->character->position = Vector3f (0.f, 0.74f, 0.0f);
cout << "Creating render entity ... success!" << endl; cout << "Creating render entity ... success!" << endl;
cout << "Creating render entity mesh ..." << endl; cout << "Creating render entity mesh ..." << endl;
@ -226,9 +321,6 @@ void ShowModulesWindow(struct module_state *state) {
} }
state->modules_window_selected_index = selected; state->modules_window_selected_index = selected;
// ImGui::NextColumn();
ImGui::Separator(); ImGui::Separator();
RuntimeModule* selected_module = nullptr; RuntimeModule* selected_module = nullptr;
@ -256,7 +348,7 @@ void ShowModulesWindow(struct module_state *state) {
ImGui::End(); ImGui::End();
} }
static bool module_step(struct module_state *state) { static bool module_step(struct module_state *state, float dt) {
if (gRenderer == nullptr) if (gRenderer == nullptr)
return false; return false;
@ -283,14 +375,9 @@ static bool module_step(struct module_state *state) {
ImGui::ShowTestWindow(); ImGui::ShowTestWindow();
} }
float deltaTime = 0.3;
std::ostringstream s;
s << "TestModule: 2 Runtime Object 4 " << deltaTime << " update called!";
handle_mouse(state); handle_mouse(state);
handle_keyboard(state); handle_keyboard(state, dt);
update_character(state, dt);
bgfx::dbgTextPrintf(1, 20, 0x6f, s.str().c_str());
return true; return true;
} }