Commit dab60261 authored by Felix Huther's avatar Felix Huther
Browse files

One small bug to Fix in Movement

parent efa493a0
......@@ -21,9 +21,9 @@ const std::string &Character::getTexture() const
return texture;
}
int Character::move()
char Character::move()
{
int direction = {};
char direction = {};
std::cout << "What direction you want to move ?\n";
std::cin >> direction;
return direction;
......@@ -34,3 +34,8 @@ void Character::setCurrentTile(Tile *newCurrentTile)
{
currentTile = newCurrentTile;
}
Tile* Character::getCurrentTile()
{
return currentTile;
}
......@@ -12,10 +12,12 @@ public:
const std::string &getTexture() const;
int move();
char move();
void setCurrentTile(Tile *newCurrentTile);
Tile* getCurrentTile();
private:
Tile* currentTile;
......
#include "dungeoncrawler.h"
#include <iostream>
DungeonCrawler::DungeonCrawler(AbstractUI * abstractUi, Level* currentLevel)
:abstractUi(abstractUi),currentLevel(currentLevel)
{
......@@ -7,6 +7,59 @@ DungeonCrawler::DungeonCrawler(AbstractUI * abstractUi, Level* currentLevel)
}
void DungeonCrawler::characterMove()
{
bool moveGranted = true;
char movingDirection= {};
Character* character = currentLevel->getCharacter();
std::vector<std::vector<Tile*>> currentField = currentLevel->getField();
while (true) {
abstractUi->draw(currentLevel);
movingDirection = character->move();
Tile * characterTile = character->getCurrentTile();
// if(!moveGranted)
// {
// std::cout << "Move not possible\n";
// }
switch (movingDirection){
case 'w':{
Tile* OneUp = currentField.at(characterTile->getPosition()->getRow()-1).at(characterTile->getPosition()->getColumn());
moveGranted = characterTile->moveTo(OneUp,character);
break;
}
case 'a':{
Tile* OneLeft = currentField.at(characterTile->getPosition()->getRow()).at(characterTile->getPosition()->getColumn()-1);
moveGranted = characterTile->moveTo(OneLeft,character);
break;
}
case 's':{
Tile* oneDown = currentField.at(characterTile->getPosition()->getRow()+1).at(characterTile->getPosition()->getColumn());
moveGranted = characterTile->moveTo(oneDown,character);
break;
}
case 'd':{
Tile* oneRight = currentField.at(characterTile->getPosition()->getRow()).at(characterTile->getPosition()->getColumn()+1);
moveGranted = characterTile->moveTo(oneRight,character);
break;
}
}
}
}
void DungeonCrawler::play()
{
currentLevel->setLevelStructure();
......@@ -15,19 +68,17 @@ void DungeonCrawler::play()
if(currentLevel->getCharacter() == nullptr)
{
const int column = 1;
const int row = 1;
const int column = 2;
const int row = 2;
Position2D pos = Position2D(row,column);
currentLevel->placeCharacter(TestChar,pos);
}
while(true){
abstractUi->userInterface();
abstractUi->draw(currentLevel);
currentLevel->placeCharacter(TestChar,pos);
currentLevel->setCharacter(TestChar);
TestChar->setCurrentTile(currentLevel->getField().at(2).at(2));
}
abstractUi->userInterface();
characterMove();
}
#ifndef DUNGEONCRAWLER_H
#define DUNGEONCRAWLER_H
#pragma once
#include "abstractui.h"
#include "level.h"
......@@ -9,12 +9,12 @@ class DungeonCrawler
public:
DungeonCrawler(AbstractUI*,Level*);
void play();
void characterMove();
private:
AbstractUI* abstractUi;
Level* currentLevel;
};
#endif // DUNGEONCRAWLER_H
......@@ -26,9 +26,11 @@ Tile *Floor::onLeave(Tile *destTile, Character *who)
//actually moves the character
bool Floor::moveTo(Tile *destTile, Character *who)
{
if(destTile != nullptr){
if(destTile->getPossibleToWalkOn()){
this->setCurrentCharacter(nullptr);
destTile->setCurrentCharacter(who);
who->setCurrentTile(destTile);
return true;
}
else return false;
......
......@@ -35,11 +35,16 @@ void Level::placeCharacter(Character *character, Position2D &position)
index->setCurrentCharacter(character);
}
std::vector<std::vector<Tile *>> Level::getField() const
{
std::vector<std::vector<Tile*>> Level::getField(){
return field;
}
void Level::setField(std::vector<std::vector<Tile*>>& field)
{
this->field = field;
}
void Level::setLevelStructure()
{
std::string wall = "wall";
......@@ -66,27 +71,6 @@ void Level::setLevelStructure()
}
}
}
// for(int i=0; i<height; i++)
// {
// field.push_back(std::vector<Tile*>());
// for(int j=0; j<width; j++)
// {
// if(i == 0 && j < width){
// Position2D pos = Position2D(i,j);
// Wall* wallPointer = new Wall(wall,pos);
// field[i][j] = wallPointer;
// field[i].push_back(wallPointer);
// }
// else {Position2D pos = Position2D(i,j);
// Floor* floorPointer= new Floor(floor,pos);
// field[i][j] = floorPointer;
// }
// }
// }
}
Character *Level::getCharacter() const
......@@ -94,4 +78,9 @@ Character *Level::getCharacter() const
return character;
}
void Level::setCharacter(Character *newCharacter)
{
character = newCharacter;
}
......@@ -15,7 +15,8 @@ public:
Tile* getTile(Position2D& position) const;
void placeCharacter(Character* character, Position2D& position);
std::vector<std::vector<Tile*>> getField() const;
std::vector<std::vector<Tile*>> getField();
void setField(std::vector<std::vector<Tile*>>& field);
void setLevelStructure();
......@@ -25,6 +26,8 @@ public:
Character *getCharacter() const;
void setCharacter(Character *newCharacter);
private:
const int height;
const int width;
......
......@@ -7,7 +7,7 @@ using namespace std;
int main()
{
AbstractUI* termUI = new TerminalUI();
Level* level = new Level(10,10);
Level* level = new Level(20,20);
DungeonCrawler dungeonCrawler(termUI,level);
dungeonCrawler.play();
......
......@@ -7,9 +7,6 @@ class Position2D
public:
Position2D(int row, int column);
int getRow() const;
int getColumn() const;
......
......@@ -38,6 +38,8 @@ void TerminalUI::userInterface()
switch (UserOperation) {
case 1: { cout << "You entered the Dungeon\n";return;
}
}
......
......@@ -8,7 +8,6 @@ class TerminalUI : public AbstractUI
public:
TerminalUI();
// AbstractUI interface
void draw(Level *lvl) override;
void userInterface() override;
void levelInterface();
......
......@@ -20,7 +20,7 @@ Character *Tile::getCurrentCharacter() const
return currentCharacter;
}
void Tile::setCurrentCharacter(Character *newCurrentCharacter)
void Tile::setCurrentCharacter(Character* newCurrentCharacter)
{
currentCharacter = newCurrentCharacter;
}
......@@ -36,8 +36,14 @@ const std::string &Tile::getTexture() const
return texture;
}
const Position2D &Tile::getPosition() const
Position2D* Tile::getPosition()
{
return position;
return &position;
}
bool Tile::getPossibleToWalkOn() const
{
return possibleToWalkOn;
}
......@@ -27,12 +27,15 @@ public:
//TEXTURE AND POSITION SPECIFIC
const std::string &getTexture() const;
const Position2D &getPosition() const;
Position2D* getPosition();
bool getPossibleToWalkOn() const;
protected:
Character* currentCharacter;
std::string texture;
Position2D position;
bool possibleToWalkOn;
};
......
......@@ -5,6 +5,6 @@
Wall::Wall(std::string &texture, Position2D &position)
:Tile(texture,position)
{
possibleToWalkOn = false;
}
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