Skip to content

Commit

Permalink
added basic player movement for Astroids demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0L042 committed Jun 7, 2024
1 parent 7b9ee60 commit 95af27e
Show file tree
Hide file tree
Showing 17 changed files with 320 additions and 6 deletions.
13 changes: 12 additions & 1 deletion Duin/Duin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<AdditionalLibraryDirectories>vendor\raylib5\lib;vendor\rlimgui\bin\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<ImportLibrary>..\bin\Debug-windows-x86_64\Duin\Duin.lib</ImportLibrary>
</Link>
<PostBuildEvent>
<Command>IF EXIST ..\bin\Debug-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Debug-windows-x86_64\Duin\Duin.dll ..\bin\Debug-windows-x86_64\Sandbox &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Debug-windows-x86_64\Duin\Duin.dll ..\bin\Debug-windows-x86_64\Sandbox &gt; nul)
IF EXIST ..\bin\Debug-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Debug-windows-x86_64\Duin\Duin.dll ..\bin\Debug-windows-x86_64\DuinEditor &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Debug-windows-x86_64\Duin\Duin.dll ..\bin\Debug-windows-x86_64\DuinEditor &gt; nul)
IF EXIST ..\bin\Debug-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Debug-windows-x86_64\Duin\Duin.dll ..\bin\Debug-windows-x86_64\Astroids &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Debug-windows-x86_64\Duin\Duin.dll ..\bin\Debug-windows-x86_64\Astroids &gt; nul)</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
Expand All @@ -118,6 +123,11 @@
<AdditionalLibraryDirectories>vendor\raylib5\lib;vendor\rlimgui\bin\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<ImportLibrary>..\bin\Release-windows-x86_64\Duin\Duin.lib</ImportLibrary>
</Link>
<PostBuildEvent>
<Command>IF EXIST ..\bin\Release-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Release-windows-x86_64\Duin\Duin.dll ..\bin\Release-windows-x86_64\Sandbox &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Release-windows-x86_64\Duin\Duin.dll ..\bin\Release-windows-x86_64\Sandbox &gt; nul)
IF EXIST ..\bin\Release-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Release-windows-x86_64\Duin\Duin.dll ..\bin\Release-windows-x86_64\DuinEditor &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Release-windows-x86_64\Duin\Duin.dll ..\bin\Release-windows-x86_64\DuinEditor &gt; nul)
IF EXIST ..\bin\Release-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Release-windows-x86_64\Duin\Duin.dll ..\bin\Release-windows-x86_64\Astroids &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Release-windows-x86_64\Duin\Duin.dll ..\bin\Release-windows-x86_64\Astroids &gt; nul)</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Dist|x64'">
<ClCompile>
Expand Down Expand Up @@ -145,7 +155,8 @@
</Link>
<PostBuildEvent>
<Command>IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\Sandbox &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\Sandbox &gt; nul)
IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\DuinEditor &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\DuinEditor &gt; nul)</Command>
IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\DuinEditor &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\DuinEditor &gt; nul)
IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\Astroids &gt; nul) ELSE (xcopy /Q /Y /I ..\bin\Dist-windows-x86_64\Duin\Duin.dll ..\bin\Dist-windows-x86_64\Astroids &gt; nul)</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions Duin/src/Duin/Core/Maths/src/Vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,19 @@ namespace Duin
}
return Vector2(x / mag, y / mag);
}

