Commit 398c049f authored by Felix Huther's avatar Felix Huther
Browse files

Praktika1 Working

parent 735769a4
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/NEWpraktika1.iml" filepath="$PROJECT_DIR$/.idea/NEWpraktika1.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
//
// Created by felix on 19/05/2022.
//
#include "AbstractUI.h"
AbstractUI::AbstractUI(Level *mCurrentLevel)
: mCurrentLevel(mCurrentLevel) {}
void AbstractUI::draw(Level *pLevel) {
}
//
// Created by felix on 19/05/2022.
//
#ifndef NEWPRAKTIKA1_ABSTRACTUI_H
#define NEWPRAKTIKA1_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 //NEWPRAKTIKA1_ABSTRACTUI_H
cmake_minimum_required(VERSION 3.22)
project(NEWpraktika1)
set(CMAKE_CXX_STANDARD 17)
add_executable(NEWpraktika1
main.cpp
Tile.cpp Tile.h
Floor.cpp Floor.h
Portal.cpp Portal.h
Wall.cpp Wall.h
Character.cpp Character.h
Level.cpp Level.h
DungeonCrawler.cpp DungeonCrawler.h
AbstractUI.cpp AbstractUI.h
Position2D.cpp Position2D.h TerminalUI.cpp TerminalUI.h)
//
// Created by felix on 19/05/2022.
//
#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;
}
//
// Created by felix on 19/05/2022.
//
#ifndef NEWPRAKTIKA1_CHARACTER_H
#define NEWPRAKTIKA1_CHARACTER_H
#include <string>
#include "Tile.h"
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 //NEWPRAKTIKA1_CHARACTER_H
//
// Created by felix on 19/05/2022.
//
#include "DungeonCrawler.h"
DungeonCrawler::DungeonCrawler(Level *level, AbstractUI* UI)
: mCurrentLevel(level), mUserInterface(UI){
}
void DungeonCrawler::play() {
while (true) {
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);
bool movPossible = oneTileUp->onEnter(characterTile, currentChar);
if (movPossible) {
characterTile->moveTo(oneTileUp, currentChar);
mCurrentLevel->placeCharacter(currentChar,onePosUp);
}
break;
}
case 'a':{
int sameRow = characterPos.getRow();
int oneColumnLeft = characterPos.getColumn()-1;
Position2D onePosRight(sameRow, oneColumnLeft);
Tile *oneTileLeft = mCurrentLevel->getTileAtPos(onePosRight);
bool movPossible = oneTileLeft->onEnter(characterTile, currentChar);
if (movPossible){
characterTile->moveTo(oneTileLeft, currentChar);
mCurrentLevel->placeCharacter(currentChar,onePosRight);
}
break;
}
case 's':{
int oneRowDown = characterPos.getRow() +1;
int sameColumn = characterPos.getColumn();
Position2D onePosRight(oneRowDown, sameColumn);
Tile *oneTileDown = mCurrentLevel->getTileAtPos(onePosRight);
bool movPossible = oneTileDown->onEnter(characterTile, currentChar);
if (movPossible){
characterTile->moveTo(oneTileDown, currentChar);
mCurrentLevel->placeCharacter(currentChar,onePosRight);
}
break;
}
case 'd':{
int sameRow = characterPos.getRow();
int oneColumnRight = characterPos.getColumn()+1;
Position2D onePosRight(sameRow, oneColumnRight);
Tile *oneTileUp = mCurrentLevel->getTileAtPos(onePosRight);
bool movPossible = oneTileUp->onEnter(characterTile, currentChar);
if (movPossible) {
characterTile->moveTo(oneTileUp, currentChar);
mCurrentLevel->placeCharacter(currentChar,onePosRight);
}
break;
}
}
}
}
\ No newline at end of file
//
// Created by felix on 19/05/2022.
//
#ifndef NEWPRAKTIKA1_DUNGEONCRAWLER_H
#define NEWPRAKTIKA1_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 //NEWPRAKTIKA1_DUNGEONCRAWLER_H
//
// Created by felix on 19/05/2022.
//
#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);
this->setCurrentCharacter(nullptr);
return true;
};
return false;
}
Tile *Floor::onLeave(Tile *destTile, Character *who) {
return this;
}
//
// Created by felix on 19/05/2022.
//
#ifndef NEWPRAKTIKA1_FLOOR_H
#define NEWPRAKTIKA1_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 //NEWPRAKTIKA1_FLOOR_H
//
// Created by felix on 19/05/2022.
//
#include "Level.h"
Level::Level(int height,int width) : mheight(height),mwidth(width) {
setStructure();
}
const int Level::getHeight() const {
return mheight;
}
const 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;
}
Position2D Level::getCharacterPosition() {
}
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++)
{
//draws wall arround the field
if((i == 0 && j < mwidth) ||
j == 0 || j == mwidth-1 ||
i == mheight-1
){
Position2D pos = Position2D(i,j);
Wall* wallPointer = new Wall(stwall, nullptr,pos);
mLevel.at(i).push_back(wallPointer);
} //fills field with floor
else {Position2D pos = Position2D(i,j);
Floor* floorPointer= new Floor(stfloor, nullptr,pos);
mLevel.at(i).push_back(floorPointer);
}
}
}
}
//
// Created by felix on 19/05/2022.
//
#ifndef NEWPRAKTIKA1_LEVEL_H
#define NEWPRAKTIKA1_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]] const int getHeight() const;
[[nodiscard]] const int getWidth() const;
Tile* getTileAtPos(Position2D position);
const Tile* constGetTileAtPos(Position2D position);
Character *getCharacter() const;
Position2D getCharacterPosition();
void placeCharacter(Character* character, Position2D position);
const std::vector<std::vector<Tile *>> &getMLevel() const;
};
#endif //NEWPRAKTIKA1_LEVEL_H
//
// Created by felix on 19/05/2022.
//
#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;
}
Tile *Portal::onLeave(Tile *destTile, Character *who) {
return this;
}
//
// Created by felix on 19/05/2022.
//
#ifndef NEWPRAKTIKA1_PORTAL_H
#define NEWPRAKTIKA1_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;
private:
Tile* destPortal;
};
#endif //NEWPRAKTIKA1_PORTAL_H
#include "position2D.h"
//
// Created by felix on 19/05/2022.
//
#include "Position2D.h"
Position2D::Position2D(int row, int column)
:row(row),column(column)
{
:row(row),column(column){}
}
int Position2D::getRow() const
{
int Position2D::getRow() const {
return row;
}
int Position2D::getColumn() const
{
int Position2D::getColumn() const {
return column;
}
#ifndef POSITION2D_H
#define POSITION2D_H
#pragma once
//
// Created by felix on 19/05/2022.
//
class Position2D
{
public:
Position2D(int row, int column);
#ifndef NEWPRAKTIKA1_POSITION2D_H
#define NEWPRAKTIKA1_POSITION2D_H
int getRow() const;
int getColumn() const;
class Position2D {
public:
Position2D(int row, int column);
private:
int row;
int column;
public:
int getRow() const;
int getColumn() const;
};
#endif // POSITION_H
#endif //NEWPRAKTIKA1_POSITION2D_H
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