138 lines
3.1 KiB
C++
138 lines
3.1 KiB
C++
#include "ball.hpp"
|
|
#include "ax_funktions.hpp"
|
|
#include <vector>
|
|
#include <math.h>
|
|
|
|
|
|
ball::ball()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
ball::ball(int x, int y, int vx, int vy)
|
|
{
|
|
//std::cout << "Ball erzeugt!"<< std::endl;
|
|
|
|
r_dir = 0;
|
|
g_dir = -1;
|
|
b_dir = 1;
|
|
|
|
velx = vx;
|
|
vely = vy;
|
|
|
|
alive = true;
|
|
|
|
rect = {x,y,20,20};
|
|
color = {255,0,0,255};
|
|
}
|
|
|
|
ball::~ball()
|
|
{
|
|
std::cout << "Ball gelöscht" << std::endl;
|
|
}
|
|
|
|
int ball::GetXPos(){
|
|
return rect.x;
|
|
}
|
|
|
|
int ball::GetYPos(){
|
|
return rect.y;
|
|
}
|
|
|
|
void ball::Draw(SDL_Renderer* renderer){
|
|
rect.x;
|
|
rect.y;
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
}
|
|
|
|
void ball::Update(float f_DeltaTime, std::vector<ball> &list){
|
|
if (this->rect.w <= 1)
|
|
{
|
|
this->alive = false;
|
|
}
|
|
|
|
color.r += r_dir;
|
|
color.g += g_dir;
|
|
color.b += b_dir;
|
|
|
|
//=================ColorCycle======================
|
|
// Clamp values between 1 and 254
|
|
//color.g = ax::clamp_int(color.g, 1, 254);
|
|
//color.b = ax::clamp_int(color.b, 1, 254);
|
|
//color.r = ax::clamp_int(color.r, 1, 254);
|
|
|
|
// Transition logic
|
|
/*if (r_dir != 0 && (color.r == 254 || color.r == 1)) {
|
|
r_dir = 0;
|
|
g_dir = (color.r == 254) ? 1 : -1;
|
|
b_dir = 0;
|
|
}
|
|
else if (g_dir != 0 && (color.g == 254 || color.g == 1)) {
|
|
g_dir = 0;
|
|
b_dir = (color.g == 254) ? 1 : -1;
|
|
r_dir = (color.g == 254) ? -1 : 1;
|
|
}
|
|
else if (b_dir != 0 && (color.b == 254 || color.b == 1)) {
|
|
b_dir = 0;
|
|
r_dir = 0;
|
|
g_dir = (color.b == 254) ? -1 : 1;
|
|
}
|
|
*/
|
|
|
|
//=================Movement======================
|
|
//Update Position
|
|
rect.x +=velx * f_DeltaTime;
|
|
rect.y +=vely * f_DeltaTime;
|
|
|
|
//Kollisioinstest Fenster 1024x768
|
|
if(rect.x > (1024 -20) || rect.x < 0)
|
|
{
|
|
velx = (velx * -0.9) + rand() % 10;
|
|
this->rect.w -=2;
|
|
this->rect.h -=2;
|
|
}
|
|
if(rect.y > (768 - 20) || rect.y < 0){
|
|
vely = (vely * -0.9) + rand() % 10;
|
|
this->rect.w -=2;
|
|
this->rect.h -=2;
|
|
}
|
|
//std::cout << velx <<"|"<< vely <<std::endl;
|
|
|
|
|
|
//==============Kollision=======================
|
|
|
|
for (ball &i : list){
|
|
if(&i == this) continue; // sich selbst überspringen
|
|
float dx = i.GetXPos() - this->GetXPos();
|
|
float dy = i.GetYPos() - this->GetYPos();
|
|
float distance = sqrt(dx*dx + dy*dy);
|
|
|
|
if (distance < this->rect.w)
|
|
{
|
|
this->rect.w /=2;
|
|
this->rect.h /=2;
|
|
|
|
int tempx = this->velx;
|
|
int tempy = this->vely;
|
|
|
|
this->velx = i.velx + rand() % 10;
|
|
this->vely = i.vely + rand() % 10;
|
|
|
|
i.velx = tempx + rand() % 10;
|
|
i.vely = tempy + rand() % 10;
|
|
|
|
// Split ball
|
|
if (list.size() < 1000)
|
|
{
|
|
list.emplace_back(ball(this->GetXPos() + 21 , this->GetYPos()+1, this->velx, (rand() % 200) -100));
|
|
list.back().rect.w /=2;
|
|
list.back().rect.h /=2;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|