class_name Block
|
|
extends StaticBody3D
|
|
|
|
|
|
@onready var collision_shape: CollisionShape3D = $CollisionShape3D
|
|
@onready var highlight_mesh: MeshInstance3D = $HighlightMesh
|
|
|
|
@onready var east_face: MeshInstance3D = $BlockFaces/EastFace
|
|
@onready var west_face: MeshInstance3D = $BlockFaces/WestFace
|
|
@onready var north_face: MeshInstance3D = $BlockFaces/NorthFace
|
|
@onready var south_face: MeshInstance3D = $BlockFaces/SouthFace
|
|
@onready var top_face: MeshInstance3D = $BlockFaces/TopFace
|
|
@onready var bottom_face: MeshInstance3D = $BlockFaces/BottomFace
|
|
|
|
var id: String
|
|
var resource: BlockResource
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
if Waila.ref.get_target() == self:
|
|
Waila.ref.release_target()
|
|
|
|
func _ready() -> void:
|
|
_apply_materials()
|
|
|
|
|
|
func get_id() -> String:
|
|
return id
|
|
|
|
func hook() -> void:
|
|
if not Globals.enable_block_highlight: return
|
|
|
|
highlight_mesh.visible = true
|
|
|
|
# TODO: Rename to something else (maybe remove())
|
|
# Move to signal?
|
|
func interact_left_click() -> void:
|
|
queue_free()
|
|
|
|
func release() -> void:
|
|
highlight_mesh.visible = false
|
|
|
|
func set_id(block_id: String) -> void:
|
|
id = block_id
|
|
_load_resource_data()
|
|
_apply_materials()
|
|
|
|
|
|
## Apply a specific material to a MeshInstance3D
|
|
## If material is null, the EastFace material from the BlockResource will be utilized
|
|
func _apply_material(face: MeshInstance3D, material: StandardMaterial3D) -> void:
|
|
if material == null:
|
|
material = resource.material_east # Default to East face (first)
|
|
|
|
face.set_surface_override_material(0, material)
|
|
|
|
func _apply_materials() -> void:
|
|
if east_face == null: return
|
|
if resource == null: return
|
|
|
|
# This could probably be improved/simplified
|
|
_apply_material(east_face, resource.material_east)
|
|
_apply_material(west_face, resource.material_west)
|
|
_apply_material(north_face, resource.material_north)
|
|
_apply_material(south_face, resource.material_south)
|
|
_apply_material(top_face, resource.material_top)
|
|
_apply_material(bottom_face, resource.material_bottom)
|
|
|
|
func _load_resource_data() -> void:
|
|
if not id:
|
|
printerr("Block ID was expected but was not received")
|
|
return
|
|
|
|
resource = DBItems.data[id]
|