From c500f8ca703f62a2b3ad668999ff1a65d88f5490 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Fri, 12 Jan 2024 12:27:03 +0800 Subject: [PATCH 01/20] Tentatively enable the annotated code. --- src/GameBall/logic/units/regular_ball.cpp | 74 +++++++++++------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index af1cc95..89a4736 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -45,43 +45,43 @@ void RegularBall::UpdateTick() { 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) { - // moving_direction -= forward; - // } - // if (input.move_right) { - // moving_direction += 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}; - // } - // } - // } + 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) { + moving_direction -= forward; + } + if (input.move_right) { + moving_direction += 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); From 94d88eecef94000316d54159067a4877261e4551 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Fri, 12 Jan 2024 12:29:20 +0800 Subject: [PATCH 02/20] Tentatively enable the annotated code and attempt to test it. --- src/GameBall/logic/units/regular_ball.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 89a4736..5b9edf7 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -44,7 +44,7 @@ void RegularBall::UpdateTick() { float delta_time = world_->TickDeltaT(); auto physics_world = world_->PhysicsWorld(); auto &sphere = physics_world->GetSphere(sphere_id_); - +//changed here auto owner = world_->GetPlayer(player_id_); if (owner) { if (UnitId() == owner->PrimaryUnitId()) { From eba728ee96a79598a443baf0d850fac494ba726b Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Fri, 12 Jan 2024 13:13:56 +0800 Subject: [PATCH 03/20] Update regular_ball.cpp --- src/GameBall/logic/units/regular_ball.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 5b9edf7..05a5238 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -44,7 +44,7 @@ void RegularBall::UpdateTick() { float delta_time = world_->TickDeltaT(); auto physics_world = world_->PhysicsWorld(); auto &sphere = physics_world->GetSphere(sphere_id_); -//changed here +//changed from here to line 85 auto owner = world_->GetPlayer(player_id_); if (owner) { if (UnitId() == owner->PrimaryUnitId()) { From 9cc1a3fd258cc6832194e1c3333835bdfdf5addb Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Mon, 15 Jan 2024 13:26:51 +0800 Subject: [PATCH 04/20] Add submission requirements. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index dc26f19..e0e616b 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ # GameX +你需要在网络学堂中提交一个文件,txt、doc、docx、pdf格式均可。 提交的内容根据你的完成情况而不同: + +如果你完成到创建分叉,那么你需要提交你的分叉仓库的地址。 +如果你完成到实现输入控制,那么你需要提交你的分叉仓库的地址和你的分支的名称。 +如果你完成到创建合并请求以及关闭 issue,那么你需要提交你的合并请求的地址。 +如果你完成了一个自由发挥的任务,那么你需要提交你自由发挥的内容,被采纳合并的合并请求的地址。 +只要你完成了一个任务,你就可以只提交这个任务的内容,不需要提交之前的任务的内容。 From 8ad17df3d7cc7fcafbc8c9c54c5a6fea165d212c Mon Sep 17 00:00:00 2001 From: chenzunh23 Date: Tue, 16 Jan 2024 17:40:09 +0800 Subject: [PATCH 05/20] implement some basic keyboard control, enabling easy exits, two types of halting, and ball activities such as jumping, mass adjustments, etc. --- src/GameBall/logic/player_input.cpp | 21 ++++ src/GameBall/logic/player_input.h | 17 +++ src/GameBall/logic/units/regular_ball.cpp | 121 +++++++++++++++------- 3 files changed, 122 insertions(+), 37 deletions(-) diff --git a/src/GameBall/logic/player_input.cpp b/src/GameBall/logic/player_input.cpp index c002a91..90ad379 100644 --- a/src/GameBall/logic/player_input.cpp +++ b/src/GameBall/logic/player_input.cpp @@ -13,6 +13,27 @@ PlayerInput PlayerInputController::GetInput() { 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); + input_.v_jump = (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS); + input_.low = (glfwGetKey(window, GLFW_KEY_0) == GLFW_PRESS); + input_.high = (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS); + input_.return_if_too_light = + (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS && + count_chances < 2); // 2 chances of returning to the base + if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) + R_pressed_now = R_pressed_before = true; + if (glfwGetKey(window, GLFW_KEY_R) == GLFW_RELEASE && R_pressed_before && + R_pressed_now) { + R_pressed_now = false; + count_chances++; + } + input_.halt = (glfwGetKey(window, GLFW_KEY_H) == GLFW_PRESS); + if (glfwGetKey(window, GLFW_KEY_T) == GLFW_PRESS) + T_pressed_now = true; + if (glfwGetKey(window, GLFW_KEY_T) == GLFW_RELEASE && T_pressed_now) + T_pressed_now = !T_pressed_now, T_pressed_before = !T_pressed_before; + input_.restart_halt = T_pressed_before; + input_.end_if_too_heavy = (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS); + input_.end = (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS); auto camera_controller = app_->CameraController(); auto pitch_yaw = camera_controller->GetPitchYaw(); auto pitch = pitch_yaw.x; diff --git a/src/GameBall/logic/player_input.h b/src/GameBall/logic/player_input.h index 98b60c0..af40d49 100644 --- a/src/GameBall/logic/player_input.h +++ b/src/GameBall/logic/player_input.h @@ -6,12 +6,24 @@ 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}; + bool v_jump{false}; // make the sphere jump if it is on the cube + bool low{false}; // lower the mass by 10% once called + bool high{false}; // increase the mass by 10% at the supply base(0,1,0) + bool return_if_too_light{ + false}; // return to the supply base and recover normal mass if called + bool halt{false}; // Stop the game and the balls will make no response until + // halting ends + bool restart_halt{false}; // Halt by pressing T once, and restart the game by + // pressing T again + bool end_if_too_heavy{false}; // end the game if the ball is too heavy + bool end{false}; // quit immediately glm::vec3 orientation{0.0f, 0.0f, 1.0f}; }; @@ -23,6 +35,11 @@ class PlayerInputController { private: GameBall *app_; PlayerInput input_{}; + bool R_pressed_before = false; // indicate whether R has been pressed before + bool R_pressed_now = false; // indicate whether R is being pressed + bool T_pressed_before = false; + bool T_pressed_now = false; + int count_chances = 0, count_halt = 0; }; } // namespace GameBall::Logic diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 5b9edf7..78e3c85 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -1,8 +1,9 @@ #include "GameBall/logic/units/regular_ball.h" +#include + #include "GameBall/core/game_ball.h" #include "GameBall/logic/world.h" - namespace GameBall::Logic::Units { RegularBall::RegularBall(World *world, uint64_t player_id, @@ -44,45 +45,91 @@ void RegularBall::UpdateTick() { float delta_time = world_->TickDeltaT(); auto physics_world = world_->PhysicsWorld(); auto &sphere = physics_world->GetSphere(sphere_id_); -//changed here + // changed here 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) { - moving_direction -= forward; - } - if (input.move_right) { - moving_direction += forward; + 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.restart_halt) { + if (input.move_forward) { + moving_direction -= right; + } + if (input.move_backward) { + moving_direction += right; + } + if (input.move_left) { + moving_direction -= forward; + } + if (input.move_right) { + moving_direction += 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 && !input.v_jump) { + sphere.angular_velocity = glm::vec3{0.0f}; + } + + if (input.low) { + sphere.mass *= 0.9f; + } + + if (input.high && std::abs(sphere.position.x) < 1.0f && + std::abs(sphere.position.z) < 1.0f && + std::abs(sphere.position.y - 1.0f) < 0.01f) { + sphere.mass *= 1.1f; + } + if (input.v_jump) { + if (sphere.position.y < 1.01f && sphere.position.y > 0.99f) + sphere.velocity.y = 10.0f; + } + if (input.return_if_too_light) + if (sphere.mass <= 0.50f) { + sphere.position.y = 1.00f; + sphere.position.x = 0.00f; + sphere.position.z = 0.00f; + sphere.mass = 1.0f; + } + if (input.end_if_too_heavy && sphere.mass >= 2.0f) { + exit(0); + } + if (input.halt) { + std::cout << "Enter a 4-digit password:\n"; + std::string password, enter; + int length = 4, i = 0; + char p; + while (i < length) { + password += getch(); + i++; + } + std::cout << "Successfully memorized password. Please enter it again " + "to return:\n"; + while (1) { + std::cin >> enter; + if (enter == password) { + input.halt = false; + break; + } + std::cout << "enter password again:"; + } + } + } + if (input.end) + exit(0); + } } - - 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); From b986b42c553322fc128ee7eebf544f775514d0b1 Mon Sep 17 00:00:00 2001 From: chenzunh23 Date: Tue, 16 Jan 2024 17:55:28 +0800 Subject: [PATCH 06/20] Why is conio.h rejected by Clang Debug? --- src/GameBall/logic/units/regular_ball.cpp | 383 +++++++++++----------- src/GameX/utils/common.h | 2 +- 2 files changed, 191 insertions(+), 194 deletions(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 28a73a8..2c3fa5f 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -1,193 +1,190 @@ -#include "GameBall/logic/units/regular_ball.h" - -#include - -#include "GameBall/core/game_ball.h" -#include "GameBall/logic/world.h" -namespace GameBall::Logic::Units { -RegularBall::RegularBall(World *world, - uint64_t player_id, - const glm::vec3 &position, - float radius, - float mass) - : Unit(world, player_id) { - radius_ = radius; - mass_ = mass; - position_ = position; - auto physics_world = world_->PhysicsWorld(); - sphere_id_ = physics_world->CreateSphere(); - auto &sphere = physics_world->GetSphere(sphere_id_); - sphere.position = position_; - sphere.SetRadiusMass(radius_, mass_); - sphere.orientation = orientation_; - sphere.velocity = velocity_; - sphere.angular_velocity = glm::vec3{0.0f}; - sphere.elasticity = 1.0f; - sphere.friction = 10.0f; - sphere.gravity = glm::vec3{0.0f, -9.8f, 0.0f}; -} - -RegularBall::~RegularBall() { - ; -} - -SYNC_ACTOR_FUNC(RegularBall) { - auto physics_world = world_->PhysicsWorld(); - auto &sphere = physics_world->GetSphere(sphere_id_); - actor->SetMass(1.0f); - actor->SetGravity(glm::vec3{0.0f, -9.8f, 0.0f}); - actor->SetTransform(glm::mat3{radius_}); - actor->SetMotion(position_, velocity_, orientation_, augular_momentum_); - actor->SetMomentOfInertia(sphere.inertia[0][0]); -} - -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.restart_halt) { - if (input.move_forward) { - moving_direction -= right; - } - if (input.move_backward) { - moving_direction += right; - } - if (input.move_left) { - moving_direction -= forward; - } - if (input.move_right) { - moving_direction += 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 && !input.v_jump) { - sphere.angular_velocity = glm::vec3{0.0f}; - } - - if (input.low) { - sphere.mass *= 0.9f; - } - - if (input.high && std::abs(sphere.position.x) < 1.0f && - std::abs(sphere.position.z) < 1.0f && - std::abs(sphere.position.y - 1.0f) < 0.01f) { - sphere.mass *= 1.1f; - } - if (input.v_jump) { - if (sphere.position.y < 1.01f && sphere.position.y > 0.99f) - sphere.velocity.y = 10.0f; - } - if (input.return_if_too_light) - if (sphere.mass <= 0.50f) { - sphere.position.y = 1.00f; - sphere.position.x = 0.00f; - sphere.position.z = 0.00f; - sphere.mass = 1.0f; - } - if (input.end_if_too_heavy && sphere.mass >= 2.0f) { - exit(0); - } - if (input.halt) { - std::cout << "Enter a 4-digit password:\n"; - std::string password, enter; - int length = 4, i = 0; - char p; - while (i < length) { - password += getch(); - i++; - } - std::cout << "Successfully memorized password. Please enter it again " - "to return:\n"; - while (1) { - std::cin >> enter; - if (enter == password) { - input.halt = false; - break; - } - std::cout << "enter password again:"; - } - } - } - if (input.end) - exit(0); - } - } - 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; - augular_momentum_ = sphere.inertia * sphere.angular_velocity; -} - -void RegularBall::SetMass(float mass) { - auto physics_world = world_->PhysicsWorld(); - auto &sphere = physics_world->GetSphere(sphere_id_); - sphere.SetRadiusMass(radius_, mass); - mass_ = mass; -} - -void RegularBall::SetGravity(const glm::vec3 &gravity) { - auto physics_world = world_->PhysicsWorld(); - auto &sphere = physics_world->GetSphere(sphere_id_); - sphere.gravity = gravity; -} - -void RegularBall::SetRadius(float radius) { - auto physics_world = world_->PhysicsWorld(); - auto &sphere = physics_world->GetSphere(sphere_id_); - sphere.SetRadiusMass(radius, mass_); - radius_ = radius; -} - -void RegularBall::SetMotion(const glm::vec3 &position, - const glm::vec3 &velocity, - const glm::mat3 &orientation, - const glm::vec3 &angular_momentum) { - auto physics_world = world_->PhysicsWorld(); - auto &sphere = physics_world->GetSphere(sphere_id_); - sphere.position = position; - sphere.velocity = velocity; - sphere.orientation = orientation; - sphere.angular_velocity = sphere.inertia_inv * angular_momentum; - position_ = position; - velocity_ = velocity; - orientation_ = orientation; - augular_momentum_ = angular_momentum; -} - -glm::vec3 RegularBall::Position() const { - return position_; -} - -glm::vec3 RegularBall::Velocity() const { - return velocity_; -} - -glm::mat3 RegularBall::Orientation() const { - return orientation_; -} - -glm::vec3 RegularBall::AngularMomentum() const { - return augular_momentum_; -} - -} // namespace GameBall::Logic::Units +#include "GameBall/logic/units/regular_ball.h" +#include "GameBall/core/game_ball.h" +#include "GameBall/logic/world.h" +namespace GameBall::Logic::Units { +RegularBall::RegularBall(World *world, + uint64_t player_id, + const glm::vec3 &position, + float radius, + float mass) + : Unit(world, player_id) { + radius_ = radius; + mass_ = mass; + position_ = position; + auto physics_world = world_->PhysicsWorld(); + sphere_id_ = physics_world->CreateSphere(); + auto &sphere = physics_world->GetSphere(sphere_id_); + sphere.position = position_; + sphere.SetRadiusMass(radius_, mass_); + sphere.orientation = orientation_; + sphere.velocity = velocity_; + sphere.angular_velocity = glm::vec3{0.0f}; + sphere.elasticity = 1.0f; + sphere.friction = 10.0f; + sphere.gravity = glm::vec3{0.0f, -9.8f, 0.0f}; +} + +RegularBall::~RegularBall() { + ; +} + +SYNC_ACTOR_FUNC(RegularBall) { + auto physics_world = world_->PhysicsWorld(); + auto &sphere = physics_world->GetSphere(sphere_id_); + actor->SetMass(1.0f); + actor->SetGravity(glm::vec3{0.0f, -9.8f, 0.0f}); + actor->SetTransform(glm::mat3{radius_}); + actor->SetMotion(position_, velocity_, orientation_, augular_momentum_); + actor->SetMomentOfInertia(sphere.inertia[0][0]); +} + +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.restart_halt) { + if (input.move_forward) { + moving_direction -= right; + } + if (input.move_backward) { + moving_direction += right; + } + if (input.move_left) { + moving_direction -= forward; + } + if (input.move_right) { + moving_direction += 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 && !input.v_jump) { + sphere.angular_velocity = glm::vec3{0.0f}; + } + + if (input.low) { + sphere.mass *= 0.9f; + } + + if (input.high && std::abs(sphere.position.x) < 1.0f && + std::abs(sphere.position.z) < 1.0f && + std::abs(sphere.position.y - 1.0f) < 0.01f) { + sphere.mass *= 1.1f; + } + if (input.v_jump) { + if (sphere.position.y < 1.01f && sphere.position.y > 0.99f) + sphere.velocity.y = 10.0f; + } + if (input.return_if_too_light) + if (sphere.mass <= 0.50f) { + sphere.position.y = 1.00f; + sphere.position.x = 0.00f; + sphere.position.z = 0.00f; + sphere.mass = 1.0f; + } + if (input.end_if_too_heavy && sphere.mass >= 2.0f) { + exit(0); + } + if (input.halt) { + std::cout << "Enter a 4-digit password:\n"; + std::string password, enter; + int length = 4, i = 0; + char p; + while (i < length) { + password += getch(); + i++; + } + std::cout << "Successfully memorized password. Please enter it again " + "to return:\n"; + while (1) { + std::cin >> enter; + if (enter == password) { + input.halt = false; + break; + } + std::cout << "enter password again:"; + } + } + } + if (input.end) + exit(0); + } + } + 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; + augular_momentum_ = sphere.inertia * sphere.angular_velocity; +} + +void RegularBall::SetMass(float mass) { + auto physics_world = world_->PhysicsWorld(); + auto &sphere = physics_world->GetSphere(sphere_id_); + sphere.SetRadiusMass(radius_, mass); + mass_ = mass; +} + +void RegularBall::SetGravity(const glm::vec3 &gravity) { + auto physics_world = world_->PhysicsWorld(); + auto &sphere = physics_world->GetSphere(sphere_id_); + sphere.gravity = gravity; +} + +void RegularBall::SetRadius(float radius) { + auto physics_world = world_->PhysicsWorld(); + auto &sphere = physics_world->GetSphere(sphere_id_); + sphere.SetRadiusMass(radius, mass_); + radius_ = radius; +} + +void RegularBall::SetMotion(const glm::vec3 &position, + const glm::vec3 &velocity, + const glm::mat3 &orientation, + const glm::vec3 &angular_momentum) { + auto physics_world = world_->PhysicsWorld(); + auto &sphere = physics_world->GetSphere(sphere_id_); + sphere.position = position; + sphere.velocity = velocity; + sphere.orientation = orientation; + sphere.angular_velocity = sphere.inertia_inv * angular_momentum; + position_ = position; + velocity_ = velocity; + orientation_ = orientation; + augular_momentum_ = angular_momentum; +} + +glm::vec3 RegularBall::Position() const { + return position_; +} + +glm::vec3 RegularBall::Velocity() const { + return velocity_; +} + +glm::mat3 RegularBall::Orientation() const { + return orientation_; +} + +glm::vec3 RegularBall::AngularMomentum() const { + return augular_momentum_; +} + +} // namespace GameBall::Logic::Units diff --git a/src/GameX/utils/common.h b/src/GameX/utils/common.h index 7461dd0..44ef6f3 100644 --- a/src/GameX/utils/common.h +++ b/src/GameX/utils/common.h @@ -1,6 +1,6 @@ #pragma once #include - +#include #include #define GAMEX_DECLARE_CLASS(ClassName) \ From 09b70670453e6681fbf450eaf895470e0f45332c Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Tue, 16 Jan 2024 18:12:46 +0800 Subject: [PATCH 07/20] Replace conio.h with curses.h --- src/GameX/utils/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GameX/utils/common.h b/src/GameX/utils/common.h index 44ef6f3..cc9f59e 100644 --- a/src/GameX/utils/common.h +++ b/src/GameX/utils/common.h @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include #define GAMEX_DECLARE_CLASS(ClassName) \ From 19b6131a434f3f1298863fa81dcc36ae17516f6e Mon Sep 17 00:00:00 2001 From: chenzunh23 Date: Tue, 16 Jan 2024 18:47:21 +0800 Subject: [PATCH 08/20] Abandon getch() and conio.h --- src/GameBall/logic/units/regular_ball.cpp | 25 +++++++---------------- src/GameX/utils/common.h | 2 -- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 2c3fa5f..5162bbd 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -102,25 +102,14 @@ void RegularBall::UpdateTick() { exit(0); } if (input.halt) { - std::cout << "Enter a 4-digit password:\n"; - std::string password, enter; - int length = 4, i = 0; - char p; - while (i < length) { - password += getch(); - i++; - } - std::cout << "Successfully memorized password. Please enter it again " - "to return:\n"; - while (1) { - std::cin >> enter; - if (enter == password) { - input.halt = false; - break; - } - std::cout << "enter password again:"; - } + std::cout << "Halting\n"; + std::cout << "Press Enter to restart the game:"; + getchar(); } + } else { + glm::vec3 zero_v{0.0f}; + sphere.velocity = zero_v; + sphere.angular_velocity *= 0; } if (input.end) exit(0); diff --git a/src/GameX/utils/common.h b/src/GameX/utils/common.h index 44ef6f3..362dbae 100644 --- a/src/GameX/utils/common.h +++ b/src/GameX/utils/common.h @@ -1,8 +1,6 @@ #pragma once #include -#include #include - #define GAMEX_DECLARE_CLASS(ClassName) \ class ClassName; \ typedef std::unique_ptr U##ClassName; \ From 74605803ebb9986a70174395ef69f0a3e3538f67 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:15:49 +0800 Subject: [PATCH 09/20] Update README.md --- README.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/README.md b/README.md index e0e616b..8b13789 100644 --- a/README.md +++ b/README.md @@ -1,8 +1 @@ -# GameX -你需要在网络学堂中提交一个文件,txt、doc、docx、pdf格式均可。 提交的内容根据你的完成情况而不同: -如果你完成到创建分叉,那么你需要提交你的分叉仓库的地址。 -如果你完成到实现输入控制,那么你需要提交你的分叉仓库的地址和你的分支的名称。 -如果你完成到创建合并请求以及关闭 issue,那么你需要提交你的合并请求的地址。 -如果你完成了一个自由发挥的任务,那么你需要提交你自由发挥的内容,被采纳合并的合并请求的地址。 -只要你完成了一个任务,你就可以只提交这个任务的内容,不需要提交之前的任务的内容。 From 3f5e89588aeee6f6fce4e6206a338bed4b70b4cd Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:16:42 +0800 Subject: [PATCH 10/20] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b13789..dc26f19 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ - +# GameX From 5380e487f9c45773b0b1472298b897b14aefcaf6 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:21:21 +0800 Subject: [PATCH 11/20] Update regular_ball.cpp to clean up main branch --- src/GameBall/logic/units/regular_ball.cpp | 69 +++++++---------------- 1 file changed, 19 insertions(+), 50 deletions(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 5162bbd..31c9dc4 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -1,6 +1,8 @@ #include "GameBall/logic/units/regular_ball.h" + #include "GameBall/core/game_ball.h" #include "GameBall/logic/world.h" + namespace GameBall::Logic::Units { RegularBall::RegularBall(World *world, uint64_t player_id, @@ -42,19 +44,21 @@ 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.restart_halt) { + 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}));l + + glm::vec3 moving_direction{}; + + float angular_acceleration = glm::radians(2880.0f); + if (input.move_forward) { moving_direction -= right; } @@ -67,54 +71,19 @@ void RegularBall::UpdateTick() { if (input.move_right) { moving_direction += 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 && !input.v_jump) { + + if (input.brake) { sphere.angular_velocity = glm::vec3{0.0f}; } - - if (input.low) { - sphere.mass *= 0.9f; - } - - if (input.high && std::abs(sphere.position.x) < 1.0f && - std::abs(sphere.position.z) < 1.0f && - std::abs(sphere.position.y - 1.0f) < 0.01f) { - sphere.mass *= 1.1f; - } - if (input.v_jump) { - if (sphere.position.y < 1.01f && sphere.position.y > 0.99f) - sphere.velocity.y = 10.0f; - } - if (input.return_if_too_light) - if (sphere.mass <= 0.50f) { - sphere.position.y = 1.00f; - sphere.position.x = 0.00f; - sphere.position.z = 0.00f; - sphere.mass = 1.0f; - } - if (input.end_if_too_heavy && sphere.mass >= 2.0f) { - exit(0); - } - if (input.halt) { - std::cout << "Halting\n"; - std::cout << "Press Enter to restart the game:"; - getchar(); - } - } else { - glm::vec3 zero_v{0.0f}; - sphere.velocity = zero_v; - sphere.angular_velocity *= 0; } - if (input.end) - exit(0); } - } + sphere.velocity *= std::pow(0.5f, delta_time); sphere.angular_velocity *= std::pow(0.2f, delta_time); From 068f92847cbea04bfd99004ebcb380a4cd2b224c Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:22:10 +0800 Subject: [PATCH 12/20] Update player_input.cpp --- src/GameBall/logic/player_input.cpp | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/GameBall/logic/player_input.cpp b/src/GameBall/logic/player_input.cpp index 90ad379..c002a91 100644 --- a/src/GameBall/logic/player_input.cpp +++ b/src/GameBall/logic/player_input.cpp @@ -13,27 +13,6 @@ PlayerInput PlayerInputController::GetInput() { 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); - input_.v_jump = (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS); - input_.low = (glfwGetKey(window, GLFW_KEY_0) == GLFW_PRESS); - input_.high = (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS); - input_.return_if_too_light = - (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS && - count_chances < 2); // 2 chances of returning to the base - if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) - R_pressed_now = R_pressed_before = true; - if (glfwGetKey(window, GLFW_KEY_R) == GLFW_RELEASE && R_pressed_before && - R_pressed_now) { - R_pressed_now = false; - count_chances++; - } - input_.halt = (glfwGetKey(window, GLFW_KEY_H) == GLFW_PRESS); - if (glfwGetKey(window, GLFW_KEY_T) == GLFW_PRESS) - T_pressed_now = true; - if (glfwGetKey(window, GLFW_KEY_T) == GLFW_RELEASE && T_pressed_now) - T_pressed_now = !T_pressed_now, T_pressed_before = !T_pressed_before; - input_.restart_halt = T_pressed_before; - input_.end_if_too_heavy = (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS); - input_.end = (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS); auto camera_controller = app_->CameraController(); auto pitch_yaw = camera_controller->GetPitchYaw(); auto pitch = pitch_yaw.x; From a871d6b6100ca35d47e97d1caf16aef899d28875 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:22:51 +0800 Subject: [PATCH 13/20] Update player_input.h to clean up main branch. --- src/GameBall/logic/player_input.h | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/GameBall/logic/player_input.h b/src/GameBall/logic/player_input.h index af40d49..98b60c0 100644 --- a/src/GameBall/logic/player_input.h +++ b/src/GameBall/logic/player_input.h @@ -6,24 +6,12 @@ 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}; - bool v_jump{false}; // make the sphere jump if it is on the cube - bool low{false}; // lower the mass by 10% once called - bool high{false}; // increase the mass by 10% at the supply base(0,1,0) - bool return_if_too_light{ - false}; // return to the supply base and recover normal mass if called - bool halt{false}; // Stop the game and the balls will make no response until - // halting ends - bool restart_halt{false}; // Halt by pressing T once, and restart the game by - // pressing T again - bool end_if_too_heavy{false}; // end the game if the ball is too heavy - bool end{false}; // quit immediately glm::vec3 orientation{0.0f, 0.0f, 1.0f}; }; @@ -35,11 +23,6 @@ class PlayerInputController { private: GameBall *app_; PlayerInput input_{}; - bool R_pressed_before = false; // indicate whether R has been pressed before - bool R_pressed_now = false; // indicate whether R is being pressed - bool T_pressed_before = false; - bool T_pressed_now = false; - int count_chances = 0, count_halt = 0; }; } // namespace GameBall::Logic From cda90d309f188da54f438293527512dec91394e0 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:25:46 +0800 Subject: [PATCH 14/20] Update regular_ball.cpp --- src/GameBall/logic/units/regular_ball.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 31c9dc4..c81de68 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -53,7 +53,7 @@ void RegularBall::UpdateTick() { 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}));l + glm::normalize(glm::cross(forward, glm::vec3{0.0f, 1.0f, 0.0f})); glm::vec3 moving_direction{}; From 42a3487bcb0e29be504e862b4d47558f209bd378 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:26:13 +0800 Subject: [PATCH 15/20] Update common.h --- src/GameX/utils/common.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/GameX/utils/common.h b/src/GameX/utils/common.h index 362dbae..7461dd0 100644 --- a/src/GameX/utils/common.h +++ b/src/GameX/utils/common.h @@ -1,6 +1,8 @@ #pragma once #include + #include + #define GAMEX_DECLARE_CLASS(ClassName) \ class ClassName; \ typedef std::unique_ptr U##ClassName; \ From d51d7f7a9bbd54cd5589bc8672b4773eab474675 Mon Sep 17 00:00:00 2001 From: chenzunh23 Date: Thu, 18 Jan 2024 21:47:15 +0800 Subject: [PATCH 16/20] Enable change of color of balls and the texture of the floor in command line. --- .../core/actors/common_ball_actor.cpp | 51 ++++++++++++++++- src/GameBall/logic/obstacles/block.cpp | 55 ++++++++++++++++--- src/GameX/utils/common.cpp | 1 - 3 files changed, 96 insertions(+), 11 deletions(-) diff --git a/src/GameBall/core/actors/common_ball_actor.cpp b/src/GameBall/core/actors/common_ball_actor.cpp index 82d6c37..48449ad 100644 --- a/src/GameBall/core/actors/common_ball_actor.cpp +++ b/src/GameBall/core/actors/common_ball_actor.cpp @@ -3,12 +3,57 @@ #include "GameBall/core/game_ball.h" namespace GameBall::Actors { - +static int count = 1; CommonBallActor::CommonBallActor(GameBall *app) : Actor(app) { entity_ = app->Scene()->CreateEntity( app->AssetManager()->StaticModelFile("models/sphere.obj")); - entity_->SetAlbedoImage( - app->AssetManager()->ImageFile("textures/2k_moon.jpg")); + if (count == 1) + std::cout << "Choose the color of the player ball:"; + else if (count == 2) + std::cout << "Choose the color of the enemy ball:"; + else if (count == 3) + std::cout << "Choose the color of the large ball:"; + else if (count == 4) + std::cout << "Choose the color of the small ball:"; + else + exit(0); + std::string enter; + while (1) { + std::cin >> enter; + if (enter == "white") { + entity_->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_ao_2k.jpg")); + break; + } else if (enter == "orange") { + entity_->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_arm_2k.jpg")); + break; + } else if (enter == "tile") { + entity_->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_disp_2k.jpg")); + break; + } else if (enter == "blue1") { + entity_->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_nor_dx_2k.jpg")); + break; + } else if (enter == "blue2") { + entity_->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_nor_gl_2k.jpg")); + break; + } else if (enter == "grey") { + entity_->SetAlbedoImage(app->AssetManager()->ImageFile("textures/floor_tiles_06_2k/floor_tiles_06_rough_2k.jpg ")); + break; + } else if (enter == "Moon") { + entity_->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/2k_moon.jpg")); + break; + } else { + std::cout<< + "We only have 7 textures, namely blue1, blue2, "<< + "white, orange, tile, grey, and Moon.Please choose again.\n"; + } + } + count++; } CommonBallActor::~CommonBallActor() { diff --git a/src/GameBall/logic/obstacles/block.cpp b/src/GameBall/logic/obstacles/block.cpp index 7107909..5fdb516 100644 --- a/src/GameBall/logic/obstacles/block.cpp +++ b/src/GameBall/logic/obstacles/block.cpp @@ -4,7 +4,7 @@ #include "GameBall/logic/world.h" namespace GameBall::Logic::Obstacles { - +short count=0; Block::Block(World *world, glm::vec3 position, float mass, @@ -22,7 +22,7 @@ Block::Block(World *world, SetMass(mass_); SetSideLength(side_length_); SetMotion(position_, velocity_, orientation_, L_); - cube.elasticity = 0.25f; + cube.elasticity = 0.5f; cube.friction = 0.5f; } @@ -32,12 +32,53 @@ SYNC_ACTOR_FUNC(Block) { actor->SetInertiaTensor(J_); actor->SetGravity(gravity_); actor->SetMotion(position_, velocity_, orientation_, L_); - if (ActorInitialize()) { - actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + if (ActorInitialize()) + if (count <2) { + std::cout << "Choose a setting:\n"; + char input; + std::cin >> input; + switch (input) { + case '1': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_ao_2k.jpg")); + break; + case '2': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_arm_2k.jpg")); + break; + case '3': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_diff_2k.jpg")); + break; + case '4': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_disp_2k.jpg")); + break; + case '5': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_nor_dx_2k.jpg")); + break; + case '6': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_nor_gl_2k.jpg")); + break; + case '7': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_rough_2k.jpg")); + break; + case '8': + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_spec_2k.jpg")); + break; + default: + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( + "textures/floor_tiles_06_2k/floor_tiles_06_diff_2k.jpg")); + } + count++; + } else + actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile( "textures/floor_tiles_06_2k/floor_tiles_06_diff_2k.jpg")); - } -} - + } void Block::SetMass(float mass) { auto physics_world = world_->PhysicsWorld(); auto &cube = physics_world->GetCube(cude_id_); diff --git a/src/GameX/utils/common.cpp b/src/GameX/utils/common.cpp index ca02f43..b0f99b8 100644 --- a/src/GameX/utils/common.cpp +++ b/src/GameX/utils/common.cpp @@ -1,3 +1,2 @@ #include "GameX/utils/common.h" - namespace GameX::Base {} From 8cd15e8900b85f6405e11d554c283a79dca8fdac Mon Sep 17 00:00:00 2001 From: chenzunh23 Date: Thu, 18 Jan 2024 23:48:10 +0800 Subject: [PATCH 17/20] A more complex setting on a much larger playgroundwith more balls, more cubes, and direct exit once the player loses or wins --- src/GameBall/core/game_ball.cpp | 38 +++++- src/GameBall/core/game_ball.h | 2 + src/GameBall/logic/player.cpp | 16 +++ src/GameBall/logic/player.h | 10 ++ src/GameBall/logic/player_input.cpp | 21 ++++ src/GameBall/logic/player_input.h | 16 +++ src/GameBall/logic/units/regular_ball.cpp | 143 +++++++++++++++++++--- 7 files changed, 224 insertions(+), 22 deletions(-) diff --git a/src/GameBall/core/game_ball.cpp b/src/GameBall/core/game_ball.cpp index a47b79e..a3aa4f0 100644 --- a/src/GameBall/core/game_ball.cpp +++ b/src/GameBall/core/game_ball.cpp @@ -37,17 +37,47 @@ void GameBall::OnInit() { auto primary_player = world->CreatePlayer(); auto enemy_player = world->CreatePlayer(); + auto large_player = world->CreatePlayer(); + auto small_player = world->CreatePlayer(); auto primary_unit = world->CreateUnit( - primary_player->PlayerId(), glm::vec3{0.0f, 1.0f, 0.0f}, 1.0f, 1.0f); + primary_player->PlayerId(), glm::vec3{20.0f, 21.0f, 0.0f}, 1.0f, 1.0f); auto enemy_unit = world->CreateUnit( - enemy_player->PlayerId(), glm::vec3{-5.0f, 1.0f, 0.0f}, 1.0f, 1.0f); + enemy_player->PlayerId(), glm::vec3{15.0f, 21.0f, 0.0f}, 1.0f, 1.0f); + auto large_unit = world->CreateUnit( + large_player->PlayerId(), glm::vec3{-30.0f, 2.0f, 0.0f}, 2.0f, 6.0f); + auto small_unit = world->CreateUnit( + small_player->PlayerId(), glm::vec3{0.0f, 20.7f, 20.0f}, 0.7f, 0.343f); auto primary_obstacle = world->CreateObstacle( - glm::vec3{0.0f, -10.0f, 0.0f}, std::numeric_limits::infinity(), + glm::vec3{0.0f, -40.0f, 0.0f}, std::numeric_limits::infinity(), + false, 80.0f); + auto obstacle2 = world->CreateObstacle( + glm::vec3{20.0f, 10.0f, 0.0f}, std::numeric_limits::infinity(), false, 20.0f); - + auto obstacle3 = world->CreateObstacle( + glm::vec3{0.0f, 10.0f, 20.0f}, std::numeric_limits::infinity(), + false, 20.0f); + auto obstacle4 = world->CreateObstacle( + glm::vec3{0.0f, 10.0f, -20.0f}, std::numeric_limits::infinity(), + false, 20.0f); + auto obstacle5 = world->CreateObstacle( + glm::vec3{-35.0f, 0.75f, 1.5f}, std::numeric_limits::infinity(), + false, 1.5f); + auto obstacle6 = world->CreateObstacle( + glm::vec3{-35.0f, 0.75f, -1.5f}, std::numeric_limits::infinity(), + false, 1.5f); + auto obstacle7 = world->CreateObstacle( + glm::vec3{-35.0f, 3.75f, 0.0f}, std::numeric_limits::infinity(), + false, 4.5f); + auto obstacle8 = world->CreateObstacle( + glm::vec3{15.0f, 15.0f, 15.0f}, std::numeric_limits::infinity(), + false, 10.0f); primary_player_id_ = primary_player->PlayerId(); + large_player_id_ = large_player->PlayerId(); + small_player_id_ = small_player->PlayerId(); primary_player->SetPrimaryUnit(primary_unit->UnitId()); + large_player->SetLargeUnit(large_unit->UnitId()); + small_player->SetSmallUnit(small_unit->UnitId()); VkExtent2D extent = FrameExtent(); float aspect = static_cast(extent.width) / extent.height; diff --git a/src/GameBall/core/game_ball.h b/src/GameBall/core/game_ball.h index 3da4bd6..6d6a25b 100644 --- a/src/GameBall/core/game_ball.h +++ b/src/GameBall/core/game_ball.h @@ -71,6 +71,8 @@ class GameBall : public GameX::Base::Application { uint64_t primary_player_id_{0}; uint64_t primary_player_primary_unit_object_id_{0}; + uint64_t large_player_id_{0}; + uint64_t small_player_id_{0}; bool ignore_next_mouse_move_{true}; diff --git a/src/GameBall/logic/player.cpp b/src/GameBall/logic/player.cpp index 6668885..44e5327 100644 --- a/src/GameBall/logic/player.cpp +++ b/src/GameBall/logic/player.cpp @@ -20,10 +20,26 @@ uint64_t Player::PrimaryUnitId() const { return primary_unit_id_; } +uint64_t Player::LargeUnitId() const { + return large_unit_id_; +} + +uint64_t Player::SmallUnitId() const { + return small_unit_id_; +} + void Player::SetPrimaryUnit(uint64_t unit_id) { primary_unit_id_ = unit_id; } +void Player::SetLargeUnit(uint64_t unit_id) { + large_unit_id_ = unit_id; +} + +void Player::SetSmallUnit(uint64_t unit_id) { + small_unit_id_ = unit_id; +} + void Player::SetInput(const PlayerInput &input) { input_ = input; } diff --git a/src/GameBall/logic/player.h b/src/GameBall/logic/player.h index ef33e2e..1ed17ff 100644 --- a/src/GameBall/logic/player.h +++ b/src/GameBall/logic/player.h @@ -12,8 +12,16 @@ class Player { uint64_t PrimaryUnitId() const; + uint64_t LargeUnitId() const; + + uint64_t SmallUnitId() const; + void SetPrimaryUnit(uint64_t unit_id); + void SetLargeUnit(uint64_t unit_id); + + void SetSmallUnit(uint64_t unit_id); + void SetInput(const PlayerInput &input); [[nodiscard]] PlayerInput GetPlayerInput() const; @@ -25,6 +33,8 @@ class Player { uint64_t player_id_{}; uint64_t primary_unit_id_{}; + uint64_t large_unit_id_{}; + uint64_t small_unit_id_{}; PlayerInput input_{}; }; diff --git a/src/GameBall/logic/player_input.cpp b/src/GameBall/logic/player_input.cpp index c002a91..25e2bdb 100644 --- a/src/GameBall/logic/player_input.cpp +++ b/src/GameBall/logic/player_input.cpp @@ -19,6 +19,27 @@ PlayerInput PlayerInputController::GetInput() { auto yaw = pitch_yaw.y; input_.orientation = {glm::sin(glm::radians(yaw)), 0.0f, -glm::cos(glm::radians(yaw))}; + input_.v_jump = (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS); + input_.low = (glfwGetKey(window, GLFW_KEY_0) == GLFW_PRESS); + input_.high = (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS); + input_.return_if_too_light = + (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS); // 2 chances of returning to the base + //changed to enable the judgement of winning or not. + if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) + R_pressed_now = R_pressed_before = true; + if (glfwGetKey(window, GLFW_KEY_R) == GLFW_RELEASE && R_pressed_before && + R_pressed_now) { + R_pressed_now = false; + count_chances++; + } + input_.halt = (glfwGetKey(window, GLFW_KEY_H) == GLFW_PRESS); + if (glfwGetKey(window, GLFW_KEY_T) == GLFW_PRESS) + T_pressed_now = true; + if (glfwGetKey(window, GLFW_KEY_T) == GLFW_RELEASE && T_pressed_now) + T_pressed_now = !T_pressed_now, T_pressed_before = !T_pressed_before; + input_.restart_halt = T_pressed_before; + input_.end_if_too_heavy = (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS); + input_.end = (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS); auto result = input_; input_ = {}; return result; diff --git a/src/GameBall/logic/player_input.h b/src/GameBall/logic/player_input.h index 98b60c0..7b90a22 100644 --- a/src/GameBall/logic/player_input.h +++ b/src/GameBall/logic/player_input.h @@ -13,6 +13,17 @@ struct PlayerInput { bool move_right{false}; bool brake{false}; glm::vec3 orientation{0.0f, 0.0f, 1.0f}; + bool v_jump{false}; // make the sphere jump if it is on the cube + bool low{false}; // lower the mass by 10% once called + bool high{false}; // increase the mass by 10% at the supply base(0,1,0) + bool return_if_too_light{ + false}; // return to the supply base and recover normal mass if called + bool halt{false}; // Stop the game and the balls will make no response until + // halting ends + bool restart_halt{false}; // Halt by pressing T once, and restart the game by + // pressing T again + bool end_if_too_heavy{false}; // end the game if the ball is too heavy + bool end{false}; // quit immediately }; class PlayerInputController { @@ -23,6 +34,11 @@ class PlayerInputController { private: GameBall *app_; PlayerInput input_{}; + bool R_pressed_before = false; // indicate whether R has been pressed before + bool R_pressed_now = false; // indicate whether R is being pressed + bool T_pressed_before = false; + bool T_pressed_now = false; + int count_chances = 0, count_halt = 0; }; } // namespace GameBall::Logic diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index c81de68..b8d93e2 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -2,8 +2,12 @@ #include "GameBall/core/game_ball.h" #include "GameBall/logic/world.h" - +#include namespace GameBall::Logic::Units { +short count_ret = 0, count_fallen=0;//count how many times R is pressed. +//count how many balls have fallen +float Y1=0.0f;//record the position of the enemy spheres +const short number_of_balls = 3; RegularBall::RegularBall(World *world, uint64_t player_id, const glm::vec3 &position, @@ -45,20 +49,19 @@ void RegularBall::UpdateTick() { 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{}; - 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); - + float angular_acceleration = glm::radians(2880.0f); + if (!input.restart_halt) { if (input.move_forward) { moving_direction -= right; } @@ -71,19 +74,94 @@ void RegularBall::UpdateTick() { if (input.move_right) { moving_direction += 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) { + if (input.brake && sphere.position.y < 1.01f && + sphere.position.y > 0.99f) { sphere.angular_velocity = glm::vec3{0.0f}; } + + if (input.low) { + sphere.mass *= 0.9f; + } + + if (input.high && std::abs(sphere.position.x) < 5.0f && + std::abs(sphere.position.z) < 5.0f && + std::abs(sphere.position.y - 1.0f) < 0.01f) { + sphere.mass *= 1.1f; + } + if (input.v_jump) { + if (sphere.position.y < 1.01f && sphere.position.y > 0.99f) + sphere.velocity.y = 10.0f; + } + if (input.return_if_too_light) { + if (sphere.mass <= 0.50f && count_ret < 2) { + sphere.position.y = 1.00f; + sphere.position.x = 0.00f; + sphere.position.z = 0.00f; + sphere.mass = 1.0f; + } + count_ret++; + } + + if (input.end_if_too_heavy && sphere.mass >= 2.0f) { + world_->RemovePlayer(owner->PrimaryUnitId()); + exit(0); + } + if (input.halt) { + std::cout << "Exit?(Y/n)\n"; + char c; + std::cin >> c; + if (c == 'Y' || c == 'y') { + world_->RemovePlayer(owner->PrimaryUnitId()); + exit(0); // This exit is more stable + } + std::cout << "______________Halting______________\n"; + std::cout << "Please enter a 4-digit password:\n"; + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + GetConsoleMode(hStdin, &mode); + SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); + std::string password, enter; + char p; + int length = 4; + for (int i = 0; i < length; i++) + std::cin >> p, password += p; + std::cout << "Successfully memorized password, enter again to " + "restart the game:\n"; + int i; + for (i = 0; i < 3; i++) { + std::cin >> enter; + if (enter == password) { + input.halt = false; + // Linux:tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + break; + } else if (i < 2) + std::cout << "Enter again:\n"; + } + if (i == 3) { + std::cout << "Sorry, but we have to stop now.\n"; + // Linux:tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + Sleep(5000); // Linux:sleep(5000) + world_->RemovePlayer(owner->PrimaryUnitId()); + exit(0); + } + } + if (input.end) { + world_->RemovePlayer(owner->PrimaryUnitId()); + exit(0); + } + } else { + glm::vec3 zero_v{0.0f}; + sphere.velocity = zero_v; + sphere.angular_velocity *= 0; } } - + } sphere.velocity *= std::pow(0.5f, delta_time); sphere.angular_velocity *= std::pow(0.2f, delta_time); @@ -91,6 +169,35 @@ void RegularBall::UpdateTick() { velocity_ = sphere.velocity; orientation_ = sphere.orientation; augular_momentum_ = sphere.inertia * sphere.angular_velocity; + + if (owner) { + if (UnitId() != owner->PrimaryUnitId()) { + Y1 = sphere.position.y; + if (Y1 < 0.50f) { + world_->RemovePlayer(player_id_); + count_fallen++; + } + if (UnitId() == owner->SmallUnitId() && sphere.position.x <= -32.75f && + sphere.position.x >= -37.25f && + std::abs(sphere.position.z) <= 2.25f) { + world_->RemovePlayer(owner->PrimaryUnitId()); + std::cerr << "You have lost the game!"; + Sleep(3000); + exit(0); + } + } else if (Y1 > sphere.position.y + 10.0f || + sphere.position.y < 0.0f && count_ret >= 2) { + world_->RemovePlayer(owner->PrimaryUnitId()); + std::cerr << "You have lost the game!"; + Sleep(3000); + exit(0); + } else if (Y1>0.0f&&count_fallen==number_of_balls) { + world_->RemovePlayer(owner->PrimaryUnitId()); + std::cerr << "$$$$$$@@@@@@You have won the game!@@@@@@$$$$$$"; + Sleep(10000); + exit(0); + } + } } void RegularBall::SetMass(float mass) { From fea00e056d92e9884cfd6683748c9c3df7b01236 Mon Sep 17 00:00:00 2001 From: chenzunh23 <145881193+chenzunh23@users.noreply.github.com> Date: Thu, 18 Jan 2024 23:53:58 +0800 Subject: [PATCH 18/20] Fix incompatible expressions. --- src/GameBall/logic/units/regular_ball.cpp | 26 +++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index b8d93e2..c24ab07 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -2,7 +2,10 @@ #include "GameBall/core/game_ball.h" #include "GameBall/logic/world.h" -#include + +#include +#include + namespace GameBall::Logic::Units { short count_ret = 0, count_fallen=0;//count how many times R is pressed. //count how many balls have fallen @@ -122,10 +125,11 @@ void RegularBall::UpdateTick() { } std::cout << "______________Halting______________\n"; std::cout << "Please enter a 4-digit password:\n"; - HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); - DWORD mode = 0; - GetConsoleMode(hStdin, &mode); - SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); + termios oldt; + tcgetattr(STDIN_FILENO, &oldt); + termios newt=oldt; + newt.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &newt); std::string password, enter; char p; int length = 4; @@ -138,15 +142,15 @@ void RegularBall::UpdateTick() { std::cin >> enter; if (enter == password) { input.halt = false; - // Linux:tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); break; } else if (i < 2) std::cout << "Enter again:\n"; } if (i == 3) { std::cout << "Sorry, but we have to stop now.\n"; - // Linux:tcsetattr(STDIN_FILENO, TCSANOW, &oldt); - Sleep(5000); // Linux:sleep(5000) + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + sleep(5000); // Linux:sleep(5000) world_->RemovePlayer(owner->PrimaryUnitId()); exit(0); } @@ -182,19 +186,19 @@ void RegularBall::UpdateTick() { std::abs(sphere.position.z) <= 2.25f) { world_->RemovePlayer(owner->PrimaryUnitId()); std::cerr << "You have lost the game!"; - Sleep(3000); + sleep(3000); exit(0); } } else if (Y1 > sphere.position.y + 10.0f || sphere.position.y < 0.0f && count_ret >= 2) { world_->RemovePlayer(owner->PrimaryUnitId()); std::cerr << "You have lost the game!"; - Sleep(3000); + sleep(3000); exit(0); } else if (Y1>0.0f&&count_fallen==number_of_balls) { world_->RemovePlayer(owner->PrimaryUnitId()); std::cerr << "$$$$$$@@@@@@You have won the game!@@@@@@$$$$$$"; - Sleep(10000); + sleep(10000); exit(0); } } From d0dcb4b3dcd0a6266b8bd7a35a998be5b267818d Mon Sep 17 00:00:00 2001 From: chenzunh23 Date: Fri, 19 Jan 2024 10:42:01 +0800 Subject: [PATCH 19/20] A slightly more difficult enhancement. --- src/GameBall/core/actors/common_ball_actor.cpp | 2 +- src/GameBall/core/game_ball.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GameBall/core/actors/common_ball_actor.cpp b/src/GameBall/core/actors/common_ball_actor.cpp index 48449ad..91a0357 100644 --- a/src/GameBall/core/actors/common_ball_actor.cpp +++ b/src/GameBall/core/actors/common_ball_actor.cpp @@ -50,7 +50,7 @@ CommonBallActor::CommonBallActor(GameBall *app) : Actor(app) { } else { std::cout<< "We only have 7 textures, namely blue1, blue2, "<< - "white, orange, tile, grey, and Moon.Please choose again.\n"; + "white, orange, tile, grey, and Moon. Please choose again.\n"; } } count++; diff --git a/src/GameBall/core/game_ball.cpp b/src/GameBall/core/game_ball.cpp index a3aa4f0..cf00dc4 100644 --- a/src/GameBall/core/game_ball.cpp +++ b/src/GameBall/core/game_ball.cpp @@ -42,7 +42,7 @@ void GameBall::OnInit() { auto primary_unit = world->CreateUnit( primary_player->PlayerId(), glm::vec3{20.0f, 21.0f, 0.0f}, 1.0f, 1.0f); auto enemy_unit = world->CreateUnit( - enemy_player->PlayerId(), glm::vec3{15.0f, 21.0f, 0.0f}, 1.0f, 1.0f); + enemy_player->PlayerId(), glm::vec3{0.0f, 21.0f, -20.0f}, 1.0f, 1.0f); auto large_unit = world->CreateUnit( large_player->PlayerId(), glm::vec3{-30.0f, 2.0f, 0.0f}, 2.0f, 6.0f); auto small_unit = world->CreateUnit( From 2a3313f8930c68961cd07eb2481d62b1cb52ba5b Mon Sep 17 00:00:00 2001 From: chenzunh23 Date: Fri, 19 Jan 2024 13:07:04 +0800 Subject: [PATCH 20/20] Fix a bug of jump and brake. --- src/GameBall/logic/units/regular_ball.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index c24ab07..95de1cb 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -83,8 +83,7 @@ void RegularBall::UpdateTick() { sphere.angular_velocity += moving_direction * angular_acceleration * delta_time; } - if (input.brake && sphere.position.y < 1.01f && - sphere.position.y > 0.99f) { + if (input.brake && std::abs(sphere.velocity.y)>=0.01f) { sphere.angular_velocity = glm::vec3{0.0f}; } @@ -98,7 +97,11 @@ void RegularBall::UpdateTick() { sphere.mass *= 1.1f; } if (input.v_jump) { - if (sphere.position.y < 1.01f && sphere.position.y > 0.99f) + if (std::abs(sphere.position.y - 1.0f) < 0.01f || + std::abs(sphere.position.y - 21.0f) < + 0.01f || + (std::abs(sphere.position.y - 7.0f) < 0.01f && std :: + abs(sphere.position.z)<2.25f&&std::abs(sphere.position.x+35.0f)<2.25f)) sphere.velocity.y = 10.0f; } if (input.return_if_too_light) { @@ -189,7 +192,7 @@ void RegularBall::UpdateTick() { sleep(3000); exit(0); } - } else if (Y1 > sphere.position.y + 10.0f || + } else if (Y1 > sphere.position.y + 10.0f&&sphere.position.y>0.99f || sphere.position.y < 0.0f && count_ret >= 2) { world_->RemovePlayer(owner->PrimaryUnitId()); std::cerr << "You have lost the game!";