1
Fork 0
mirror of https://github.com/Steffo99/swear-jar.git synced 2024-11-25 17:14:20 +00:00
swear-jar/interface/ghost/ghost.gd

86 lines
2.7 KiB
GDScript3
Raw Normal View History

2023-10-02 13:12:08 +00:00
extends Area2D
class_name Ghost
2023-10-02 15:53:13 +00:00
## Ghost previewing the instantiation of a scene.
2023-10-02 13:12:08 +00:00
2023-10-08 01:56:29 +00:00
## Color to modulate this with if the body currently isn't overlapping anything.
@export var valid_color: Color = Color.WHITE
## Color to modulate this with if the body currently is overlapping something.
@export var invalid_color: Color = Color.RED
## The [Instantiator] to use to spawn the ghosted item.
@onready var instantiator: Instantiator = $Instantiator
## The [OverlapChecker] to use to see if a solid block is overlapping the ghost.
@onready var overlap_checker: OverlapChecker = $OverlapChecker
## The [PlaceableAreaChecker] to use to see if the ghost is currently inside the placeable area.
@onready var placeable_area_checker: PlaceableAreaChecker = $PlaceableAreaChecker
## The [OverlapFreer] to use to delete the [PhysicsBody2D] behind the ghost before instantiation.
@onready var overlap_freer: OverlapFreer = $OverlapFreer
2023-10-02 15:53:13 +00:00
## The [CollisionShape2D] to use to check for placement checks.
##
## MUST consist of a [RectangleShape2D].
@onready var placement_shape: CollisionShape2D = $PlacementShape
2023-10-02 14:22:03 +00:00
2023-10-02 15:53:13 +00:00
## The [Sprite2D] node previewing the scene.
@onready var preview_sprite: Sprite2D = $PlacementShape/PreviewSprite
2023-10-02 14:22:03 +00:00
2023-10-02 16:29:05 +00:00
## The collision mask of objects that should prevent this object's placement.
@export_flags_2d_physics var collision_mask_prevent_placement: int
## The collision mask of objects that should be deleted on this object's placement.
@export_flags_2d_physics var collision_mask_delete_placement: int
2023-10-02 15:53:13 +00:00
## The texture that the preview sprite should display.
@export var preview_texture: Texture2D:
get:
return preview_texture
set(value):
preview_texture = value
2023-10-02 16:29:05 +00:00
# Quick priority fix
if preview_sprite:
preview_sprite.texture = value
2023-10-02 14:22:03 +00:00
2023-10-05 22:09:24 +00:00
2023-10-02 16:29:05 +00:00
func _ready():
collision_mask = collision_mask_prevent_placement | collision_mask_delete_placement
preview_sprite.texture = preview_texture
2023-10-05 22:09:24 +00:00
func _physics_process(_delta: float):
2023-10-05 22:11:08 +00:00
# Update collision
update_can_place()
2023-10-05 22:09:24 +00:00
2023-10-02 15:53:13 +00:00
2023-10-08 01:56:29 +00:00
var can_place: bool:
get:
return can_place
set(value):
can_place = value
modulate = valid_color if value else invalid_color
2023-10-02 16:29:05 +00:00
2023-10-08 01:56:29 +00:00
## Update the value of [can_place].
# DIRTY HACK: Relies on the placeable area being perfectly surrounded by solid bodies.
func update_can_place() -> void:
can_place = overlap_checker.is_overlapping_with == null and placeable_area_checker.is_overlapping_with != null
2023-10-02 16:29:05 +00:00
func materialize():
2023-10-08 01:56:29 +00:00
# Compatibility stub for Instantiator
2023-10-02 18:28:08 +00:00
if not can_place:
return null
var overlapping_bodies = get_overlapping_bodies()
for body in overlapping_bodies:
if body is PhysicsBody2D:
if body.collision_layer & collision_mask_delete_placement:
body.queue_free()
2023-10-08 01:56:29 +00:00
var inst = instantiator.instantiate()
# TODO: Remove this
return inst