mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
modify entity class items
This commit is contained in:
+19
-19
@@ -1,5 +1,6 @@
|
||||
#include "Entity.h"
|
||||
#include <SFML/Graphics/PrimitiveType.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <SFML/Graphics/RenderStates.hpp>
|
||||
#include <SFML/Graphics/RenderTarget.hpp>
|
||||
#include <SFML/Graphics/Vertex.hpp>
|
||||
@@ -7,14 +8,12 @@
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
|
||||
Entity::Entity() {};
|
||||
|
||||
//mainly derived from https://www.sfml-dev.org/tutorials/3.0/graphics/vertex-array/#example-tile-map
|
||||
bool Entity::loadTexture(const std::filesystem::path& texturePath, sf::Vector2u tileSize, int tiles) {
|
||||
bool Entity::loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles) {
|
||||
if(!texture.loadFromFile(texturePath))
|
||||
return false;
|
||||
|
||||
//assumes tiles are animation tiles of one row tileSize height and tiles width
|
||||
//assumes tiles are animation tiles of one row graphicTileSize height and tiles width
|
||||
int height = 1;
|
||||
int width = height * tiles;
|
||||
int doubleTriVertex = 6;
|
||||
@@ -26,25 +25,24 @@ bool Entity::loadTexture(const std::filesystem::path& texturePath, sf::Vector2u
|
||||
for (size_t i = 0; i < width; ++i) {
|
||||
for (size_t j = 0; j < height; ++j) {
|
||||
const int tileNumber = i;
|
||||
|
||||
const int tu = tileNumber % (texture.getSize().x / tileSize.x);
|
||||
const int tv = tileNumber / (texture.getSize().x / tileSize.x);
|
||||
const int tu = tileNumber % (texture.getSize().x / graphicTileSize.x);
|
||||
const int tv = tileNumber / (texture.getSize().x / graphicTileSize.x);
|
||||
|
||||
sf::Vertex* triangles = &vertexArray[(i + j * width) * doubleTriVertex];
|
||||
|
||||
triangles[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
|
||||
triangles[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
|
||||
triangles[2].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
|
||||
triangles[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
|
||||
triangles[4].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
|
||||
triangles[5].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
|
||||
triangles[0].position = sf::Vector2f(i * graphicTileSize.x, j * graphicTileSize.y);
|
||||
triangles[1].position = sf::Vector2f((i + 1) * graphicTileSize.x, j * graphicTileSize.y);
|
||||
triangles[2].position = sf::Vector2f(i * graphicTileSize.x, (j + 1) * graphicTileSize.y);
|
||||
triangles[3].position = sf::Vector2f(i * graphicTileSize.x, (j + 1) * graphicTileSize.y);
|
||||
triangles[4].position = sf::Vector2f((i + 1) * graphicTileSize.x, j * graphicTileSize.y);
|
||||
triangles[5].position = sf::Vector2f((i + 1) * graphicTileSize.x, (j + 1) * graphicTileSize.y);
|
||||
|
||||
triangles[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
|
||||
triangles[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
|
||||
triangles[2].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
|
||||
triangles[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
|
||||
triangles[4].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
|
||||
triangles[5].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
|
||||
triangles[0].texCoords = sf::Vector2f(tu * graphicTileSize.x, tv * graphicTileSize.y);
|
||||
triangles[1].texCoords = sf::Vector2f((tu + 1) * graphicTileSize.x, tv * graphicTileSize.y);
|
||||
triangles[2].texCoords = sf::Vector2f(tu * graphicTileSize.x, (tv + 1) * graphicTileSize.y);
|
||||
triangles[3].texCoords = sf::Vector2f(tu * graphicTileSize.x, (tv + 1) * graphicTileSize.y);
|
||||
triangles[4].texCoords = sf::Vector2f((tu + 1) * graphicTileSize.x, tv * graphicTileSize.y);
|
||||
triangles[5].texCoords = sf::Vector2f((tu + 1) * graphicTileSize.x, (tv + 1) * graphicTileSize.y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,3 +54,5 @@ void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||
states.texture = &texture;
|
||||
target.draw(vertexArray, states);
|
||||
};
|
||||
|
||||
|
||||
|
||||
+9
-3
@@ -1,8 +1,10 @@
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
|
||||
#include "TileMap.h"
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Graphics/PrimitiveType.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <SFML/Graphics/RenderStates.hpp>
|
||||
#include <SFML/Graphics/RenderTarget.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
@@ -21,14 +23,18 @@ enum class MovementDir {
|
||||
|
||||
class Entity : public sf::Drawable, public sf::Transformable {
|
||||
public:
|
||||
Entity();
|
||||
Entity(TileMap& map) : map(map) {};
|
||||
|
||||
//assumes entity texture is one row of various animation texture states
|
||||
bool loadTexture(const std::filesystem::path& texturePath, sf::Vector2u tileSize, int tiles);
|
||||
bool loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles);
|
||||
|
||||
void setTextureFrame(const sf::IntRect& rectangle);
|
||||
protected:
|
||||
TileMap map;
|
||||
|
||||
sf::VertexArray vertexArray;
|
||||
sf::Texture texture;
|
||||
|
||||
int animationTiles;
|
||||
int movementSpeed = 5;
|
||||
|
||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
|
||||
+2
-2
@@ -97,7 +97,7 @@ int main()
|
||||
|
||||
//MazeCharacter pacman("assets/pink_pac_man_movement48.png", mazeMap);
|
||||
|
||||
Entity pacman;
|
||||
Entity pacman(mazeMap);
|
||||
pacman.loadTexture("assets/pink_pac_man_movement48.png", {48, 48}, 5);
|
||||
|
||||
while (window.isOpen())
|
||||
@@ -181,7 +181,7 @@ int main()
|
||||
|
||||
sf::Vector2f pacmanVeloFloat = {pacmanVelo.x * 1.0f, pacmanVelo.y * 1.0f};
|
||||
pinkPacmanSpriteOne.move(pacmanSpeed * pacmanVeloFloat);
|
||||
//pacman.move(pacmanSpeed * pacmanVeloFloat);
|
||||
pacman.move(pacmanSpeed * pacmanVeloFloat);
|
||||
|
||||
window.display();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user