0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00
SDL_Minigame/src/TransformComponent.cpp
2024-01-29 20:53:35 +01:00

75 lines
1.4 KiB
C++

#include "TransformComponent.h"
#include "Constants.h"
#include "SoundManager.h"
TransformComponent::TransformComponent()
{
position.zero();
}
TransformComponent::TransformComponent(int scale)
{
position.zero();
this->scale = scale;
}
TransformComponent::TransformComponent(float x, float y)
{
this->position.x = x;
this->position.y = y;
}
TransformComponent::TransformComponent(float x, float y, int scale)
{
this->position.x = x;
this->position.y = y;
this->scale = scale;
}
TransformComponent::TransformComponent(float x, float y, int w, int h, int scale)
{
this->position.x = x;
this->position.y = y;
this->width = w;
this->height = h;
this->scale = scale;
}
void TransformComponent::init()
{
velocity.zero();
}
void TransformComponent::update()
{
// if(velocity.x != 0 && velocity.y != 0)
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 = 0;
}
else if (newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH)
{
newPos.x = SCREEN_SIZE_WIDTH - (this->width * this->scale);
}
if (newPos.y < 0)
{
newPos.y = 0;
}
else if (newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT)
{
newPos.y = SCREEN_SIZE_HEIGHT - (this->height * this->scale);
}
position = newPos;
}