Vector2 LimitLength(float minLength, float maxLength) const
{
float mag = Magnitude();
if (mag < minLength)
{
return Normalized() * minLength;
}
else if (mag > maxLength)
{
return Normalized() * maxLength;
}
return *this;
}
};
}
14 changes: 14 additions & 0 deletions ExampleProjects/Astroids/Astroids.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@
<AdditionalLibraryDirectories>..\..\Duin\vendor\raylib5\lib;..\..\Duin\vendor\rlimgui\bin\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="src\Components\Component_AstroidInput.h" />
<ClInclude Include="src\Components\Component_Movement.h" />
<ClInclude Include="src\Components\Component_PlayerInput.h" />
<ClInclude Include="src\Components\Component_Renderable.h" />
<ClInclude Include="src\Components\Component_Transform.h" />
<ClInclude Include="src\Components\Handler_PlayerInput.h" />
<ClInclude Include="src\Components\Handler_PlayerMovement.h" />
<ClInclude Include="src\Components\Handler_Renderable.h" />
<ClInclude Include="src\Components\Handler_UpdateTransform.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Astroids.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Duin\Duin.vcxproj">
<Project>{F558847C-E18F-850D-8A7C-6B107692010F}</Project>
Expand Down
40 changes: 40 additions & 0 deletions ExampleProjects/Astroids/Astroids.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Components">
<UniqueIdentifier>{6B7DD516-5735-1764-C03C-F0BFAC13B254}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Components\Component_AstroidInput.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Component_Movement.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Component_PlayerInput.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Component_Renderable.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Component_Transform.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Handler_PlayerInput.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Handler_PlayerMovement.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Handler_Renderable.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="src\Components\Handler_UpdateTransform.h">
<Filter>Components</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Astroids.cpp" />
</ItemGroup>
</Project>
Binary file added ExampleProjects/Astroids/assets/spaceship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions ExampleProjects/Astroids/imgui.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400

50 changes: 50 additions & 0 deletions ExampleProjects/Astroids/src/Astroids.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
#include <Duin.h>
#include <Duin/EntryPoint.h>

#include <memory>

#include "Components/Component_Movement.h"
#include "Components/Component_PlayerInput.h"
#include "Components/Component_Transform.h"
#include "Components/Component_Renderable.h"
#include "Components/Handler_PlayerInput.h"
#include "Components/Handler_PlayerMovement.h"
#include "Components/Handler_UpdateTransform.h"
#include "Components/Handler_Renderable.h"

class Astroids : public Duin::Application
{
public:
Astroids() {}
~Astroids() {}

void Initialize() override;
void Exit() override;
void Ready() override;
void HandleInputs(Duin::InputEvent e) override;
void Process(double rDelta) override;
void PhysicsProcess(double pDelta) override;
void Draw() override;

Duin::Registry* registry;
Handler_PlayerInput* handlerPlayerInput;
Handler_PlayerMovement* handlerPlayerMovement;
Handler_UpdateTransform* handlerUpdateTransform;
Handler_Renderable* handlerRenderable;
std::shared_ptr<Duin::Entity> player;

void CreatePlayer();
};
Duin::Application* Duin::CreateApplication() { return new Astroids(); }

void Astroids::Initialize()
{
registry = new Duin::Registry();
handlerPlayerInput = new Handler_PlayerInput(registry);
handlerPlayerMovement = new Handler_PlayerMovement(registry);
handlerUpdateTransform = new Handler_UpdateTransform(registry, 1280 - 25, 720 - 25);
handlerRenderable = new Handler_Renderable(registry);
CreatePlayer();
}

void Astroids::Exit()
{
delete registry;
delete handlerPlayerInput;
delete handlerPlayerMovement;
delete handlerUpdateTransform;
delete handlerRenderable;
}

void Astroids::Ready()
Expand All @@ -27,6 +62,7 @@ void Astroids::Ready()

void Astroids::HandleInputs(Duin::InputEvent e)
{
handlerPlayerInput->Update(e);
}

void Astroids::Process(double rDelta)
Expand All @@ -35,8 +71,22 @@ void Astroids::Process(double rDelta)

void Astroids::PhysicsProcess(double pDelta)
{
handlerPlayerMovement->Update();

handlerUpdateTransform->Update(pDelta);
}

void Astroids::Draw()
{
handlerRenderable->Update();
}

