Pong/Levels/Game.cpp

324 lines
9.0 KiB
C++
Raw Permalink Normal View History

2023-11-11 18:56:55 -08:00
#include "Game.h"
#include "LWE/IO/FontAtlas.h"
#include <LWE/GameLoop.h>
#include <LWE/IO/RenderWindow.h>
2023-11-14 21:33:11 -08:00
#include <LWE/Systems/AudioSystem.h>
2023-11-11 18:56:55 -08:00
#include <LWE/Systems/RigidBodySystem.h>
#include <LWE/Systems/Portrait2DSystem.h>
2023-11-14 00:07:42 -08:00
#include <LWE/Systems/DynamicsSystem.h>
2023-11-11 18:56:55 -08:00
#include <LWE/Systems/GuiSystem.h>
2023-11-14 21:33:11 -08:00
#include <LWE/Components/AudioSource.h>
2023-11-11 18:56:55 -08:00
#include <LWE/Components/AABB2D.h>
#include <LWE/Components/Circle.h>
#include <LWE/Components/Portrait2D.h>
2023-11-14 00:07:42 -08:00
#include <LWE/Components/Dynamics.h>
2023-11-11 18:56:55 -08:00
#include <LWE/Gui/LabelGui.h>
2023-11-14 21:33:11 -08:00
#include <LWE/IO/Audio/Audio.h>
2023-11-11 18:56:55 -08:00
#include <LWE/IO/HID/Input.h>
#include <LWE/IO/HID/GenericInputHandler.h>
#include <LWE/IO/HID/Keyboard.h>
2023-11-14 00:07:42 -08:00
#include <LWE/HRNG.h>
#include <LWE/System/CPU.h>
const float Game::paddleSpeed = 400.0f;
2023-11-14 21:33:11 -08:00
const float Game::ballSpeed = 15000.0f;
2023-11-11 18:56:55 -08:00
Game::~Game()
{
}
Game::Game()
2023-11-14 00:07:42 -08:00
: Level("Game"), plyScore(0), aiScore(0)
2023-11-11 18:56:55 -08:00
{
}
Game::Game(const Game& lvl)
2023-11-14 00:07:42 -08:00
: Level(lvl), plyScore(lvl.plyScore), aiScore(lvl.aiScore)
2023-11-11 18:56:55 -08:00
{
}
Game& Game::operator=(const Game& lvl)
{
if (this == &lvl)
return *this;
Level::operator=(lvl);
2023-11-14 00:07:42 -08:00
plyScore = lvl.plyScore;
aiScore = lvl.aiScore;
2023-11-11 18:56:55 -08:00
return *this;
}
void Game::SetupResources()
{
Level::SetupResources();
// Setup Resource Code Here
AddResource(new lwe::Mesh("PortraitGui", lwe::portraitGuiVerts, lwe::portraitGuiIndices));
2023-11-14 21:33:11 -08:00
AddResource(lwe::Audio::FromFile_Heap("Resources/Audio/Wall.wav", lwe::DataType::FLOAT));
AddResource(lwe::Audio::FromFile_Heap("Resources/Audio/Paddle.wav", lwe::DataType::FLOAT));
AddResource(lwe::Audio::FromFile_Heap("Resources/Audio/Score.wav", lwe::DataType::FLOAT));
2023-11-11 18:56:55 -08:00
constexpr lwe::UInt_8 circleData[] = {255, 255, 255, 255};
lwe::Img* circle = new lwe::Img("Circle", 8, 4, 1, 1, (lwe::Byte*)circleData, lwe::ImgAspect::IMG_ASPECT_COLOR);
AddResource(circle);
}
void Game::Setup()
{
Level::Setup();
// Setup Code Here
lwe::GameLoop* gl = (lwe::GameLoop*)GetParent("GameLoop");
lwe::RenderWindow* win = gl->GetWindow();
// Adding Systems Here
2023-11-14 21:33:11 -08:00
AddSystem(new lwe::AudioSystem());
2023-11-11 18:56:55 -08:00
AddSystem(new lwe::Portrait2DSystem());
AddSystem(new lwe::RigidBodySystem());
2023-11-14 00:07:42 -08:00
AddSystem(new lwe::DynamicsSystem());
2023-11-11 18:56:55 -08:00
lwe::GuiSystem* gui = new lwe::GuiSystem();
gui->AddResource(new lwe::FontAtlas("Resources/Fonts/Hack-Regular_48.ehf"));
AddSystem(gui);
lwe::Vec2_f scale = win->GetSwapChain()->GetScale();
lwe::Vec2_f center = win->GetSwapChain()->GetScale() / 2;
// Score Gui
lwe::LabelGui* plyScore = new lwe::LabelGui("PlyScore", "Hack-Regular_48", "0");
plyScore->SetPosition({20.0f, 20.0f});
gui->AddGui(plyScore);
lwe::LabelGui* aiScore = new lwe::LabelGui("AiScore", "Hack-Regular_48", "0");
aiScore->SetPosition({scale.x - 68.0f, 20.0f});
gui->AddGui(aiScore);
// Bounds Entity
lwe::Entity* bounds = new lwe::Entity("Bounds");
bounds->SetScale({win->GetSwapChain()->GetScale(), 1.0f});
lwe::AABB2D* collider = new lwe::AABB2D();
2023-11-14 00:07:42 -08:00
collider->SetInverted(true);
collider->EnableStatic(true);
2023-11-11 18:56:55 -08:00
bounds->AddComponent(collider);
AddEntity(bounds);
// Field Separator Entity
lwe::Entity* fieldSep = new lwe::Entity("FieldSeparator");
fieldSep->SetScale({10.0f, scale.y, 1.0f});
fieldSep->SetPos({center.x - 5.0f, 0.0f, 0.0f});
lwe::Portrait2D* fieldSepImg = new lwe::Portrait2D("PaddleImg", "Circle");
fieldSepImg->SetColor({0.75f});
fieldSep->AddComponent(fieldSepImg);
AddEntity(fieldSep);
// Paddle Entity Template
lwe::Entity* paddle = new lwe::Entity("Paddle");
paddle->SetScale({20.0f, 200.0f, 1.0f});
2023-11-14 00:07:42 -08:00
lwe::AABB2D* paddleCollider = new lwe::AABB2D("Collider");
paddleCollider->EnableStatic(true);
paddle->AddComponent(paddleCollider);
2023-11-11 18:56:55 -08:00
lwe::Portrait2D* paddleImg = new lwe::Portrait2D("PaddleImg", "Circle");
paddleImg->SetColor({0.75f});
paddle->AddComponent(paddleImg);
AddTemplate(paddle);
// Player's Paddle
lwe::Entity* plyPaddle = CreateEntity("Paddle", "PlyPaddle");
plyPaddle->SetPos({50.0f, center.y - 100.0f, 0.0f});
// Player's Paddle
lwe::Entity* aiPaddle = CreateEntity("Paddle", "AiPaddle");
aiPaddle->SetPos({scale.x - 50.0f, center.y - 100.0f, 0.0f});
// Ball Entity
lwe::Entity* ball = new lwe::Entity("Ball");
ball->SetScale({20.0f, 20.0f, 1.0f});
ball->SetPos({center.x - 10.0f, center.y - 10.0f, 0.0f});
2023-11-14 00:07:42 -08:00
lwe::AABB2D* ballCollider = new lwe::AABB2D("Collider");
2023-11-14 21:33:11 -08:00
ballCollider->SetCollidedCb([](lwe::RigidBody* a, lwe::RigidBody* b)
{
lwe::Entity* aOwner = (lwe::Entity*)a->GetParent();
lwe::Entity* bOwner = (lwe::Entity*)b->GetParent();
static lwe::UInt_64 id = 0;
if (bOwner->GetId() == "Bounds")
{
lwe::AudioSource* audio = new lwe::AudioSource("Audio_" + lwe::Str_8::FromNum(id++),"Wall");
audio->EnableAutoDelete(true);
audio->EnableLoop(false);
audio->Play();
aOwner->AddComponent(audio);
}
else
{
lwe::AudioSource* audio = new lwe::AudioSource("Audio_" + lwe::Str_8::FromNum(id++),"Paddle");
audio->EnableAutoDelete(true);
audio->EnableLoop(false);
audio->Play();
aOwner->AddComponent(audio);
}
});
2023-11-11 18:56:55 -08:00
ball->AddComponent(ballCollider);
2023-11-14 00:07:42 -08:00
lwe::Dynamics* ballDynamics = new lwe::Dynamics(1.0f, 1.01f);
ball->AddComponent(ballDynamics);
2023-11-11 18:56:55 -08:00
lwe::Portrait2D* ballImg = new lwe::Portrait2D("BallImg", "Circle");
ballImg->SetColor({1.0f, 0.0f, 0.0f});
ball->AddComponent(ballImg);
AddEntity(ball);
}
void Game::PostInitialize(lwe::GpuCmdBuffer* cmdBuffer)
{
Level::PostInitialize(cmdBuffer);
// Post Initialization Code Here
}
void Game::OnUpdate(lwe::Input* input, const float delta)
{
Level::OnUpdate(input, delta);
// Update Code Here
const lwe::GenericInputHandler* handler = (lwe::GenericInputHandler*)input->GetHandler("GenericInputHandler");
const lwe::Keyboard* keyboard = handler->GetPrimaryKeyboard();
2023-11-14 00:07:42 -08:00
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);
}
2023-11-11 18:56:55 -08:00
lwe::Entity* plyPaddle = GetEntity("PlyPaddle");
2023-11-14 00:07:42 -08:00
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;
}
2023-11-11 18:56:55 -08:00
if (keyboard->IsDown(lwe::Keyboard::W))
2023-11-14 00:07:42 -08:00
{
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);
}
2023-11-11 18:56:55 -08:00
if (keyboard->IsDown(lwe::Keyboard::S))
2023-11-14 00:07:42 -08:00
{
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);
}
2023-11-11 18:56:55 -08:00
}
void Game::PreRender(lwe::GpuCmdBuffer* cmdBuffer)
{
Level::PreRender(cmdBuffer);
// Pre-Render Code Here
2023-11-14 00:07:42 -08:00
}
void Game::ResetGame(const lwe::Entity* bounds, lwe::Entity* ball)
{
2023-11-14 21:33:11 -08:00
lwe::AudioSource* audio = new lwe::AudioSource("Score", "Score");
audio->EnableAutoDelete(true);
audio->EnableLoop(false);
audio->Play();
ball->AddComponent(audio);
2023-11-14 00:07:42 -08:00
ball->SetPos(bounds->GetScale() / 2.0f - ball->GetScale() / 2.0f);
lwe::Dynamics* ballDyn = (lwe::Dynamics*)ball->GetComponent("Dynamics");
2023-11-14 21:33:11 -08:00
ballDyn->ResetVelocity();
2023-11-14 00:07:42 -08:00
started = false;
}