Commit 45016cfa authored by Felix Huther's avatar Felix Huther
Browse files

alot of stuff added

parent 7a3379c8
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
TEMPLATE = app
CONFIG += console c++17
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
abstractui.cpp \
character.cpp \
dungeoncrawler.cpp \
floor.cpp \
level.cpp \
main.cpp \
portal.cpp \
position2d.cpp \
terminalui.cpp \
tile.cpp \
wall.cpp
HEADERS += \
abstractui.h \
character.h \
dungeoncrawler.h \
floor.h \
level.h \
portal.h \
position2d.h \
terminalui.h \
tile.h \
wall.h
#include "AbstractUI.h"
AbstractUI::AbstractUI(Level *mCurrentLevel)
: mCurrentLevel(mCurrentLevel) {}
void AbstractUI::draw(Level *pLevel) {
}
#ifndef ABSTRACTUI_H
#define ABSTRACTUI_H
#include "level.h"
#include <iostream>
class AbstractUI {
public:
AbstractUI(Level *mCurrentLevel);
virtual void draw(Level *pLevel) = 0;
virtual char askMoveFromUser() = 0 ;
protected:
Level* mCurrentLevel;
};
#endif // ABSTRACTUI_H
#include "character.h"
Character::Character(std::string texture) {}
const std::string &Character::getTexture() const {
return texture;
}
Tile *Character::getCurrentTile() const {
return currentTile;
}
void Character::setCurrentTile(Tile *currentTile) {
Character::currentTile = currentTile;
}
#ifndef CHARACTER_H
#define CHARACTER_H
#include <string>
class Tile;
class Character {
//CONSTRUCTOR
public:
Character(std::string texture);
//MEMBER VARIABLES
private:
std::string texture;
Tile* currentTile;
//GETTER AND SETTER
public:
void setCurrentTile(Tile *currentTile);
Tile *getCurrentTile() const;
const std::string &getTexture() const;
};
#endif // CHARACTER_H
#include "dungeoncrawler.h"
#include <iostream>
DungeonCrawler::DungeonCrawler(Level *level, AbstractUI* UI)
: mCurrentLevel(level), mUserInterface(UI){
}
void DungeonCrawler::play() {
while(true) {
bool movPossible;
Character *currentChar = mCurrentLevel->getCharacter();
mUserInterface->draw(mCurrentLevel);
char movingDirection = mUserInterface->askMoveFromUser();
Tile *characterTile = currentChar->getCurrentTile();
Position2D characterPos = currentChar->getCurrentTile()->getPosition();
switch (movingDirection) {
case 'w': {
int oneRowUp = characterPos.getRow()-1;
int sameColumn = characterPos.getColumn();
Position2D onePosUp(oneRowUp, sameColumn);
Tile *oneTileUp = mCurrentLevel->getTileAtPos(onePosUp);
if(typeid(*oneTileUp) == typeid(Portal)){
Portal* portal = static_cast<Portal*>(oneTileUp);
oneTileUp = portal->getDestPortal();
onePosUp = oneTileUp->getPosition();
oneTileUp = mCurrentLevel->getTileAtPos(onePosUp);
}
movPossible = oneTileUp->onEnter(characterTile, currentChar);
if (movPossible) {
characterTile->moveTo(oneTileUp, currentChar);
}
break;
}
case 'a':{
int sameRow = characterPos.getRow();
int oneColumnLeft = characterPos.getColumn()-1;
Position2D onePosLeft(sameRow, oneColumnLeft);
Tile *oneTileLeft = mCurrentLevel->getTileAtPos(onePosLeft);
if(typeid(*oneTileLeft) == typeid(Portal)){
Portal* portal = static_cast<Portal*>(oneTileLeft);
oneTileLeft = portal->getDestPortal();
onePosLeft= oneTileLeft->getPosition();
oneTileLeft = mCurrentLevel->getTileAtPos(onePosLeft);
}
movPossible = oneTileLeft->onEnter(characterTile, currentChar);
if (movPossible){
characterTile->moveTo(oneTileLeft, currentChar);
}
break;
}
case 's':{
int oneRowDown = characterPos.getRow() +1;
int sameColumn = characterPos.getColumn();
Position2D onePosDown(oneRowDown, sameColumn);
Tile *oneTileDown = mCurrentLevel->getTileAtPos(onePosDown);
if(typeid(*oneTileDown) == typeid(Portal)){
Portal* portal = static_cast<Portal*>(oneTileDown);
oneTileDown = portal->getDestPortal();
onePosDown = oneTileDown->getPosition();
oneTileDown = mCurrentLevel->getTileAtPos(onePosDown);
}
movPossible = oneTileDown->onEnter(characterTile, currentChar);
if (movPossible){
characterTile->moveTo(oneTileDown, currentChar);
}
break;
}
case 'd':{
int sameRow = characterPos.getRow();
int oneColumnRight = characterPos.getColumn()+1;
Position2D onePosRight(sameRow, oneColumnRight);
Tile *oneTileRight = mCurrentLevel->getTileAtPos(onePosRight);
if(typeid(*oneTileRight) == typeid(Portal)){
Portal* portal = static_cast<Portal*>(oneTileRight);
oneTileRight = portal->getDestPortal();
onePosRight = oneTileRight->getPosition();
oneTileRight = mCurrentLevel->getTileAtPos(onePosRight);
}
movPossible = oneTileRight->onEnter(characterTile, currentChar);
if (movPossible) {
characterTile->moveTo(oneTileRight, currentChar);
}
break;
}
}
}
}
#ifndef DUNGEONCRAWLER_H
#define DUNGEONCRAWLER_H
#include "level.h"
#include "character.h"
#include "abstractui.h"
class AbstractUI;
class DungeonCrawler {
public:
DungeonCrawler(Level *level, AbstractUI* UI);
void play();
private:
Level* mCurrentLevel;
AbstractUI* mUserInterface;
};
#endif // DUNGEONCRAWLER_H
#include "floor.h"
Floor::Floor(const std::string &texture, Character *currentCharacter, const Position2D &position)
: Tile(texture,currentCharacter,position) {}
Tile *Floor::onEnter(Tile *fromTile, Character *who) {
return this;
}
bool Floor::moveTo(Tile *destTile, Character *who) {
if(!destTile->hasCharacter()){
destTile->setCurrentCharacter(this->currentCharacter);
currentCharacter->setCurrentTile(destTile);
this->setCurrentCharacter(nullptr);
return true;
};
return false;
}
Tile *Floor::onLeave(Tile *destTile, Character *who) {
return this;
}
#ifndef FLOOR_H
#define FLOOR_H
#include <string>
#include "tile.h"
class Floor : public Tile{
public:
Floor(const std::string &texture, Character *currentCharacter, const Position2D &position);
Tile* onEnter(Tile* fromTile,Character* who) override;
bool moveTo(Tile* destTile, Character* who) override;
Tile* onLeave(Tile* destTile,Character* who) override;
};
#endif // FLOOR_H
#include "level.h"
Level::Level(int height,int width) : mwidth(width),mheight(height) {
setStructure();
}
int Level::getHeight() const {
return mheight;
}
int Level::getWidth() const {
return mwidth;
}
Level::~Level() {
mcharacter = nullptr;
for (auto& inner : mLevel) {
for (auto& item : inner) {
item = nullptr;
delete item;
}
}
}
void Level::placeCharacter(Character *character, Position2D position) {
int row = position.getRow();
int column = position.getColumn();
mcharacter = character;
mLevel.at(row).at(column)->setCurrentCharacter(character);
character->setCurrentTile(mLevel.at(row).at(column));
}
Tile* Level::getTileAtPos(Position2D position) {
int row = position.getRow();
int column = position.getColumn();
return mLevel.at(row).at(column);
}
const Tile *Level::constGetTileAtPos(Position2D position) {
int row = position.getRow();
int column = position.getColumn();
return mLevel.at(row).at(column);
}
const std::vector<std::vector<Tile *>> &Level::getMLevel() const {
return mLevel;
}
Character *Level::getCharacter() const {
return mcharacter;
}
void Level::setStructure() {
std::string stwall = "wall";
std::string stportal = "portal";
std::string stfloor = "floor";
for(int i = 0 ; i < mheight; i++){
std::vector<Tile*> vec;
mLevel.push_back(vec);
for(int j = 0 ; j < mwidth;j++)
{
Position2D pos = Position2D(i,j);
//draws wall arround the field
if((i == 0 && j < mwidth) ||
j == 0 || j == mwidth-1 ||
i == mheight-1
){
Tile* wallpointer = new Wall(stwall,nullptr,pos);
mLevel.at(i).push_back(wallpointer);
} //fills field with floor
else if(i == 4 && j == 4)
{ Tile* destPortal = new Floor(stfloor,nullptr,Position2D(8,8));
Tile* portalPointer = new Portal(stportal,nullptr,pos,destPortal);
mLevel.at(i).push_back(portalPointer);
}
else
{
Tile* floorPointer= new Floor(stfloor, nullptr,pos);
mLevel.at(i).push_back(floorPointer);
}
}
}
}
#ifndef LEVEL_H
#define LEVEL_H
#include <vector>
#include "tile.h"
#include "portal.h"
#include "wall.h"
#include "floor.h"
#include "position2d.h"
class Level {
//MEMBER VARIABLES
private:
const int mwidth;
const int mheight;
Character* mcharacter;
std::vector<std::vector<Tile*>>mLevel;
void setStructure();
//CONSTRUCTOR
public:
virtual ~Level();
explicit Level(int height,int width);
//GETTER AND SETTER
public:
[[nodiscard]] int getHeight() const;
[[nodiscard]] int getWidth() const;
Tile* getTileAtPos(Position2D position);
const Tile* constGetTileAtPos(Position2D position);
Character *getCharacter() const;
void placeCharacter(Character* character, Position2D position);
const std::vector<std::vector<Tile *>> &getMLevel() const;
};
#endif // LEVEL_H
#include "level.h"
#include "abstractui.h"
#include "terminalui.h"
#include "dungeoncrawler.h"
int main() {
Level* level = new Level(10,30);
Character* myCharacter = new Character("metal");
level->placeCharacter(myCharacter,Position2D(2,2));
AbstractUI* termUI = new TerminalUI(level);
DungeonCrawler crawler(level,termUI);
crawler.play();
return 0;
}
#include "portal.h"
Portal::Portal(const std::string &texture, Character *currentCharacter, const Position2D &position, Tile* destPortal)
: Tile(texture,currentCharacter,position),destPortal(destPortal) {}
Tile *Portal::onEnter(Tile *fromTile, Character *who) {
return destPortal;
}
bool Portal::moveTo(Tile *destTile, Character *who) {
if(!destTile->hasCharacter()){
destPortal->setCurrentCharacter(who);
who->setCurrentTile(destPortal);
this->setCurrentCharacter(nullptr);
return true;
}
else return false;
return true;
}
Tile *Portal::onLeave(Tile *destTile, Character *who) {
return this;
}
Tile *Portal::getDestPortal() const
{
return destPortal;
}
#ifndef PORTAL_H
#define PORTAL_H
#include <string>
#include "tile.h"
class Portal : public Tile{
public:
Portal(const std::string &texture, Character *currentCharacter, const Position2D &position,Tile* destPortal);
Tile* onEnter(Tile* fromTile,Character* who) override;
bool moveTo(Tile* destTile, Character* who) override;
Tile* onLeave(Tile* destTile,Character* who) override;
Tile *getDestPortal() const;
private:
Tile* destPortal;
};
#endif // PORTAL_H
#include "position2d.h"
Position2D::Position2D(int row, int column)
:row(row),column(column){}
int Position2D::getRow() const {
return row;
}