 # lonestar

0x10c-inspired space game based on the DCPU-16
git clone git://squid-tech.com/lonestar.git
Log | Files | Refs | README

```commit 6a0e79f46bdfa7cfd77adfc63996e1b600e495f2
parent ac2d80d7e08dbd86e58ee68468b7b136504e84d5
Author: Josh Moore <jxm5210@rit.edu>
Date:   Tue, 26 Nov 2019 18:08:52 -0500

Fix ship floor/ceiling mesh generation

Diffstat:
```
```1 file changed, 44 insertions(+), 24 deletions(-)
@@ -100,6 +100,41 @@ public class ShipLoader
return triangles;
}

+    // return true if check lies within the polygon defined by points
+    //\TODO FIX THIS - doesn't handle straight lines at all
+    bool containsPoint(List<Vector3> points, Vector3 p)
+    {
+        // return true if the point is a point on the polygon
+        if (points.Contains(p)) return true;
+
+        /*
+        // trace a ray to zero
+        // if the ray intersects the polygon an even number of times, the point is outside
+        // if the ray intersects the polygon an odd number of times, the point is inside
+        int intersections = 0;
+        for (float x = 0; x <= check.x; x += SHIP_SCALE)
+        {
+            if (points.Contains(new Vector3(x, 0, check.z)))
+            {
+                intersections++;
+            }
+        }
+
+        return !(intersections % 2 == 0);*/
+
+        int j = points.Count - 1;
+        bool inside = false;
+        for (int i = 0; i < points.Count; j = i++)
+        {
+            Vector3 pi = points[i];
+            Vector3 pj = points[j];
+            if (((pi.z <= p.z && p.z < pj.z) || (pj.z <= p.z && p.z < pi.z)) &&
+                (p.x < (pj.x - pi.x) * (p.z - pi.z) / (pj.z - pi.z) + pi.x))
+                inside = !inside;
+        }
+        return inside;
+    }
+
// Load ship XML from a file, assemble the ship, then place the player inside their ship
//\TODO server-side this, so the player sends their XML and the server handles it
public void loadShip(GameObject ships, PlayerController player)
@@ -172,40 +207,20 @@ public class ShipLoader
}

// generate a list of vertices corresponding to the floor/ceiling mesh
-        // based on the scan-line polyfill algorithm
+        //\TODO fill poly correctly
List<Vector3> generatedVerts = new List<Vector3>();
for (int y = 0; y < 24; y++)
+        {
for (int x = 0; x < 32; x++)
{
Vector3 pos = new Vector3(x * SHIP_SCALE, 0, y * SHIP_SCALE);
-                Vector3 next = pos + new Vector3(SHIP_SCALE, 0, 0);

-                {
-                    // check for transition ON
-                    if (hullPoints.Contains(next))
-                    {
-                        x++;
-                    }
-                }
-                else if (addingPoints && hullPoints.Contains(pos))
+                if (containsPoint(hullPoints, pos))
{
-                    // check for transition OFF
-                    if (!hullPoints.Contains(next))
-                    {
-                    }
-                }
-                {
-                    // ON state - just add the point
}
}
+        }

// copy generated mesh grid to Vector3[] array
// this is our mesh vertices
@@ -214,6 +229,11 @@ public class ShipLoader
{
gridPoints[i] = generatedVerts[i];
gridPoints[i + gridPoints.Length / 2] = generatedVerts[i];
+
+            /*GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
+            sphere.transform.position = gridPoints[i] - new Vector3(0, SHIP_SCALE / 2, 0);
+            sphere.transform.localScale = new Vector3(0.4f, 0.2f, 0.2f);
+            sphere.transform.parent = shipObject.transform;*/
}

// calculate mesh triangles
```