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

added bounding box

This commit is contained in:
Nimac0 2024-01-26 23:51:44 +01:00
parent 251d7f6661
commit 6e23d0d3d8
6 changed files with 41 additions and 24 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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;

View File

@ -3,6 +3,7 @@
#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)
{ {
@ -10,6 +11,13 @@ ColliderComponent::ColliderComponent(const char* tag)
this->hasCollision = true; 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() void ColliderComponent::init()
{ {
if (!entity->hasComponent<TransformComponent>()) { if (!entity->hasComponent<TransformComponent>()) {
@ -25,6 +33,7 @@ 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.w = transform->width * transform->scale;
collider.h = transform->height * transform->scale; collider.h = transform->height * transform->scale;
} }

View File

@ -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;
} }

View File

@ -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 > SCREEN_SIZE_WIDTH || newPos.y < 0 || newPos.y > SCREEN_SIZE_HEIGHT)
return;
position = newPos;
} }