87 lines
2.3 KiB
GDScript3
87 lines
2.3 KiB
GDScript3
# Skip's attempts to recreate common GML commands in Godot
|
|
class_name GML
|
|
|
|
# [ SIGNAL CHEATSHEET ]
|
|
#
|
|
# Declare and emit (with args) signal from source.
|
|
# <Projectile.gd>
|
|
# signal hit
|
|
# func _on_Hero_body_entered:
|
|
# emit_signal("hit", "fck that hurt", 1)
|
|
#
|
|
# Connect and handle (with args) signal from destination.
|
|
# <Hero.gd>
|
|
# func _ready:
|
|
# var proj = get_tree().get_root().find_node("Projectile",true,false)
|
|
# proj.connect("hit", self, "handle_hit")
|
|
# func handle_hit(message, damage):
|
|
# print(message)
|
|
# health -= damage
|
|
|
|
|
|
static func choose(choices):
|
|
# Picks a random item from a given array, "choices", of any size.
|
|
# Ex: cardinal = choose(["north", "south", "east", "west"])
|
|
|
|
return choices[randi() % choices.size()]
|
|
|
|
|
|
|
|
|
|
static func instance_create(x, y, scene_path, parent) -> Node:
|
|
# Creates an instance of a given node at the given coordinates
|
|
# as a child of a given parent.
|
|
# Ex: instance_create(30, 100, "res://Actors/EnemyShip.tscn", self)
|
|
var scene = load(scene_path)
|
|
var id = scene.instance()
|
|
id.position = Vector2(x, y)
|
|
parent.add_child(id)
|
|
|
|
|
|
return id
|
|
|
|
|
|
|
|
|
|
static func image_angle(me, degrees):
|
|
me.rotation_degrees = degrees
|
|
|
|
|
|
|
|
|
|
static func point_direction(x1, y1, x2, y2) -> float:
|
|
# Returns the angle (in degrees) specified by two given points.
|
|
# Ex: rotation_degrees = point_direction($Hero.position.x, $Hero.position.y, $Enemy.position.x, $Enemy.position.y)
|
|
|
|
return rad2deg(Vector2(x2, y2).angle_to_point(Vector2(x1, y1)))
|
|
|
|
|
|
|
|
|
|
static func move_xy(distance, angle_in_degrees) -> Vector2:
|
|
# Returns new (relative) coordinates depicting a point having moved a given distance (in pixels) in a given direction (angle in degrees).
|
|
# Ex: Enemy.position += move_xy(speed, Hero.position.angle_to_point(Enemy.position))
|
|
return Vector2(cos(deg2rad(angle_in_degrees)),sin(deg2rad(angle_in_degrees))) * distance
|
|
|
|
|
|
|
|
|
|
static func distance_to_object(node1, node2) -> float:
|
|
# Returns the distance between node1 and node2 (in pixels)
|
|
# Ex: if distance_to_object(self, $Alarm) < 50:
|
|
return node1.position.distance_to(node2.position)
|
|
|
|
|
|
static func game_end(me):
|
|
# Quit the game.
|
|
# Ex: game_end(self)
|
|
|
|
me.get_tree().quit()
|
|
|
|
static func room_restart(me):
|
|
# Restart the scene.
|
|
# Ex: room_restart(self)
|
|
|
|
me.get_tree().reload_current_scene()
|
|
|