0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

reformatted inconsistent code and optimized imports

This commit is contained in:
Benedikt Galbavy 2024-01-24 16:28:41 +01:00
parent f36632c9fd
commit baf3a206b6
35 changed files with 219 additions and 216 deletions

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 101 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 69 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 44 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -1,13 +1,12 @@
#include <SDL_render.h>
#include <map> #include <map>
#include <string> #include <string>
#include "TextureManager.h" class Vector2D;
#include "Vector2D.h" class Manager;
#include "Components.h"
#include "ECS.h"
class AssetManager {
class AssetManager
{
public: public:
AssetManager(Manager* manager); AssetManager(Manager* manager);

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "SDL.h"
#include <SDL.h>
#include "Component.h" #include "Component.h"
class TransformComponent; class TransformComponent;

View File

@ -17,20 +17,9 @@ class Component
public: public:
Entity* entity; Entity* entity;
virtual void init() virtual void init() {}
{ virtual void update() {}
// implementation in derived classes (when neccessary) virtual void draw() {}
}
virtual void update()
{
// implementation in derived classes (when neccessary)
}
virtual void draw()
{
// implementation in derived classes (when neccessary)
}
virtual ~Component() = default; virtual ~Component() = default;
}; };

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "ECS.h" #include "ECS.h"
#include "Component.h" #include "Component.h"
#include "Manager.h" #include "Manager.h"

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <iostream>
#include <cstddef>
using Group = std::size_t; using Group = std::size_t;

View File

@ -1,14 +1,8 @@
#pragma once #pragma once
#include <iostream> #include "Component.h"
#include <vector>
#include <memory>
#include <algorithm>
#include <bitset>
#include <array>
#include "Constants.h" #include "Constants.h"
class Component; class Component;
class Entity; class Entity;
class Manager; class Manager;

View File

@ -1,13 +1,15 @@
#pragma once #pragma once
#include <array> #include <array>
#include <vector> #include <memory>
#include <bitset> #include <bitset>
#include "Constants.h" #include <vector>
#include "Component.h"
#include "SpriteComponent.h"
#include "ECS.h" #include "ECS.h"
#include "Constants.h"
class Manager; class Manager;
class Component;
using ComponentBitSet = std::bitset<MAX_COMPONENTS>; using ComponentBitSet = std::bitset<MAX_COMPONENTS>;
using GroupBitSet = std::bitset<MAX_GROUPS>; using GroupBitSet = std::bitset<MAX_GROUPS>;

View File

