mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
add basic movement for entity
This commit is contained in:
+20
-14
@@ -35,22 +35,28 @@ 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::move(MovementDir dir) {
|
||||
switch (dir) {
|
||||
case MovementDir::UP:
|
||||
activeSprite->move(movementSpeed.componentWiseMul({0, -1}));
|
||||
this->setPosition(activeSprite->getPosition());
|
||||
case MovementDir::DOWN:
|
||||
activeSprite->move(movementSpeed.componentWiseMul({0, 1}));
|
||||
this->setPosition(activeSprite->getPosition());
|
||||
case MovementDir::LEFT:
|
||||
activeSprite->move(movementSpeed.componentWiseMul({-1, 0}));
|
||||
this->setPosition(activeSprite->getPosition());
|
||||
case MovementDir::RIGHT:
|
||||
activeSprite->move(movementSpeed.componentWiseMul({1, 0}));
|
||||
this->setPosition(activeSprite->getPosition());
|
||||
case MovementDir::STATIC:
|
||||
activeSprite->move(movementSpeed.componentWiseMul({0, 0}));
|
||||
this->setPosition(activeSprite->getPosition());
|
||||
};
|
||||
};
|
||||
|
||||
+5
-3
@@ -30,9 +30,9 @@ struct EntityAnimation {
|
||||
std::vector<sf::Sprite> sprites;
|
||||
};
|
||||
|
||||
class Entity {
|
||||
class Entity : public sf::Transformable {
|
||||
public:
|
||||
Entity() : movementSpeed(5) {};
|
||||
Entity() : movementSpeed({5.0f, 5.0f}) {};
|
||||
|
||||
void setAnimationTiles(sf::Texture& textureSheet, sf::Vector2i pixelLocation, const std::string& animationName, sf::Vector2i tileSize, unsigned int animationTiles, unsigned int pixelGap);
|
||||
|
||||
@@ -40,6 +40,8 @@ public:
|
||||
|
||||
void setActiveSprite(const std::string& animationName, int animationTile);
|
||||
|
||||
void move(MovementDir dir);
|
||||
|
||||
protected:
|
||||
sf::Vector2i tileSize;
|
||||
|
||||
@@ -47,7 +49,7 @@ protected:
|
||||
|
||||
sf::Sprite* activeSprite;
|
||||
|
||||
int movementSpeed;
|
||||
sf::Vector2f movementSpeed;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -116,26 +116,33 @@ void Game::run() {
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) {
|
||||
pacman.setActiveSprite("up_walking", 0);
|
||||
pacman.move(MovementDir::UP);
|
||||
}
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
|
||||
pacman.setActiveSprite("left_walking", 0);
|
||||
pacman.move(MovementDir::LEFT);
|
||||
}
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)|| sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) {
|
||||
pacman.setActiveSprite("down_walking", 0);
|
||||
pacman.move(MovementDir::DOWN);
|
||||
}
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) {
|
||||
pacman.setActiveSprite("right_walking", 0);
|
||||
pacman.move(MovementDir::RIGHT);
|
||||
}
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) {
|
||||
pacman.setActiveSprite("static", 0);
|
||||
pacman.move(MovementDir::STATIC);
|
||||
}
|
||||
|
||||
window.draw(*pacman.getActiveSprite());
|
||||
|
||||
std::cout << pacman.getPosition().x << ", " << pacman.getPosition().y << "\n";
|
||||
|
||||
//window.draw();
|
||||
|
||||
window.display();
|
||||
|
||||
Reference in New Issue
Block a user