Refactor code structure for improved readability and maintainability; removed redundant code blocks and optimized existing functions.
This commit is contained in:
+60
-31
@@ -1,14 +1,15 @@
|
||||
extends Control
|
||||
|
||||
const ITEM_BUTTON = preload("uid://d3xjfxwe3w16v")
|
||||
signal produce_items(item_id: String, amount: int)
|
||||
|
||||
const OWNIVERSE_ITEM = 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
|
||||
const SWIPE_DURATION: float = 0.4
|
||||
|
||||
@onready var stage_page_scroller: ScrollContainer = $StageRows/PageScroller
|
||||
|
||||
@@ -65,23 +66,44 @@ var g_counters: Dictionary[String, Counter] = {
|
||||
"swipe": Counter.new(),
|
||||
}
|
||||
|
||||
var ActiveItems: Dictionary = {}
|
||||
var g_active_items: Dictionary = {}
|
||||
|
||||
var owniverse:Owniverse = Owniverse.new()
|
||||
var owniverse_items: Dictionary[String, OwniverseItem] = {}
|
||||
var locked_items:Dictionary = {}
|
||||
|
||||
var item_list_to_update: Dictionary = {}
|
||||
|
||||
var owniverse = Owniverse.new()
|
||||
|
||||
func set_datetime(value: String):
|
||||
date_time.text = value
|
||||
|
||||
#region Item manipulations: update and visibility
|
||||
|
||||
|
||||
func add_to_update_list(item_id: String, amount: int) -> void:
|
||||
if item_list_to_update.has(item_id):
|
||||
item_list_to_update[item_id] += amount
|
||||
else:
|
||||
item_list_to_update[item_id] = amount
|
||||
|
||||
|
||||
func set_owniverse_item_value() -> void:
|
||||
for item_id in owniverse.items_to_update:
|
||||
var item:OwniverseItem = owniverse_items[item_id]
|
||||
item.description.text = str(roundi(owniverse.items_to_update[item_id]))
|
||||
owniverse.items_to_update = {}
|
||||
|
||||
|
||||
func unlock_owniverse_item() -> void:
|
||||
pass
|
||||
|
||||
#endregion
|
||||
|
||||
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") \
|
||||
@@ -100,14 +122,15 @@ func _set_description(item_name: String, page: int) -> void:
|
||||
|
||||
|
||||
func _control_page(item: OwniverseItem) -> void:
|
||||
if !ActiveItems.has(item.item_page):
|
||||
ActiveItems[item.item_page] = null
|
||||
# TODO переписать это ↓
|
||||
if !g_active_items.has(item.item_page):
|
||||
g_active_items[item.item_page] = null
|
||||
|
||||
var current_item: OwniverseItem = ActiveItems[item.item_page]
|
||||
var current_item: OwniverseItem = g_active_items[item.item_page]
|
||||
if current_item == item:
|
||||
return
|
||||
|
||||
ActiveItems[item.item_page] = item
|
||||
g_active_items[item.item_page] = item
|
||||
|
||||
if current_item != null and current_item.item_page == item.item_page:
|
||||
current_item.set_selection(false)
|
||||
@@ -125,21 +148,35 @@ func _control_page(item: OwniverseItem) -> void:
|
||||
item_pages[current_item.item_sub_page].visible = false
|
||||
|
||||
|
||||
func _init_items() -> bool:
|
||||
|
||||
func _init_owniverse_items() -> bool:
|
||||
owniverse_items = {}
|
||||
for item_id in Global.OwniverseItems:
|
||||
var item_props: Dictionary = Global.OwniverseItems[item_id]
|
||||
var item: OwniverseItem = ITEM_BUTTON.instantiate()
|
||||
var item: OwniverseItem = OWNIVERSE_ITEM.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)
|
||||
item.item_activated.connect(item_activated)
|
||||
|
||||
if owniverse.entities.has(item_id):
|
||||
item.visible = owniverse.entities[item_id].is_unlocked
|
||||
|
||||
owniverse_items[item_id] = item
|
||||
|
||||
return false
|
||||
|
||||
|
||||
func item_activated(item: OwniverseItem) -> void:
|
||||
_control_page(item)
|
||||
_set_item_description(item)
|
||||
|
||||
print(item.item_id)
|
||||
if item.item_id == "S" or item.item_id == "H":
|
||||
owniverse.produce(item.item_id, 1)
|
||||
|
||||
|
||||
func _check_counters(delta: float) -> Array:
|
||||
var actions: Array = []
|
||||
@@ -165,8 +202,6 @@ func _do_actions(actions: Array) -> void:
|
||||
|
||||
|
||||
#region Swipe
|
||||
|
||||
|
||||
func _swipe_to_nearest_page() -> void:
|
||||
if stage_page_scroller.scroll_horizontal % PAGE_WIDTH == 0: return
|
||||
|
||||
@@ -198,7 +233,9 @@ func _input(event: InputEvent) -> void:
|
||||
|
||||
func _ready() -> void:
|
||||
owniverse.init_enitities()
|
||||
_init_items()
|
||||
_init_owniverse_items()
|
||||
|
||||
g_counters.swipe.sec = 0.2
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
@@ -210,15 +247,6 @@ func _process(delta: float) -> void:
|
||||
_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
|
||||
@@ -237,6 +265,7 @@ func _change_speed(delta: int) -> void:
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user