2025-08-26 09:24:09 +08:00
|
|
|
@tool
|
|
|
|
|
extends Control
|
2025-08-26 11:39:47 +08:00
|
|
|
class_name ColorBar
|
2025-08-26 09:24:09 +08:00
|
|
|
|
|
|
|
|
@export var minValue: float = 0
|
|
|
|
|
@export var maxValue: float = 100
|
|
|
|
|
@export var currentValue: float = 50
|
2025-08-27 20:27:01 +08:00
|
|
|
@export var backBox: StyleBox
|
2025-08-27 20:47:04 +08:00
|
|
|
@export var middleBox1: StyleBox
|
|
|
|
|
@export var middleBox2: StyleBox
|
2025-08-27 20:27:01 +08:00
|
|
|
@export var frontBox: StyleBox
|
|
|
|
|
@export var speed1: float = 0.8
|
2025-08-26 09:24:09 +08:00
|
|
|
@export var speed2: float = 0.01
|
|
|
|
|
|
|
|
|
|
var middleValue = 0
|
|
|
|
|
var frontValue = 0
|
|
|
|
|
var forwardDirection = -1
|
2025-09-06 16:51:12 +08:00
|
|
|
var lastChangeTime = 0
|
2025-08-26 09:24:09 +08:00
|
|
|
|
|
|
|
|
func getPercent(value: float):
|
|
|
|
|
return (value - minValue) / (maxValue - minValue)
|
|
|
|
|
func setCurrent(value: float):
|
2025-08-27 20:47:04 +08:00
|
|
|
if value == currentValue:
|
|
|
|
|
return
|
2025-08-26 09:24:09 +08:00
|
|
|
forwardDirection = sign(value - currentValue)
|
2025-08-27 20:48:21 +08:00
|
|
|
currentValue = clamp(value, minValue, maxValue)
|
2025-09-06 16:51:12 +08:00
|
|
|
lastChangeTime = WorldManager.getTime()
|
|
|
|
|
func forceSync():
|
2025-08-26 09:24:09 +08:00
|
|
|
middleValue = currentValue
|
|
|
|
|
frontValue = currentValue
|
2025-09-06 16:51:12 +08:00
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
|
forceSync()
|
|
|
|
|
lastChangeTime = WorldManager.getTime()
|
2025-08-26 09:24:09 +08:00
|
|
|
func _draw():
|
2025-08-27 20:27:01 +08:00
|
|
|
draw_style_box(backBox, Rect2(0, 0, size.x, size.y))
|
2025-09-06 17:13:34 +08:00
|
|
|
draw_style_box(middleBox2 if forwardDirection > 0 else middleBox1, Rect2(0, 0, size.x * getPercent(middleValue), size.y))
|
2025-08-27 20:27:01 +08:00
|
|
|
draw_style_box(frontBox, Rect2(0, 0, size.x * getPercent(frontValue), size.y))
|
|
|
|
|
func _physics_process(_delta: float) -> void:
|
2025-09-06 17:53:11 +08:00
|
|
|
if forwardDirection > 0:
|
2025-09-06 17:13:34 +08:00
|
|
|
middleValue = lerpf(middleValue, currentValue, speed1 if forwardDirection > 0 else speed2)
|
2025-09-06 17:53:11 +08:00
|
|
|
if WorldManager.getTime() - lastChangeTime > GameRule.detainTime:
|
|
|
|
|
frontValue = lerpf(frontValue, currentValue, speed1 if forwardDirection < 0 else speed2)
|
|
|
|
|
else:
|
|
|
|
|
if WorldManager.getTime() - lastChangeTime > GameRule.detainTime:
|
|
|
|
|
middleValue = lerpf(middleValue, currentValue, speed1 if forwardDirection > 0 else speed2)
|
|
|
|
|
frontValue = lerpf(frontValue, currentValue, speed1 if forwardDirection < 0 else speed2)
|
2025-08-26 09:24:09 +08:00
|
|
|
queue_redraw()
|