Last active
October 28, 2019 06:27
-
-
Save regakakobigman/abf29e5f7b208602366f3339f52f7003 to your computer and use it in GitHub Desktop.
Automatically check nodes in the "AxisLock" group to make sure they're axis-aligned
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
class_name Test | |
# Base class for automated test autoloads | |
enum { | |
MODE_IGNORE, | |
MODE_WARN, | |
MODE_ASSERT, | |
MODE_CRASH | |
} | |
var epsilon := 0.01 | |
var mode = MODE_WARN | |
var update_interval = 97 # Updates every X physics frames | |
var _update_interval = 0 # Update interval counter | |
var update_amount = 5 # Checks this many cases every update_interval. If -1, checks every case | |
func _physics_process(delta: float): | |
_update_interval += 1 | |
if _update_interval >= update_interval: | |
test(update_amount) | |
_update_interval = 0 | |
func test(amount): | |
assert false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Test | |
# Asserts that all nodes in the AxisLock group are correctly axis locked | |
func _init(): | |
randomize() | |
epsilon = 0.001 # Each tested node's global y origin must be within epsilon of 0 | |
mode = MODE_WARN | |
update_interval = 97 # Updates every X physics frames | |
_update_interval = 0 # Update interval counter | |
update_amount = 5 # Checks this many cases every update_interval. If -1, checks every case | |
func test(amount): | |
if mode == MODE_IGNORE: return | |
var nodes = get_tree().get_nodes_in_group("AxisLock") | |
if amount == -1 or nodes.size() <= amount: | |
for node in nodes: test_node(node) | |
return | |
var checked = [] | |
for i in amount: | |
# Test random nodes, but only once | |
if checked.size() >= nodes.size(): break | |
var j = randi() % nodes.size() | |
while j in checked: | |
j = randi() % nodes.size() | |
checked.append(j) | |
test_node(nodes[j]) | |
func test_node(node: Spatial): | |
assert node is Spatial # All AxisLock-grouped nodes must derive from Spatial to test their origin | |
var y = node.global_transform.origin.y | |
var passed: bool = -epsilon <= y and y <= epsilon | |
var error = "Node %s %s is not y-axis locked (Test: -%s <= %s <= %s)" % [node.name, str(node), epsilon, y, epsilon] | |
if not passed: | |
printerr(error) | |
match mode: | |
MODE_WARN: | |
pass | |
MODE_ASSERT: | |
assert passed | |
MODE_CRASH: | |
if not passed: | |
get_tree().quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment