Browse Source

Path rendering and logic

Kajetan Johannes Hammerle 1 year ago
parent
commit
cf97b5f8e9
1 changed files with 98 additions and 7 deletions
  1. 98 7
      Main.cpp

+ 98 - 7
Main.cpp

@@ -3,6 +3,7 @@
 
 #include "data/Array.h"
 #include "data/ArrayList.h"
+#include "data/List.h"
 #include "math/Vector.h"
 #include "rendering/Shader.h"
 #include "rendering/VertexBuffer.h"
@@ -67,27 +68,27 @@ struct Hexagon {
         }
     }
 
-    Vector2 getTopCorner() {
+    Vector2 getTopCorner() const {
         return mid + Vector2(0.0f, RADIUS);
     }
 
-    Vector2 getBottomCorner() {
+    Vector2 getBottomCorner() const {
         return mid - Vector2(0.0f, RADIUS);
     }
 
-    Vector2 getLeftTopCorner() {
+    Vector2 getLeftTopCorner() const {
         return mid - Vector2(WIDTH, -LINE_LENGTH) * 0.5f;
     }
 
-    Vector2 getLeftBottomCorner() {
+    Vector2 getLeftBottomCorner() const {
         return mid - Vector2(WIDTH, LINE_LENGTH) * 0.5f;
     }
 
-    Vector2 getRightTopCorner() {
+    Vector2 getRightTopCorner() const {
         return mid + Vector2(WIDTH, LINE_LENGTH) * 0.5f;
     }
 
-    Vector2 getRightBottomCorner() {
+    Vector2 getRightBottomCorner() const {
         return mid + Vector2(WIDTH, -LINE_LENGTH) * 0.5f;
     }
 };
@@ -95,9 +96,10 @@ struct Hexagon {
 static Array<Hexagon, 19> hexagons;
 
 struct Corner {
-    int player = 0;
+    int player = -1;
     Vector2 mid;
     ArrayList<int, 3> hexagons;
+    ArrayList<int, 3> paths;
 
     void addToBuffer() const {
         addSquare(mid, 0.05f, Color4(255, 255, 255, 255));
@@ -106,6 +108,34 @@ struct Corner {
 
 static Array<Corner, 54> corners;
 
+struct Path {
+    int player = -1;
+    Vector2 from;
+    Vector2 to;
+    int cornerA = -1;
+    int cornerB = -1;
+
+    void addToBuffer() const {
+        Vector2 diff = to - from;
+        Vector2 normal(diff[1], -diff[0]);
+        normal.normalize();
+        normal *= 0.01f;
+
+        Vector2 a = from + normal;
+        Vector2 b = from - normal;
+        Vector2 c = to + normal;
+        Vector2 d = to - normal;
+        addTriangle(a, c, b, Color4(255, 255, 255, 255));
+        addTriangle(c, d, b, Color4(255, 255, 255, 255));
+    }
+
+    Vector2 getMid() const {
+        return (to + from) * 0.5f;
+    }
+};
+
+static List<Path> paths;
+
 static void initResources() {
     for(int i = 0; i < 4; i++) {
         hexagons[i].resource = Resource::WHEAT;
@@ -199,11 +229,69 @@ static void initHexagonCorners() {
     }
 }
 
+static bool doesPathExist(const Path& p) {
+    Vector2 mid = p.getMid();
+    for(const Path& po : paths) {
+        if((mid - po.getMid()).squareLength() < 0.0001f) {
+            return true;
+        }
+    }
+    return false;
+}
+
+static void initPaths() {
+    for(int i = 0; i < corners.getLength(); i++) {
+        for(int k = 0; k < corners.getLength(); k++) {
+            if(i == k || (corners[i].mid - corners[k].mid).length() >=
+                             LINE_LENGTH * 1.01f) {
+                continue;
+            }
+            Path p;
+            p.from = corners[i].mid;
+            p.to = corners[k].mid;
+            if(doesPathExist(p)) {
+                continue;
+            }
+            paths.add(p);
+        }
+    }
+    LOG_INFO(
+        StringBuffer<256>("Got ").append(paths.getLength()).append(" paths"));
+}
+
+static void initCornerPaths() {
+    for(int c = 0; c < corners.getLength(); c++) {
+        for(int i = 0; i < paths.getLength(); i++) {
+            Vector2 mid = paths[i].getMid();
+            if((corners[c].mid - mid).length() >= RADIUS) {
+                continue;
+            }
+            if(corners[c].paths.add(i)) {
+                LOG_WARNING("Corner path overflow");
+            }
+            if(paths[i].cornerA == -1) {
+                paths[i].cornerA = c;
+            } else if(paths[i].cornerB == -1) {
+                paths[i].cornerB = c;
+            } else {
+                LOG_WARNING("Path got too much corners");
+            }
+        }
+    }
+    for(const Path& p : paths) {
+        if(p.cornerA == -1 || p.cornerB == -1) {
+            LOG_WARNING("Path is missing corners");
+        }
+    }
+}
+
 static void init() {
     initResources();
     initHexagonMid();
     initCornerMid();
     initHexagonCorners();
+    initPaths();
+    initCornerPaths();
 }
 
 static void buildBuffer() {
@@ -214,6 +302,9 @@ static void buildBuffer() {
     for(const Corner& c : corners) {
         c.addToBuffer();
     }
+    for(const Path& p : paths) {
+        p.addToBuffer();
+    }
     vertexBuffer.setData(buffer, GL::BufferUsage::STATIC);
 }