Skip to content

Instantly share code, notes, and snippets.

View andrew-wilkes's full-sized avatar

Andrew Wilkes andrew-wilkes

View GitHub Profile
@andrew-wilkes
andrew-wilkes / count-bits-in-number.gd
Created August 9, 2021 11:22
Count number of bits in a number
extends Node2D
"""
See: https://www.geeksforgeeks.org/count-total-bits-number/
Given a positive number n, count total bit in it.
Input : 183
Output : 8
@andrew-wilkes
andrew-wilkes / clouds.gdshader
Last active October 8, 2021 06:09
Godot Clouds Shader
// https://www.shadertoy.com/view/4tdSWr
shader_type canvas_item;
uniform float cloudscale = 1.1;
uniform float speed = 0.03;
uniform float clouddark = 0.5;
uniform float cloudlight = 0.3;
uniform float cloudcover = 0.2;
uniform float cloudalpha = 8.0;
uniform float skytint = 0.5;
@andrew-wilkes
andrew-wilkes / color.gd
Created December 24, 2021 05:01
Color code
# Switch between saturated and pastel palettes
var s = 0.5 if n > 15 else 1.0
button.modulate = Color.from_hsv(fmod(n / 15.0, 2.0), s, 1.0)
var target = get_node("new_parent_node")
var source = get_node("child")
self.remove_child(source)
target.add_child(source)
source.set_owner(target) # I think that this line of code is only needed in Editor scripts
extends SceneTree
var t = []
func _init():
for j in 5:
var k = []
var p_time = OS.get_ticks_msec()
@andrew-wilkes
andrew-wilkes / Fibonacci.gd
Last active February 23, 2022 08:54
Optimized Fibonacci number calculator using GDScript
extends Control
# Fibonacci number calculation using recursion
# This has some optimization where prior numbers are stored in an array
# Also, the array is expanded in size as needed
func _ready():
# Calculate the nth number and pass in the array to be referenced
print(fibo(34, [1, 1]))
@andrew-wilkes
andrew-wilkes / collatz.gd
Created March 12, 2022 09:53
Collatz conjecture 3n + 1 code
extends Node2D
# Has a Line2D child node to plot a curve using each number of the series that tends to 1
const XSTEP = 10
var x = 0
func _ready():
calc(211)
@andrew-wilkes
andrew-wilkes / audio.gd
Created September 2, 2022 04:43
Audio File Parser
class_name Audio
var player: AudioStreamPlayer
var info
func _init(audio_player_instance):
player = audio_player_instance
func load_data(path: String):
@andrew-wilkes
andrew-wilkes / rgb-to-hsv.gd
Last active November 22, 2022 11:25
GDScript function to convert RGB color to HSV values
# I/O values are in the range 0.0 .. 1.0
func rgb_to_hsv(c: Color):
var M: float = rgb.max()
var m: float = rgb.min()
var v = M
var s = 0.0 if M < 0.001 else 1.0 - m / M
var h = (c.r - c.g / 2.0 - c.b / 2.0) / sqrt(c.r*c.r + c.g*c.g + c.b*c.b - c.r*c.g - c.r*c.b - c.g*c.b)
return { h=h, s=s, v=v }
@andrew-wilkes
andrew-wilkes / spiral.shader
Last active December 9, 2022 02:04
godot spiral shader
shader_type canvas_item;
const float PI = 3.14159265358979323846;
const float w = 0.1; // Width of line
void fragment() {
float dis = 0.1; // Displacement (spacing) of lines
// Get an angle value around a circle of 0.0 .. 1.0
vec2 pt = (UV - vec2(0.5)) * 2.0; // +/-1.0