mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
modify drawing of entity
This commit is contained in:
+11
-11
@@ -35,33 +35,33 @@ void Entity::setActiveSprite(const std::string& animationName, int animationTile
|
|||||||
for (auto& animation : animations) {
|
for (auto& animation : animations) {
|
||||||
if (animation.name == animationName) {
|
if (animation.name == animationName) {
|
||||||
activeSprite = &animation.sprites.at(animationTile);
|
activeSprite = &animation.sprites.at(animationTile);
|
||||||
activeSprite->setPosition(this->getPosition());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||||
|
states.transform *= getTransform();
|
||||||
|
if (activeSprite) {
|
||||||
|
target.draw(*activeSprite, states);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Entity::move(MovementDir dir) {
|
void Entity::move(MovementDir dir) {
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case MovementDir::UP:
|
case MovementDir::UP:
|
||||||
activeSprite->move(movementSpeed.componentWiseMul({0, -1}));
|
Transformable::move({0, -movementSpeed.y});
|
||||||
this->setPosition(activeSprite->getPosition());
|
|
||||||
break;
|
break;
|
||||||
case MovementDir::DOWN:
|
case MovementDir::DOWN:
|
||||||
activeSprite->move(movementSpeed.componentWiseMul({0, 1}));
|
Transformable::move({0, movementSpeed.y});
|
||||||
this->setPosition(activeSprite->getPosition());
|
|
||||||
break;
|
break;
|
||||||
case MovementDir::LEFT:
|
case MovementDir::LEFT:
|
||||||
activeSprite->move(movementSpeed.componentWiseMul({-1, 0}));
|
Transformable::move({-movementSpeed.x, 0});
|
||||||
this->setPosition(activeSprite->getPosition());
|
|
||||||
break;
|
break;
|
||||||
case MovementDir::RIGHT:
|
case MovementDir::RIGHT:
|
||||||
activeSprite->move(movementSpeed.componentWiseMul({1, 0}));
|
Transformable::move({movementSpeed.x, 0});
|
||||||
this->setPosition(activeSprite->getPosition());
|
|
||||||
break;
|
break;
|
||||||
case MovementDir::STATIC:
|
case MovementDir::STATIC:
|
||||||
activeSprite->move(movementSpeed.componentWiseMul({0, 0}));
|
|
||||||
this->setPosition(activeSprite->getPosition());
|
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-1
@@ -30,7 +30,7 @@ struct EntityAnimation {
|
|||||||
std::vector<sf::Sprite> sprites;
|
std::vector<sf::Sprite> sprites;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Entity : public sf::Transformable {
|
class Entity : public sf::Transformable, public sf::Drawable {
|
||||||
public:
|
public:
|
||||||
Entity() : movementSpeed({5.0f, 5.0f}) {};
|
Entity() : movementSpeed({5.0f, 5.0f}) {};
|
||||||
|
|
||||||
@@ -43,6 +43,8 @@ public:
|
|||||||
void move(MovementDir dir);
|
void move(MovementDir dir);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
|
|
||||||
sf::Vector2i tileSize;
|
sf::Vector2i tileSize;
|
||||||
|
|
||||||
std::vector<EntityAnimation> animations;
|
std::vector<EntityAnimation> animations;
|
||||||
|
|||||||
+1
-1
@@ -139,7 +139,7 @@ void Game::run() {
|
|||||||
pacman.move(MovementDir::STATIC);
|
pacman.move(MovementDir::STATIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.draw(*pacman.getActiveSprite());
|
window.draw(pacman);
|
||||||
|
|
||||||
//window.draw();
|
//window.draw();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user