Advertisement
iRadEntertainment

wall_side.gd

Jun 19th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. # Original Author Miziziziz
  2. # https://github.com/Miziziziz/2D-directional-shadows-godot
  3. #
  4. # modified by iRad (Yellatch Games)
  5. # draw a fake wall on one edge of the polygon
  6.  
  7. extends Node2D
  8.  
  9. export var wall_height = 10
  10. export var wall_bot_color: Color = Color(0.25,0.25,0.25)
  11. export var wall_top_color: Color = Color(0.5,0.5,0.5)
  12.  
  13. onready var vertices = get_parent().polygon
  14. onready var num_of_vertices = vertices.size()
  15.  
  16. var cam = null
  17.  
  18.  
  19. func _ready():
  20.     if not cam:
  21.         set_process(false)
  22.  
  23. func _process(delta):
  24.     if not cam:
  25.         return
  26.     update()
  27.  
  28. func _draw():
  29.     if not cam:
  30.         return
  31.    
  32.     var normals = []
  33.     for vertice_ind in range(num_of_vertices):
  34.         var vertice = vertices[vertice_ind]
  35.         var next_vertice = vertices[(vertice_ind + 1) % num_of_vertices]
  36.         var normal = (next_vertice - vertice).normalized().rotated(PI / 2.0)
  37.         normals.append(normal)
  38.    
  39.     var top_vertices = []
  40.     for vertice_ind in range(num_of_vertices):
  41.         var vertice = vertices[vertice_ind]
  42.         var next_vertice = vertices[(vertice_ind + 1) % num_of_vertices]
  43.         var normal = normals[vertice_ind]
  44.         var vertice_proj = vertice - (cam.global_position - (global_position+vertice) )*wall_height/100
  45.         if (cam.global_position - (global_position+vertice) ).dot(normal) > 0:
  46.             var next_vertice_proj = next_vertice - (cam.global_position - (global_position+next_vertice) )*wall_height/100
  47.             draw_polygon([vertice, vertice_proj, next_vertice_proj, next_vertice],
  48.                         [wall_bot_color, wall_top_color, wall_top_color, wall_bot_color],
  49.                         [Vector2(0,0),Vector2(0,1),Vector2(1,1),Vector2(1,0)],
  50.                         get_parent().texture,null,true)
  51.         top_vertices.append(vertice_proj)
  52.        
  53.        
  54.     draw_polygon(top_vertices,get_parent().vertex_colors,get_parent().uv,get_parent().texture,null,true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement