Files
owniverse/Stage/stage.gd
T
Kirill fc86261a9d Add new entity and item definitions, refactor related code
- Introduced new CSV files for entities and items, defining various properties and characteristics.
- Refactored the stage.gd script to handle item selection and descriptions instead of entities.
- Updated item button script to reflect changes in item properties and signals.
- Created a new entity.gd script to define the OwniverseEntity class with initialization and parsing methods.
- Modified owniverse.gd to initialize entities from the new definitions.
- Adjusted versioning in export presets and project files to reflect recent changes.
2026-04-08 00:23:22 +03:00

243 lines
7.6 KiB
GDScript

extends Control
const ITEM_BUTTON = preload("uid://d3xjfxwe3w16v")
const SPEED_TEXTURE_NAME_TEMPLATE = "res://UI/TopMenu/Time&Speed/Time%s.png"
const PAGE_COUNT = 5
const PAGE_WIDTH = 1080
const DRAG_TOLERANCE = 360
const SWIPE_DURATION: float = 2
const SWIPE_DELAY = 2000 # msec
@onready var stage_page_scroller: ScrollContainer = $StageRows/PageScroller
@onready var item_pages: Array[Control] = [
$StageRows/PageScroller/HBoxContainer/ForcePage/Forces,
$StageRows/PageScroller/HBoxContainer/ResourcePage/HBoxContainer/Resources,
$StageRows/PageScroller/HBoxContainer/StarPage/Stars,
$StageRows/PageScroller/HBoxContainer/StructurePage/Structures,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Life,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/LivingPlanet,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ0,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ1,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ2,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ3,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ4
]
@onready var g_item_descriptions: Array = [
$StageRows/PageScroller/HBoxContainer/ForcePage/Description,
$StageRows/PageScroller/HBoxContainer/ResourcePage/Description,
$StageRows/PageScroller/HBoxContainer/StarPage/Description,
$StageRows/PageScroller/HBoxContainer/StructurePage/Description,
$StageRows/PageScroller/HBoxContainer/LifePage/Description
]
@onready var date_time: Label = $StageRows/TopMenu/InfoPanel/DateTime
@onready var g_speed_icons: Array = [
$StageRows/TopMenu/InfoPanel/Speed/Speed1,
$StageRows/TopMenu/InfoPanel/Speed/Speed2,
$StageRows/TopMenu/InfoPanel/Speed/Speed3,
$StageRows/TopMenu/InfoPanel/Speed/Speed4,
$StageRows/TopMenu/InfoPanel/Speed/Speed5,
$StageRows/TopMenu/InfoPanel/Speed/Speed6,
$StageRows/TopMenu/InfoPanel/Speed/Speed7,
$StageRows/TopMenu/InfoPanel/Speed/Speed8
]
@onready var g_time_minus: TextureButton = $StageRows/TopMenu/TimeMinus
@onready var g_time_plus: TextureButton = $StageRows/TopMenu/TimePlus
var g_tween: Tween = null
var g_dragged_start: Vector2 = Vector2.ZERO
var g_scroll_start: int = 0
var g_current_page: int = 0
var g_last_scroll_updated: int = 0
var f_check_swipe: bool = false
var g_swipe_time_delta: float = 0
var g_counters: Dictionary[String, Counter] = {
"swipe": Counter.new(),
}
var ActiveItems: Dictionary = {}
var owniverse = Owniverse.new()
func set_datetime(value: String):
date_time.text = value
func _on_indicator_pressed(indicator_name: String, page: int = 0):
_set_description(indicator_name, page)
func item_selected(item: OwniverseItem) -> void:
_control_page(item)
_set_item_description(item)
func _set_item_description(item: OwniverseItem) -> void:
var description: String = "[b]" \
+ Global.get_localized_item_description(item.item_name, "label") \
+"[/b]" \
+"\n" \
+ Global.get_localized_item_description(item.item_name, "description")
var desc_page = clamp(item.item_page, 0, 4)
g_item_descriptions[desc_page].text = description
func _set_description(item_name: String, page: int) -> void:
var description = Global.get_description(item_name + ".Name")
description += "\n"
description += Global.get_description(item_name + ".Description")
g_item_descriptions[page].text = description
func _control_page(item: OwniverseItem) -> void:
if !ActiveItems.has(item.item_page):
ActiveItems[item.item_page] = null
var current_item: OwniverseItem = ActiveItems[item.item_page]
if current_item == item:
return
ActiveItems[item.item_page] = item
if current_item != null and current_item.item_page == item.item_page:
current_item.set_selection(false)
item.set_selection(true)
# subpages
if item.item_sub_page != -1:
item_pages[item.item_sub_page].visible = true
if current_item == null \
or current_item.item_sub_page == -1 \
or current_item.item_sub_page == item.item_sub_page:
return
item_pages[current_item.item_sub_page].visible = false
func _init_items() -> bool:
for item_id in Global.OwniverseItems:
var item_props: Dictionary = Global.OwniverseItems[item_id]
var item: OwniverseItem = ITEM_BUTTON.instantiate()
if !item_props.has("page"):
printerr("Item `", item_id, "` has no `page` key.")
return false
var page: int = int(item_props.page)
item_pages[page].add_child(item)
item.init_button(item_props)
item.item_activated.connect(item_selected)
return false
func _check_counters(delta: float) -> Array:
var actions: Array = []
for k in g_counters:
var cnt: Counter = g_counters[k]
if not cnt.is_active:
continue
cnt.value += delta
if cnt.value >= cnt.sec:
actions.append(k)
cnt.value = 0
cnt.is_active = false
return actions
func _do_actions(actions: Array) -> void:
for action: String in actions:
match action:
"swipe":
_swipe_to_nearest_page()
#region Swipe
func _swipe_to_nearest_page() -> void:
if stage_page_scroller.scroll_horizontal % PAGE_WIDTH == 0: return
var new_page: int = round(1.0 * stage_page_scroller.scroll_horizontal / PAGE_WIDTH)
if g_tween != null:
if g_tween.is_running():
return
g_tween = stage_page_scroller.get_tree().create_tween()
g_tween.tween_property(stage_page_scroller, "scroll_horizontal",
new_page * PAGE_WIDTH, SWIPE_DURATION).set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
func _input(event: InputEvent) -> void:
if event is InputEventScreenDrag:
stage_page_scroller.scroll_horizontal = clamp(
stage_page_scroller.scroll_horizontal - PAGE_COUNT * event.screen_relative.x,
0, PAGE_WIDTH * (PAGE_COUNT - 1))
g_counters.swipe.is_active = true
g_counters.swipe.value = 0
elif event is InputEventPanGesture:
g_counters.swipe.is_active = true
g_counters.swipe.value = 0
elif event is InputEventScreenTouch:
pass
#endregion
func _ready() -> void:
owniverse.init_enitities()
_init_items()
func _process(delta: float) -> void:
# Update date
var sdt: String = str(snapped(owniverse.add_time_delta(delta), 0.1))
set_datetime(sdt)
var actions = _check_counters(delta)
_do_actions(actions)
func _on_burger_button_pressed() -> void:
print("b")
pass # Replace with function body.
func _on_report_button_pressed() -> void:
print("r")
pass # Replace with function body.
func _change_speed(delta: int) -> void:
var speed: int = owniverse.change_speed(delta)
var ind: int = 1
for icon in g_speed_icons:
if ind <= speed:
icon.visible = true
else:
icon.visible = false
ind += 1
if speed == 1:
g_time_minus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % "x")
g_time_plus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % "2")
elif speed == Global.SPEED_LIMIT:
g_time_minus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % str(Global.SPEED_LIMIT - 1))
g_time_plus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % "x")
else:
g_time_minus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % str(speed - 1))
g_time_plus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % str(speed + 1))
func _on_topmenu_button_pressed(ButtonName) -> void:
print(ButtonName)