2024-07-30 16:09:44 +00:00
@ tool
2024-07-31 10:21:17 +00:00
@ icon ( " res://textures/stickers/icon_sticker2.svg " )
class_name Sticker extends Area2D
## Handle all the sticker-relative gameplay.
##
## Sticker node expect to be a child of a Node2D, who serve as a root for
## all the fonctionnalities. You need to assign nodes for the stickerdetection
## and for the sprites.
2024-07-25 10:02:35 +00:00
2024-07-31 22:54:42 +00:00
#region Exported variables
@ export var Sticker_mode : bool = false
2024-07-31 10:21:17 +00:00
@ export_group ( " Nodes " )
## The look of the object. If it's multiple sprites, they should be under one main sprite
@ export var WorldSprite : Sprite2D
## Optional - The look of the object when grabbed. If empty, OutlineMat will be applied to WorldSprite
@ export var StickerSprite : Sprite2D
2024-08-03 20:42:06 +00:00
## Optional - The detection zone, if not the same as the sticker area
@ export var DetectionArea : Area2D :
set ( new_area ) :
if ( new_area != self ) :
DetectionArea = new_area
else :
printerr ( " DetectionArea cannot be the sticker itself " )
2024-07-31 10:21:17 +00:00
@ export_group ( " Material " )
2024-07-30 16:09:44 +00:00
@ export var OutlineMat : ShaderMaterial = preload ( " res://shaders/shaderMaterial_Outline.tres " )
@ export var Foiled : bool #TODO: Foil material and logic
2024-07-31 16:04:21 +00:00
@ export var CreateShape : bool :
set ( value ) :
var test = CollisionShape2D . new ( )
test . set_name ( " StickerShape " )
add_child ( test )
test . set_owner ( get_tree ( ) . get_edited_scene_root ( ) )
test = null
2024-07-31 22:54:42 +00:00
#endregion
2024-07-25 10:02:35 +00:00
2024-07-31 10:21:17 +00:00
var meta : PackedStringArray = [ " sticker " ]
2024-07-31 22:54:42 +00:00
var detected_solids : Array
2024-08-03 20:42:06 +00:00
var detected_zones : Array
2024-08-03 22:32:50 +00:00
var Grabbed : bool = false
2024-07-31 22:54:42 +00:00
2024-08-05 19:35:55 +00:00
## Emitted when the sticker overlap a blocking body, preventing to be spawned as an asset in the level. Usually received by the cursor to change his sprite
signal spawn_mode_changed ( can_spawn : bool )
2024-07-31 22:54:42 +00:00
#region Functions
2024-08-03 20:42:06 +00:00
func _get_configuration_warnings ( ) :
if ( get_parent ( ) == get_tree ( ) . get_edited_scene_root ( ) ) :
return [ " This node should have a parent " ]
return [ ]
2024-07-31 10:21:17 +00:00
func _init ( ) :
collision_layer = 2
2024-08-02 15:26:19 +00:00
# TODO: Stickerzone maybe will not need to check everything, and detection will be on the zone detection area only. maybe we want a fallback if sticker area and detection area are the same shape
collision_mask = 61
2024-07-31 10:21:17 +00:00
set_meta ( " tags " , meta )
2024-07-31 22:54:42 +00:00
monitoring = true
2024-08-03 20:42:06 +00:00
if Engine . is_editor_hint ( ) :
update_configuration_warnings ( )
2024-07-31 10:21:17 +00:00
func _enter_tree ( ) :
set_meta ( " tags " , meta )
if ( get_parent ( ) is Sprite2D and WorldSprite == null ) :
WorldSprite = get_parent ( )
2024-08-03 20:42:06 +00:00
if Engine . is_editor_hint ( ) :
update_configuration_warnings ( )
2024-07-31 10:21:17 +00:00
2024-07-25 10:02:35 +00:00
func _ready ( ) :
2024-07-30 16:09:44 +00:00
if ( get_parent ( ) == get_tree ( ) . root ) :
printerr ( " stickers should always have a parent " )
breakpoint
2024-08-03 20:42:06 +00:00
if ( DetectionArea != null ) :
DetectionArea . area_entered . connect ( _on_area_entered )
DetectionArea . area_exited . connect ( _on_area_exited )
DetectionArea . body_entered . connect ( _on_body_entered )
DetectionArea . body_exited . connect ( _on_body_exited )
DetectionArea . collision_mask = 61
else :
area_entered . connect ( _on_area_entered )
area_exited . connect ( _on_area_exited )
body_entered . connect ( _on_body_entered )
body_exited . connect ( _on_body_exited )
2024-08-05 19:35:55 +00:00
2024-07-31 10:21:17 +00:00
2024-08-02 15:26:19 +00:00
# TODO : Solids and interactive zone detection area should not be the same as the sticker area.
# IDEA: Need to export a child Area2D
2024-07-31 22:54:42 +00:00
func _on_body_entered ( body : Node2D ) :
print ( " body entered " , body )
detected_solids . append ( body )
2024-08-05 19:35:55 +00:00
spawn_mode_changed . emit ( false )
2024-07-31 22:54:42 +00:00
func _on_body_exited ( body : Node2D ) :
print ( " body exited " , body )
detected_solids . erase ( body )
2024-08-05 19:35:55 +00:00
if ( Sticker_mode and Grabbed and detected_solids . size ( ) == 0 ) :
spawn_mode_changed . emit ( true )
2024-07-31 22:54:42 +00:00
func _on_area_entered ( area : Area2D ) :
print ( " area entered " , area )
#TODO:Filter by type
2024-08-03 20:42:06 +00:00
detected_zones . append ( area )
2024-07-31 22:54:42 +00:00
func _on_area_exited ( area : Area2D ) :
print ( " area exited " , area )
2024-08-03 20:42:06 +00:00
detected_zones . erase ( area )
2024-07-31 10:21:17 +00:00
2024-08-03 20:42:06 +00:00
func _process ( _delta ) :
2024-07-31 10:21:17 +00:00
pass
2024-08-03 22:32:50 +00:00
2024-07-31 10:21:17 +00:00
## When the sticker is released
2024-08-03 20:42:06 +00:00
func on_released ( _CastResult : Array = [ ] ) :
2024-07-30 16:09:44 +00:00
print ( self , " released " )
2024-08-03 22:32:50 +00:00
Grabbed = false
2024-08-03 20:42:06 +00:00
#Need a traceresult to know if it should stay in the stickerstate or be released in the world
2024-07-31 22:54:42 +00:00
if ( detected_solids . size ( ) == 0 ) :
get_parent ( ) . top_level = false
disable_ChildNodes_collisions ( false )
update_ChildNodes_visibilty ( true )
Sticker_mode = false
2024-08-03 22:32:50 +00:00
on_unhover ( ) #security,but i don't like it
2024-07-30 16:09:44 +00:00
get_parent ( ) . reparent ( MapManager . current_scene . get_child ( 0 ) )
2024-08-03 22:32:50 +00:00
2024-07-31 22:54:42 +00:00
2024-07-25 10:02:35 +00:00
2024-07-30 16:09:44 +00:00
func on_click ( ) :
print ( self , " clicked " )
2024-07-25 10:02:35 +00:00
2024-07-31 10:21:17 +00:00
func redraw ( ) :
if ( StickerSprite != null ) :
StickerSprite . queue_redraw ( )
if ( WorldSprite != null ) :
WorldSprite . queue_redraw ( )
2024-07-30 16:09:44 +00:00
func on_hover ( ) :
2024-07-31 22:54:42 +00:00
if ( Sticker_mode == false ) :
if ( StickerSprite != null ) :
WorldSprite . visible = false
StickerSprite . visible = true
elif ( WorldSprite != null ) :
WorldSprite . material = OutlineMat
redraw ( )
2024-07-31 10:21:17 +00:00
2024-07-25 10:02:35 +00:00
2024-07-30 16:09:44 +00:00
func on_unhover ( ) :
2024-07-31 22:54:42 +00:00
if ( Sticker_mode == false ) :
if ( StickerSprite != null ) :
StickerSprite . visible = false
if ( WorldSprite != null ) :
WorldSprite . material = null
WorldSprite . visible = true
redraw ( )
2024-07-31 10:21:17 +00:00
2024-07-31 22:54:42 +00:00
2024-07-30 16:09:44 +00:00
func on_grab ( _offset : Vector2 = Vector2 ( 0.0 , 0.0 ) ) :
2024-07-31 22:54:42 +00:00
Sticker_mode = true
2024-08-03 22:32:50 +00:00
2024-07-30 16:09:44 +00:00
get_parent ( ) . top_level = true
2024-08-01 10:31:54 +00:00
get_parent ( ) . reparent ( MapManager )
2024-07-31 10:21:17 +00:00
update_ChildNodes_visibilty ( false )
2024-07-31 16:04:21 +00:00
disable_ChildNodes_collisions ( true )
2024-08-03 22:32:50 +00:00
Grabbed = true
2024-07-31 22:54:42 +00:00
2024-07-31 10:21:17 +00:00
func update_ChildNodes_visibilty ( _visible : bool = true ) :
2024-07-30 16:09:44 +00:00
for _childNode in get_parent ( ) . get_children ( ) :
2024-07-31 22:53:59 +00:00
if ( _childNode != WorldSprite and _childNode != StickerSprite ) :
2024-07-31 10:21:17 +00:00
_childNode . visible = _visible
2024-07-31 22:54:42 +00:00
2024-07-31 10:21:17 +00:00
func disable_ChildNodes_collisions ( _disable : bool = true ) :
for _childNode in get_parent ( ) . get_children ( ) :
2024-07-31 22:53:59 +00:00
if ( _childNode != self ) :
2024-07-31 22:54:42 +00:00
_childNode . process_mode = Node . PROCESS_MODE_DISABLED if _disable else Node . PROCESS_MODE_ALWAYS
#endregion