Browse Source

exercise 3

Kajetan Johannes Hammerle 3 years ago
parent
commit
efb66a36e1
6 changed files with 89 additions and 0 deletions
  1. 11 0
      Entity.h
  2. 6 0
      Main.cpp
  3. 23 0
      Map.h
  4. 25 0
      PathFinder.h
  5. 12 0
      Position.h
  6. 12 0
      Tile.h

+ 11 - 0
Entity.h

@@ -0,0 +1,11 @@
+#ifndef ENTITY_H
+#define ENTITY_H
+
+struct Entity {
+    virtual ~Entity() = 0;
+    // these methods are used by tiles
+    virtual bool canSwim() const = 0;
+    virtual bool canFly() const = 0;
+};
+
+#endif

+ 6 - 0
Main.cpp

@@ -6,6 +6,12 @@
 #include "gaming-core/utils/ArrayList.h"
 #include "gaming-core/utils/Logger.h"
 
+#include "Entity.h"
+#include "Map.h"
+#include "PathFinder.h"
+#include "Position.h"
+#include "Tile.h"
+
 // All implementations in this class use ints insteads of size_t because size_t
 // is interely slow compared to ints. C++20 also adds new methods to call for
 // signed container sizes. Several online resources also suggest to use unsigned

+ 23 - 0
Map.h

@@ -0,0 +1,23 @@
+#ifndef MAP_H
+#define MAP_H
+
+#include "Position.h"
+#include "Tile.h"
+
+struct Map final {
+    // OK is 0 to allow interpreting the enum as bool
+    enum LoadResult { OK, FILE_NOT_FOUND, INVALID_FILE_FORMAT };
+
+    // returns the tile at the given position or a null tile if the position is
+    // not valid
+    const Tile& getTile(const Position& p) const;
+    // returns the width of the map, 0 is no map is loaded
+    int getWidth() const;
+    // return the hight of the map, 0 is no map is loaded
+    int getHeight() const;
+
+    // loads a map from a file and returns the result
+    LoadResult load(const char* path);
+};
+
+#endif

+ 25 - 0
PathFinder.h

@@ -0,0 +1,25 @@
+#ifndef PATH_FINDER_H
+#define PATH_FINDER_H
+
+#include "Entity.h"
+#include "Map.h"
+#include "Position.h"
+#include "Vector.h"
+
+struct PathFinder final {
+    struct Path final {
+        // FOUND is 0 to allow interpreting the enum as bool
+        enum Result { FOUND, OUT_OF_RANGE, NOT_FOUND };
+        Result result;
+        // contains the path if result equals FOUND
+        Vector<Position> path;
+    };
+
+    // a path finder is bound to a map
+    PathFinder(const Map& map);
+    // search for a path in a radius between the positions for an entity
+    Path find(const Position& from, const Position& to, const Entity& e,
+              float range);
+};
+
+#endif

+ 12 - 0
Position.h

@@ -0,0 +1,12 @@
+#ifndef POSITION_H
+#define POSITION_H
+
+struct Position final {
+    int x;
+    int y;
+
+    Position();
+    Position(int x, int y);
+};
+
+#endif

+ 12 - 0
Tile.h

@@ -0,0 +1,12 @@
+#ifndef TILE_H
+#define TILE_H
+
+#include "Entity.h"
+
+struct Tile {
+    virtual ~Tile() = 0;
+    // returns true if the tile is solid for the given entity
+    virtual bool isSolid(const Entity& e) const = 0;
+};
+
+#endif