109 lines
2.6 KiB
GDScript3
Raw Normal View History

2019-11-21 21:45:48 +01:00
extends Spatial
2019-11-21 23:52:54 +01:00
# export variables
export(NodePath) var mesh_path
export(NodePath) var color_cast_left
export(NodePath) var color_cast_up
2019-11-21 23:52:54 +01:00
export(bool) var can_turn
export(bool) var is_turned
2019-11-21 23:52:54 +01:00
export(int) var x_rot
export(int) var y_rot
export(int) var z_rot
export(Color) var content_color
2019-12-14 21:05:09 +01:00
onready var outline = get_node("Mesh/Outline") as MeshInstance
2020-01-02 01:59:35 +01:00
onready var fork_material = preload("res://Materials/Pipe_Dream.tres")
2019-12-14 21:05:09 +01:00
# signals
signal flow_changed
# constant variables
const NULL_COLOR = Color(0, 0, 0, 1)
# private variables
var _left_cast : RayCast
var _up_cast : RayCast
var _left_color : Color
var _up_color : Color
var _mesh : MeshInstance
2019-11-21 21:45:48 +01:00
2019-12-14 21:05:09 +01:00
2019-11-21 21:45:48 +01:00
func _ready():
_left_cast = get_node(color_cast_left) as RayCast
_up_cast = get_node(color_cast_up) as RayCast
_mesh = get_node(mesh_path) as MeshInstance
2019-11-21 21:45:48 +01:00
2019-12-14 21:05:09 +01:00
2019-11-21 23:52:54 +01:00
func do_interact(var player):
if(is_turned):
2019-11-21 23:52:54 +01:00
rotate_x(x_rot * PI/180)
rotate_y(y_rot * PI/180)
rotate_z(z_rot * PI/180)
2019-12-28 18:36:48 +01:00
is_turned = false
2019-11-21 23:52:54 +01:00
else:
rotate_x(-x_rot * PI/180)
rotate_y(-y_rot * PI/180)
rotate_z(-z_rot * PI/180)
2019-12-28 18:36:48 +01:00
is_turned = true
emit_signal("flow_changed")
2020-01-02 01:59:35 +01:00
func _get_color_from_cast(ray_cast : RayCast):
if ray_cast.is_colliding():
var collider = ray_cast.get_collider()
if collider.is_in_group("Pipes"):
var new_color = collider.content_color
if new_color != null:
return new_color
2020-01-02 01:59:35 +01:00
func _blend_colors(var col1, var col2, var _all_colors: Array):
var cols_of_1 = 2.0
var cols_of_2 = 2.0
for c in _all_colors:
if col1 == c:
cols_of_1 = 1.0
elif col2 == c:
cols_of_2 = 1.0
var mix_pcent = cols_of_2/(cols_of_1 + cols_of_2)
var mix_col = col1.linear_interpolate(col2, mix_pcent)
return mix_col
func update_content_color(var _all_colors: Array):
2019-12-28 18:36:48 +01:00
_left_color = NULL_COLOR
_up_color = NULL_COLOR
if _left_cast != null:
var new_color = _get_color_from_cast(_left_cast)
if new_color != null:
_left_color = new_color
if _up_cast != null:
2020-01-02 01:59:35 +01:00
var new_color2 = _get_color_from_cast(_up_cast)
if new_color2 != null:
_up_color = new_color2
2019-12-28 18:36:48 +01:00
if _left_color != NULL_COLOR and _up_color != NULL_COLOR:
2020-01-02 01:59:35 +01:00
var mix = _blend_colors(_left_color, _up_color, _all_colors)
2019-12-28 18:36:48 +01:00
content_color = mix
elif _left_color != NULL_COLOR:
content_color = _left_color
elif _up_color != NULL_COLOR:
content_color = _up_color
2020-01-02 01:59:35 +01:00
else:
content_color = NULL_COLOR
2019-11-23 20:44:25 +01:00
if _mesh != null:
var material = _mesh.get_surface_material(0)
if content_color != NULL_COLOR:
if material == null:
material = SpatialMaterial.new()
_mesh.material_override = material
material.albedo_color = content_color
2019-11-23 20:44:25 +01:00
else:
2020-01-08 16:02:04 +01:00
if can_turn:
2020-01-02 01:59:35 +01:00
_mesh.material_override = fork_material
else:
2019-11-23 20:44:25 +01:00
_mesh.material_override = null