Skip to content

Commit

Permalink
Initial basic structure (#1)
Browse files Browse the repository at this point in the history
* Initial state.

* Added NcursesInstance, NcursesView.

* Added initial view requests.

* Fixed MapRequest.

* Added observer pattern behaviour between PathosInstance and View.

* MapObject containing Char.

* Initial Hostile abstract mob.
  • Loading branch information
kallentu authored Dec 21, 2018
1 parent 5ecc385 commit 8599a5c
Show file tree
Hide file tree
Showing 23 changed files with 421 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pathos

# Prerequisites
*.d

Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SRCDIR = src
CXXFLAGS = -std=c++17 -I${SRCDIR} -Wall -Wextra -Wpedantic -MMD -g
SOURCES = $(shell find ${SRCDIR} -name '*.cc')
OBJECTS = ${SOURCES:.cc=.o}
DEPENDS = ${SOURCES:.cc=.d}
EXEC = pathos

${EXEC}: ${OBJECTS}
${CXX} ${LDFLAGS} $^ -lncurses -o $@

-include ${DEPENDS}

.PHONY: clean

clean:
rm -f ${OBJECTS} ${DEPENDS}
27 changes: 27 additions & 0 deletions src/abstract/Observable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef PATHOS_OBSERVABLE
#define PATHOS_OBSERVABLE

#include <memory>
#include <unordered_set>

#include "abstract/Observer.h"

namespace Pathos {

template <typename Event> class Observable {
std::unordered_set<Observer<Event> *> observers;

public:
virtual ~Observable() {}

void addObserver(Observer<Event> *o) { observers.insert(o); }
void removeObserver(Observer<Event> *o) { observers.erase(0); }
void notify(Event *e) {
for (auto *o : observers)
o->process(e);
}
};

} // namespace Pathos

#endif // PATHOS_OBSERVABLE
14 changes: 14 additions & 0 deletions src/abstract/Observer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef PATHOS_OBSERVER
#define PATHOS_OBSERVER

namespace Pathos {

template <typename Event> class Observer {
public:
virtual ~Observer() {}
virtual void process(Event *e) = 0;
};

} // namespace Pathos

#endif // PATHOS_OBSERVER
17 changes: 17 additions & 0 deletions src/core/PathosInstance.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "core/PathosInstance.h"
#include "abstract/Observable.h"
#include "request/ViewRequest.h"
#include "view/curses/MapView.h"
#include "view/curses/NcursesInstance.h"

using namespace Pathos;

PathosInstance::PathosInstance()
: curses{std::make_unique<NcursesInstance>()},
view{std::make_unique<MapView>(curses.get())} {
Observable<ViewRequest>::addObserver(view.get());
}

PathosInstance::~PathosInstance() {}

void PathosInstance::run() {}
27 changes: 27 additions & 0 deletions src/core/PathosInstance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef PATHOS_PATHOS_INSTANCE
#define PATHOS_PATHOS_INSTANCE

#include <memory>

#include "abstract/Observable.h"
#include "request/ViewRequest.h"

namespace Pathos {

class View;
class NcursesInstance;

class PathosInstance : public Observable<ViewRequest> {
std::unique_ptr<NcursesInstance> curses;
std::unique_ptr<View> view;

public:
PathosInstance();
~PathosInstance();

void run();
};

} // namespace Pathos

#endif // PATHOS_PATHOS_INSTANCE
3 changes: 3 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core/PathosInstance.h"

int main() { return 0; }
21 changes: 21 additions & 0 deletions src/map/Map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef PATHOS_MAP
#define PATHOS_MAP

#include <vector>

#include "map/MapObject.h"

namespace Pathos {

class Map {
std::vector<std::vector<MapObject>> map;

public:
// TODO:
Map(MapObject *m) {}
virtual ~Map() {}
};

} // namespace Pathos

#endif // PATHOS_MAP
20 changes: 20 additions & 0 deletions src/map/MapObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PATHOS_MAP_OBJECT
#define PATHOS_MAP_OBJECT

namespace Pathos {

class MapObject {
public:
enum class Char { Lantern };

MapObject(Char c) : c{c} {}
virtual ~MapObject() {}

private:
// Object's symbol in the view.
Char c;
};

} // namespace Pathos

#endif // PATHOS_MAP_OBJECT
16 changes: 16 additions & 0 deletions src/mob/hostile/Hostile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PATHOS_HOSTILE
#define PATHOS_HOSTILE

#include "map/MapObject.h"

namespace Pathos {

class Hostile : MapObject {
public:
Hostile() : MapObject(MapObject::Char::Lantern) {}
virtual ~Hostile() {}
};

} // namespace Pathos

#endif // PATHOS_HOSTILE
15 changes: 15 additions & 0 deletions src/request/MapRequest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "request/MapRequest.h"
#include "map/Map.h"
#include "view/View.h"

using namespace Pathos;

// TODO: Complete after Map

MapRequest::MapRequest(Map *m) {}

MapRequest::~MapRequest() {}

void MapRequest::beDrawnBy(View &view) const { view.draw(*this); }

void MapRequest::convertMap(Map *m) {}
26 changes: 26 additions & 0 deletions src/request/MapRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef PATHOS_MAP_REQUEST
#define PATHOS_MAP_REQUEST

#include <vector>

#include "request/ViewRequest.h"

namespace Pathos {

class Map;

struct MapRequest : public ViewRequest {
std::vector<std::vector<char>> map;

MapRequest(Map *m);
~MapRequest();

void beDrawnBy(View &view) const override;

private:
void convertMap(Map *m);
};

} // namespace Pathos

#endif // PATHOS_MAP_REQUEST
15 changes: 15 additions & 0 deletions src/request/NotificationRequest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "request/NotificationRequest.h"
#include "view/View.h"
#include <string>
#include <vector>

using namespace Pathos;

// TODO

NotificationRequest::NotificationRequest(std::vector<std::string> &n)
: notifications{n} {}

NotificationRequest::~NotificationRequest() {}

void NotificationRequest::beDrawnBy(View &view) const { view.draw(*this); }
24 changes: 24 additions & 0 deletions src/request/NotificationRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef PATHOS_NOTIFICATION_REQUEST
#define PATHOS_NOTIFICATION_REQUEST

#include <string>
#include <vector>

#include "request/ViewRequest.h"

namespace Pathos {

class View;

struct NotificationRequest : public ViewRequest {
std::vector<std::string> notifications;

NotificationRequest(std::vector<std::string> &n);
~NotificationRequest();

void beDrawnBy(View &view) const override;
};

} // namespace Pathos

#endif // PATHOS_NOTIFICATION_REQUEST
15 changes: 15 additions & 0 deletions src/request/ViewRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef PATHOS_VIEW_REQUEST
#define PATHOS_VIEW_REQUEST

namespace Pathos {

class View;

struct ViewRequest {
virtual ~ViewRequest() {}
virtual void beDrawnBy(View &view) const = 0;
};

} // namespace Pathos

#endif // PATHOS_VIEW_REQUEST
5 changes: 5 additions & 0 deletions src/view/View.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "view/View.h"

using namespace Pathos;

void View::process(ViewRequest *req) { req->beDrawnBy(*this); }
24 changes: 24 additions & 0 deletions src/view/View.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef PATHOS_VIEW
#define PATHOS_VIEW

#include "abstract/Observer.h"
#include "request/ViewRequest.h"

namespace Pathos {

class MapRequest;
class NotificationRequest;

class View : public Observer<ViewRequest> {
public:
virtual ~View() {}

void process(ViewRequest *req);

virtual void draw(const MapRequest &req) = 0;
virtual void draw(const NotificationRequest &req) = 0;
};

} // namespace Pathos

#endif // PATHOS_VIEW
13 changes: 13 additions & 0 deletions src/view/curses/MapView.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "view/curses/MapView.h"
#include "view/curses/NcursesInstance.h"
#include "view/curses/NcursesView.h"

using namespace Pathos;

MapView::MapView(NcursesInstance *curses)
: NcursesView(curses), height{NcursesView::getHeight()},
width{NcursesView::getWidth()} {}

void MapView::draw(const MapRequest &req) {}

void MapView::draw(const NotificationRequest &req) {}
27 changes: 27 additions & 0 deletions src/view/curses/MapView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef PATHOS_MAP_VIEW
#define PATHOS_MAP_VIEW

#include <vector>

#include "view/curses/NcursesView.h"

namespace Pathos {

class MapRequest;
class NotificationRequest;
class NcursesInstance;

class MapView : public NcursesView {
std::vector<std::vector<char>> map;
size_t height, width;

public:
MapView(NcursesInstance *curses);

void draw(const MapRequest &req) override;
void draw(const NotificationRequest &req) override;
};

} // namespace Pathos

#endif // PATHOS_MAP_VIEW
26 changes: 26 additions & 0 deletions src/view/curses/NcursesInstance.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "view/curses/NcursesInstance.h"

#include <ncurses.h>
#include <string>

using namespace Pathos;

NcursesInstance::NcursesInstance() {
initscr();
updateBounds();
raw();
}

NcursesInstance::~NcursesInstance() { endwin(); }

void NcursesInstance::move(size_t y, size_t x) { wmove(stdscr, y, x); }

void NcursesInstance::movePrint(size_t y, size_t x, std::string s) {
mvprintw(y, x, "%s", s.c_str());
}

void NcursesInstance::print(std::string s) { printw("%s", s.c_str()); }

void NcursesInstance::refresh() { refresh(); }

void NcursesInstance::updateBounds() { getmaxyx(stdscr, height, width); }
Loading

0 comments on commit 8599a5c

Please sign in to comment.