@ -1,18 +1,15 @@
#pragma once #pragma once
#include <stdio.h>
#include <iostream>
#include <SDL.h> #include <SDL.h>
#include <SDL_image.h> #include <SDL_image.h>
#include <vector> #include <vector>
#include <string.h>
class AssetManager; class AssetManager;
class ColliderComponent; class ColliderComponent;
class Game class Game
{ {
public: public:
Game(); Game();
~Game(); ~Game();
@ -32,7 +29,7 @@ class Game
bool getWinner(); bool getWinner();
private: private:
int counter = 0; int counter = 0;
bool isRunning = false; bool isRunning = false;
SDL_Window* window; SDL_Window* window;

View File

@ -3,14 +3,14 @@
class GameObject class GameObject
{ {
public: public:
GameObject(const char* texturesheet, int x, int y); GameObject(const char* texturesheet, int x, int y);
~GameObject() = default; ~GameObject() = default;
void update(); void update();
void render(); void render();
private: private:
int xPos; int xPos;
int yPos; int yPos;

View File

@ -1,65 +1,29 @@
#include "Components.h" #pragma once
class HealthComponent : public Component { #include "Component.h"
class Manager;
class HealthComponent : public Component
{
public: public:
HealthComponent(int health, Manager* manager, bool player) : health(health), manager(manager), player(player) {} HealthComponent(int health, Manager* manager, bool player) : health(health), manager(manager), player(player) {}
~HealthComponent() {} ~HealthComponent() {}
void getDamage() { void getDamage() { this->health--; }
this->health--; int getHealth() { return this->health; }
}
int getHealth() { void init() override;
return this->health;
}
void init() override void createAllHearts();
{ void createHeartComponents(int x);
createAllHearts();
}
void createAllHearts() {
int x; //starting position for first health icon
if(player) {
x = 10;
} else {
x = 730;
}
for(int i = 0; i < health; i++) {
//checks for player side
if(player) {
createHeartComponents(x);
x += 50;
continue;
}
createHeartComponents(x);
x -= 50;
}
}
void createHeartComponents(int x) {
auto& heart(manager->addEntity());
heart.addComponent<TransformComponent>(x,5,2);
heart.addComponent<SpriteComponent>("assets/heart.png");
heart.addGroup((size_t)GroupLabel::HEARTS);
}
private: private:
int health; int health;
Manager* manager; Manager* manager;
bool player; //true if player1 / false if player2 bool player; //true if player1 / false if player2
}; };

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <SDL.h> #include <SDL.h>
#include "Component.h" #include "Component.h"
#include "Vector2D.h" #include "Vector2D.h"

View File

@ -3,6 +3,7 @@
#include <array> #include <array>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "Constants.h" #include "Constants.h"
class Entity; class Entity;

View File

@ -1,46 +1,24 @@
#pragma once #pragma once
#include "ECS.h" #include "Component.h"
#include "Components.h"
#include "Vector2D.h" #include "Vector2D.h"
class ProjectileComponent : public Component { class TransformComponent;
class ProjectileComponent : public Component
{
//can maybe be split in separate .cpp file //can maybe be split in separate .cpp file
public: public:
ProjectileComponent(int range, int speed, Vector2D velocity, bool source) : range(range), speed(speed), velocity(velocity), source(source) {}
ProjectileComponent(int range, int speed, Vector2D velocity, bool source) : range(range), speed(speed), velocity(velocity), source(source) {
}
~ProjectileComponent() {} ~ProjectileComponent() {}
void init() override { void init() override;
transformComponent = &entity->getComponent<TransformComponent>(); void update() override;
}
void update() override {
transformComponent->velocity = velocity;
distance += speed;
if (distance > range) {
entity->destroy();
entity->getComponent<ColliderComponent>().removeCollision();
//std::cout << "out of range" << std::endl;
}
}
bool getSource() {
return this->source;
}
bool getSource() { return this->source; }
private: private:
TransformComponent* transformComponent; TransformComponent* transformComponent;
int range = 0; int range = 0;

View File

@ -1,19 +1,21 @@
#pragma once #pragma once
#include <map>
#include <SDL_render.h>
#include "AnimationHandler.h" #include "AnimationHandler.h"
#include "Component.h" #include "Component.h"
#include "Game.h"
#include <map>
class TransformComponent; class TransformComponent;
class SpriteComponent : public Component class SpriteComponent : public Component
{ {
public: public:
int animationIndex = 0; int animationIndex = 0;
std::map<AnimationType, Animation*> animations; std::map<AnimationType, Animation*> animations;
private: private:
TransformComponent* transform; TransformComponent* transform;
SDL_Texture* texture; SDL_Texture* texture;
SDL_Rect srcRect, destRect; SDL_Rect srcRect, destRect;
@ -22,7 +24,7 @@ class SpriteComponent : public Component
int frames = 0; int frames = 0;
int speed = 100; int speed = 100;
public: public:
SpriteComponent() = default; SpriteComponent() = default;
SpriteComponent(const char* path); SpriteComponent(const char* path);
SpriteComponent(const char* path, bool isAnimated); SpriteComponent(const char* path, bool isAnimated);

View File

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <map> #include <map>
#include <string> #include <string>
class TextureDict class TextureDict
{ {
public: public:
const std::map<int, std::string> textureDictionary = const std::map<int, std::string> textureDictionary = {
{
{1, "assets/water.png"}, {1, "assets/water.png"},
{2, "assets/dirt.png"}, {2, "assets/dirt.png"},
{3, "assets/grass.png"}, {3, "assets/grass.png"},

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "SDL.h"
#include <SDL.h>
#include "Component.h" #include "Component.h"
#include "TextureDict.h" #include "TextureDict.h"

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include "Vector2D.h"
#include "Component.h" #include "Component.h"
#include "Vector2D.h"
class TransformComponent : public Component class TransformComponent : public Component
{ {
public: public:
Vector2D position; Vector2D position;
Vector2D velocity; Vector2D velocity;

View File

@ -2,7 +2,7 @@
class Vector2D class Vector2D
{ {
public: public:
float x; float x;
float y; float y;

View File

@ -1,11 +1,9 @@
#include "AssetManager.h" #include "AssetManager.h"
#include "Component.h"
#include "TextureManager.h"
#include "Components.h" #include "Components.h"
#include <cstddef>
AssetManager::AssetManager(Manager* manager) : man(manager) { AssetManager::AssetManager(Manager* manager) : man(manager) {}
}
AssetManager::~AssetManager() {} AssetManager::~AssetManager() {}

View File

@ -1,7 +1,8 @@
#include "ColliderComponent.h" #include "ColliderComponent.h"
#include "TransformComponent.h"
#include "Entity.h" #include "Entity.h"
#include "Game.h" #include "Game.h"
#include "TransformComponent.h"
ColliderComponent::ColliderComponent(const char* tag) ColliderComponent::ColliderComponent(const char* tag)
{ {
@ -11,10 +12,10 @@ ColliderComponent::ColliderComponent(const char* tag)
void ColliderComponent::init() void ColliderComponent::init()
{ {
if (!entity->hasComponent<TransformComponent>()) if (!entity->hasComponent<TransformComponent>()) {
{
entity->addComponent<TransformComponent>(); entity->addComponent<TransformComponent>();
} }
transform = &entity->getComponent<TransformComponent>(); transform = &entity->getComponent<TransformComponent>();
Game::colliders.push_back(this); Game::colliders.push_back(this);
} }

View File

@ -1,5 +1,5 @@
#pragma once
#include "Entity.h" #include "Entity.h"
#include "Manager.h" #include "Manager.h"
#include "Component.h" #include "Component.h"

View File

@ -1,15 +1,10 @@
#include "Game.h" #include "Game.h"
#include "TextureManager.h"
#include "Manager.h" #include "Components.h"
#include "Map.h"
#include "Entity.h"
#include "Component.h"
#include "TransformComponent.h"
#include "TileComponent.h"
#include "ColliderComponent.h"
#include "SpriteComponent.h"
#include "KeyboardController.h"
#include "AssetManager.h" #include "AssetManager.h"
#include "Map.h"
#include "TextureManager.h"
Map* map; Map* map;
Manager manager; Manager manager;

View File

@ -1,4 +1,5 @@
#include "GameObject.h" #include "GameObject.h"
#include "TextureManager.h" #include "TextureManager.h"
#include "Game.h" #include "Game.h"

40
src/HealthComponent.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "HealthComponent.h"
#include "Components.h"
void HealthComponent::init()
{
createAllHearts();
}
void HealthComponent::createAllHearts()
{
int x; //starting position for first health icon
if(player) {
x = 10;
} else {
x = 730;
}
for(int i = 0; i < health; i++) {
//checks for player side
if(player) {
createHeartComponents(x);
x += 50;
continue;
}
createHeartComponents(x);
x -= 50;
}
}
void HealthComponent::createHeartComponents(int x)
{
auto& heart(manager->addEntity());
heart.addComponent<TransformComponent>(x,5,2);
heart.addComponent<SpriteComponent>("assets/heart.png");
heart.addGroup((size_t)GroupLabel::HEARTS);
}

View File

@ -1,8 +1,8 @@
#include "KeyboardController.h" #include "KeyboardController.h"
#include "TransformComponent.h"
#include "Entity.h" #include "Game.h"
#include "Components.h"
#include "AssetManager.h" #include "AssetManager.h"
#include "SpriteComponent.h"
KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity) KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity)
{ {

View File

@ -1,4 +1,7 @@
#include "Manager.h" #include "Manager.h"
#include <algorithm>
#include "Entity.h" #include "Entity.h"
void Manager::update() void Manager::update()

View File

@ -1,5 +1,7 @@
#include "Map.h" #include "Map.h"
#include <fstream> #include <fstream>
#include "Constants.h" #include "Constants.h"
#include "Game.h" #include "Game.h"

View File

@ -0,0 +1,21 @@
#include "ProjectileComponent.h"
#include "Components.h"
void ProjectileComponent::init()
{
transformComponent = &entity->getComponent<TransformComponent>();
}
void ProjectileComponent::update()
{
transformComponent->velocity = velocity;
distance += speed;
if (distance > range) {
entity->destroy();
entity->getComponent<ColliderComponent>().removeCollision();
//std::cout << "out of range" << std::endl;
}
}

View File

@ -1,7 +1,10 @@
#include "AnimationHandler.h" #include "SpriteComponent.h"
#include "TransformComponent.h"
#include "Entity.h" #include <SDL_timer.h>
#include "TextureManager.h" #include "TextureManager.h"
#include "Entity.h"
#include "TransformComponent.h"
SpriteComponent::SpriteComponent(const char* path) SpriteComponent::SpriteComponent(const char* path)
{ {

View File

@ -1,7 +1,8 @@
#include "TextureManager.h" #include "TextureManager.h"
#include <cstdio>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include "Game.h" #include "Game.h"
SDL_Texture* TextureManager::loadTexture(const char* fileName) SDL_Texture* TextureManager::loadTexture(const char* fileName)

View File

@ -1,6 +1,11 @@
#include "TileComponent.h" #include "TileComponent.h"
#include <iostream>
#include "Entity.h" #include "Entity.h"
#include "TransformComponent.h" #include "TransformComponent.h"
#include "SpriteComponent.h"
#include "TileComponent.h"
TileComponent::TileComponent(int x, int y, int w, int h, int id) TileComponent::TileComponent(int x, int y, int w, int h, int id)
{ {
@ -11,8 +16,7 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id)
tileID = id; tileID = id;
auto it = textureDict.textureDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h) auto it = textureDict.textureDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h)
if (it == textureDict.textureDictionary.end()) if (it == textureDict.textureDictionary.end()) {
{
std::cout << "it end" << std::endl; std::cout << "it end" << std::endl;
return; return;
} }