Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ set(GAMEX_ASSETS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/assets)

add_subdirectory(src/GameX)
add_subdirectory(src/GameBall)
add_subdirectory(src/IrrKlang)

add_subdirectory(test)
2 changes: 1 addition & 1 deletion assets
1 change: 1 addition & 0 deletions src/GameBall/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ file(GLOB_RECURSE SOURCES *.cpp *.h)

add_executable(GameBall ${SOURCES})
target_link_libraries(GameBall PRIVATE GameX absl::flags absl::flags_parse)
target_link_libraries(GameBall PRIVATE Sound)

# Delete main.cpp from sources
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
Expand Down
6 changes: 4 additions & 2 deletions src/GameBall/core/game_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ GameBall::~GameBall() {

void GameBall::OnInit() {
auto world = logic_manager_->World();
float ground_size = 40.0f;

scene_->SetEnvmapImage(asset_manager_->ImageFile("textures/envmap.hdr"));

Expand All @@ -42,10 +43,11 @@ void GameBall::OnInit() {
auto enemy_unit = world->CreateUnit<Logic::Units::RegularBall>(
enemy_player->PlayerId(), glm::vec3{-5.0f, 1.0f, 0.0f}, 1.0f, 1.0f);
auto primary_obstacle = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{0.0f, -10.0f, 0.0f}, std::numeric_limits<float>::infinity(),
false, 20.0f);
glm::vec3{0.0f, -ground_size, 0.0f},
std::numeric_limits<float>::infinity(), false, 2 * ground_size);

primary_player_id_ = primary_player->PlayerId();
enemy_player_id_ = enemy_player->PlayerId();

primary_player->SetPrimaryUnit(primary_unit->UnitId());

Expand Down
1 change: 1 addition & 0 deletions src/GameBall/core/game_ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class GameBall : public GameX::Base::Application {
std::unique_ptr<CameraControllerThirdPerson> camera_controller_;

uint64_t primary_player_id_{0};
uint64_t enemy_player_id_{0};
uint64_t primary_player_primary_unit_object_id_{0};

bool ignore_next_mouse_move_{true};
Expand Down
4 changes: 4 additions & 0 deletions src/GameBall/logic/logic.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "GameBall/logic/logic.h"
#include "IrrKlang/PlaySound.h"

namespace GameBall::Logic {

Expand All @@ -17,11 +18,14 @@ class World *Manager::World() const {
}

void Manager::Start() {
PlaySound::InitSoundEngine();
PlaySound::Play(R"(../../../assets/audio/background.mp3)", true);
logic_thread_ = std::thread(&Manager::LogicThread, this);
}

void Manager::Stop() {
stop_signal_ = true;
PlaySound::ReleaseSoundEngine();
logic_thread_.join();
}

Expand Down
6 changes: 6 additions & 0 deletions src/GameBall/logic/player_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ PlayerInputController::PlayerInputController(GameBall *app) : app_(app) {

PlayerInput PlayerInputController::GetInput() {
auto window = app_->Window();
input_.speed_up = (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS);
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);
input_.left_arrow = (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS);
input_.right_arrow = (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
input_.restart = (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS);
input_.grow = (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS);
input_.shrink = (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS);
auto camera_controller = app_->CameraController();
auto pitch_yaw = camera_controller->GetPitchYaw();
auto pitch = pitch_yaw.x;
Expand Down
6 changes: 6 additions & 0 deletions src/GameBall/logic/player_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ struct PlayerInput {
bool move_backward{false};
bool move_left{false};
bool move_right{false};
bool speed_up{false};
bool brake{false};
bool left_arrow{false};
bool right_arrow{false};
bool restart{false};
bool grow{false};
bool shrink{false};
glm::vec3 orientation{0.0f, 0.0f, 1.0f};
};

Expand Down
191 changes: 142 additions & 49 deletions src/GameBall/logic/units/regular_ball.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "GameBall/logic/units/regular_ball.h"

#include "GameBall/core/game_ball.h"
#include "GameBall/logic/events.h"
#include "GameBall/logic/world.h"
#include "IrrKlang/PlaySound.h"

namespace GameBall::Logic::Units {
RegularBall::RegularBall(World *world,
Expand All @@ -21,8 +23,8 @@ RegularBall::RegularBall(World *world,
sphere.orientation = orientation_;
sphere.velocity = velocity_;
sphere.angular_velocity = glm::vec3{0.0f};
sphere.elasticity = 1.0f;
sphere.friction = 10.0f;
sphere.elasticity = sphere.elasticities[sphere.type];
sphere.friction = sphere.frictions[sphere.type];
sphere.gravity = glm::vec3{0.0f, -9.8f, 0.0f};
}

Expand All @@ -33,63 +35,154 @@ RegularBall::~RegularBall() {
SYNC_ACTOR_FUNC(RegularBall) {
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);
actor->SetMass(1.0f);
actor->SetMass(sphere.masses[sphere.type]);
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]);
}

long double last_change_time = -1, cur_time = 0;
glm::vec3 player_pos = {0.0f, 0.0f, 0.0f};
void RegularBall::UpdateTick() {
float delta_time = world_->TickDeltaT();
cur_time += delta_time;
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};
// }
// }
// }

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;
bool restart = false;
float delta_v = 0.5f, delta_av = 0.2f;
auto owner = world_->GetPlayer(player_id_);

if (owner) {
if (UnitId() == owner->PrimaryUnitId()) {
// Controls player ball
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 (input.left_arrow && (cur_time - last_change_time >= 1.0) &&
sphere.radius == 1.0f) {
sphere.type = (sphere.type - 1) < 0 ? 2 : (sphere.type - 1);
last_change_time = cur_time;
LAND_INFO("Sphere changed into type {}.", sphere.type);
PlaySound::Play(R"(../../../assets/audio/pop.mp3)");
}
if (input.right_arrow && (cur_time - last_change_time >= 1.0) &&
sphere.radius == 1.0f) {
sphere.type = (sphere.type + 1) % 3;
last_change_time = cur_time;
LAND_INFO("Sphere changed into type {}.", sphere.type);
PlaySound::Play(R"(../../../assets/audio/pop.mp3)");
}
if (input.shrink && (cur_time - last_change_time >= 1.0) &&
sphere.radius > 0.25f) {
sphere.position =
sphere.position + glm::vec3{0.0f, -0.5f * sphere.radius, 0.0f};
sphere.radius *= 0.5f;
sphere.mass *= 0.125f;
last_change_time = cur_time;
LAND_INFO("Sphere shrank.");
}
if (input.grow && (cur_time - last_change_time >= 1.0) &&
sphere.radius < 4.0f) {
sphere.position =
sphere.position + glm::vec3{0.0f, sphere.radius, 0.0f};
sphere.radius *= 2.0f;
sphere.mass *= 8.0f;
last_change_time = cur_time;
LAND_INFO("Sphere enlarged.");
}
if (input.brake) {
PlaySound::Play(R"(../../../assets/audio/brake.mp3)");
sphere.angular_velocity *= 0.7f;
input.speed_up = false;
} else if (input.speed_up) {
PlaySound::Play(R"(../../../assets/audio/acce.mp3)");
sphere.velocity *= std::pow(3 * delta_v, delta_time);
sphere.angular_velocity *= std::pow(3 * delta_av, delta_time);
} else {
sphere.velocity *= std::pow(delta_v, delta_time);
sphere.angular_velocity *= std::pow(delta_av, delta_time);
}
restart = input.restart || sphere.position.y <= -7.0f;
if (restart) {
LAND_INFO("You LOSE~");
PlaySound::Play(R"(../../../assets/audio/lose.mp3)");
sphere.position = glm::vec3{0.0f, sphere.radius + 0.1f, 0.0f};
}

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

sphere.elasticity = sphere.elasticities[sphere.type];
sphere.friction = sphere.frictions[sphere.type];
sphere.mass = sphere.masses[sphere.type];
player_pos = sphere.position;
} else {
// Controls enemy ball
if(cur_time>=5.0){
glm::vec3 forward = glm::normalize(player_pos - sphere.position);
glm::vec3 back =
glm::normalize(glm::vec3{0.0f, 0.0f, 0.0f} - sphere.position);
glm::vec3 right =
glm::normalize(glm::cross(forward, glm::vec3{0.0f, 1.0f, 0.0f}));
back = glm::normalize(glm::cross(back, glm::vec3{0.0f, 1.0f, 0.0f}));
glm::vec3 moving_direction{};
if (abs(sphere.position.x) <= 38.0f && abs(sphere.position.z) <= 38.0f)
moving_direction -= right;
else
moving_direction -= back;
float angular_acceleration = glm::radians(2880.0f);

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

sphere.velocity *= std::pow(delta_v, delta_time);
sphere.angular_velocity *= std::pow(delta_av, delta_time);
if (sphere.position.y <= -7.0f) {
LAND_INFO("You WON!!");
PlaySound::Play(R"(../../../assets/audio/win.mp3)");
sphere.position = glm::vec3{-5.0f, sphere.radius + 0.1f, 0.0f};
restart = true;
}
}
}

if (!restart) {
position_ = sphere.position;
velocity_ = sphere.velocity;
orientation_ = sphere.orientation;
augular_momentum_ = sphere.inertia * sphere.angular_velocity;
} else {
velocity_ = sphere.velocity = glm::vec3{0.0f};
augular_momentum_ = sphere.angular_velocity = glm::vec3{0.0f};
orientation_ = sphere.orientation = glm::mat3{1.0f};
}
}

void RegularBall::SetMass(float mass) {
Expand Down
4 changes: 4 additions & 0 deletions src/GameX/physics/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ struct Sphere : public RigidBody {
void SetRadiusMass(float radius = 1.0f, float mass = 1.0f);

float radius{1.0f};
int type = 0;
float elasticities[3] = {1.0f, 2.0f, 3.0f};
float masses[3] = {1.0f, 0.7f, 0.5f};
float frictions[3] = {10.0f, 13.0f, 15.0f};
};
} // namespace GameX::Physics
2 changes: 2 additions & 0 deletions src/GameX/physics/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>

#include "GameX/physics/collision.h"
#include "IrrKlang/PlaySound.h"

namespace GameX::Physics {
World::World() {
Expand Down Expand Up @@ -53,6 +54,7 @@ void World::SolveCollisions() {
if (DetectCollision(sphere1.second, sphere2.second, collision)) {
collision_pairs.emplace_back(&sphere1.second, &sphere2.second,
collision);
PlaySound::Play(R"(../../../assets/audio/collision.mp3)");
}
}
for (auto &cube : cubes_) {
Expand Down
12 changes: 12 additions & 0 deletions src/IrrKlang/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.22)

add_library(Sound PlaySound.cpp PlaySound.h)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)

target_link_libraries(Sound PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/irrKlang.lib)

install(TARGETS Sound
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
25 changes: 25 additions & 0 deletions src/IrrKlang/PlaySound.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "PlaySound.h"

namespace PlaySound {
ISoundEngine *engine;

void InitSoundEngine(){
engine = createIrrKlangDevice();
if(!engine){
std::cerr << "Error creating sound engine."<< std::endl;
return;
}
}

void Play(const std::string& path, bool looped){
engine->play2D(path.c_str(), looped);
}

void Play(char* path, bool looped){
engine->play2D(path, looped);
}

void ReleaseSoundEngine(){
engine->drop();
}
} // namespace PlaySound
Loading