├── .gitattributes ├── .gitignore ├── LightningGenerator.tscn ├── default_env.tres ├── icon.png ├── icon.png.import ├── lightning.gd └── project.godot /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Godot-specific ignores 3 | .import/ 4 | export.cfg 5 | export_presets.cfg 6 | 7 | # Mono-specific ignores 8 | .mono/ 9 | data_*/ 10 | -------------------------------------------------------------------------------- /LightningGenerator.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=2 format=2] 2 | 3 | [ext_resource path="res://lightning.gd" type="Script" id=1] 4 | 5 | [node name="LightningGenerator" type="Node2D"] 6 | script = ExtResource( 1 ) 7 | 8 | [node name="ColorRect" type="ColorRect" parent="."] 9 | margin_right = 1146.0 10 | margin_bottom = 717.0 11 | color = Color( 0.0431373, 0.0431373, 0.137255, 1 ) 12 | __meta__ = { 13 | "_edit_use_anchors_": false 14 | } 15 | 16 | [node name="Tween" type="Tween" parent="."] 17 | 18 | [node name="Timer" type="Timer" parent="."] 19 | autostart = true 20 | 21 | [node name="back" type="ColorRect" parent="."] 22 | margin_right = 1058.0 23 | margin_bottom = 639.0 24 | color = Color( 0.992157, 0.984314, 0.984314, 0 ) 25 | __meta__ = { 26 | "_edit_use_anchors_": false 27 | } 28 | -------------------------------------------------------------------------------- /default_env.tres: -------------------------------------------------------------------------------- 1 | [gd_resource type="Environment" load_steps=2 format=2] 2 | 3 | [sub_resource type="ProceduralSky" id=1] 4 | 5 | [resource] 6 | background_mode = 2 7 | background_sky = SubResource( 1 ) 8 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nefrace/astar_lightning/d14410c23e0794422608607dceaf72d42f9aff7a/icon.png -------------------------------------------------------------------------------- /icon.png.import: -------------------------------------------------------------------------------- 1 | [remap] 2 | 3 | importer="texture" 4 | type="StreamTexture" 5 | path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" 6 | metadata={ 7 | "vram_texture": false 8 | } 9 | 10 | [deps] 11 | 12 | source_file="res://icon.png" 13 | dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] 14 | 15 | [params] 16 | 17 | compress/mode=0 18 | compress/lossy_quality=0.7 19 | compress/hdr_mode=0 20 | compress/bptc_ldr=0 21 | compress/normal_map=0 22 | flags/repeat=0 23 | flags/filter=true 24 | flags/mipmaps=false 25 | flags/anisotropic=false 26 | flags/srgb=2 27 | process/fix_alpha_border=true 28 | process/premult_alpha=false 29 | process/HDR_as_SRGB=false 30 | process/invert_color=false 31 | stream=false 32 | size_limit=0 33 | detect_3d=true 34 | svg/scale=1.0 35 | -------------------------------------------------------------------------------- /lightning.gd: -------------------------------------------------------------------------------- 1 | extends Node2D 2 | 3 | export var points_w = 200 4 | export var points_h = 120 5 | export var points_gap = 5 6 | 7 | var astar : AStar2D = AStar2D.new() 8 | # Called when the node enters the scene tree for the first time. 9 | func _ready(): 10 | $Timer.connect("timeout", self, "on_timeout") # Just callback 11 | 12 | for i in range(points_w): # Fill it with points 13 | for j in range(points_h): 14 | astar.add_point(i+j*points_w, Vector2(i*points_gap, j*points_gap), rand_range(1, 1000)) 15 | 16 | for i in range(points_w): # Connect neighbors to each other 17 | for j in range(points_h): 18 | for ii in range(-1, 2): 19 | for jj in range(-1, 2): 20 | var di = i+ii 21 | var dj = j+jj 22 | var ida = i+j*points_w 23 | var idb = di+dj*points_w 24 | if !(ii == 0 and jj == 0): 25 | if di >= 0 and di < points_w and dj >= 0 and dj <= points_h: 26 | astar.connect_points(ida, idb, true) 27 | 28 | 29 | func on_timeout(): # When timer is out 30 | var a = Vector2(rand_range(0, points_w * points_gap), 0) # Random top point 31 | var b = Vector2(rand_range(0, points_w * points_gap), points_h * points_gap) # Random bottom point 32 | var l = lightning(a, b, 5, self) # Generate lightning 33 | 34 | $Tween.interpolate_property(l, "modulate", Color(1,1,1,1), Color(1,1,1,0), .6, Tween.TRANS_BOUNCE, Tween.EASE_IN) # Fade smooth 35 | $Tween.interpolate_property($back, "color", Color(1,1,1,0.7), Color(1,1,1,0), .3, Tween.TRANS_BACK, Tween.EASE_IN) 36 | $Tween.interpolate_callback(l, .6, "queue_free") 37 | $Tween.start() 38 | 39 | $Timer.wait_time = rand_range(.3, 3) 40 | $Timer.start() 41 | 42 | func lightning(start_point, end_point, width, parent): 43 | var start_id = astar.get_closest_point(start_point) # Getting indexes of points 44 | var end_id = astar.get_closest_point(end_point) 45 | var path = astar.get_point_path(start_id, end_id) # Calculate path 46 | var line : Line2D = Line2D.new() # Visual line class 47 | for i in range(path.size()-1, 1, -1): 48 | if rand_range(0, 10) < 5: 49 | path.remove(i) 50 | line.width = width 51 | line.default_color = Color.white 52 | line.points = path 53 | parent.add_child(line) 54 | if width > 1: # if line is thick, we can generate some branches 55 | var branches = randi()%3+1 56 | for i in range(branches): 57 | var point_id = randi()%(path.size()-1) # Getting random index in path array 58 | var begin = path[point_id] 59 | var dir = (path[point_id+1]-begin).normalized() # direction vector of lightning in 'begin' point 60 | var end = begin + ((dir*width*50)).rotated(rand_range(-PI/3, PI/3)) # some point in distance from 'begin' 61 | lightning(begin, end, width-1, line) # Recursively generate a new lightning and it as children to current. 62 | # On each new iteration new branches will be added as childs to the old ones 63 | return line 64 | -------------------------------------------------------------------------------- /project.godot: -------------------------------------------------------------------------------- 1 | ; Engine configuration file. 2 | ; It's best edited using the editor UI and not directly, 3 | ; since the parameters that go here are not all obvious. 4 | ; 5 | ; Format: 6 | ; [section] ; section goes between [] 7 | ; param=value ; assign values to parameters 8 | 9 | config_version=4 10 | 11 | _global_script_classes=[ ] 12 | _global_script_class_icons={ 13 | 14 | } 15 | 16 | [application] 17 | 18 | config/name="astar_lightning" 19 | config/icon="res://icon.png" 20 | 21 | [rendering] 22 | 23 | environment/default_environment="res://default_env.tres" 24 | --------------------------------------------------------------------------------