Yes I was hoping I could get some feed back on my code. I know everythings in main, but when I try to add custom headers I get all kinds of errors. Thanks.
#include <iostream>
#include <SFML/Graphics.hpp>
#include<cmath>
//#include"block.h"
//sf::RenderWindow win(sf::VideoMode(800,600),"RPG");
using namespace std;
enum class State{PAUSE,PLAYING};
class Block{
sf::Texture tex;
sf::Sprite spr;
sf::Vector2f pos;
public:
int pos_i;
Block(float x=0.f,float y=0.f,float hi=0.f,float wd=0.f){
sf::Vector2f size(hi,wd);
tex.loadFromFile("1.png");
pos.x=x; pos.y=y;
spr.setTexture(tex);
spr.setTextureRect(sf::IntRect(0,0,size.x,size.y));
spr.setPosition(sf::Vector2f(x,y));
}
void draw(sf::RenderWindow* win){win->draw(spr);}
void set_size(sf::Vector2f size){spr.setTextureRect(sf::IntRect(0,0,size.x,size.y));}
sf::Vector2f get_pos(){return pos;}
sf::Sprite& get_tex(){return spr;}
void set_pos(sf::Vector2f pos){spr.move(pos);}
};
class World{
int wd;
int hi;
int level[6][6];
public:
std::vector<Block> block1;
int count;
//void set_pos(sf::Vector2f pos,int i,int j){block[i*j].set_pos(pos);}
World():level{
{1,0,0,0,0,0},
{1,0,0,0,0,0},
{1,0,0,0,0,0},
{1,0,0,0,0,0},
{1,0,0,0,0,0},
{1,1,1,1,1,1}}
{
wd=6;
hi=6;
//blocks=new Block[wd*hi];
for(int i=0;i<hi;i++){
for(int j=0;j<wd;j++){
if(level[i][j]==1){
Block blk(j*24,i*24,24,24);
block1.push_back(blk);
sf::Vector2f pos=blk.get_pos();
cout<<" x "<<j<<" y "<<i<<'\n';
cout<<" pos x "<<pos.x<<" pos y "<<pos.y<<'\n';
}
}
}
//block[1].set_pos(sf::Vector2f(1.f*16,4.f*16));
}
void draw(sf::RenderWindow* win){
for(int i=0;i<block1.size();i++){
block1[i].draw(win);
}
}
};
class weapon{
Block blc;
bool m_up;
bool m_dn;
bool m_lt;
bool m_rt;
sf::Vector2f m_res;
public:
weapon():blc(0,0,8,8){}
void move_rt(){m_rt=true;}
void move_lt(){m_lt=true;}
void move_up(){m_up=true;}
void move_dn(){m_dn=true;}
void stop_rt(){m_rt=false;}
void stop_lt(){m_lt=false;}
void stop_up(){m_up=false;}
void stop_dn(){m_dn=false;}
void setPos(sf::Vector2f pos){blc.set_pos(pos);}
sf::Sprite getWep(){return blc.get_tex();}
void update( sf::Vector2f pos ,sf::Vector2i mse_pos){
if(m_up)
blc.set_pos(pos - sf::Vector2f(0.f,10.f));
if(m_dn)
blc.set_pos(pos + sf::Vector2f(0.f,10.f));
if(m_rt)
blc.set_pos(pos - sf::Vector2f(10.f,0.f));
if(m_lt)
blc.set_pos(pos + sf::Vector2f(10.f,0.f));
float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;
blc.get_tex().setRotation(angle);
}
};
class Player{
Block blc;
weapon Weapon;
sf::Vector2f m_res;
bool m_up;
bool m_dn;
bool m_lt;
bool m_rt;
float m_speed;
sf::Time last_hit;
public:
Player():blc(0,0,16,16){}
void spawn(sf::Vector2f res,sf::Vector2f loc){
blc.set_pos(loc);
Weapon.setPos(sf::Vector2f(loc.x+5.0f,loc.y+5.0));
m_res.x=res.x;
m_res.y=res.y;
}
weapon& getWep(){return Weapon;}
bool hit(sf::Time time_hit);
sf::FloatRect get_pos(){return blc.get_tex().getGlobalBounds();}
sf::Vector2f get_cen(){return blc.get_pos();}
float get_rot(){return blc.get_tex().getRotation();}
sf::Sprite get_pla(){return blc.get_tex();}
void move_rt(){m_rt=true;}
void move_lt(){m_lt=true;}
void move_up(){m_up=true;}
void move_dn(){m_dn=true;}
void stop_rt(){m_rt=false;}
void stop_lt(){m_lt=false;}
void stop_up(){m_up=false;}
void stop_dn(){m_dn=false;}
void update( sf::Vector2f pos ,sf::Vector2i mse_pos){
if(m_up)
blc.set_pos(pos - sf::Vector2f(0.f,10.f));
if(m_dn)
blc.set_pos(pos + sf::Vector2f(0.f,10.f));
if(m_rt)
blc.set_pos(pos - sf::Vector2f(10.f,0.f));
if(m_lt)
blc.set_pos(pos + sf::Vector2f(10.f,0.f));
float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;
blc.get_tex().setRotation(angle);
}
};
int main()
{
//State s=State::PAUSE;
sf::Vector2f res;
res.x=sf::VideoMode::getDesktopMode().width;
res.y=sf::VideoMode::getDesktopMode().height;
sf::RenderWindow win(sf::VideoMode(res.x,res.y),"RPG");
sf::RenderWindow win1(sf::VideoMode(res.x,res.y),"RPG");
sf::View mainView;
mainView.reset(sf::FloatRect(0,0,res.x,res.y));
mainView.setViewport(sf::FloatRect(0,0,1.0f,1.0f));
sf::Clock clock;
sf::Time gameTotal;
sf::Vector2f mouseWPos;
sf::Vector2i mouseSPos;
World w;
Player pla;
weapon wep;
pla.spawn(res,sf::Vector2f(2.f,2.f));
while(win.isOpen()){
sf::Event eve;
while(win.pollEvent(eve)){
if(true){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
pla.move_up();
pla.getWep().move_up();
}
else{
pla.stop_up();
pla.getWep().stop_up();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
pla.move_dn();
pla.getWep().move_dn();
}
else{
pla.stop_dn();
pla.getWep().stop_dn();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
pla.move_lt();
pla.getWep().move_lt();
}
else{
pla.stop_lt();
pla.getWep().stop_lt();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard:)){
pla.move_rt();
pla.getWep().move_rt();
}
else{
pla.stop_rt();
pla.getWep().stop_rt();
}
}
sf::Vector2f plaPos(pla.get_cen());
pla.update(plaPos,sf::Mouse::getPosition());
pla.getWep().update(plaPos,sf::Mouse::getPosition());
}
clock.restart();
win.clear(sf::Color::Black);
win.draw(pla.getWep().getWep());
win.draw(pla.get_pla());
w.draw(&win);
win.setView(mainView);
win.display();
}
//cout << "Hello world!" << endl;
return 0;
}