Created
January 30, 2018 23:00
-
-
Save iRadEntertainment/b649b893717de137d932fb77c8331a86 to your computer and use it in GitHub Desktop.
Godot Engine (GDscript) - 2D adjustable trail by Yellatch Games
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
#Draw trails with lines | |
#A NON-PERFECT SCRIPT FOR Godot v2.1.* *NOTE THIS SCRIPT STILL HAVE SOME REF ISSUES, CHECK FOR ERRORS IN THE DEBUGGER | |
#This script has to be attached to scene with only a Control Node | |
#This scene can be instanced as child of a moving node | |
#after that the code can be initialized calling scia_setup() func: | |
#arguments ( nuovo_oggetto = "the parent node reference goes here" | |
num_step = "number of steps (50 by default) | |
nuovo_colore = Color() (Opaque white by default) | |
nuova_trasp = Initial alpha transparency (adjust the formula for other fading effects) | |
nuova_larghezza = Initial width in px (15 px by default) ) | |
extends Control | |
var pos = Vector2() | |
var step = 0 | |
var lista_posizioni = [] | |
var oggetto_da_tracciare = null | |
var colore = Color() | |
var trasparenza = 1 | |
var larghezza = 5 | |
#flag to check if the parent object is queued | |
var oggetto_rif_esiste = false | |
func _draw(): | |
var inv = get_global_transform().inverse() #draw on global coordinates (not relative to the instanced scene) | |
draw_set_transform(inv.get_origin(), inv.get_rotation(), inv.get_scale()) #""as comment above"" | |
if step != 0: | |
for i in range(1,step): | |
var rapporto = float(i-1)/step | |
var trasp_step = trasparenza-(trasparenza*rapporto) #Alpha blending/fading formula | |
colore.a = trasp_step | |
draw_line(lista_posizioni[i], lista_posizioni[i-1], colore, larghezza*(1-rapporto)) | |
func scia_setup(nuovo_oggetto, num_step=50, nuovo_colore=Color(1,1,1), nuova_trasp = 1, nuova_larghezza = 15): | |
oggetto_da_tracciare = weakref(nuovo_oggetto).get_ref() | |
pos = nuovo_oggetto.get_global_pos() | |
step = num_step | |
colore = nuovo_colore | |
trasparenza = nuova_trasp | |
larghezza = nuova_larghezza | |
lista_posizioni = [] | |
#set the flag, the parent node exists | |
nuovo_oggetto.connect("cancellato",self,"_nuovo_oggetto_cancellato") | |
oggetto_rif_esiste = true | |
#Inizialize the array with all the steps (will be all equal) | |
for i in range(0,step): | |
lista_posizioni.append(pos) | |
if step != 0: | |
set_fixed_process(true) | |
func _nuovo_oggetto_cancellato(): | |
oggetto_rif_esiste = false | |
#_process will take care of adding a new pos to the "lista_posizioni" array | |
func _fixed_process(delta): | |
if oggetto_rif_esiste: | |
lista_posizioni.push_front(oggetto_da_tracciare.get_global_pos()) | |
lista_posizioni.pop_back() | |
update() | |
#when the parent node will be queued the trail will start fading (reducing the pos in the array) | |
elif step > 0: | |
step -= 1 | |
lista_posizioni.pop_back() | |
update() | |
#when all the steps will be popped out, it will queue_free | |
else: | |
queue_free() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment