Hello, so I want my enemy to disapper when its life == false, but it doesn't, I mean I see my player jumping on the enemy because command p.dy -= 0.5; triggers, which actually tells my that the enemy doesn't erase
This is the iterator:
std::list<Entity*> enemyArray;
std::list<Entity*>::iterator iter1;
std::vector<Object> e = lvl.GetObjects("EasyEnemy");
for (int i = 0; i < e.size(); i++)
enemyArray.push_back(new Enemy(easyEnemyImage, lvl, e[i].rect.left, e[i].rect.top, 79, 59, "EasyEnemy"));
This is the main part:
for (iter1 = enemyArray.begin(); iter1 != enemyArray.end();)
{
(*iter1)->update(time);
if ((*iter1)->life == false)
{
iter1 = enemyArray.erase(iter1);
delete (*iter1);
}
else { iter1++; }
}
for (iter1 = enemyArray.begin(); iter1 != enemyArray.end(); iter1++)
{
if (p.getRect().intersects((*iter1)->getRect()))
{
if ((*iter1)->name == "EasyEnemy")
{
if ((p.dy > 0) && (p.onGround == false))
{
(*iter1)->health = 0;
p.dy -= 0.5;
}
else
{
p.dy -= 0.5;
p.dx -= 0.5;
p.health -= 25;
}
}
}
}
This is the draw part:
for (iter1 = enemyArray.begin(); iter1 != enemyArray.end(); iter1++)
{
window.draw((*iter1)->sprite);
}
I tried to do (*iter1)->life == false;, (*iter1)->health -= 100;, even in iterator update if ((*iter->health = 0), non of those worked
Here is the whole main.cpp code:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "view.h"
#include <iostream>
#include <sstream>
#include "mission.h"
#include "iostream"
#include "level.h"
#include <vector>
#include <list>
#include "tinyxml\tinyxml.h"
using namespace sf;
class Entity {
public:
std::vector<Object> obj;
float dx, dy, x, y, speed, moveTimer;
int w, h, health;
bool life, isMove, onGround;
Texture texture;
Sprite sprite;
String name;
Entity(Image &image, float X, float Y, int W, int H, String Name) {
x = X; y = Y; w = W; h = H; name = Name; moveTimer = 0;
speed = 0;
dx = 0; dy = 0;
life = true; onGround = false; isMove = false;
texture.loadFromImage(image);
sprite.setTexture(texture);
sprite.setOrigin(w / 2, h / 2);
}
FloatRect getRect()
{
return FloatRect(x, y, w, h);
}
virtual void update(float time) = 0;
};
class Player :public Entity {
public:
int health = 100;
enum { left, right, jump, stay } state;
int playerScore;
Music death;
Music deathS;
Music death2;
Music soundtrack;
Music soundtrack2;
Music claySong;
int Pcounter = 0;
int Dcounter = 0;
SoundBuffer lowhpBuffer;
Sound lowhp;
Player(Image &image, Level &lev, float X, float Y, int W, int H, String Name) :Entity(image, X, Y, W, H, Name){
playerScore = 0; state = stay; obj = lev.GetAllObjects();
lowhpBuffer.loadFromFile("sounds/lowhp.ogg");
lowhp.setBuffer(lowhpBuffer);
death.openFromFile("music/death.ogg"); death.setLoop(true);
deathS.openFromFile("music/deathS.ogg"); deathS.setVolume(100); deathS.setLoop(true);
death2.openFromFile("music/death2.ogg"); death2.setVolume(100);
soundtrack.openFromFile("music/soundtrack.ogg"); soundtrack.setVolume(50);
soundtrack2.openFromFile("music/soundtrack2.ogg");
claySong.openFromFile("sounds/claySong.ogg");
soundtrack.play(); soundtrack.setLoop(true);
claySong.play(); claySong.setLoop(true);
if (name == "Player1"){
sprite.setTextureRect(IntRect(10, 306, w, h));
}
}
void control() {
if (life == true) {
if (Keyboard::isKeyPressed(Keyboard::A)) {
state = left;
speed = 0.3;
}
if (Keyboard::isKeyPressed(Keyboard::D)) {
state = right;
speed = 0.3;
}
if ((Keyboard::isKeyPressed(Keyboard::Space) && (onGround == true))) {
state = jump;
dy = -0.5;
//onGround = false;
}
}
}
void update(float time)
{
control();
switch (state) {
case left: dx = -speed; break;
case right: dx = speed; break;
case jump: break;
case stay: break;
}
x += dx*time;
checkCollisionWithMap(dx, 0);
y += dy*time;
checkCollisionWithMap(0, dy);
if (!isMove) speed = 0;
sprite.setPosition(x + w / 2, y + h / 2);
if (health <= 0) { life = false; }
if (life == true) { getplayercoordinateforview(x, y); }
dy = dy + 0.0015*time;
if (life == false)
{
if (Dcounter < 3000) {
Dcounter++;
if ((Dcounter >= 0) && (Dcounter < 300)) {
sprite.setColor(Color(235, 65, 7));
view.zoom(0.997);
view.move(0, -0.05);
view.rotate(0.2);
}
if ((Dcounter >= 300) && (Dcounter < 600)) {
view.rotate(-0.2);
view.zoom(0.997);
view.move(0.2, 0.2);
}
if ((Dcounter >= 600) && (Dcounter < 670)) {
view.zoom(0.990);
view.move(0, -0.3);
}
if ((Dcounter >= 670) && (Dcounter < 900)) {
view.move(-0.3, 0);
deathS.stop();
}
if (Dcounter >= 900) {
view.zoom(1.005);
view.move(0.2, 0);
if (Dcounter >= 1000) {
view.rotate(-0.5);
if (death.getVolume() > 26) {
death.setVolume(25);
}
if (death2.getStatus() == Sound::Playing) {
}
else
{
death2.play();
}
}
}
}
if (death.getStatus() == Sound::Playing)
{
}
else
{
soundtrack.stop();
soundtrack2.stop();
claySong.stop();
death.play();
deathS.play();
}
}
}
float getplayercoordinateX() {
return x;
}
void checkCollisionWithMap(float Dx, float Dy) {
onGround = false;
for (int i = 0; i<obj.size(); i++)
if (getRect().intersects(obj[i].rect))
{
if (obj[i].name == "solid")
{
if (Dy>0) { y = obj[i].rect.top - h; dy = 0; onGround = true; }
if (Dy<0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; }
if (Dx>0) { x = obj[i].rect.left - w; }
if (Dx<0) { x = obj[i].rect.left + obj[i].rect.width; }
}
if (obj[i].name == "lava")
{
if (Dy>0) { y = obj[i].rect.top - h; dy = 0; onGround = true; }
if (Dy<0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; }
if (Dx>0) { x = obj[i].rect.left - w; }
if (Dx < 0) { x = obj[i].rect.left + obj[i].rect.width; }
if (life = true) { life = false; }
}
if (obj[i].name == "jump")
{
if (Dy > 0) { y = obj[i].rect.top - h; dy = 0; }
if (Dy < 0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; }
if (Dx > 0) { x = obj[i].rect.left - w; }
if (Dx < 0) { x = obj[i].rect.left + obj[i].rect.width; }
dy -= 1.5; onGround = false;
}
if (obj[i].name == "enemy")
{
soundtrack.stop();
claySong.stop();
if (soundtrack2.getStatus() == Sound::Playing) { continue; }
else {
soundtrack2.play();
}
sprite.setColor(Color(237, 28, 36));
}
if (obj[i].name == "poison")
{
if (life == true) {
Pcounter++;
if (Pcounter >= 200) {
health -= 25;
Pcounter = 0;
lowhp.play();
}
}
}
}
}
};
class Enemy :public Entity{
public:
int health;
Enemy(Image &image, Level &lvl, float X, float Y, int W, int H, String Name) :Entity(image, X, Y, W, H, Name) {
obj = lvl.GetObjects("solid"); health = 100;
if (name == "EasyEnemy") {
health = 100;
sprite.setTextureRect(IntRect(0, 0, w, h));
dx = 0.1;
}
}
void checkCollisionWithMap(float Dx, float Dy) {
for (int i = 0; i<obj.size(); i++)
if (getRect().intersects(obj[i].rect))
{
if (Dy>0) { y = obj[i].rect.top - h; dy = 0; onGround = true; }
if (Dy<0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; }
if (Dx>0) { x = obj[i].rect.left - w; dx = -0.1; sprite.scale(-1, 1); }
if (Dx<0) { x = obj[i].rect.left + obj[i].rect.width; dx = 0.1; sprite.scale(-1, 1); }
}
}
void update(float time) {
if (name == "EasyEnemy")
{
//moveTime += time; if (moveTimer>3000) {dx*=-1; moveTimer = 0; }
checkCollisionWithMap(dx, 0);
x += dx*time;
sprite.setPosition(x + w / 2, y + h / 2);
if (health <= 0) { life = false; }
}
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(1980, 1080), "Glinskii Adventures");
view.reset(sf::FloatRect(0, 0, 640, 400));
window.setFramerateLimit(90); // fps
//int iterCounter = 0;
Level lvl;
lvl.LoadFromFile("mainmap.tmx");
Image heroImage;
heroImage.loadFromFile("images/hero.jpg");
heroImage.createMaskFromColor(Color(125, 99, 86));
Image easyEnemyImage;
easyEnemyImage.loadFromFile("images/enemy1.jpg");
easyEnemyImage.createMaskFromColor(Color(255, 255, 255));
/*Image map_image;
map_image.loadFromFile("images/map.png");
Texture map;
map.loadFromImage(map_image);
Sprite map_sprite;
map_sprite.setTexture(map);
Image house_image;
house_image.loadFromFile("images/house.png");
house_image.createMaskFromColor(Color(255, 255, 255));
Texture house;
house.loadFromImage(house_image);
Sprite house_sprite;
house_sprite.setTexture(house);
house_sprite.setPosition(80, 539);*/
Image quest_image;
quest_image.loadFromFile("images/tab.jpg");
Texture quest_texture;
quest_texture.loadFromImage(quest_image);
Sprite s_quest;
s_quest.setTexture(quest_texture);
s_quest.setTextureRect(IntRect(0, 0, 224, 352));
/*Image map2;
map2.loadFromFile("images/map2.png");
Texture map2texture;
map2texture.loadFromImage(map2);
Sprite map2s;
map2s.setTexture(map2texture);*/
Object player = lvl.GetObject("player");
Object easyEnemyObject = lvl.GetObject("easyEnemy");
/*SoundBuffer lowhpBuffer;
lowhpBuffer.loadFromFile("sounds/lowhp.ogg");
Sound lowhp(lowhpBuffer);*/
/*SoundBuffer walkBuffer;
walkBuffer.loadFromFile("sounds/walk.ogg");
Sound walk(walkBuffer);
walk.setVolume(100);*/
Font font;
font.loadFromFile("fonts/classic.TTF");
Text text("", font, 20);
text.setStyle(Text::Bold);
Text pickText("", font, 20);
pickText.setStyle(Text::Bold);
Text text2("", font, 20);
text2.setStyle(Text::Bold);
Text glinPlate("", font, 20);
glinPlate.setStyle(Text::Bold);
glinPlate.setStyle(Text::Underlined);
Text nickname("", font, 20);
std::list<Entity*> enemyArray;
std::list<Entity*>::iterator iter1;
std::vector<Object> e = lvl.GetObjects("EasyEnemy");
for (int i = 0; i < e.size(); i++)
enemyArray.push_back(new Enemy(easyEnemyImage, lvl, e[i].rect.left, e[i].rect.top, 79, 59, "EasyEnemy"));
/*std::list<Entity*> entities;
std::list<Entity*>::iterator it;
std::vector<Object> e = lvl.GetObjects("EasyEnemy");
for (int i = 0; i < e.size(); i++)
entities.push_back(new Enemy(easyEnemyImage, lvl, e[i].rect.left, e[i].rect.top, 79, 59, "EasyEnemy"));*/
Player p(heroImage, lvl, player.rect.left, player.rect.top, 70, 86, "Player1");
//Enemy easyEnemy(easyEnemyImage, lvl, easyEnemyObject.rect.left, easyEnemyObject.rect.top, 79, 59, "EasyEnemy");
//float CurrentFrame = 0;
Clock clock;
Clock gameTimeClock;
int gameTime = 0;
bool showMissionText = true;
while (window.isOpen())
{
float time = clock.getElapsedTime().asMicroseconds();
clock.restart();
time = time / 800;
if (p.life == true) gameTime = gameTimeClock.getElapsedTime().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
window.setKeyRepeatEnabled(false);
if (event.type == sf::Event::Closed)
window.close();
/*if (p.life == true)
{
if ((event.type == Event::KeyPressed) && (event.key.code == Keyboard::D))
{
walk.play();
}
}*/
/*if (p.life == false) {
if (death.getStatus() == Sound::Playing) {
continue;
}
else
{
soundtrack.stop();
claySong.stop();
death.play();
deathS.play();
}
}*/
if ((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Tab))
{
switch (showMissionText)
{
case true:
{
std::ostringstream task;
task << getTextMission(getCurrentMission(p.getplayercoordinateX()));
text2.setString("\n" + task.str());
showMissionText = false;
glinPlate.setString("NO:");
nickname.setString("XxX_Daun_XxX");
break;
}
case false:
{
text2.setString("");
showMissionText = true;
break;
}
}
}
}
/*for (iter1 = enemyArray.begin(); iter1 != enemyArray.end(); iter1++)
{
window.draw(enemyArray[iterCounter].getRect);
iterCounter++;
}*/
/*for (iter1 = enemyArray.begin(); iter1 != enemyArray.end();)
{
(*iter1)->update(time);
if ((*iter1)->health = 0)
{
iter1 = enemyArray.erase(iter1);
delete (*iter1);
}
else iter1++;
}*/
for (iter1 = enemyArray.begin(); iter1 != enemyArray.end();)
{
(*iter1)->update(time);
if ((*iter1)->life == false)
{
iter1 = enemyArray.erase(iter1);
delete (*iter1);
}
else { iter1++; }
}
for (iter1 = enemyArray.begin(); iter1 != enemyArray.end(); iter1++)
{
if (p.getRect().intersects((*iter1)->getRect()))
{
if ((*iter1)->name == "EasyEnemy")
{
if ((p.dy > 0) && (p.onGround == false))
{
(*iter1)->health = 0;
p.dy -= 0.5;
}
else
{
p.dy -= 0.5;
p.dx -= 0.5;
p.health -= 25;
}
}
}
}
p.update(time);
/* for (it = entities.begin(); it != entities.end();)
{
//Entity *b = *it;
(*it)->update(time);
if ((*it)->life == false)
{
it = entities.erase(it);
delete (*it);
}
else { it++; }
}
for (it = entities.begin(); it != entities.end(); it++)
{
if ((*it)->getRect().intersects(p.getRect()));
{
if ((*it)->name == "EasyEnemy")
{
if ((p.dy > 0) && (p.onGround == false))
{
}
else
{
}
}
}
}*/
/*for (it = entities.begin(); it != entities.end(); it++) {
(*it)->update(time);
}*/
changeview();
window.setView(view);
window.clear();
lvl.Draw(window);
std::ostringstream playerHealthString, gameTimeString;
playerHealthString << p.health; gameTimeString << gameTime;
text.setString("Health: " + playerHealthString.str() + "\nGame time: " + gameTimeString.str());
text.setPosition(view.getCenter().x - 315, view.getCenter().y - 200);
window.draw(text);
std::ostringstream playerScoreString;
playerScoreString << p.playerScore;
pickText.setString("Coins: " + playerScoreString.str());
pickText.setPosition(view.getCenter().x + 140, view.getCenter().y - 200);
window.draw(pickText);
if (!showMissionText)
{
text2.setPosition(view.getCenter().x - 315, view.getCenter().y - 100);
s_quest.setPosition(view.getCenter().x - 320, view.getCenter().y - 100);
glinPlate.setPosition(view.getCenter().x - 315, view.getCenter().y - 100);
nickname.setPosition(view.getCenter().x - 20, view.getCenter().y);
window.draw(s_quest); window.draw(text2); window.draw(glinPlate); window.draw(nickname);
}
for (iter1 = enemyArray.begin(); iter1 != enemyArray.end(); iter1++)
{
window.draw((*iter1)->sprite);
}
window.draw(p.sprite);
window.display();
}
return 0;
}