0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +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 <string>
#include "TextureManager.h"
#include "Vector2D.h"
#include "Components.h"
#include "ECS.h"
class AssetManager {
class Vector2D;
class Manager;
class AssetManager
{
public:
AssetManager(Manager* manager);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,42 +1,39 @@
#pragma once
#include <stdio.h>
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <vector>
#include <string.h>
class AssetManager;
class ColliderComponent;
class Game
{
public:
Game();
~Game();
public:
Game();
~Game();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void handleEvents();
void update();
void render();
void clean();
bool running() const;
void handleEvents();
void update();
void render();
void clean();
bool running() const;
static void addTile(int id, int x, int y);
static SDL_Renderer* renderer;
static SDL_Event event;
static std::vector<ColliderComponent*> colliders;
static AssetManager* assets;
static void addTile(int id, int x, int y);
static SDL_Renderer* renderer;
static SDL_Event event;
static std::vector<ColliderComponent*> colliders;
static AssetManager* assets;
bool getWinner();
bool getWinner();
private:
int counter = 0;
bool isRunning = false;
SDL_Window* window;
private:
int counter = 0;
bool isRunning = false;
SDL_Window* window;
//true for player1 win / false for player2 win;
bool winner;
//true for player1 win / false for player2 win;
bool winner;
};

View File

@ -3,19 +3,19 @@
class GameObject
{
public:
GameObject(const char* texturesheet, int x, int y);
~GameObject() = default;
public:
GameObject(const char* texturesheet, int x, int y);
~GameObject() = default;
void update();
void render();
void update();
void render();
private:
int xPos;
int yPos;
private:
int xPos;
int yPos;
SDL_Texture* objTexture;
SDL_Rect srcRect;
SDL_Rect destRect;
SDL_Texture* objTexture;
SDL_Rect srcRect;
SDL_Rect destRect;
};

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:
HealthComponent(int health, Manager* manager, bool player) : health(health), manager(manager), player(player) {}
~HealthComponent() {}
void getDamage() {
this->health--;
}
void getDamage() { this->health--; }
int getHealth() { return this->health; }
int getHealth() {
return this->health;
}
void init() override;
void init() override
{
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);
}
void createAllHearts();
void createHeartComponents(int x);
private:
int health;
Manager* manager;
bool player; //true if player1 / false if player2
};

View File

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

View File

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

View File

@ -1,46 +1,24 @@
#pragma once
#include "ECS.h"
#include "Components.h"
#include "Component.h"
#include "Vector2D.h"
class ProjectileComponent : public Component {
class TransformComponent;
class ProjectileComponent : public Component
{
//can maybe be split in separate .cpp file
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() {}
void init() override {
transformComponent = &entity->getComponent<TransformComponent>();
}
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;
}
void init() override;
void update() override;
bool getSource() { return this->source; }
private:
TransformComponent* transformComponent;
int range = 0;

View File

@ -1,37 +1,39 @@
#pragma once
#include <map>
#include <SDL_render.h>
#include "AnimationHandler.h"
#include "Component.h"
#include "Game.h"
#include <map>
class TransformComponent;
class SpriteComponent : public Component
{
public:
int animationIndex = 0;
public:
int animationIndex = 0;
std::map<AnimationType, Animation*> animations;
std::map<AnimationType, Animation*> animations;
private:
TransformComponent* transform;
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
private:
TransformComponent* transform;
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
bool animated = false;
int frames = 0;
int speed = 100;
bool animated = false;
int frames = 0;
int speed = 100;
public:
SpriteComponent() = default;
SpriteComponent(const char* path);
SpriteComponent(const char* path, bool isAnimated);
~SpriteComponent();
public:
SpriteComponent() = default;
SpriteComponent(const char* path);
SpriteComponent(const char* path, bool isAnimated);
~SpriteComponent();
void setTexture(const char* path);
void setTexture(const char* path);
void init() override;
void update() override;
void draw() override;
void play(AnimationType type);
void init() override;
void update() override;
void draw() override;
void play(AnimationType type);
};

View File

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

View File

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

View File

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

View File

@ -2,18 +2,18 @@
class Vector2D
{
public:
float x;
float y;
public:
float x;
float y;
Vector2D();
Vector2D(float x, float y);
Vector2D();
Vector2D(float x, float y);
friend Vector2D& operator+(Vector2D& vector1, const Vector2D& vector2);
friend Vector2D& operator-(Vector2D& vector1, const Vector2D& vector2);
friend Vector2D& operator*(Vector2D& vector1, const Vector2D& vector2);
friend Vector2D& operator/(Vector2D& vector1, const Vector2D& vector2);
friend Vector2D& operator+(Vector2D& vector1, const Vector2D& vector2);
friend Vector2D& operator-(Vector2D& vector1, const Vector2D& vector2);
friend Vector2D& operator*(Vector2D& vector1, const Vector2D& vector2);
friend Vector2D& operator/(Vector2D& vector1, const Vector2D& vector2);
Vector2D& operator*(const int& i);
Vector2D& zero();
Vector2D& operator*(const int& i);
Vector2D& zero();
};

View File

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

View File

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

View File

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

View File

@ -1,15 +1,10 @@
#include "Game.h"
#include "TextureManager.h"
#include "Manager.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 "Components.h"
#include "AssetManager.h"
#include "Map.h"
#include "TextureManager.h"
Map* map;
Manager manager;

View File

@ -1,4 +1,5 @@
#include "GameObject.h"
#include "TextureManager.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 "TransformComponent.h"
#include "Entity.h"
#include "Game.h"
#include "Components.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)
{

View File

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

View File

@ -1,5 +1,7 @@
#include "Map.h"
#include <fstream>
#include "Constants.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 "TransformComponent.h"
#include "Entity.h"
#include "SpriteComponent.h"
#include <SDL_timer.h>
#include "TextureManager.h"
#include "Entity.h"
#include "TransformComponent.h"
SpriteComponent::SpriteComponent(const char* path)
{

View File

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

View File

@ -1,6 +1,11 @@
#include "TileComponent.h"
#include <iostream>
#include "Entity.h"
#include "TransformComponent.h"
#include "SpriteComponent.h"
#include "TileComponent.h"
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;
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;
return;
}