mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
Merge pull request #16 from Nimac0/bounding-box
implemented bounding box
This commit is contained in:
commit
42858da5fc
@ -1,22 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <stdint.h>
|
||||||
struct Animation
|
struct Animation
|
||||||
{
|
{
|
||||||
int index;
|
uint8_t index;
|
||||||
int frames;
|
uint8_t frames;
|
||||||
int speed;
|
uint8_t speed;
|
||||||
|
|
||||||
Animation() {}
|
Animation() {}
|
||||||
|
|
||||||
Animation(int i, int f, int s)
|
Animation(uint8_t index, uint8_t frames, uint8_t speed)
|
||||||
{
|
{
|
||||||
index = i;
|
this->index = index;
|
||||||
frames = f;
|
this->frames = frames;
|
||||||
speed = s;
|
this->speed = speed;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
enum AnimationType
|
enum AnimationType //TODO enum class
|
||||||
{
|
{
|
||||||
IDLE = 0,
|
IDLE = 0,
|
||||||
WALK_R = 1,
|
WALK_R = 1,
|
||||||
|
|||||||
@ -13,8 +13,10 @@ public:
|
|||||||
const char* tag;
|
const char* tag;
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
bool hasCollision; //added for removing collision of destroyed projectiles
|
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);
|
||||||
|
ColliderComponent(const char* tag, float hitboxScale);
|
||||||
|
|
||||||
void init() override;
|
void init() override;
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|||||||
@ -11,7 +11,7 @@ class KeyboardController : public Component
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
const Uint8* keystates = SDL_GetKeyboardState(NULL);
|
const uint8_t* keystates = SDL_GetKeyboardState(NULL);
|
||||||
SDL_Scancode up;
|
SDL_Scancode up;
|
||||||
SDL_Scancode down;
|
SDL_Scancode down;
|
||||||
SDL_Scancode left;
|
SDL_Scancode left;
|
||||||
@ -21,8 +21,8 @@ public:
|
|||||||
SpriteComponent* sprite;
|
SpriteComponent* sprite;
|
||||||
|
|
||||||
//for attack cooldown in between shots
|
//for attack cooldown in between shots
|
||||||
Uint32 lastFireTime;
|
uint32_t lastFireTime;
|
||||||
Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
|
uint32_t fireCooldown = 800; //in ms can be adjusted to change possible attack-speed
|
||||||
|
|
||||||
KeyboardController() = default;
|
KeyboardController() = default;
|
||||||
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity);
|
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity);
|
||||||
|
|||||||
@ -21,8 +21,8 @@ private:
|
|||||||
SDL_Rect srcRect, destRect;
|
SDL_Rect srcRect, destRect;
|
||||||
|
|
||||||
bool animated = false;
|
bool animated = false;
|
||||||
int frames = 0;
|
uint8_t frames = 0;
|
||||||
int speed = 100;
|
uint8_t speed = 100;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SpriteComponent() = default;
|
SpriteComponent() = default;
|
||||||
|
|||||||
@ -21,6 +21,6 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source
|
|||||||
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
|
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
|
||||||
projectile.addComponent<SpriteComponent>(texturePath);
|
projectile.addComponent<SpriteComponent>(texturePath);
|
||||||
projectile.addComponent<ProjectileComponent>(range, speed, velocity, source);
|
projectile.addComponent<ProjectileComponent>(range, speed, velocity, source);
|
||||||
projectile.addComponent<ColliderComponent>("projectile");
|
projectile.addComponent<ColliderComponent>("projectile", 0.6f);
|
||||||
projectile.addGroup((size_t)GroupLabel::PROJECTILE);
|
projectile.addGroup((size_t)GroupLabel::PROJECTILE);
|
||||||
}
|
}
|
||||||
@ -3,11 +3,20 @@
|
|||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include "TransformComponent.h"
|
#include "TransformComponent.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
ColliderComponent::ColliderComponent(const char* tag)
|
ColliderComponent::ColliderComponent(const char* tag)
|
||||||
{
|
{
|
||||||
this->tag = tag;
|
this->tag = tag;
|
||||||
this->hasCollision = true;
|
this->hasCollision = true;
|
||||||
|
this->hitboxScale = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ColliderComponent::ColliderComponent(const char* tag, float hitboxScale) //adding hitboxScale helps scaling hitbox and texture/entity seperately
|
||||||
|
{
|
||||||
|
this->tag = tag;
|
||||||
|
this->hitboxScale = hitboxScale;
|
||||||
|
this->hasCollision = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColliderComponent::init()
|
void ColliderComponent::init()
|
||||||
@ -25,8 +34,9 @@ void ColliderComponent::update()
|
|||||||
collider.x = transform->position.x;
|
collider.x = transform->position.x;
|
||||||
collider.y = transform->position.y;
|
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()
|
void ColliderComponent::removeCollision()
|
||||||
|
|||||||
@ -113,14 +113,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
player.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
player.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
||||||
player.addComponent<SpriteComponent>("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed
|
player.addComponent<SpriteComponent>("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed
|
||||||
player.addComponent<KeyboardController>(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<KeyboardController>(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<ColliderComponent>("player"); //adds tag (for further use, reference tag)
|
player.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
|
||||||
player.addComponent<HealthComponent>(5, &manager, true);
|
player.addComponent<HealthComponent>(5, &manager, true);
|
||||||
player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
|
player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
|
||||||
|
|
||||||
enemy.addComponent<TransformComponent>(600, 500, 2);
|
enemy.addComponent<TransformComponent>(600, 500, 2);
|
||||||
enemy.addComponent<SpriteComponent>("assets/chicken_spritesheet.png", true);
|
enemy.addComponent<SpriteComponent>("assets/chicken_spritesheet.png", true);
|
||||||
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0));
|
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0));
|
||||||
enemy.addComponent<ColliderComponent>("enemy");
|
enemy.addComponent<ColliderComponent>("enemy", 0.8f);
|
||||||
enemy.addComponent<HealthComponent>(5, &manager, false);
|
enemy.addComponent<HealthComponent>(5, &manager, false);
|
||||||
enemy.addGroup((size_t)GroupLabel::ENEMIES);
|
enemy.addGroup((size_t)GroupLabel::ENEMIES);
|
||||||
|
|
||||||
|
|||||||
@ -15,9 +15,9 @@ SpriteComponent::SpriteComponent(const char* path, bool isAnimated)
|
|||||||
{
|
{
|
||||||
animated = isAnimated;
|
animated = isAnimated;
|
||||||
|
|
||||||
Animation* idle = new Animation((int)AnimationType::IDLE, 2, 200);
|
Animation* idle = new Animation((uint8_t)AnimationType::IDLE, 2, 200);
|
||||||
Animation* walkR = new Animation((int)AnimationType::WALK_R, 2, 200);
|
Animation* walkR = new Animation((uint8_t)AnimationType::WALK_R, 2, 200);
|
||||||
Animation* walkL = new Animation((int)AnimationType::WALK_L, 2, 200);
|
Animation* walkL = new Animation((uint8_t)AnimationType::WALK_L, 2, 200);
|
||||||
|
|
||||||
animations.emplace(IDLE, idle);
|
animations.emplace(IDLE, idle);
|
||||||
animations.emplace(WALK_R, walkR);
|
animations.emplace(WALK_R, walkR);
|
||||||
@ -68,7 +68,7 @@ void SpriteComponent::draw()
|
|||||||
|
|
||||||
void SpriteComponent::play(AnimationType type)
|
void SpriteComponent::play(AnimationType type)
|
||||||
{
|
{
|
||||||
animationIndex = animations.at(type)->index;
|
this->animationIndex = animations.at(type)->index;
|
||||||
frames = animations.at(type)->frames;
|
this->frames = animations.at(type)->frames;
|
||||||
speed = animations.at(type)->speed;
|
this->speed = animations.at(type)->speed;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
#include "TransformComponent.h"
|
#include "TransformComponent.h"
|
||||||
|
#include "Constants.h"
|
||||||
|
|
||||||
TransformComponent::TransformComponent()
|
TransformComponent::TransformComponent()
|
||||||
{
|
{
|
||||||
@ -41,7 +42,14 @@ void TransformComponent::init()
|
|||||||
void TransformComponent::update()
|
void TransformComponent::update()
|
||||||
{
|
{
|
||||||
// if(velocity.x != 0 && velocity.y != 0)
|
// 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;
|
float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector
|
||||||
position.y += velocity.y * speed * multiplier;
|
Vector2D newPos(
|
||||||
|
position.x + velocity.x * speed * multiplier,
|
||||||
|
position.y + velocity.y * speed * multiplier
|
||||||
|
);
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
game = new Game();
|
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())
|
while (game->running())
|
||||||
{
|
{
|
||||||
frameStart = SDL_GetTicks();
|
frameStart = SDL_GetTicks();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user