Hello ^_^
im working on my project and I have a problem with the collision-class.
All is working fine and if getGlobalBounds turn to "true", the collision is detect. But.. it dont delete the Sprite from the list. It still "moving" and my list is still "full".
Please, dont be angry. Im still in practice. ._. Im trying my best. But the Collision is very hard (not to create - its only because of make OOP and use the right syntax and right return-methods(?)). So I hope you can show me, what I can do. I know, the Code is not "clean". But for the first one, it should be a good step.
Collision.hpp:#ifndef COLLISION_HPP
#define COLLISION_HPP
#include "Enemy.hpp"
#include "Player.hpp"
#include <SFML\Graphics.hpp>
#include <list>
using namespace sf;
class Collision
{
public:
void Collision2Attack(std::list<sf::Sprite>enemyList,
std::list<sf::Sprite>::iterator enemyIt,
sf::Sprite hitBoxR,
RenderWindow &window
);
void update4List(std::list<sf::Sprite>enemyList,
std::list<sf::Sprite>::iterator enemyIt
);
};
#endif
Collision.cpp:#include "Enemy.hpp"
#include "Player.hpp"
#include "Collision.hpp"
#include <SFML\Graphics.hpp>
#include <list>
using namespace sf;
Enemy enemy;
Player player;
void Collision::Collision2Attack(std::list<sf::Sprite>enemyList,
std::list<sf::Sprite>::iterator enemyIt,
sf::Sprite hitBoxR,
RenderWindow &window
)
{
for(enemyIt=enemyList.begin();enemyIt!=enemyList.end();)
{
if( hitBoxR.getGlobalBounds().intersects(enemyIt->getGlobalBounds() ))
{
//enemyIt=enemyList.erase(enemyIt);
enemyIt->setPosition(0,0);
std::cout << "delete" << std::endl;
Collision::update4List(enemyList, enemyIt);
}
else
{
enemyIt++;
}
}
}
void Collision::update4List(std::list<sf::Sprite>enemyList,
std::list<sf::Sprite>::iterator enemyIt
)
{
::enemy.update4ListE(enemyList,
enemyIt
);
}
Enemy.cpp:#include "Enemy.hpp"
#include "Collision.hpp"
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
using namespace sf;
Enemy::Enemy()
{
Speed = 300.f;
enemyTex.loadFromFile("enemy_l.png");
enemyTime = 0.0000;
}
void Enemy::Draw(RenderWindow &window, float ElapsedTime)
{
for(enemyIt=enemyList.begin();enemyIt!=enemyList.end();enemyIt++)
{
enemyIt->move(-Speed*ElapsedTime,0);
window.draw(*enemyIt);
}
}
void Enemy::Process(RenderWindow &window, float ElapsedTime, float enemyTimer)
{
enemyTime += enemyTimer;
if(enemyTime>=9000)
{
enemySprite = new sf::Sprite;
enemySprite->setTexture(enemyTex);
enemySprite->setPosition(720, 410);
enemySprite->setScale(0.6,0.6);
enemyList.push_back(*enemySprite);
enemyTime=0;
}
for(enemyIt=enemyList.begin();enemyIt!=enemyList.end();)
{
if(enemyIt->getPosition().x<=0)
{
enemyList.erase(enemyIt);
enemyIt=enemyList.begin();
}
else
{
enemyIt++;
}
}
}
void Enemy::update4ListE(std::list<sf::Sprite>enemyList,
std::list<sf::Sprite>::iterator enemyIt
)
{
enemyList = enemyList;
enemyIt = enemyIt;
std::cout << static_cast<int>(enemyList.size() ) << std::endl;
}
So I didnt know, how the Enemy-Class can get the "new List", so I thought i just create a simply "update4ListE" method for updating the current List on the other class, but it isnt working.
So I want to ask you (because you all have more skills). ._.
Greetings