Commit 7daefb54 authored by Felix Huther's avatar Felix Huther
Browse files

portals work like they should

parent 044c72b2
#include "AbstractUI.h"
#include "abstractui.h";
AbstractUI::AbstractUI(Level *mCurrentLevel)
: mCurrentLevel(mCurrentLevel) {}
: mCurrentLevel(mCurrentLevel) {}
AbstractUI::~AbstractUI()
{
mCurrentLevel = nullptr;
delete mCurrentLevel;
}
void AbstractUI::draw(Level *pLevel) {
}
......
......@@ -6,6 +6,8 @@
class AbstractUI {
public:
AbstractUI(Level *mCurrentLevel);
virtual ~AbstractUI() = 0;
virtual void draw(Level *pLevel) = 0;
virtual char askMoveFromUser() = 0 ;
......
......@@ -9,6 +9,7 @@ class Character {
//CONSTRUCTOR
public:
Character(std::string texture);
~Character() = default;
//MEMBER VARIABLES
......
......@@ -6,19 +6,29 @@ DungeonCrawler::DungeonCrawler(Level *level, AbstractUI* UI)
}
DungeonCrawler::~DungeonCrawler()
{
mCurrentLevel= nullptr;
mUserInterface = nullptr;
delete mCurrentLevel;
delete mUserInterface;
}
void DungeonCrawler::play() {
while(true) {
bool movPossible;
char movingDirection;
Character *currentChar = mCurrentLevel->getCharacter();
mUserInterface->draw(mCurrentLevel);
char movingDirection = mUserInterface->askMoveFromUser();
Tile *characterTile = currentChar->getCurrentTile();
Position2D characterPos = currentChar->getCurrentTile()->getPosition();
system("CLS");
mUserInterface->draw(mCurrentLevel);
movingDirection = mUserInterface->askMoveFromUser();
switch (movingDirection) {
case 'w': {
int oneRowUp = characterPos.getRow()-1;
......@@ -38,6 +48,7 @@ void DungeonCrawler::play() {
}
movPossible = oneTileUp->onEnter(characterTile, currentChar);
if (movPossible) {
characterTile->moveTo(oneTileUp, currentChar);
}
......
......@@ -10,6 +10,7 @@ class DungeonCrawler {
public:
DungeonCrawler(Level *level, AbstractUI* UI);
~DungeonCrawler();
void play();
private:
......
#include "floor.h"
Floor::Floor(const std::string &texture, Character *currentCharacter, const Position2D &position)
: Tile(texture,currentCharacter,position) {}
: Tile(texture,currentCharacter,position) {}
Floor::~Floor()
{
currentCharacter = nullptr;
delete currentCharacter;
}
Tile *Floor::onEnter(Tile *fromTile, Character *who) {
return this;
......
......@@ -7,6 +7,7 @@
class Floor : public Tile{
public:
Floor(const std::string &texture, Character *currentCharacter, const Position2D &position);
~Floor();
Tile* onEnter(Tile* fromTile,Character* who) override;
bool moveTo(Tile* destTile, Character* who) override;
......
......@@ -75,10 +75,25 @@ void Level::setStructure() {
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 if((i == 4 && j == 4) || (i == 8 && j == 8))
{
Tile* portalOne;
Tile* portalTwo;
if(i == 4)
{
portalOne = new Portal(stfloor,nullptr,pos,nullptr);
mLevel.at(i).push_back(portalOne);
}
else
{
portalTwo = new Portal(stportal,nullptr,pos,portalOne);
Portal* portalPointer = static_cast<Portal*>(portalOne);
portalPointer->setDestPortal(portalTwo);
mLevel.at(i).push_back(portalTwo);
}
}
else
{
......
......@@ -21,7 +21,7 @@ private:
//CONSTRUCTOR
public:
virtual ~Level();
~Level();
explicit Level(int height,int width);
//GETTER AND SETTER
......
#include "portal.h"
Portal::Portal(const std::string &texture, Character *currentCharacter, const Position2D &position, Tile* destPortal)
: Tile(texture,currentCharacter,position),destPortal(destPortal) {}
: Tile(texture,currentCharacter,position),destPortal(destPortal),firstEnter(false) {}
Portal::~Portal()
{
currentCharacter = nullptr;
delete currentCharacter;
destPortal = nullptr;
delete destPortal;
}
Tile *Portal::onEnter(Tile *fromTile, Character *who) {
return destPortal;
}
bool Portal::moveTo(Tile *destTile, Character *who) {
if(!destTile->hasCharacter()){
if(firstEnter){
destPortal->setCurrentCharacter(who);
destTile->setCurrentCharacter(nullptr);
who->setCurrentTile(destPortal);
this->setCurrentCharacter(nullptr);
return true;
}
else return false;
return true;
else
{
destTile->setCurrentCharacter(who);
this->setCurrentCharacter(nullptr);
who->setCurrentTile(destTile);
return true;
}
}
......@@ -23,8 +38,13 @@ Tile *Portal::onLeave(Tile *destTile, Character *who) {
return this;
}
Tile *Portal::getDestPortal() const
Tile *Portal::getDestPortal()
{
return destPortal;
}
void Portal::setDestPortal(Tile *newDestPortal)
{
destPortal = newDestPortal;
}
......@@ -9,6 +9,7 @@
class Portal : public Tile{
public:
Portal(const std::string &texture, Character *currentCharacter, const Position2D &position,Tile* destPortal);
~Portal();
Tile* onEnter(Tile* fromTile,Character* who) override;
......@@ -16,10 +17,17 @@ public:
Tile* onLeave(Tile* destTile,Character* who) override;
Tile *getDestPortal() const;
Tile *getDestPortal();
void setDestPortal(Tile *newDestPortal);
void setFirstEnter(bool newFirstEnter);
bool getFirstEnter() const;
private:
Tile* destPortal;
bool firstEnter;
};
......
......@@ -6,6 +6,12 @@
TerminalUI::TerminalUI(Level *mCurrentLevel)
: AbstractUI(mCurrentLevel) {}
TerminalUI::~TerminalUI()
{
mCurrentLevel = nullptr;
delete mCurrentLevel;
}
void TerminalUI::draw(Level *pLevel) {
std::vector<std::vector<Tile*>>field = mCurrentLevel->getMLevel();
......
......@@ -6,6 +6,10 @@
class TerminalUI : public AbstractUI{
public:
TerminalUI(Level *mCurrentLevel);
~TerminalUI();
void draw(Level *pLevel) override;
char askMoveFromUser() override;
......
......@@ -3,7 +3,13 @@
#include <string>
Tile::Tile(std::string texture, Character *currentCharacter, Position2D position)
: texture(texture), currentCharacter(currentCharacter), position(position){}
: texture(texture), currentCharacter(currentCharacter), position(position){}
Tile::~Tile()
{
currentCharacter = nullptr;
delete currentCharacter;
}
const std::string &Tile::getTexture() const {
return texture;
......@@ -21,3 +27,4 @@ bool Tile::hasCharacter() {
if(currentCharacter == nullptr) return false;
else return true;
}
......@@ -10,6 +10,7 @@ class Character;
class Tile {
public:
Tile(std::string texture, Character *currentCharacter, Position2D position);
virtual ~Tile();
protected:
std::string texture;
......
......@@ -2,7 +2,13 @@
#include "wall.h"
Wall::Wall(const std::string &texture, Character *currentCharacter, const Position2D &position)
: Tile(texture,currentCharacter,position) {}
: Tile(texture,currentCharacter,position) {}
Wall::~Wall()
{
currentCharacter = nullptr;
delete currentCharacter;
}
Tile *Wall::onLeave(Tile *destTile, Character *who) {
return nullptr;
......
......@@ -8,6 +8,7 @@
class Wall : public Tile{
public:
Wall(const std::string &texture, Character *currentCharacter, const Position2D &position);
~Wall();
Tile* onEnter(Tile* fromTile,Character* who) override;
bool moveTo(Tile* destTile, Character* who) override;
......
......@@ -19,6 +19,5 @@ int main()
for(auto members: hoersaal){
members->PrintName();
}
return 0;
}
......@@ -16,6 +16,7 @@ public:
private:
const Sportverein mSportverein;
mutable int mutableInt;
// AbstractUI interface
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment