You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.0 KiB
88 lines
2.0 KiB
|
|
#include <glm/geometric.hpp> |
|
#include "player.h" |
|
#include "frametime.h" |
|
|
|
namespace player { |
|
|
|
/* player's camera */ |
|
|
|
std::shared_ptr<camera> player_camera = std::shared_ptr<camera>(new camera { |
|
glm::vec3(0.0f, 0.0f, 3.0f), |
|
glm::vec3(0.0f, 0.0f, -1.0f), |
|
glm::vec3(0.0f, 1.0f, 0.0f), |
|
glm::vec3(0.f,0.f,0.f) |
|
}); |
|
|
|
|
|
std::shared_ptr<camera> get_camera() { |
|
return player_camera; |
|
} |
|
|
|
|
|
void camera::update() { |
|
if (pitch > 89.0f) |
|
pitch = 89.0f; |
|
if (pitch < -89.0f) |
|
pitch = -89.0f; |
|
|
|
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); |
|
direction.y = sin(glm::radians(pitch)); |
|
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); |
|
front = glm::normalize(direction); |
|
view = glm::lookAt(pos, pos + front, up); |
|
} |
|
|
|
void camera::set_camera(Shader *shader) { |
|
glUniform3f(glGetUniformLocation(shader->program, "cameraPosition"), pos.x, pos.y, pos.z); |
|
} |
|
|
|
|
|
|
|
/* player */ |
|
|
|
std::shared_ptr<player> player_data = std::shared_ptr<player>(new player {}); |
|
|
|
player::player() { |
|
move_mode = movement::WALKING; |
|
cam = get_camera(); |
|
} |
|
|
|
|
|
std::shared_ptr<player> get_player() { |
|
return player_data; |
|
} |
|
|
|
void player::update() { |
|
physics.pos += framestep * physics.velocity; |
|
cam->pos = physics.pos; |
|
} |
|
|
|
/* |
|
* Move in direction relative to player. |
|
*/ |
|
void player::move(float velocity, glm::vec3 direction) { |
|
|
|
glm::vec3 amount {}; |
|
|
|
auto left_direction = glm::cross(cam->front, cam->up); |
|
|
|
amount += direction.x * left_direction; |
|
amount += direction.y * cam->up; |
|
amount += direction.z * cam->front; |
|
|
|
amount *= velocity; |
|
|
|
if (move_mode == movement::WALKING) { |
|
amount *= glm::vec3(1,0,1); |
|
} |
|
|
|
fmt::print("Player {} {} {}\n", physics.pos.x, physics.pos.y, physics.pos.z); |
|
|
|
physics.velocity = amount; |
|
|
|
|
|
} |
|
|
|
|
|
};
|
|
|