Water2D.gd 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. tool
  2. extends Sprite
  3. var drop_index: int = 0
  4. var area2d: Area2D
  5. var collision_shape: CollisionShape2D
  6. var owner_id: int
  7. var area_rid: RID
  8. enum SURFACE_TYPES {
  9. PLAIN,
  10. GRADIENT
  11. }
  12. export(bool) var automatic_impact = true
  13. export(SURFACE_TYPES) var surface_type = SURFACE_TYPES.GRADIENT setget set_surface_type
  14. export(Vector2) var deformation_speed_1 = Vector2(1.0, 1.0) setget set_deformation_speed_1
  15. export(Vector2) var deformation_speed_2 = Vector2(-2.0, -2.0) setget set_deformation_speed_2
  16. export(Vector2) var deformation_strength = Vector2(1.0, 1.0) setget set_deformation_strength
  17. export(Vector2) var deformation_size = Vector2(1.0, 1.0) setget set_deformation_size
  18. export(float, 0.0, 10000.0) var water_level = 20.0 setget set_water_level
  19. export(Color) var water_color = Color(0.0, 0.0,1.0, 0.75) setget set_water_color
  20. export(float, 0.0, 10000.0) var surface_width = 2.0 setget set_surface_width
  21. export(Color) var surface_color = Color(1.0, 1.0,1.0, 0.75) setget set_surface_color
  22. export(float, 0.0, 10000.0) var surface_deformation_strength = 1.0 setget set_surface_deformation_strength
  23. export(float, 0.0, 10000.0) var wave_speed = 1.0 setget set_wave_speed
  24. export(float, 0.0, 10000.0) var wave_distance_attenuation = 1.0 setget set_wave_distance_attenuation
  25. export(float, 0.0, 10000.0) var wave_time_attenuation = 1.0 setget set_wave_time_attenuation
  26. signal water_entered(water, impact_pos, body_id, body, body_shape, area_shape)
  27. signal water_exited(water, impact_pos, body_id, body, body_shape, area_shape)
  28. signal water_splashed(water, impact_pos, body_id, body, body_shape, area_shape)
  29. # Called when the node enters the scene tree for the first time.
  30. func _enter_tree():
  31. texture = preload("icon.png")
  32. material = preload("water2D_material.tres")
  33. add_shape()
  34. func add_shape():
  35. if not Engine.editor_hint:
  36. area2d = Area2D.new()
  37. collision_shape = CollisionShape2D.new()
  38. adjust_water_area()
  39. area2d.add_child(collision_shape)
  40. area2d.connect("area_shape_entered", self, "_on_Area2D_body_shape_entered")
  41. area2d.connect("body_shape_entered", self, "_on_Area2D_body_shape_entered")
  42. area2d.connect("area_shape_exited", self, "_on_Area2D_body_shape_exited")
  43. area2d.connect("body_shape_exited", self, "_on_Area2D_body_shape_exited")
  44. add_child(area2d)
  45. func adjust_water_area():
  46. if not Engine.editor_hint:
  47. var new_rect = RectangleShape2D.new()
  48. var sprite_size = get_rect().size
  49. var new_rect_extents = sprite_size / 2
  50. var half_water_level = material.get_shader_param('water_level')/ 2 / scale.y
  51. new_rect_extents.y -= half_water_level
  52. if new_rect_extents.y < 0.0 : new_rect_extents.y = 0
  53. new_rect.extents = new_rect_extents
  54. collision_shape.set_shape(new_rect)
  55. area2d.position.y = half_water_level
  56. func _ready() :
  57. material.set_shader_param("scale", scale)
  58. func compute_impact_pos(body):
  59. var size = get_rect().size * scale
  60. var sprite_top_left = global_position - size / 2;
  61. var body_pos_relative_top_left = body.global_position - sprite_top_left
  62. var surface_y = material.get_shader_param("water_level")
  63. return Vector2(body_pos_relative_top_left.x, surface_y)
  64. func _on_Area2D_body_shape_entered(body_id, body, body_shape, area_shape):
  65. var impact_pos = compute_impact_pos(body)
  66. if impact_pos.y <= body.global_position.y :
  67. if automatic_impact :
  68. add_impact(impact_pos, 10.0, 5.0)
  69. emit_signal("water_splashed", self, impact_pos, body_id, body, body_shape, area_shape)
  70. emit_signal("water_entered", self, impact_pos, body_id, body, body_shape, area_shape)
  71. func _on_Area2D_body_shape_exited(body_id, body, body_shape, area_shape):
  72. var impact_pos = compute_impact_pos(body)
  73. emit_signal("water_exited", self, impact_pos, body_id, body, body_shape, area_shape)
  74. func add_impact(pos, length, height):
  75. material.set_shader_param("impact_height_" + str(drop_index), height)
  76. material.set_shader_param("impact_length_" + str(drop_index), length)
  77. material.set_shader_param("impact_pos_" + str(drop_index), pos)
  78. material.set_shader_param("impact_time_" + str(drop_index), OS.get_ticks_msec() / 1000.0)
  79. drop_index = (drop_index + 1) % 8
  80. func _process(delta):
  81. var current_time = OS.get_ticks_msec() / 1000.0
  82. material.set_shader_param("current_time", current_time)
  83. if Engine.editor_hint:
  84. if ! area2d:
  85. add_shape()
  86. material.set_shader_param("scale", scale)
  87. adjust_water_area()
  88. func _on_Water2D_item_rect_changed():
  89. adjust_water_area()
  90. material.set_shader_param("scale", scale)
  91. func set_surface_type(_value):
  92. surface_type = _value
  93. material.set_shader_param("surface_type", _value)
  94. func set_deformation_speed_1( _value ):
  95. deformation_speed_1 = _value
  96. material.set_shader_param("deformation_speed_1", _value)
  97. func set_deformation_speed_2( _value ):
  98. deformation_speed_2 = _value
  99. material.set_shader_param("deformation_speed_2", _value)
  100. func set_deformation_strength( _value ):
  101. deformation_strength = _value
  102. material.set_shader_param("deformation_strength", _value)
  103. func set_deformation_size( _value ):
  104. deformation_size = _value
  105. material.set_shader_param("tile_factor", Vector2(1.0 / _value.x, 1.0 / _value.y))
  106. func set_water_level( _value ):
  107. water_level = _value
  108. material.set_shader_param("water_level", _value)
  109. func set_water_color( _value ):
  110. water_color = _value
  111. material.set_shader_param("water_color", _value)
  112. func set_surface_width( _value ):
  113. surface_width = _value
  114. material.set_shader_param("surface_width", _value)
  115. func set_surface_color( _value ):
  116. surface_color = _value
  117. material.set_shader_param("surface_color", _value)
  118. func set_surface_deformation_strength( _value ):
  119. surface_deformation_strength = _value
  120. material.set_shader_param("surface_deformation_strength", _value)
  121. func set_wave_speed( _value ):
  122. wave_speed = _value
  123. material.set_shader_param("wave_speed", _value)
  124. func set_wave_distance_attenuation( _value ):
  125. wave_distance_attenuation = _value
  126. material.set_shader_param("wave_distance_attenuation", _value)
  127. func set_wave_time_attenuation( _value ):
  128. wave_time_attenuation = _value
  129. material.set_shader_param("wave_time_attenuation", _value)