Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]Construct a more complex setting with more balls, more cubes, and direct exit once the player loses or wins. #91

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c500f8c
Tentatively enable the annotated code.
chenzunh23 Jan 12, 2024
94d88ee
Tentatively enable the annotated code and attempt to test it.
chenzunh23 Jan 12, 2024
eba728e
Update regular_ball.cpp
chenzunh23 Jan 12, 2024
9cc1a3f
Add submission requirements.
chenzunh23 Jan 15, 2024
8ad17df
implement some basic keyboard control, enabling easy exits, two types…
Jan 16, 2024
efb2c7e
Merge branch 'main' of https://github.com/chenzunh23/GameX
chenzunh23 Jan 16, 2024
b986b42
Why is conio.h rejected by Clang Debug?
Jan 16, 2024
09b7067
Replace conio.h with curses.h
chenzunh23 Jan 16, 2024
19b6131
Abandon getch() and conio.h
Jan 16, 2024
11c65ab
Merge branch 'main' of https://github.com/chenzunh23/GameX
chenzunh23 Jan 16, 2024
7460580
Update README.md
chenzunh23 Jan 16, 2024
3f5e895
Update README.md
chenzunh23 Jan 16, 2024
5380e48
Update regular_ball.cpp to clean up main branch
chenzunh23 Jan 16, 2024
068f928
Update player_input.cpp
chenzunh23 Jan 16, 2024
a871d6b
Update player_input.h to clean up main branch.
chenzunh23 Jan 16, 2024
cda90d3
Update regular_ball.cpp
chenzunh23 Jan 16, 2024
42a3487
Update common.h
chenzunh23 Jan 16, 2024
d51d7f7
Enable change of color of balls and the texture of the floor in comma…
chenzunh23 Jan 18, 2024
8cd15e8
A more complex setting on a much larger playgroundwith more balls, mo…
chenzunh23 Jan 18, 2024
fea00e0
Fix incompatible expressions.
chenzunh23 Jan 18, 2024
d0dcb4b
A slightly more difficult enhancement.
chenzunh23 Jan 19, 2024
46694f6
Merge branch 'Resolve_conflicts' of https://github.com/chenzunh23/Gam…
chenzunh23 Jan 19, 2024
2a3313f
Fix a bug of jump and brake.
chenzunh23 Jan 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/GameBall/core/actors/common_ball_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
38 changes: 34 additions & 4 deletions src/GameBall/core/game_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Logic::Units::RegularBall>(
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<Logic::Units::RegularBall>(
enemy_player->PlayerId(), glm::vec3{-5.0f, 1.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<Logic::Units::RegularBall>(
large_player->PlayerId(), glm::vec3{-30.0f, 2.0f, 0.0f}, 2.0f, 6.0f);
auto small_unit = world->CreateUnit<Logic::Units::RegularBall>(
small_player->PlayerId(), glm::vec3{0.0f, 20.7f, 20.0f}, 0.7f, 0.343f);
auto primary_obstacle = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{0.0f, -10.0f, 0.0f}, std::numeric_limits<float>::infinity(),
glm::vec3{0.0f, -40.0f, 0.0f}, std::numeric_limits<float>::infinity(),
false, 80.0f);
auto obstacle2 = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{20.0f, 10.0f, 0.0f}, std::numeric_limits<float>::infinity(),
false, 20.0f);

auto obstacle3 = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{0.0f, 10.0f, 20.0f}, std::numeric_limits<float>::infinity(),
false, 20.0f);
auto obstacle4 = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{0.0f, 10.0f, -20.0f}, std::numeric_limits<float>::infinity(),
false, 20.0f);
auto obstacle5 = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{-35.0f, 0.75f, 1.5f}, std::numeric_limits<float>::infinity(),
false, 1.5f);
auto obstacle6 = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{-35.0f, 0.75f, -1.5f}, std::numeric_limits<float>::infinity(),
false, 1.5f);
auto obstacle7 = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{-35.0f, 3.75f, 0.0f}, std::numeric_limits<float>::infinity(),
false, 4.5f);
auto obstacle8 = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{15.0f, 15.0f, 15.0f}, std::numeric_limits<float>::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<float>(extent.width) / extent.height;
Expand Down
2 changes: 2 additions & 0 deletions src/GameBall/core/game_ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
55 changes: 48 additions & 7 deletions src/GameBall/logic/obstacles/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

Expand All @@ -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_);
Expand Down
16 changes: 16 additions & 0 deletions src/GameBall/logic/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions src/GameBall/logic/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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_{};
};
Expand Down
21 changes: 21 additions & 0 deletions src/GameBall/logic/player_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/GameBall/logic/player_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Loading