From c458513f423564a0b1f26e2b82419921149c2fdf Mon Sep 17 00:00:00 2001 From: karutoh Date: Tue, 14 Nov 2023 00:07:42 -0800 Subject: [PATCH] Pong development has been completed. --- .idea/editor.xml | 81 ++++++++++++ .idea/inspectionProfiles/Project_Default.xml | 8 ++ Levels/Game.cpp | 129 ++++++++++++++++++- Levels/Game.h | 10 ++ 4 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/editor.xml b/.idea/editor.xml index 46540da..f9a3953 100644 --- a/.idea/editor.xml +++ b/.idea/editor.xml @@ -2,5 +2,86 @@ \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..9009a21 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/Levels/Game.cpp b/Levels/Game.cpp index 196f4df..7a51257 100644 --- a/Levels/Game.cpp +++ b/Levels/Game.cpp @@ -5,26 +5,33 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include #include +#include +#include + +const float Game::paddleSpeed = 400.0f; +const float Game::ballSpeed = 400.0f; Game::~Game() { } Game::Game() - : Level("Game") + : Level("Game"), plyScore(0), aiScore(0) { } Game::Game(const Game& lvl) - : Level(lvl) + : Level(lvl), plyScore(lvl.plyScore), aiScore(lvl.aiScore) { } @@ -35,6 +42,9 @@ Game& Game::operator=(const Game& lvl) Level::operator=(lvl); + plyScore = lvl.plyScore; + aiScore = lvl.aiScore; + return *this; } @@ -63,6 +73,7 @@ void Game::Setup() // Adding Systems Here AddSystem(new lwe::Portrait2DSystem()); AddSystem(new lwe::RigidBodySystem()); + AddSystem(new lwe::DynamicsSystem()); lwe::GuiSystem* gui = new lwe::GuiSystem(); gui->AddResource(new lwe::FontAtlas("Resources/Fonts/Hack-Regular_48.ehf")); @@ -85,6 +96,8 @@ void Game::Setup() bounds->SetScale({win->GetSwapChain()->GetScale(), 1.0f}); lwe::AABB2D* collider = new lwe::AABB2D(); + collider->SetInverted(true); + collider->EnableStatic(true); bounds->AddComponent(collider); AddEntity(bounds); @@ -105,6 +118,10 @@ void Game::Setup() lwe::Entity* paddle = new lwe::Entity("Paddle"); paddle->SetScale({20.0f, 200.0f, 1.0f}); + lwe::AABB2D* paddleCollider = new lwe::AABB2D("Collider"); + paddleCollider->EnableStatic(true); + paddle->AddComponent(paddleCollider); + lwe::Portrait2D* paddleImg = new lwe::Portrait2D("PaddleImg", "Circle"); paddleImg->SetColor({0.75f}); paddle->AddComponent(paddleImg); @@ -124,9 +141,12 @@ void Game::Setup() ball->SetScale({20.0f, 20.0f, 1.0f}); ball->SetPos({center.x - 10.0f, center.y - 10.0f, 0.0f}); - lwe::Circle* ballCollider = new lwe::Circle("BallCollider", 50.0f); + lwe::AABB2D* ballCollider = new lwe::AABB2D("Collider"); ball->AddComponent(ballCollider); + lwe::Dynamics* ballDynamics = new lwe::Dynamics(1.0f, 1.01f); + ball->AddComponent(ballDynamics); + lwe::Portrait2D* ballImg = new lwe::Portrait2D("BallImg", "Circle"); ballImg->SetColor({1.0f, 0.0f, 0.0f}); ball->AddComponent(ballImg); @@ -151,13 +171,100 @@ void Game::OnUpdate(lwe::Input* input, const float delta) const lwe::Keyboard* keyboard = handler->GetPrimaryKeyboard(); + lwe::Entity* bounds = GetEntity("Bounds"); + + lwe::Entity* ball = GetEntity("Ball"); + lwe::Vec3_f ballPos = ball->GetPos(); + lwe::Vec3_f ballScale = ball->GetScale(); + + if (ballPos.x <= bounds->GetPos().x) + { + aiScore++; + + lwe::GuiSystem* gui = (lwe::GuiSystem*)GetSystem("GuiSystem"); + lwe::LabelGui* aiScoreLabel = (lwe::LabelGui*)gui->GetGui("AiScore"); + aiScoreLabel->SetText(lwe::Str_8::FromNum(aiScore)); + + ResetGame(bounds, ball); + } + else if (ballPos.x + ballScale.x >= bounds->GetScale().x) + { + plyScore++; + + lwe::GuiSystem* gui = (lwe::GuiSystem*)GetSystem("GuiSystem"); + lwe::LabelGui* plyScoreLabel = (lwe::LabelGui*)gui->GetGui("PlyScore"); + plyScoreLabel->SetText(lwe::Str_8::FromNum(plyScore)); + + ResetGame(bounds, ball); + } + lwe::Entity* plyPaddle = GetEntity("PlyPaddle"); + if (!started && keyboard->IsTouched(lwe::Keyboard::Space)) + { + if (!lwe::CPU::HasRDRND()) + return; + + lwe::Dynamics* ballDynamics = (lwe::Dynamics*)ball->GetComponent("Dynamics", ""); + //ballDynamics->ApplyForce({0.0f, ballSpeed, 0.0f}); + + lwe::UInt_8 result = lwe::HRNG::Generate_s8(0, 4); + if (result == 0) + ballDynamics->ApplyForce({ballSpeed, ballSpeed, 0.0f}); + else if (result == 1) + ballDynamics->ApplyForce({-ballSpeed, ballSpeed, 0.0f}); + else if (result == 2) + ballDynamics->ApplyForce({ballSpeed, -ballSpeed, 0.0f}); + else if (result == 3) + ballDynamics->ApplyForce({-ballSpeed, -ballSpeed, 0.0f}); + + started = true; + } + if (keyboard->IsDown(lwe::Keyboard::W)) - plyPaddle->SetPos(plyPaddle->GetPos() - lwe::Vec3_f(0.0f, 400.0f, 0.0f) * delta); + { + lwe::Vec3_f newPos = plyPaddle->GetPos() - lwe::Vec3_f(0.0f, paddleSpeed, 0.0f) * delta; + if (newPos.y <= bounds->GetPos().y) + newPos.y = bounds->GetPos().y; + + plyPaddle->SetPos(newPos); + } if (keyboard->IsDown(lwe::Keyboard::S)) - plyPaddle->SetPos(plyPaddle->GetPos() + lwe::Vec3_f(0.0f, 400.0f, 0.0f) * delta); + { + lwe::Vec3_f newPos = plyPaddle->GetPos() + lwe::Vec3_f(0.0f, paddleSpeed, 0.0f) * delta; + if (newPos.y + plyPaddle->GetScale().y >= bounds->GetScale().y) + newPos.y = bounds->GetScale().y - plyPaddle->GetScale().y; + + plyPaddle->SetPos(newPos); + } + + lwe::Entity* aiPaddle = GetEntity("AiPaddle"); + lwe::Vec3_f aiPaddlePos = aiPaddle->GetPos(); + lwe::Vec3_f aiPaddleScale = aiPaddle->GetScale(); + + if (aiPaddlePos.y + aiPaddleScale.y / 2.0f < ballPos.y + ballScale.y / 2.0f) + { + aiPaddlePos = aiPaddlePos + lwe::Vec3_f(0.0f, paddleSpeed, 0.0f) * delta; + if (aiPaddlePos.y + aiPaddleScale.y / 2.0f > ballPos.y + ballScale.y / 2.0f) + aiPaddlePos.y = ballPos.y + ballScale.y / 2.0f - aiPaddleScale.y / 2.0f; + + if (aiPaddlePos.y + aiPaddleScale.y >= bounds->GetScale().y) + aiPaddlePos.y = bounds->GetScale().y - aiPaddleScale.y; + + aiPaddle->SetPos(aiPaddlePos); + } + else + { + aiPaddlePos = aiPaddlePos - lwe::Vec3_f(0.0f, paddleSpeed, 0.0f) * delta; + if (aiPaddlePos.y + aiPaddleScale.y / 2.0f < ballPos.y + ballScale.y / 2.0f) + aiPaddlePos.y = ballPos.y + ballScale.y / 2.0f - aiPaddleScale.y / 2.0f; + + if (aiPaddlePos.y <= bounds->GetPos().y) + aiPaddlePos.y = bounds->GetPos().y; + + aiPaddle->SetPos(aiPaddlePos); + } } void Game::PreRender(lwe::GpuCmdBuffer* cmdBuffer) @@ -165,4 +272,14 @@ void Game::PreRender(lwe::GpuCmdBuffer* cmdBuffer) Level::PreRender(cmdBuffer); // Pre-Render Code Here -} \ No newline at end of file +} + +void Game::ResetGame(const lwe::Entity* bounds, lwe::Entity* ball) +{ + ball->SetPos(bounds->GetScale() / 2.0f - ball->GetScale() / 2.0f); + + lwe::Dynamics* ballDyn = (lwe::Dynamics*)ball->GetComponent("Dynamics"); + ballDyn->SetVelocity(0.0f); + + started = false; +} diff --git a/Levels/Game.h b/Levels/Game.h index af52646..7f000f6 100644 --- a/Levels/Game.h +++ b/Levels/Game.h @@ -5,6 +5,13 @@ class Game : public lwe::Level { +private: + static const float paddleSpeed; + static const float ballSpeed; + lwe::UInt_64 plyScore; + lwe::UInt_64 aiScore; + bool started; + public: ~Game() override; @@ -23,4 +30,7 @@ public: void OnUpdate(lwe::Input* input, const float delta) override; void PreRender(lwe::GpuCmdBuffer* cmdBuffer) override; + +private: + void ResetGame(const lwe::Entity* bounds, lwe::Entity* ball); }; \ No newline at end of file