From 6e23d0d3d89a3fde4b3f8f508e5afea21c845a28 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Fri, 26 Jan 2024 23:51:44 +0100 Subject: [PATCH 1/4] added bounding box --- include/AnimationHandler.h | 18 +++++++++--------- include/KeyboardController.h | 8 ++++---- include/SpriteComponent.h | 4 ++-- src/ColliderComponent.cpp | 9 +++++++++ src/SpriteComponent.cpp | 12 ++++++------ src/TransformComponent.cpp | 14 +++++++++++--- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/include/AnimationHandler.h b/include/AnimationHandler.h index ea6bf71..1fc1024 100644 --- a/include/AnimationHandler.h +++ b/include/AnimationHandler.h @@ -1,22 +1,22 @@ #pragma once - +#include struct Animation { - int index; - int frames; - int speed; + uint8_t index; + uint8_t frames; + uint8_t speed; Animation() {} - Animation(int i, int f, int s) + Animation(uint8_t index, uint8_t frames, uint8_t speed) { - index = i; - frames = f; - speed = s; + this->index = index; + this->frames = frames; + this->speed = speed; } }; -enum AnimationType +enum AnimationType //TODO enum class { IDLE = 0, WALK_R = 1, diff --git a/include/KeyboardController.h b/include/KeyboardController.h index a37ca93..528b0b1 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -11,7 +11,7 @@ class KeyboardController : public Component { public: TransformComponent* transform; - const Uint8* keystates = SDL_GetKeyboardState(NULL); + const uint8_t* keystates = SDL_GetKeyboardState(NULL); SDL_Scancode up; SDL_Scancode down; SDL_Scancode left; @@ -20,9 +20,9 @@ public: SpriteComponent* sprite; - //for attack cooldown in between shots - Uint32 lastFireTime; - Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed + //for attack cooldown in between shots + uint32_t lastFireTime; + uint32_t fireCooldown = 800; //in ms can be adjusted to change possible attack-speed KeyboardController() = default; KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity); diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 5a51c59..d110922 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -21,8 +21,8 @@ private: SDL_Rect srcRect, destRect; bool animated = false; - int frames = 0; - int speed = 100; + uint8_t frames = 0; + uint8_t speed = 100; public: SpriteComponent() = default; diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp index 6418aea..f7b133e 100644 --- a/src/ColliderComponent.cpp +++ b/src/ColliderComponent.cpp @@ -3,6 +3,7 @@ #include "Entity.h" #include "Game.h" #include "TransformComponent.h" +#include ColliderComponent::ColliderComponent(const char* tag) { @@ -10,6 +11,13 @@ ColliderComponent::ColliderComponent(const char* tag) this->hasCollision = true; } +/*ColliderComponent::ColliderComponent(const char* tag, float hitboxScale) //adding hitboxScale helps scaling hitbox and texture/entity seperately +{ + this->tag = tag; + this->hitboxScale + this->hasCollision = true; +}*/ + void ColliderComponent::init() { if (!entity->hasComponent()) { @@ -25,6 +33,7 @@ void ColliderComponent::update() collider.x = transform->position.x; collider.y = transform->position.y; + collider.w = transform->width * transform->scale; collider.h = transform->height * transform->scale; } diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 1d64ffa..c0a279b 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -15,9 +15,9 @@ SpriteComponent::SpriteComponent(const char* path, bool isAnimated) { animated = isAnimated; - Animation* idle = new Animation((int)AnimationType::IDLE, 2, 200); - Animation* walkR = new Animation((int)AnimationType::WALK_R, 2, 200); - Animation* walkL = new Animation((int)AnimationType::WALK_L, 2, 200); + Animation* idle = new Animation((uint8_t)AnimationType::IDLE, 2, 200); + Animation* walkR = new Animation((uint8_t)AnimationType::WALK_R, 2, 200); + Animation* walkL = new Animation((uint8_t)AnimationType::WALK_L, 2, 200); animations.emplace(IDLE, idle); animations.emplace(WALK_R, walkR); @@ -68,7 +68,7 @@ void SpriteComponent::draw() void SpriteComponent::play(AnimationType type) { - animationIndex = animations.at(type)->index; - frames = animations.at(type)->frames; - speed = animations.at(type)->speed; + this->animationIndex = animations.at(type)->index; + this->frames = animations.at(type)->frames; + this->speed = animations.at(type)->speed; } diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 1f71846..403ee69 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -1,4 +1,5 @@ #include "TransformComponent.h" +#include "Constants.h" TransformComponent::TransformComponent() { @@ -41,7 +42,14 @@ void TransformComponent::init() void TransformComponent::update() { // if(velocity.x != 0 && velocity.y != 0) - double multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector - position.x += velocity.x * speed * multiplier; - position.y += velocity.y * speed * multiplier; + + float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector + Vector2D newPos( + position.x + velocity.x * speed * multiplier, + position.y + velocity.y * speed * multiplier + ); + if (newPos.x < 0 || newPos.x > SCREEN_SIZE_WIDTH || newPos.y < 0 || newPos.y > SCREEN_SIZE_HEIGHT) + return; + + position = newPos; } From 59fc17ec251f4a1172d86e0dab8cbd443c75a5d6 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Sat, 27 Jan 2024 16:07:25 +0100 Subject: [PATCH 2/4] refined hitboxes so they are not bigger than the sprite --- include/ColliderComponent.h | 2 ++ src/AssetManager.cpp | 2 +- src/ColliderComponent.cpp | 11 ++++++----- src/Game.cpp | 4 ++-- src/TransformComponent.cpp | 2 +- src/main.cpp | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/ColliderComponent.h b/include/ColliderComponent.h index a4f286d..7c587ae 100644 --- a/include/ColliderComponent.h +++ b/include/ColliderComponent.h @@ -13,8 +13,10 @@ public: const char* tag; TransformComponent* transform; bool hasCollision; //added for removing collision of destroyed projectiles + float hitboxScale; //adds a seperate variable for the scale of the hitbox (not the sprite) so each sprite can have a different hitbox size if needed ColliderComponent(const char* tag); + ColliderComponent(const char* tag, float hitboxScale); void init() override; void update() override; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 63a76c6..766fe27 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -21,6 +21,6 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source projectile.addComponent(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects projectile.addComponent(texturePath); projectile.addComponent(range, speed, velocity, source); - projectile.addComponent("projectile"); + projectile.addComponent("projectile", 0.6f); projectile.addGroup((size_t)GroupLabel::PROJECTILE); } \ No newline at end of file diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp index f7b133e..759c0e2 100644 --- a/src/ColliderComponent.cpp +++ b/src/ColliderComponent.cpp @@ -9,14 +9,15 @@ ColliderComponent::ColliderComponent(const char* tag) { this->tag = tag; this->hasCollision = true; + this->hitboxScale = 1; } -/*ColliderComponent::ColliderComponent(const char* tag, float hitboxScale) //adding hitboxScale helps scaling hitbox and texture/entity seperately +ColliderComponent::ColliderComponent(const char* tag, float hitboxScale) //adding hitboxScale helps scaling hitbox and texture/entity seperately { this->tag = tag; - this->hitboxScale + this->hitboxScale = hitboxScale; this->hasCollision = true; -}*/ +} void ColliderComponent::init() { @@ -34,8 +35,8 @@ void ColliderComponent::update() collider.y = transform->position.y; - collider.w = transform->width * transform->scale; - collider.h = transform->height * transform->scale; + collider.w = (transform->width * transform->scale) * this->hitboxScale; + collider.h = (transform->height * transform->scale) * this->hitboxScale; } void ColliderComponent::removeCollision() diff --git a/src/Game.cpp b/src/Game.cpp index 26fa8d0..a979a8b 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -113,14 +113,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo player.addComponent(80,80,2); //posx, posy, scale player.addComponent("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed player.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(1, 0));//custom keycontrols can be added - player.addComponent("player"); //adds tag (for further use, reference tag) + player.addComponent("player", 0.8f); //adds tag (for further use, reference tag) player.addComponent(5, &manager, true); player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order enemy.addComponent(600, 500, 2); enemy.addComponent("assets/chicken_spritesheet.png", true); enemy.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); - enemy.addComponent("enemy"); + enemy.addComponent("enemy", 0.8f); enemy.addComponent(5, &manager, false); enemy.addGroup((size_t)GroupLabel::ENEMIES); diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 403ee69..9f73934 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -48,7 +48,7 @@ void TransformComponent::update() position.x + velocity.x * speed * multiplier, position.y + velocity.y * speed * multiplier ); - if (newPos.x < 0 || newPos.x > SCREEN_SIZE_WIDTH || newPos.y < 0 || newPos.y > SCREEN_SIZE_HEIGHT) + if (newPos.x < 0 || newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH || newPos.y < 0 || newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT) return; position = newPos; diff --git a/src/main.cpp b/src/main.cpp index c8ee4e3..2e7a993 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char* argv[]) game = new Game(); - game->init("RPG_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); + game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); while (game->running()) { frameStart = SDL_GetTicks(); From 7fbcda681cc68f041126268b026321a07a8a7b07 Mon Sep 17 00:00:00 2001 From: Nanogamer7 <45211068+Nanogamer7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:19:07 +0100 Subject: [PATCH 3/4] Fixes inconsistent indentations on KeyboardController.h --- include/KeyboardController.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 528b0b1..1466719 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -20,8 +20,8 @@ public: SpriteComponent* sprite; - //for attack cooldown in between shots - uint32_t lastFireTime; + //for attack cooldown in between shots + uint32_t lastFireTime; uint32_t fireCooldown = 800; //in ms can be adjusted to change possible attack-speed KeyboardController() = default; @@ -35,4 +35,4 @@ private: //for creation of projectiles TransformComponent* player; //for starting position of projectile Vector2D fireVelocity; //decide source of projectile and flying direction -}; \ No newline at end of file +}; From 679f7ab8c29a3a6e1c982e97a2fc76525b2d0dd4 Mon Sep 17 00:00:00 2001 From: Nanogamer7 <45211068+Nanogamer7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:19:58 +0100 Subject: [PATCH 4/4] Fixed inconsistent indentations in ColliderComponent.cpp --- src/ColliderComponent.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp index 759c0e2..d1c59c0 100644 --- a/src/ColliderComponent.cpp +++ b/src/ColliderComponent.cpp @@ -8,7 +8,7 @@ ColliderComponent::ColliderComponent(const char* tag) { this->tag = tag; - this->hasCollision = true; + this->hasCollision = true; this->hitboxScale = 1; } @@ -41,5 +41,5 @@ void ColliderComponent::update() void ColliderComponent::removeCollision() { - this->hasCollision = false; -} \ No newline at end of file + this->hasCollision = false; +}