Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include "gameManager.h"
- /*cmake -G "MinGW Makefiles" ..*/
- int main() {
- GameManager gameManager;
- if (!gameManager.initialize()) {
- return 1;
- }
- gameManager.runGameLoop();
- return 0;
- }
- //entityImplementation.cpp
- #include <SFML/Graphics.hpp>
- #include "entity.h"
- Entity::Entity(sf::Texture &texture,sf::Vector2f position):sprite(texture){
- sprite.setPosition(position);
- }
- void Entity::setPos(sf::Vector2f position){
- sprite.setPosition(position);
- }
- sf::Vector2f Entity::getPos(){
- return sprite.getPosition();
- }
- void Entity::setTexture(sf::Texture& texture){
- sprite.setTexture(texture);
- }
- void Entity::draw(sf::RenderWindow& window) {
- window.draw(this->sprite);
- }
- sf::Vector2f Entity::getSpeed(){
- return speed;
- }
- void Entity::setSpeed(sf::Vector2f newSpeed){
- speed = newSpeed;
- }
- Entity::~Entity(){
- };
- //player.cpp
- #include "entity.h"
- #include "player.h"
- #include <SFML/Graphics.hpp>
- Player::Player(sf::Texture& texture, sf::Vector2f position) : Entity(texture, position){
- setSpeed({1.0,1.0});
- }
- //world.cpp
- #include "world.h"
- #include "player.h"
- #include "memory.h"
- World::World(std::unique_ptr<Entity> player):player(std::move(player)){
- }
- World::World() : player(nullptr) {}
- Entity* World::getPlayer() const {
- return player.get();
- }
- void World::update(float dtSeconds,sf::Vector2f movementInput){
- Entity* player = this->getPlayer();
- player->setPos((player->getPos() + movementInput));
- }
- void World::draw(sf::RenderWindow& window){
- player->draw(window);
- }
- //entityManager.cpp
- #include <string>
- #include "entity.h"
- #include <SFML/Graphics.hpp>
- #include "entityManager.h"
- #include <memory>
- #include <iostream>
- #include "player.h"
- sf::Texture* EntityManager::loadTexture(std::string path) {
- auto it = textureMap.find(path);
- if (it != textureMap.end()) {
- return &it->second;
- } else {
- sf::Texture newTexture;
- if (newTexture.loadFromFile(path)) {
- textureMap[path] = newTexture;
- return &textureMap[path];
- } else {
- throw std::runtime_error("Error loading texture: " + path);
- }
- }
- }
- std::unique_ptr<Entity> EntityManager::entityFactory(std::string entityType,std::string path,sf::Vector2f position){
- sf::Texture* texture = loadTexture(path);
- if (entityType == "Player"){
- return std::make_unique<Player>(*texture,position);
- }
- throw std::runtime_error("Error loading texture: " + path + " of type: " + entityType);
- }
- //gameManager.cpp
- #include "gameManager.h"
- #include <iostream>
- #include <stdexcept>
- #include <SFML/Graphics.hpp>
- #include <cmath>
- #include <map>
- #include <string>
- GameManager::GameManager() : initialization_failed(false), window(sf::VideoMode({1920u, 1080u}), "Game") {}
- GameManager::~GameManager() {}
- bool GameManager::initialize() {
- initialization_failed = false;
- try {
- player = entityManager.entityFactory("Player", "build/assets/texture/cube.png", sf::Vector2f(250, 250));
- world = World(std::move(player));
- } catch (const std::runtime_error& error) {
- std::cerr << "Error creating game object: " << error.what() << std::endl;
- initialization_failed = true;
- }
- if (initialization_failed) {
- std::cerr << "Game initialization failed. Exiting." << std::endl;
- return false;
- }
- window.setFramerateLimit(144);
- return true;
- }
- sf::RenderWindow& GameManager::getWindow() {
- return window;
- }
- void GameManager::runGameLoop() {
- auto player = world.getPlayer();
- std::map <std::string,bool> inputs;
- sf::Vector2f movementInput;
- auto InputToMovementDirection = [=] (std::map<std::string,bool> inputs){
- sf::Vector2f direction;
- for (auto input: inputs){
- if(input.second){
- if(input.first == "pressingW"){
- direction.y -= player->getSpeed().y;
- }
- if(input.first == "pressingA"){
- direction.x -= player->getSpeed().x;
- }
- if(input.first == "pressingS"){
- direction.y += player->getSpeed().y;
- }
- if(input.first == "pressingD"){
- direction.x += player->getSpeed().x;
- }
- if(input.first == "pressingLeftMouse"){
- //reserved
- }
- if(input.first == "pressingRightMouse"){
- //reserved
- }
- }
- }
- return direction;
- };
- while (window.isOpen()) {
- while (const std::optional event = window.pollEvent()) {
- if (event->is<sf::Event::Closed>()) {
- window.close();
- }
- }
- inputs["pressingW"] = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W);
- inputs["pressingA"] = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A);
- inputs["pressingS"] = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S);
- inputs["pressingD"] = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D);
- inputs["pressingLeftMouse"] = sf::Mouse::isButtonPressed(sf::Mouse::Button::Left);
- inputs["pressingRightMouse"] = sf::Mouse::isButtonPressed(sf::Mouse::Button::Right);
- movementInput = InputToMovementDirection(inputs);
- sf::Time deltaTime = clock.restart();
- float dtSeconds = deltaTime.asSeconds();
- world.update(dtSeconds, movementInput);
- window.clear();
- world.draw(window);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment