SHARE
TWEET

Untitled

a guest Feb 17th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2. class_name Trail
  3.  
  4. export var length = 10
  5. export var strength = 2.0
  6. export var positions_update_delta = 0.5
  7.  
  8. var positions = []
  9. var update_timer = 0.0
  10. var trail_start_index = 0
  11. var draw_trail = false
  12.  
  13. func _ready():
  14.     set_physics_process(true)
  15.     init_position()
  16.  
  17. func reset():
  18.     positions = []
  19.     update_timer = 0.0
  20.     trail_start_index = 0
  21.     draw_trail = false
  22.     init_position()
  23.  
  24. func init_position():
  25.     for x in range(0, length+1):
  26.         positions.append(global_position)
  27.  
  28. func _physics_process(delta):
  29.     update_timer -= delta
  30.     if (update_timer <= 0):
  31.         update_timer = positions_update_delta * delta
  32.         add_position()
  33.         update()
  34.  
  35. func add_position():
  36.     move_all_positions_array()
  37.     positions[length] = global_position
  38.     if (draw_trail):
  39.         trail_start_index = max(trail_start_index - 1, 0)
  40.     else:
  41.         trail_start_index = clamp(trail_start_index+1, 0, length)
  42.  
  43. func move_all_positions_array():
  44.     for i in range(0, length):
  45.         positions[i] = positions[i+1]
  46.  
  47. func _draw():
  48.     draw_set_transform(-global_position, 0, Vector2(1,1))
  49.     for x in range(trail_start_index, length):
  50.         if ((positions[x] - positions[x+1]).length() != 0):
  51.             draw_circle(positions[x], strength * (x-trail_start_index), ColorTheme.Colors[4])
  52.             draw_line(positions[x], positions[x+1], ColorTheme.Colors[4], strength * (x-trail_start_index) * 2.0)
  53.  
  54. func set_draw_trail(x):
  55.     draw_trail = x
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top