Mam problem z funkcją Ai z class Ziemi dlatego .
Problem polega na tym że gdy wywołuje dwie funkcje Ai z obiektów class Ziemia.
To jeden obiekt nie działa.
#pragma once
#ifndef RESORCES_H_INCLUDED
#define RESORCES_H_INCLUDED
#endif //RESORCES_H_INCLUDED
#include <TardiGrap.hpp>
#include "Level.h"
#include "Player.h"
#include "NPC.h"
#include "Object.h"
using namespace tge;
class Gra : public Game
{
private:
vector <Object*> plan3;
vector <Object*> plan2;
vector <Object*> plan1;
vector <Object*> npc;
Player Gracz;
Psychic *fizyka;
int fps;
bool Pauzy;
public:
Ziemia *po;
Ziemia *up;
void Pauza()
{
if(Pauzy)Sleep(15);
}
Gra(int x,int y,string link)
: Game(x,y,link)
{
Gracz.create(1,800,50,4,"Lotos.png");
fps=60;
app.setFramerateLimit(fps);
po=new Ziemia(400,800,1000,400,"Pod.png");
plan1.push_back(po);
srand(time(NULL));
fizyka=new Psychic(0,0);
npc.push_back(&Gracz);
Pauzy=false;
up=new Ziemia(460,100,1000,400,"Pod.png");
plan1.push_back(up);
}
void Run()
{
while(app.isOpen())
{
while(app.pollEvent(event))
{
if(event.type=sf::Event::Closed)
{
Ending(&app);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
Ending(&app);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::P))
{
if(Pauzy==false) Pauzy=true;
else Pauzy=false;
}
Gracz.Control(&event);
}
Gracz.Ai(&app);
po->Ai(&Gracz,po);//to są te funkcje.
up->Ai(&Gracz,up);
app.clear();
for(int i=0;i<plan1.size();i++)
{
app.draw(plan1[i]->GetBody());
}
for(int i=0;i<npc.size();i++)
{
app.draw(npc[i]->GetBody());
}
app.display();
}
}
Object* GetObject(int p,int i)
{
if(p==1) return plan1[i];
else if(p==2)return plan2[i];
else if(p==3)return plan3[i];
else if(p==4)return npc[i];
}
void Ending(sf::RenderWindow *op)
{
npc.clear();
plan1.clear();
plan2.clear();
plan3.clear();
op->close();
}
};
Ziemia
#pragma once
#ifndef OBJECT_H_INCLUDED
#define OBJECT_H_INCLUDED
#endif // OBJECT_H_INCLUDED
#include "Resorces.h"
#include "Player.h"
#include "NPC.h"
using namespace tge;
using namespace std;
class Ziemia : public Object
{
int xs,ys;
public:
Ziemia(int x,int y,int bx,int by,string link);
void Ai(Player *play,Ziemia *o);
void NPCAi(NPC *npc);
};
funkcja Ai
#include "Resorces.h"
using namespace tge;
using namespace std;
void Ziemia::Ai(Player *play,Ziemia *o)
{
if((play->GetBody().getPosition().y+50>=cialo.getPosition().y-20)&&(play->GetBody().getPosition().x>=cialo.getPosition().x)
&&(play->GetBody().getPosition().x<=cialo.getPosition().x+xs)&&(play->GetBody().getPosition().y<cialo.getPosition().y+ys))
{
play->SetGrav(false);play->EnabledR[1]=true;play->EnabledR[0]=true;
}
else play->SetGrav(true);
if((play->GetBody().getPosition().y<cialo.getPosition().y+ys)&&(play->GetBody().getPosition().y>cialo.getPosition().y-50)
&&(play->GetBody().getPosition().x>=cialo.getPosition().x) &&(play->GetBody().getPosition().x<=cialo.getPosition().x+xs))
{
//lewy bok.
if(play->GetBody().getPosition().x>cialo.getPosition().x-50)//kiedyś -40.
{
if(play->GetBody().getPosition().x<cialo.getPosition().x+xs/2)
{play->EnabledR[1]=false;play->EnabledR[0]=true;
play->CanJump=true;
}
}
if(play->GetBody().getPosition().x<cialo.getPosition().x-90)
{
play->EnabledR[0]=true;play->EnabledR[1]=true;
}
//prawy bok.
if(play->GetBody().getPosition().x<cialo.getPosition().x+xs+50)//kiedyś +40.
{
if(play->GetBody().getPosition().x>cialo.getPosition().x+xs/2)
{play->EnabledR[0]=false;play->EnabledR[1]=true;
play->CanJump=true;
}
}
if(play->GetBody().getPosition().x>cialo.getPosition().x+xs+90)
{
play->EnabledR[1]=true;play->EnabledR[0]=true;
}
}
else play->EnabledR[1]=true;play->EnabledR[0]=true;
if((play->GetBody().getPosition().y<=cialo.getPosition().y+ys)&&(play->GetBody().getPosition().y>cialo.getPosition().y+ys-100)
&&(play->GetBody().getPosition().x+50>=cialo.getPosition().x)&&(play->GetBody().getPosition().x<=cialo.getPosition().x+xs))
{
play->CanJump=false;
}
else play->CanJump=true;
if(!play->EnabledR[0]) play->EnabledR[1]=true;
else if(!play->EnabledR[1])play->EnabledR[0]=true;
}