95 lines
2.5 KiB
GDScript
95 lines
2.5 KiB
GDScript
@tool
|
|
extends Node2D
|
|
|
|
@export var flower_area := Vector2(400, 50):
|
|
set(val):
|
|
flower_area = val
|
|
queue_redraw()
|
|
@export var gizmo_outline_color := Color(0.4, 0.4, 0.2, 0.8):
|
|
set(val):
|
|
gizmo_outline_color = val
|
|
queue_redraw()
|
|
@export var scatter_on_start := false
|
|
@export var debug_scatter := false:
|
|
set(val):
|
|
debug_scatter = false
|
|
if Engine.is_editor_hint():
|
|
_init_flowers(true)
|
|
@export var auto_fade_distance := 50.0
|
|
@export var focus_node: Node2D
|
|
|
|
var flowers = [] as Array[AnimatedSprite2D]
|
|
var faded_flowers = [] as Array[String]
|
|
|
|
var fade_animation_dict = {
|
|
"flower1_开放": "flower1_枯萎",
|
|
"flower2_开放": "flower2_枯萎",
|
|
}
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
_init_flowers()
|
|
if Engine.is_editor_hint():
|
|
queue_redraw()
|
|
|
|
|
|
func _enter_tree() -> void:
|
|
if is_node_ready():
|
|
_init_flowers()
|
|
|
|
|
|
func _init_flowers(scatter := scatter_on_start) -> void:
|
|
flowers.clear()
|
|
for f in get_children():
|
|
if f is AnimatedSprite2D:
|
|
flowers.append(f)
|
|
for i in range(flowers.size()):
|
|
var f = flowers[i]
|
|
# clamp to action area
|
|
if scatter:
|
|
var pos = Vector2(randf() * flower_area.x, randf() * flower_area.y)
|
|
f.position = pos
|
|
# if (
|
|
# not Engine.is_editor_hint()
|
|
# and ArchiveManager.archive.global_data_dict.has("faded_flowers")
|
|
# ):
|
|
# faded_flowers = ArchiveManager.archive.global_data_dict["faded_flowers"]
|
|
# for f in flowers:
|
|
# if faded_flowers.has(f.name):
|
|
# var animation_name = fade_animation_dict.get(f.animation, "")
|
|
# if animation_name:
|
|
# f.play(animation_name)
|
|
|
|
|
|
func _draw() -> void:
|
|
if Engine.is_editor_hint():
|
|
# draw gizmo
|
|
var area_rect = Rect2(Vector2.ZERO, flower_area)
|
|
# fill
|
|
var fill_color = gizmo_outline_color
|
|
fill_color.a = 0.4
|
|
draw_rect(area_rect, fill_color)
|
|
# outline
|
|
draw_rect(area_rect, gizmo_outline_color, false, 1.0)
|
|
|
|
|
|
func _physics_process(_delta: float) -> void:
|
|
if Engine.is_editor_hint():
|
|
return
|
|
if not focus_node or not focus_node.is_visible_in_tree():
|
|
return
|
|
var focus_pos_x = focus_node.global_position.x
|
|
# var faded := false
|
|
for f in flowers:
|
|
var f_pos_x = f.global_position.x
|
|
var distance = abs(focus_pos_x - f_pos_x)
|
|
if distance < auto_fade_distance:
|
|
var animation_name = fade_animation_dict.get(f.animation, "")
|
|
if animation_name:
|
|
f.play(animation_name)
|
|
faded_flowers.append(f.name)
|
|
# faded = true
|
|
# if faded and not Engine.is_editor_hint():
|
|
# ArchiveManager.archive.global_data_dict["faded_flowers"] = faded_flowers
|