Skip to content

Instantly share code, notes, and snippets.

@regakakobigman
regakakobigman / raybounce.gd
Created December 28, 2019 19:57
Bouncing rays in GDScript for lasers or beams of light
# Try passing in randf() * x for distance_from_surface or angle_adjust to get some slight randomness
func bounce_rays(
space: Physics2DDirectSpaceState,
initial_pos: Vector2,
initial_dir: Vector2,
count: int,
length: float,
exclude:=[],
distance_from_surface: float = 0.0,
@regakakobigman
regakakobigman / Test.gd
Last active October 28, 2019 06:27
Automatically check nodes in the "AxisLock" group to make sure they're axis-aligned
extends Node
class_name Test
# Base class for automated test autoloads
enum {
MODE_IGNORE,
MODE_WARN,
MODE_ASSERT,
MODE_CRASH
}
@regakakobigman
regakakobigman / fake_beta_distribution.gd
Last active October 26, 2019 04:14
A (totally fake) Beta distribution sampler for Godot
func randfb(curve: Curve) -> float:
# Returns a float mapped onto a symmetric distribution using a curve
# Also known as: a poor man's Beta distribution ;)
return symmetric_interpolate(randf(), curve)
func symmetric_interpolate(f: float, curve: Curve) -> float:
# Returns a float mapped onto a curve as if the curve were symmetric
return 0.5 + curve.interpolate(f) / (2 if randi()%2==0 else -2)
@regakakobigman
regakakobigman / MeshInstanceUV.gd
Created October 12, 2019 21:53
MeshInstance that animates its UV offset for a trippy visual effect
tool
extends MeshInstance
class_name MeshInstanceUV
var _time := Vector2()
export var speed := Vector2(1.0, 1.0)
export var sinusoid_factor := Vector2(1.0, 1.0)
export var linear_factor := Vector2(1.0, 1.0)
@regakakobigman
regakakobigman / moving.gd
Last active May 9, 2020 03:23
Only take the most recent axis input
var moving := Vector2()
func _physics_process(delta: float) -> void:
# Input singleton should only be used from _physics_process
update_moving()
func update_moving() -> void:
# This makes moving a little more intuitive with the arrow keys
@regakakobigman
regakakobigman / get_node_property.gd
Last active March 29, 2024 01:31
Gets the property of a node using a NodePath
# This is an awful solution, but NodePath is missing an important method, so there's no great alternative that I've found.
# Try me with get_node_property(self, "Control/Spatial/CollisionShape2D:shape:extents:x")
func get_node_property(from: Node, path: NodePath):
assert ":" in path as String # Causes a hard crash
path = path as NodePath
var node_path = get_as_node_path(path)
var property_path = (path.get_concatenated_subnames() as NodePath).get_as_property_path()
return from.get_node(node_path).get_indexed(property_path)
@regakakobigman
regakakobigman / pos_margin_convert.gd
Last active August 3, 2019 00:00
Convert between position + rect extents and margins
# This helps a lot when trying to use Controls to represent a translated CollisionShape2D for a tool script
# I don't know if it's useful for anything else
static func pos_to_margin(position: Vector2, extents: Vector2) -> Dictionary:
var margin_left := position.x - extents.x
var margin_right := position.x + extents.x
var margin_top := position.y - extents.y
var margin_bottom := position.y + extents.y
return {
"margin_left": margin_left,
@regakakobigman
regakakobigman / numeronym_generator.py
Last active July 28, 2019 22:54
Generate obnoxious numeronyms (like i18n for "Internationalization")
from typing import Pattern, Match
import re
# This pattern matches words separated by whitespace or hyphens.
_pattern: Pattern = r"([A-Za-z0-9']+)"
_regex: Match = re.compile(_pattern)
def obnoxiousify(text: str) -> str:
@regakakobigman
regakakobigman / angle_lerping.gd
Last active July 16, 2019 10:35
Tools for dealing with angles in GDScript
# Useful functions for calculating distances between angles
static func lerp_angle(from: float, to: float, weight: float) -> float:
return from + short_angle_dist(from, to) * weight
static func short_angle_dist(from: float, to: float) -> float:
var difference = fmod(to - from, PI * 2)
return fmod(2 * difference, PI * 2) - difference
# A chunk of code I came up with for more snappy angle lerping
@regakakobigman
regakakobigman / line_splitter.gd
Last active May 30, 2019 17:21
Line splitter
# originally by GogetaX (Discord: @274253761399095297)
func LineSpliter(txt,MaxTextLength):
var Finish = false
var a = -1
var Lines = []
var t = ""
var LastBreak = -1
while !Finish:
a += 1