-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEntity.cpp
More file actions
37 lines (29 loc) · 843 Bytes
/
Copy pathEntity.cpp
File metadata and controls
37 lines (29 loc) · 843 Bytes
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
#include "Entity.hpp"
Entity::Entity(const sf::Texture& texture, sf::Vector2i tileSize, int tileNumber)
: m_sprite(texture)
, m_tileSize(tileSize)
{
const int tu = tileNumber % (texture.getSize().x / tileSize.x);
const int tv = tileNumber / (texture.getSize().x / tileSize.x);
m_sprite.setTextureRect({ tu * tileSize.x, tv * tileSize.y, tileSize.x, tileSize.y });
}
const sf::Vector2i& Entity::getPosition() const
{
return m_position;
}
void Entity::setPosition(int x, int y)
{
const float fx = static_cast<float>(x * m_tileSize.x);
const float fy = static_cast<float>(y * m_tileSize.y);
m_sprite.setPosition(fx, fy);
m_position.x = x;
m_position.y = y;
}
void Entity::move(int dx, int dy)
{
setPosition(m_position.x + dx, m_position.y + dy);
}
void Entity::draw(sf::RenderWindow& window) const
{
window.draw(m_sprite);
}