mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
added two player movement/made keyboard component reuseable, collision handling sucks though
This commit is contained in:
parent
b73c295363
commit
a3201c77c2
@ -16,6 +16,7 @@ SDL_Event Game::event;
|
|||||||
std::vector<ColliderComponent*> Game::colliders;
|
std::vector<ColliderComponent*> Game::colliders;
|
||||||
|
|
||||||
auto& player(manager.addEntity());
|
auto& player(manager.addEntity());
|
||||||
|
auto& enemy(manager.addEntity());
|
||||||
auto& wall(manager.addEntity());
|
auto& wall(manager.addEntity());
|
||||||
|
|
||||||
Game::Game()
|
Game::Game()
|
||||||
@ -58,11 +59,16 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
|
|
||||||
//ecs implementation
|
//ecs implementation
|
||||||
|
|
||||||
player.addComponent<TransformComponent>(2);
|
player.addComponent<TransformComponent>(0,0,2); //posx, posy, scale
|
||||||
player.addComponent<SpriteComponent>("assets/chicken_neutral_knight.png");
|
player.addComponent<SpriteComponent>("assets/chicken_neutral_knight.png");
|
||||||
player.addComponent<KeyboardController>();
|
player.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D);//custom keycontrols can be added
|
||||||
player.addComponent<ColliderComponent>("player");
|
player.addComponent<ColliderComponent>("player");
|
||||||
|
|
||||||
|
enemy.addComponent<TransformComponent>(400, 400, 2);
|
||||||
|
enemy.addComponent<SpriteComponent>("assets/chicken_neutral.png");
|
||||||
|
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT);
|
||||||
|
enemy.addComponent<ColliderComponent>("enemy");
|
||||||
|
|
||||||
wall.addComponent<TransformComponent>(300.0f, 300.0f, 300, 20, 1);
|
wall.addComponent<TransformComponent>(300.0f, 300.0f, 300, 20, 1);
|
||||||
wall.addComponent<SpriteComponent>("assets/stone.png");
|
wall.addComponent<SpriteComponent>("assets/stone.png");
|
||||||
wall.addComponent<ColliderComponent>("wall");
|
wall.addComponent<ColliderComponent>("wall");
|
||||||
@ -87,6 +93,7 @@ void Game::handleEvents()
|
|||||||
void Game::update()
|
void Game::update()
|
||||||
{
|
{
|
||||||
Vector2D playerPos = player.getComponent<TransformComponent>().position;
|
Vector2D playerPos = player.getComponent<TransformComponent>().position;
|
||||||
|
Vector2D enemyPos = enemy.getComponent<TransformComponent>().position;
|
||||||
|
|
||||||
manager.refresh();
|
manager.refresh();
|
||||||
manager.update();
|
manager.update();
|
||||||
@ -97,9 +104,14 @@ void Game::update()
|
|||||||
{
|
{
|
||||||
player.getComponent<TransformComponent>().position = playerPos;
|
player.getComponent<TransformComponent>().position = playerPos;
|
||||||
}
|
}
|
||||||
|
if (SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "enemy"))
|
||||||
|
{
|
||||||
|
enemy.getComponent<TransformComponent>().position = enemyPos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void Game::render()
|
void Game::render()
|
||||||
{
|
{
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|||||||
43
TestProject/TestProject/KeyboardController.cpp
Normal file
43
TestProject/TestProject/KeyboardController.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include "KeyboardController.h"
|
||||||
|
|
||||||
|
KeyboardController::KeyboardController()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right)
|
||||||
|
{
|
||||||
|
this->up = up;
|
||||||
|
this->down = down;
|
||||||
|
this->left = left;
|
||||||
|
this->right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
KeyboardController::~KeyboardController()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void KeyboardController::init()
|
||||||
|
{
|
||||||
|
transform = &entity->getComponent<TransformComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KeyboardController::update()
|
||||||
|
{
|
||||||
|
transform->velocity.x = 0;
|
||||||
|
transform->velocity.y = 0;
|
||||||
|
|
||||||
|
if (keystates[this->up]) {
|
||||||
|
transform->velocity.y = -1;
|
||||||
|
}
|
||||||
|
if (keystates[this->left]) {
|
||||||
|
transform->velocity.x = -1;
|
||||||
|
}
|
||||||
|
if (keystates[this->down]) {
|
||||||
|
transform->velocity.y = 1;
|
||||||
|
}
|
||||||
|
if (keystates[this->right]) {
|
||||||
|
transform->velocity.x = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,28 +8,17 @@ class KeyboardController : public Component
|
|||||||
public:
|
public:
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
const Uint8* keystates = SDL_GetKeyboardState(NULL);
|
const Uint8* keystates = SDL_GetKeyboardState(NULL);
|
||||||
|
SDL_Scancode up;
|
||||||
|
SDL_Scancode down;
|
||||||
|
SDL_Scancode left;
|
||||||
|
SDL_Scancode right;
|
||||||
|
|
||||||
void init() override
|
KeyboardController();
|
||||||
{
|
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right);
|
||||||
transform = &entity->getComponent<TransformComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void update() override
|
~KeyboardController();
|
||||||
{
|
|
||||||
transform->velocity.x = 0;
|
|
||||||
transform->velocity.y = 0;
|
|
||||||
|
|
||||||
if (keystates[SDL_SCANCODE_UP] || keystates[SDL_SCANCODE_W]) {
|
void init() override;
|
||||||
transform->velocity.y = -1;
|
|
||||||
}
|
void update() override;
|
||||||
if (keystates[SDL_SCANCODE_LEFT] || keystates[SDL_SCANCODE_A]) {
|
|
||||||
transform->velocity.x = -1;
|
|
||||||
}
|
|
||||||
if (keystates[SDL_SCANCODE_DOWN] || keystates[SDL_SCANCODE_S]) {
|
|
||||||
transform->velocity.y = 1;
|
|
||||||
}
|
|
||||||
if (keystates[SDL_SCANCODE_RIGHT] || keystates[SDL_SCANCODE_D]) {
|
|
||||||
transform->velocity.x = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -157,6 +157,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Game.cpp" />
|
<ClCompile Include="Game.cpp" />
|
||||||
<ClCompile Include="GameObject.cpp" />
|
<ClCompile Include="GameObject.cpp" />
|
||||||
|
<ClCompile Include="KeyboardController.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Map.cpp" />
|
<ClCompile Include="Map.cpp" />
|
||||||
<ClCompile Include="TextureManager.cpp" />
|
<ClCompile Include="TextureManager.cpp" />
|
||||||
|
|||||||
@ -36,6 +36,9 @@
|
|||||||
<ClCompile Include="Vector2D.cpp">
|
<ClCompile Include="Vector2D.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="KeyboardController.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Game.h">
|
<ClInclude Include="Game.h">
|
||||||
|
|||||||
@ -2,11 +2,13 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
std::map<int, const char*> textureDict =
|
class TextureDict
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
const std::map<int, const char*> textureDictionary =
|
||||||
{
|
{
|
||||||
{0, "assets/water.png"},
|
{0, "assets/water.png"},
|
||||||
{1, "assets/dirt.png"},
|
{1, "assets/dirt.png"},
|
||||||
{2, "assets/grass.png"}
|
{2, "assets/grass.png"}
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,8 @@ public:
|
|||||||
int tileID;
|
int tileID;
|
||||||
const char* path;
|
const char* path;
|
||||||
|
|
||||||
|
TextureDict textureDict;
|
||||||
|
|
||||||
TileComponent() = default;
|
TileComponent() = default;
|
||||||
|
|
||||||
TileComponent(int x, int y, int w, int h, int id)
|
TileComponent(int x, int y, int w, int h, int id)
|
||||||
@ -24,7 +26,7 @@ public:
|
|||||||
this->tileRect.h = h;
|
this->tileRect.h = h;
|
||||||
tileID = id;
|
tileID = id;
|
||||||
|
|
||||||
auto it = textureDict.find(tileID)->second;
|
auto it = textureDict.textureDictionary.find(tileID)->second; //every id has its own distinct texture (in texturedict.h)
|
||||||
this->path = it;
|
this->path = it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,6 +33,13 @@ public:
|
|||||||
this->position.y = y;
|
this->position.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TransformComponent(float x, float y, int scale)
|
||||||
|
{
|
||||||
|
this->position.x = x;
|
||||||
|
this->position.y = y;
|
||||||
|
this->scale = scale;
|
||||||
|
}
|
||||||
|
|
||||||
TransformComponent(float x, float y, int w, int h, int scale)
|
TransformComponent(float x, float y, int w, int h, int scale)
|
||||||
{
|
{
|
||||||
this->position.x = x;
|
this->position.x = x;
|
||||||
@ -49,7 +56,9 @@ public:
|
|||||||
|
|
||||||
void update() override
|
void update() override
|
||||||
{
|
{
|
||||||
position.x += velocity.x * speed;
|
// if(velocity.x != 0 && velocity.y != 0)
|
||||||
position.y += velocity.y * speed;
|
double multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1;
|
||||||
|
position.x += velocity.x * speed * multiplier;
|
||||||
|
position.y += velocity.y * speed * multiplier;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
BIN
TestProject/TestProject/x64/Debug/KeyboardController.obj
Normal file
BIN
TestProject/TestProject/x64/Debug/KeyboardController.obj
Normal file
Binary file not shown.
Binary file not shown.
@ -1,2 +1,24 @@
|
|||||||
Map.cpp
|
Game.cpp
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\TransformComponent.h(61,36): warning C4244: '+=': conversion from 'double' to 'float', possible loss of data
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\TransformComponent.h(62,36): warning C4244: '+=': conversion from 'double' to 'float', possible loss of data
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\SpriteComponent.h(39,48): warning C4244: '=': conversion from 'float' to 'int', possible loss of data
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\SpriteComponent.h(40,48): warning C4244: '=': conversion from 'float' to 'int', possible loss of data
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\ColliderComponent.h(30,35): warning C4244: '=': conversion from 'float' to 'int', possible loss of data
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\ColliderComponent.h(31,35): warning C4244: '=': conversion from 'float' to 'int', possible loss of data
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\ECS.h(67,34): warning C4244: 'argument': conversion from 'int' to 'float', possible loss of data
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\TileComponent.h(37,15): message : see reference to function template instantiation 'T &Entity::addComponent<TransformComponent,int&,int&,int&,int&,int>(int &,int &,int &,int &,int &&)' being compiled
|
||||||
|
with
|
||||||
|
[
|
||||||
|
T=TransformComponent
|
||||||
|
]
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\ECS.h(67,34): warning C4244: 'argument': conversion from '_Ty' to 'float', possible loss of data
|
||||||
|
with
|
||||||
|
[
|
||||||
|
_Ty=int
|
||||||
|
]
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\Game.cpp(62,8): message : see reference to function template instantiation 'T &Entity::addComponent<TransformComponent,int,int,int>(int &&,int &&,int &&)' being compiled
|
||||||
|
with
|
||||||
|
[
|
||||||
|
T=TransformComponent
|
||||||
|
]
|
||||||
TestProject.vcxproj -> C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\x64\Debug\TestProject.exe
|
TestProject.vcxproj -> C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\x64\Debug\TestProject.exe
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,5 +1,6 @@
|
|||||||
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\Game.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\Game.obj
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\Game.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\Game.obj
|
||||||
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\GameObject.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\GameObject.obj
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\GameObject.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\GameObject.obj
|
||||||
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\KeyboardController.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\KeyboardController.obj
|
||||||
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\main.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\main.obj
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\main.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\main.obj
|
||||||
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\Map.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\Map.obj
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\Map.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\Map.obj
|
||||||
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\TextureManager.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\TextureManager.obj
|
C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\TextureManager.cpp;C:\Users\User\Desktop\FH_TECHNIKUM\3.Semester\INNO1\TestProject\TestProject\x64\Debug\TextureManager.obj
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user