StickerClone/core/Sticker.gd
Lucas Peter 79a577272a
update sticker code
Rework code, make sticker a Area2D Node that handle all sticker logic
2024-07-31 12:21:17 +02:00

92 lines
2.5 KiB
GDScript

@tool
@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.
@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
@export_group("Material")
@export var OutlineMat:ShaderMaterial = preload("res://shaders/shaderMaterial_Outline.tres")
@export var Foiled:bool #TODO: Foil material and logic
var meta:PackedStringArray = ["sticker"]
func _init():
collision_layer = 2
collision_mask = 0
set_meta("tags",meta)
monitoring = false
func _enter_tree():
set_meta("tags",meta)
if (get_parent() is Sprite2D and WorldSprite == null):
WorldSprite = get_parent()
func _ready():
if (get_parent() == get_tree().root):
printerr("stickers should always have a parent")
breakpoint
func _process(delta):
pass
## When the sticker is released
func on_released(CastResult:Array=[]):
print(self," released")
#TODO: Need a traceresult to know if it should stay in the stickerstate or be released in the world
get_parent().top_level = false
get_parent().reparent(MapManager.current_scene.get_child(0))
disable_ChildNodes_collisions(false)
update_ChildNodes_visibilty(true)
func on_click():
print(self," clicked")
func redraw():
if (StickerSprite != null):
StickerSprite.queue_redraw()
if (WorldSprite != null):
WorldSprite.queue_redraw()
func on_hover():
if (StickerSprite != null):
WorldSprite.visible = false
StickerSprite.visible = true
elif (WorldSprite != null):
WorldSprite.material = OutlineMat
redraw()
func on_unhover():
if (StickerSprite != null):
StickerSprite.visible = false
if (WorldSprite != null):
WorldSprite.material = null
WorldSprite.visible = true
redraw()
func on_grab(_offset:Vector2=Vector2(0.0,0.0)):
get_parent().top_level = true
get_parent().reparent(get_tree().root)
update_ChildNodes_visibilty(false)
func update_ChildNodes_visibilty(_visible:bool=true):
for _childNode in get_parent().get_children():
if(_childNode != WorldSprite):
_childNode.visible = _visible
func disable_ChildNodes_collisions(_disable:bool=true):
for _childNode in get_parent().get_children():
_childNode.set_deferred("disabled",_disable)