aboutsummaryrefslogtreecommitdiffstats
path: root/Scripts/Road.gd
diff options
context:
space:
mode:
authorLibravatar IcECreAm777 <hgk.1998@googlemail.com>2023-07-09 17:31:37 +0200
committerLibravatar IcECreAm777 <hgk.1998@googlemail.com>2023-07-09 17:31:37 +0200
commit6b6d02ead8b1f5b0f5cf44d0a88abe50769128ca (patch)
treef84fd031e519515d6e72a917e8316a54f6572e31 /Scripts/Road.gd
parent338c1b1c778af4a64aac3a3ae2cd5c06304a65cc (diff)
parentfd37d1030f79705fc7a2498bcd356ebc86462544 (diff)
download2023-6b6d02ead8b1f5b0f5cf44d0a88abe50769128ca.tar.gz
2023-6b6d02ead8b1f5b0f5cf44d0a88abe50769128ca.tar.bz2
2023-6b6d02ead8b1f5b0f5cf44d0a88abe50769128ca.zip
Merge branch 'master' of bunny.garden:jam/gmtk/2023
the commit.
Diffstat (limited to 'Scripts/Road.gd')
-rw-r--r--Scripts/Road.gd82
1 files changed, 82 insertions, 0 deletions
diff --git a/Scripts/Road.gd b/Scripts/Road.gd
new file mode 100644
index 0000000..4b19c70
--- /dev/null
+++ b/Scripts/Road.gd
@@ -0,0 +1,82 @@
+extends Line2D
+
+var epsilon = 2
+
+@onready var leftBorder = $LeftBorder
+@onready var rightBorder = $RightBorder
+
+# Called when the node enters the scene tree for the first time.
+func _ready():
+ pass # Replace with function body.
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+func _process(delta):
+ pass
+
+func simplify():
+ points = simplify_rec(points, epsilon)
+
+func simplify_rec(pointArray: PackedVector2Array, epsilon: float):
+ var dmax = 0
+ var index = -1
+ if pointArray.size() < 3:
+ return pointArray
+ var end = pointArray.size() - 1
+ for i in range(1, end-1):
+ var d = find_perpendicular_distance(pointArray[i], pointArray[0], pointArray[end])
+ if d > dmax:
+ index = i
+ dmax = d
+
+ if dmax > epsilon:
+ var recResults1 = simplify_rec(pointArray.slice(0, index), epsilon)
+ var recResults2 = simplify_rec(pointArray.slice(index, end), epsilon)
+ recResults1.append_array(recResults2)
+ return recResults1
+ else:
+ return [pointArray[0], pointArray[end]]
+
+func find_perpendicular_distance(p: Vector2, p1: Vector2, p2: Vector2):
+ var result
+ var slope
+ var intercept
+ if p1.x == p2.x:
+ result = p.x - p1.x
+ else:
+ slope = (p2.y - p1.y) / (p2.x - p1.x)
+ intercept = p1.y - (slope * p1.x)
+ result = abs(slope * p.x - p.y + intercept) / sqrt(pow(slope, 2) + 1)
+
+ return result
+
+func get_bisector(left_side: bool, p2: Vector2, p1: Vector2 = p2, p3: Vector2 = p2):
+ var v1 = (p2 - p1).normalized()
+ var v2 = (p2 - p3).normalized()
+ var normal
+ if v1 == -v2:
+ normal = v2.rotated(-PI/2)
+ else:
+ normal = (v1 + v2).normalized()
+ if v2.rotated(-PI/2).dot(v1) < 0:
+ normal = -normal
+
+ if left_side:
+ normal = -normal
+ return normal
+
+
+func draw_borders():
+ for pointIndex in range(0, points.size() - 1):
+ var normal
+ if pointIndex == 0:
+ continue
+ elif pointIndex == points.size() -1:
+ continue
+ #normal = Vector2.ZERO
+ else:
+ normal = get_bisector(true, points[pointIndex], points[pointIndex-1], points[pointIndex+1])
+
+ var point = points[pointIndex] + (normal*50)
+ leftBorder.add_point(point)
+ point = points[pointIndex] - (normal*50)
+ rightBorder.add_point(point)