mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 00:17:01 +00:00
add collisions
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
|
||||
#include "MazeMap.h"
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Graphics/PrimitiveType.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
|
||||
+12
-4
@@ -118,22 +118,30 @@ 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 (map.entityCanMove(pacman, MovementDir::UP))
|
||||
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 (map.entityCanMove(pacman, MovementDir::LEFT))
|
||||
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 (map.entityCanMove(pacman, MovementDir::DOWN))
|
||||
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 (map.entityCanMove(pacman, MovementDir::RIGHT))
|
||||
pacman.move(MovementDir::RIGHT);
|
||||
}
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) {
|
||||
|
||||
+27
-1
@@ -111,4 +111,30 @@ sf::Vector2i MazeMap::getTileCoords(sf::Vector2f screenPos) {
|
||||
|
||||
return {tileX, tileY};
|
||||
};
|
||||
|
||||
|
||||
bool MazeMap::entityCanMove(Entity& entity, MovementDir dir) {
|
||||
sf::Vector2i coords = getTileCoords(entity.getPosition());
|
||||
|
||||
int xTile = coords.x;
|
||||
int yTile = coords.y;
|
||||
|
||||
switch (dir) {
|
||||
case MovementDir::UP:
|
||||
return !isWall(xTile, yTile - 1);
|
||||
break;
|
||||
case MovementDir::DOWN:
|
||||
return !isWall(xTile, yTile + 1);
|
||||
break;
|
||||
case MovementDir::LEFT:
|
||||
return !isWall(xTile - 1, yTile);
|
||||
break;
|
||||
case MovementDir::RIGHT:
|
||||
return !isWall(xTile + 1, yTile);
|
||||
break;
|
||||
case MovementDir::STATIC:
|
||||
return true;
|
||||
break;
|
||||
};
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef MAZEMAP_H
|
||||
#define MAZEMAP_H
|
||||
|
||||
#include "Entity.h"
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
@@ -27,6 +28,8 @@ public:
|
||||
int convert2DCoords(int x, int y) const { return x + y * width; }
|
||||
bool isLegalTile(int x, int y) const;
|
||||
|
||||
bool entityCanMove(Entity& entity, MovementDir dir);
|
||||
|
||||
sf::Vector2i getTileCoords(sf::Vector2f screenPos);
|
||||
|
||||
unsigned int getWidth() const { return width; }
|
||||
|
||||
Reference in New Issue
Block a user