ld55/src2/Pentagram/pentagram.gd

66 lines
1.6 KiB
GDScript
Raw Normal View History

2024-04-14 15:43:40 +00:00
extends Node2D
class_name Pentagram
var A: Vector2
var B: Vector2
var C: Vector2
var D: Vector2
var E: Vector2
var center: Vector2
var radius: int
var points: Array
var indexes: Array
var snaped = false
var snap_addr = 0
func _ready():
self.refresh()
get_viewport().size_changed.connect(self.refresh)
self.indexes = [
0, 1, 2, 3, 4
]
func refresh():
var view = get_viewport()
self.radius = 256
self.center = view.size/2
var x = -PI/5
var y = -PI/16
self.A = self.center + self.get_point(-PI/2 - x) * self.radius
self.B = self.center + self.get_point(0 - y) * self.radius
self.C = self.center + self.get_point(PI/2) * self.radius
self.D = self.center + self.get_point(PI + y) * self.radius
self.E = self.center + self.get_point(-PI/2 + x) * self.radius
self.points = [self.A, self.B, self.C, self.D, self.E]
func _draw():
# draw_circle(self.center, self.radius, Color.BLACK)
var w = 8
draw_line(self.A, self.C, Color.DARK_RED, w)
draw_line(self.C, self.E, Color.DARK_RED, w)
draw_line(self.E, self.B, Color.DARK_RED, w)
draw_line(self.B, self.D, Color.DARK_RED, w)
draw_line(self.D, self.A, Color.DARK_RED, w)
func get_point(angle):
return Vector2(cos(angle), sin(angle))
func nearest(rune):
var point = null
var dist = null
for pts in self.points:
var d = rune.distance_squared_to(pts)
if dist == null or dist < d:
dist = d
point = pts
return point
func snap(pos: Vector2, dist):
self.snaped = false
for pts in self.points:
if pos.distance_squared_to(pts) < dist * dist:
self.snaped = true
self.snap_addr = self.points.find(pts)
return pts
return pos