aka gravity ninja golf
1class_name Mouse
2extends Node2D
3
4var mycolor: clr.colors = clr.colors.UNSET
5var is_ready: bool = false
6
7var mousepoly: PackedVector2Array
8var mousecolors: PackedColorArray
9var basescale = 1.0
10var targetscale = Vector2.ONE
11var targetposition = Vector2.ZERO
12
13# basescale is to shrink the mouse when we left click, we use scale for both the stretching and clicking
14# so that's why we have to use this kinda ugly approach. i just implicitly animate the clicking by modifying
15# basescale whenever i am clicking
16func _process(delta: float):
17 var diff = targetposition - position
18 # if i leave the mouse be for a while, the diff will reset to 0, which means that all the
19 # rotation resets to 0. you can see the sketch cursor spin if you look carefully!
20 rotation = diff.angle()
21 targetscale = Vector2(1.0 + diff.length()/100, max(1.0 - diff.length()/400,.17))
22 position = lerp(position, targetposition, delta * 17.0)
23 if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
24 basescale = lerp(basescale, .8, delta*20)
25 else:
26 basescale = lerp(basescale, 1.0, delta*20)
27 scale = targetscale * basescale
28
29# i use the target to lag behind the mouse's actual current position (to) (it will always lag behind
30# mouse because the mouse is rendered by the OS, so i prefer to just make it feel more intentional)
31# since we have the mouse's actual position, we calculate how far the prior position of the mouse
32# is from the new target, so that we can scale it more if it changes more
33func set_target(to: Vector2):
34 targetposition = to
35
36func setmycolor(to: clr.colors):
37 mycolor = to
38 mousecolors.fill(clr.color_enum_to_color(to))
39 queue_redraw()
40
41# i call sketch from the game's process
42func sketch():
43 mousepoly = genpolygon(11, 14.0, 1.0)
44 # redundant, but i do this because i'm not sure how the spawner works, and if
45 # i don't use the mycolor setter there may be some desync here
46 mousecolors.fill(clr.color_enum_to_color(mycolor))
47 queue_redraw()
48
49
50func _init():
51 mousepoly = genpolygon(11, 14.0, 1.0)
52 mousecolors = PackedColorArray()
53 mousecolors.resize(11)
54 mousecolors.fill(clr.white)
55
56func _draw():
57 draw_polygon(mousepoly, mousecolors)
58
59## genpolygon generates a (nonconvex polygon) with [param n] vertices, and the given [param radius]
60## and [param deviation]
61func genpolygon(n: int, radius: float, deviation: float) -> PackedVector2Array:
62 var rads = []
63 rads.resize(n)
64 var rots = []
65 rots.resize(n)
66 var rsum = 0
67 for i in range(n):
68 rads[i] = randfn(radius, deviation)
69 rots[i] = rsum + randf()
70 rsum = rots[i]
71 var poly = PackedVector2Array()
72 poly.resize(n)
73 var randrot = randf() * TAU
74 for i in range(n):
75 poly[i] = Vector2(rads[i], 0).rotated(rots[i] / rsum * TAU + randrot)
76 return poly