Thank you guys for advises.
Never thought of containers before, gonna google it now.
I've cleaned code as much as possible.
For now my collision is based on x, and y coordinates, it works sometimes.
But now I need to solve one problem: when bubble is eaten, the new bubble should be drawn, so there are always 10 bubbles on the screen. I guess that container thing gonna help.
#include <SFML/Graphics.hpp>
#include "objects.h"
#include <stdlib.h>
#include <math.h>
#include <Thor/Math.hpp>
//Globals
sf::VideoMode mode = sf::VideoMode::getDesktopMode(); // Get Your Desktop resolution
int Score;
int speed = 15;
const int NUM_ENEMIES = 10;
int main()
{ float timer;
//Playerdata
Player p;
p.x = mode.width / 2;
p.y = -mode.height / 2;
p.speed = speed + 10;
p.scale = 1.5;
p.SWidth = mode.width;
p.SHeight = mode.height;
//EnemyData
AngryBubble enemy[10];
float a, b;
a = 0.2;
b = 3.5;
for( int i = 0; i < NUM_ENEMIES; i++)
{
enemy[i].position.x = thor::random(0,mode.width);
enemy[i].position.y = thor::random(0,mode.height);
enemy[i].speed = 3;
enemy[i].scale = thor::random(a, b);
enemy[i].angry = false;
enemy[i].alive = false;
}
//Open window with your display resolution
sf::RenderWindow window(sf::VideoMode(mode.width,mode.height), "Bubble!");
sf::Clock clock;
p.DrawPlayer();
for( int i = 0; i < NUM_ENEMIES; i++)
{ if (!enemy[i].alive) // need to change later
enemy[i].alive = true;
enemy[i].DrawEnemy();
}
while (window.isOpen())
{
sf::Event event;
sf::Keyboard keyevent;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//Controlls
p.PlayerMove();
//Enemy logic
// Movement
for( int i = 0; i < NUM_ENEMIES; i++) //Angry Enemies ( if enemy is bigger than you it going to angry and it'll try to eat you...)
{ if(enemy[i].enemy.getScale().x > p.PlayerBubble.getScale().x)
{
if(p.PlayerBubble.getPosition().y < enemy[i].enemy.getPosition().y)
{
enemy[i].enemy.move(0, -enemy[i].speed);
if(enemy[i].enemy.getPosition().y < 0) // Enemy won't go off the screen
enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, 0);
}
if(p.PlayerBubble.getPosition().y > enemy[i].enemy.getPosition().y)
{
enemy[i].enemy.move(0, enemy[i].speed);
if(enemy[i].enemy.getPosition().y > mode.height)
enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, mode.height);
}
if(p.PlayerBubble.getPosition().x > enemy[i].enemy.getPosition().x)
{
enemy[i].enemy.move(enemy[i].speed, 0);
if(enemy[i].enemy.getPosition().x > mode.width)
enemy[i].enemy.setPosition(mode.width, enemy[i].enemy.getPosition().y);
}
if(p.PlayerBubble.getPosition().x < enemy[i].enemy.getPosition().x)
{
enemy[i].enemy.move(-enemy[i].speed, 0);
if(enemy[i].enemy.getPosition().x < 0)
enemy[i].enemy.setPosition(0, enemy[i].enemy.getPosition().y);
if(enemy[i].enemy.getPosition().x == p.PlayerBubble.getPosition().x || enemy[i].enemy.getPosition().y == p.PlayerBubble.getPosition().y)
{ enemy[i].alive = false;
p.PlayerBubble.setScale(p.scale - 1, p.scale - 1);
}
}
}
}
for( int i = 0; i < NUM_ENEMIES; i++) //Non-Angry Enemies ( if enemy is smaller than you it going to run away from you...)
{ if(enemy[i].enemy.getScale().x < p.PlayerBubble.getScale().x) {
if(p.PlayerBubble.getPosition().y > enemy[i].enemy.getPosition().y)
{
enemy[i].enemy.move(0, -enemy[i].speed);
if(enemy[i].enemy.getPosition().y < 0)
enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, 0);
}
if(p.PlayerBubble.getPosition().y < enemy[i].enemy.getPosition().y)
{
enemy[i].enemy.move(0, enemy[i].speed);
if(enemy[i].enemy.getPosition().y > mode.height)
enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, mode.height);
}
if(p.PlayerBubble.getPosition().x < enemy[i].enemy.getPosition().x)
{
enemy[i].enemy.move(enemy[i].speed, 0);
if(enemy[i].enemy.getPosition().x > mode.width)
enemy[i].enemy.setPosition(mode.width, enemy[i].enemy.getPosition().y);
}
if(p.PlayerBubble.getPosition().x > enemy[i].enemy.getPosition().x)
{
enemy[i].enemy.move(-enemy[i].speed, 0);
if(enemy[i].enemy.getPosition().x < 0)
enemy[i].enemy.setPosition(0, enemy[i].enemy.getPosition().y);
if(enemy[i].enemy.getPosition().x == p.PlayerBubble.getPosition().x || enemy[i].enemy.getPosition().y == p.PlayerBubble.getPosition().y)
{ enemy[i].alive = false;
p.PlayerBubble.setScale(p.scale + 1, p.scale + 1);
}
}
} }
window.clear(sf::Color::Blue);
window.setVerticalSyncEnabled(true); //For Smooth animation
timer = clock.getElapsedTime().asMilliseconds();
clock.restart();
window.draw(p.PlayerBubble);
for( int i = 0; i < NUM_ENEMIES; i++)
{ if(enemy[i].alive)
window.draw(enemy[i].enemy);
}
window.display();
}
return 0;
}
And here what I mean by refresh:
sf:: Font MyFont;
MyFont.loadFromFile("font.ttf");
sf::Text myText;
myText.setFont(MyFont);
myText.setCharacterSize(30);
std::string s;
std::stringstream out;
out << p.PlayerBubble.getPosition().x << " : " << p.PlayerBubble.getPosition().y;
myText.setString(out.str());
myText.setPosition( mode.width - 300, mode.height -100); // y = close to 0
myText.setColor(sf::Color::Black);
I for example want to display current x and y coordinates of player, but they are not updating.
I hope now my explanation is a bit better.