Skip to content

Commit

Permalink
Player control
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyJazz committed Dec 30, 2023
1 parent 0452ac5 commit e8febc0
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/GameBall/core/game_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ void GameBall::OnInit() {
glm::vec3{0.0f, -10.0f, 0.0f}, std::numeric_limits<float>::infinity(),
false, 20.0f);

primary_unit->SetMotion(glm::vec3{0.0f, 1.0f, 0.0f}, glm::vec3{0.0f},
glm::mat3{1.0f}, glm::vec3{0.0f, 0.0f, 1.0f});
primary_unit->SetMotion(glm::vec3{0.0f, 1.0f, 0.0f},
glm::vec3{0.0f, 10.0f, 0.0f}, glm::mat3{1.0f},
glm::vec3{0.0f, 0.0f, 1.0f});

primary_player_id_ = primary_player->PlayerId();

Expand All @@ -60,6 +61,9 @@ void GameBall::OnInit() {
camera_controller_ =
std::make_unique<CameraControllerThirdPerson>(camera_.get(), aspect);

player_input_controller_ =
std::make_unique<Logic::PlayerInputController>(this);

logic_manager_->Start();
}

Expand All @@ -86,6 +90,8 @@ void GameBall::OnUpdate() {
.count();
last_time = current_time;

auto player_input = player_input_controller_->GetInput();

{
std::lock_guard<std::mutex> lock(logic_manager_->logic_mutex_);
logic_manager_->world_->SyncWorldState(this);
Expand All @@ -97,6 +103,7 @@ void GameBall::OnUpdate() {
if (primary_unit) {
primary_player_primary_unit_object_id_ = primary_unit->ObjectId();
}
primary_player->SetPlayerInput(player_input);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/GameBall/core/game_ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ class GameBall : public GameX::Base::Application {

void CursorPosCallback(double xpos, double ypos) override;

CameraControllerThirdPerson *CameraController() {
return camera_controller_.get();
}

private:
friend class Logic::Manager;
friend class Logic::World;

GameX::Graphics::UScene scene_;
GameX::Graphics::UFilm film_;
uint64_t synced_world_version_ = 0;
uint64_t synced_world_version_{0};
std::unique_ptr<Logic::Manager> logic_manager_;
std::unique_ptr<class AssetManager> asset_manager_;
std::map<uint64_t, Actor *> actors_;
Expand All @@ -69,5 +73,7 @@ class GameBall : public GameX::Base::Application {
uint64_t primary_player_primary_unit_object_id_{0};

bool ignore_next_mouse_move_{true};

std::unique_ptr<Logic::PlayerInputController> player_input_controller_;
};
} // namespace GameBall
2 changes: 1 addition & 1 deletion src/GameBall/logic/obstacles/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Block::Block(World *world,
SetMass(mass_);
SetSideLength(side_length_);
SetMotion(position_, velocity_, orientation_, L_);
cube.elasticity = 0.0f;
cube.elasticity = 0.25f;
cube.friction = 0.5f;
}

Expand Down
14 changes: 14 additions & 0 deletions src/GameBall/logic/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ uint64_t Player::PrimaryUnitId() const {
void Player::SetPrimaryUnit(uint64_t unit_id) {
primary_unit_id_ = unit_id;
}

void Player::SetPlayerInput(const PlayerInput &input) {
input_ = input;
}

PlayerInput Player::GetPlayerInput() const {
return input_;
}

PlayerInput Player::TakePlayerInput() {
auto input = input_;
input_ = {};
return input;
}
} // namespace GameBall::Logic
9 changes: 9 additions & 0 deletions src/GameBall/logic/player.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "GameBall/core/utils.h"
#include "GameBall/logic/player_input.h"

namespace GameBall::Logic {
class World;
Expand All @@ -13,10 +14,18 @@ class Player {

void SetPrimaryUnit(uint64_t unit_id);

void SetPlayerInput(const PlayerInput &input);

[[nodiscard]] PlayerInput GetPlayerInput() const;

PlayerInput TakePlayerInput();

private:
World *world_;
uint64_t player_id_{};

uint64_t primary_unit_id_{};

PlayerInput input_{};
};
} // namespace GameBall::Logic
26 changes: 25 additions & 1 deletion src/GameBall/logic/player_input.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
#include "GameBall/logic/player_input.h"

namespace GameBall::Logic {}
#include "GameBall/core/game_ball.h"

namespace GameBall::Logic {
PlayerInputController::PlayerInputController(GameBall *app) : app_(app) {
}

PlayerInput PlayerInputController::GetInput() {
auto window = app_->Window();
input_.move_forward = (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS);
input_.move_backward = (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS);
input_.move_left = (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS);
input_.move_right = (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS);
input_.brake = (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS);
auto camera_controller = app_->CameraController();
auto pitch_yaw = camera_controller->GetPitchYaw();
auto pitch = pitch_yaw.x;
auto yaw = pitch_yaw.y;
input_.orientation = {glm::sin(glm::radians(yaw)), 0.0f,
-glm::cos(glm::radians(yaw))};
auto result = input_;
input_ = {};
return result;
}

} // namespace GameBall::Logic
16 changes: 16 additions & 0 deletions src/GameBall/logic/player_input.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#pragma once
#include "GameBall/core/utils.h"

namespace GameBall {
class GameBall;
}

namespace GameBall::Logic {
struct PlayerInput {
bool move_forward{false};
bool move_backward{false};
bool move_left{false};
bool move_right{false};
bool brake{false};
glm::vec3 orientation{0.0f, 0.0f, 1.0f};
};

class PlayerInputController {
public:
PlayerInputController(GameBall *app);
PlayerInput GetInput();

private:
GameBall *app_;
PlayerInput input_{};
};

} // namespace GameBall::Logic
46 changes: 44 additions & 2 deletions src/GameBall/logic/units/regular_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ RegularBall::RegularBall(World *world,
sphere.orientation = orientation_;
sphere.velocity = velocity_;
sphere.angular_velocity = glm::vec3{0.0f};
sphere.elasticity = 0.0f;
sphere.friction = 0.5f;
sphere.elasticity = 1.0f;
sphere.friction = 10.0f;
sphere.gravity = glm::vec3{0.0f, -9.8f, 0.0f};
}

Expand All @@ -41,8 +41,50 @@ SYNC_ACTOR_FUNC(RegularBall) {
}

void RegularBall::UpdateTick() {
float delta_time = world_->TickDeltaT();
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);

auto owner = world_->GetPlayer(player_id_);
if (owner) {
if (UnitId() == owner->PrimaryUnitId()) {
auto input = owner->TakePlayerInput();

glm::vec3 forward = glm::normalize(glm::vec3{input.orientation});
glm::vec3 right =
glm::normalize(glm::cross(forward, glm::vec3{0.0f, 1.0f, 0.0f}));

glm::vec3 moving_direction{};

float angular_acceleration = glm::radians(2880.0f);

if (input.move_forward) {
moving_direction -= right;
}
if (input.move_backward) {
moving_direction += right;
}
if (input.move_left) {
sphere.angular_velocity -= forward;
}
if (input.move_right) {
sphere.angular_velocity += forward;
}

if (glm::length(moving_direction) > 0.0f) {
moving_direction = glm::normalize(moving_direction);
sphere.angular_velocity +=
moving_direction * angular_acceleration * delta_time;
}

if (input.brake) {
sphere.angular_velocity = glm::vec3{0.0f};
}
}
}
sphere.velocity *= std::pow(0.5f, delta_time);
sphere.angular_velocity *= std::pow(0.2f, delta_time);

position_ = sphere.position;
velocity_ = sphere.velocity;
orientation_ = sphere.orientation;
Expand Down

0 comments on commit e8febc0

Please sign in to comment.