-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.cpp
75 lines (64 loc) · 1.51 KB
/
chunk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "displayer.h"
Chunk::Chunk(sf::Vector2i pos)
{
m_chunk_pos = pos;
pos *= CHUNKS_SIZE;
m_rect = sf::IntRect(pos, sf::Vector2i(CHUNKS_SIZE, CHUNKS_SIZE));
m_texture.create(CHUNKS_SIZE * SPRITE_SIZE, CHUNKS_SIZE * SPRITE_SIZE);
m_sprite.setPosition(sf::Vector2f((float)(m_rect.left - MAP_MAX_SIZE / 2), (float)(m_rect.top - MAP_MAX_SIZE / 2)) * (float)SPRITE_SIZE);
}
bool Chunk::add_square(Square* square)
{
sf::Vector2i pos(square->get_pos());
if (m_rect.contains(pos))
{
square->set_chunk_pos(m_chunk_pos);
m_squares.push_back(square);
return true;
}
return false;
}
void Chunk::display(void)
{
for (auto it(m_squares.begin()); it != m_squares.end(); ++it)
(*it)->draw(m_texture);
m_texture.display();
m_sprite.setTexture(m_texture.getTexture());
}
void Chunk::draw(sf::RenderWindow& window)
{
window.draw(m_sprite);
}
void Chunk::move(sf::Vector2f direction)
{
m_sprite.move(direction);
}
void Chunk::draw_square(Square* square)
{
square->draw(m_texture);
m_texture.display();
m_sprite.setTexture(m_texture.getTexture());
m_drawed_squares++;
}
bool Chunk::is_inWindow(sf::FloatRect draw_zone) const
{
if (draw_zone.intersects(m_sprite.getGlobalBounds()))
return true;
return false;
}
sf::Vector2i Chunk::get_pos(void) const
{
return sf::Vector2i(m_rect.left, m_rect.top);
}
sf::Vector2i Chunk::get_chunk_pos(void) const
{
return m_chunk_pos;
}
int Chunk::get_nb_squares(void) const
{
return m_squares.size();
}
std::vector<Square*> Chunk::get_squares(void) const
{
return m_squares;
}