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