Hi,
I got problem. I got Tilemap.
Tilemap.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"
#include "Enemy.h"
using namespace std;
using namespace sf;
class newTiled{
public:
newTiled(void);
~newTiled(void);
void draw(RenderWindow &);
void Update(Player &player, Time &dt);
private:
vector <RectangleShape>::const_iterator wallit;
vector <RectangleShape> wallGroup;
int map_width = 42;
int map_height = 12;
Texture tileset_texture;
Sprite tileset_sprite;
Texture bg_txt;
Sprite bg_sprite;
RectangleShape t;
int counter;
int level[42 * 12] ={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
};
Tilemap.cpp
#include "newTiled.h"
#include <cmath>
using namespace sf;
using namespace std;
newTiled::newTiled(){
if(!tileset_texture.loadFromFile("assets/images/tajlmap.png"))
return;
if(!bg_txt.loadFromFile("assets/images/bg2.png"))
return;
tileset_sprite.setTexture(tileset_texture);
bg_sprite.setTexture(bg_txt);
}
newTiled::~newTiled(){
}
void newTiled::draw(RenderWindow &window){
window.draw(bg_sprite);
t = RectangleShape(Vector2f(64, 64));
t.setFillColor(sf::Color::Transparent);
for(int y = 0; y < map_height; ++y){
for(int x = 0; x < map_width; ++x){
int tilecount = x + y * map_width;
tileset_sprite.setPosition(x * 64, y * 64);
tileset_sprite.setTextureRect(IntRect(level[tilecount] * 64, 0, 64, 64));
if(level[tilecount] == 2){
t.setPosition(x*64, y*64);
wallGroup.push_back(t);
}
window.draw(tileset_sprite);
}
}
counter = 0;
for(wallit = wallGroup.begin(); wallit != wallGroup.end(); wallit++){
window.draw(wallGroup[counter]);
counter++;
}
}
void newTiled::Update(Player &player, Time &dt){
float bottom, top, left, right;
int posX = floor((int)(player.getPosition().x / 64));
int posY = floor((int)(player.getPosition().y / 64));
int tilecount = posX + posY * map_width;
if(level[tilecount] == 1)
player.m_onGround = true;
else
player.m_onGround = false;
counter = 0;
for(wallit = wallGroup.begin(); wallit != wallGroup.end(); wallit++){
bottom = wallGroup[counter].getPosition().y + 64;
top = wallGroup[counter].getPosition().y;
right = wallGroup[counter].getPosition().x + 66;
left = wallGroup[counter].getPosition().x;
if(player.getBounds().intersects(wallGroup[counter].getGlobalBounds())){
if(player.right > left && player.right > right){
player.setPosition(player.getPosition().x - 1, player.getPosition().y);
}
else if (player.left < right && player.left < left){
player.setPosition(right + player.left, player.getPosition().y);
}
//top tile collision
else if(player.bottom > top && player.bottom > bottom){
} else if (player.top < bottom && player.top < top){
}
}else {
// player.m_onTile = false;
// player.setColor(sf::Color::Red);
}
counter++;
}
}
And my Player is sf::RectangleShape. I check his sides with :
bottom = m_rect.getPosition().y + m_rect.getSize().y;
left = m_rect.getPosition().x;
right = m_rect.getPosition().x + m_rect.getSize().x;
top = m_rect.getPosition().y;
And I got problem with wall collision from left/right/top/bottom just to stop player from moving etc.