void Astroids::CreatePlayer()
{
std::shared_ptr<Duin::TextureResource> texture = std::make_shared<Duin::TextureResource>("assets/spaceship.png");
player = Duin::Entity::Create(registry);
player->AddComponent<Component_PlayerInput>();
player->AddComponent<Component_Movement>();
player->AddComponent<Component_Transform>();
player->AddComponent<Component_Renderable>(texture, Duin::Vector2{ 25, 25 });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <Duin.h>

struct Component_AstroidInput
{
bool enableStatusComponent = true;
};
19 changes: 19 additions & 0 deletions ExampleProjects/Astroids/src/Components/Component_Movement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <Duin.h>

struct Component_Movement
{
float maxSpeed = 1.0f;
float acceleration = 1.0f;

float currSpeed = 0.0f;
Duin::Vector2 inputDir;
Duin::Vector2 prevVelocity;
Duin::Vector2 currVelocity;

Component_Movement() = default;
Component_Movement(float maxSpeed, float acceleration)
: maxSpeed(maxSpeed), acceleration(acceleration)
{}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <Duin.h>

struct Component_PlayerInput
{
bool enableStatusComponent = true;
};
17 changes: 17 additions & 0 deletions ExampleProjects/Astroids/src/Components/Component_Renderable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <Duin.h>

#include <memory>

struct Component_Renderable
{
std::shared_ptr<Duin::TextureResource> texture;
Duin::Vector2 size;

Component_Renderable(std::shared_ptr<Duin::TextureResource> texture, Duin::Vector2 size)
: texture(texture), size(size)
{
texture->SetTextureSize(size);
}
};
9 changes: 9 additions & 0 deletions ExampleProjects/Astroids/src/Components/Component_Transform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <Duin.h>

struct Component_Transform
{
Duin::Vector2 prevPosition;
Duin::Vector2 position;
};
25 changes: 25 additions & 0 deletions ExampleProjects/Astroids/src/Components/Handler_PlayerInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <Duin.h>

#include "Component_PlayerInput.h"
#include "Component_Movement.h"

struct Handler_PlayerInput
{
Duin::Registry* registry;

Handler_PlayerInput() = default;
Handler_PlayerInput(Duin::Registry* registry)
: registry(registry)
{}

void Update(Duin::InputEvent e)
{
auto view = registry->View<Component_PlayerInput, Component_Movement>();
for (auto [entity, c_pinput, c_movement] : view.each())
{
c_movement.inputDir = e.GetInputVector(Duin::KEY_W, Duin::KEY_S, Duin::KEY_A, Duin::KEY_D);
}
}
};
27 changes: 27 additions & 0 deletions ExampleProjects/Astroids/src/Components/Handler_PlayerMovement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <Duin.h>

#include "Component_PlayerInput.h"
#include "Component_Movement.h"

struct Handler_PlayerMovement
{
Duin::Registry* registry;

Handler_PlayerMovement() = default;
Handler_PlayerMovement(Duin::Registry* registry)
: registry(registry)
{}

void Update()
{
auto view = registry->View<Component_PlayerInput, Component_Movement>();
for (auto [entity, c_pinput, c_movement] : view.each())
{
c_movement.prevVelocity = c_movement.currVelocity;
c_movement.currVelocity += c_movement.inputDir * c_movement.acceleration;
c_movement.currVelocity.LimitLength(0.0f, c_movement.maxSpeed);
}
}
};
25 changes: 25 additions & 0 deletions ExampleProjects/Astroids/src/Components/Handler_Renderable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <Duin.h>

#include "Component_Renderable.h"
#include "Component_Transform.h"

struct Handler_Renderable
{
Duin::Registry* registry;

Handler_Renderable() = default;
Handler_Renderable(Duin::Registry* registry)
: registry(registry)
{}

void Update()
{
auto view = registry->View<Component_Renderable, Component_Transform>();
for (auto [entity, c_renderable, c_transform] : view.each())
{
c_renderable.texture->Draw(c_transform.position);
}
}
};
Loading

0 comments on commit 95af27e

Please sign in to comment.