Refactor code structure for improved readability and maintainability

This commit is contained in:
Kirill
2026-05-26 23:21:44 +03:00
parent 3bab036cc4
commit edb5ec9dfa
28 changed files with 238 additions and 426 deletions
+49 -12
View File
@@ -1,6 +1,8 @@
extends Node
class_name Owniverse
signal unlock_item(item_id: String)
var g_datetime: float = 0
var g_speed: int = 1
var g_speed_in_years = 1
@@ -11,31 +13,57 @@ var items_to_update: Dictionary[String, float] = {}
var cumulative_amount: Dictionary[String, float] = {}
var auto_production: Array = []
var locked_entities: Array = []
#region Production
func cycle(delta: float) -> void:
_check_locked_items()
func produce(item_id: String, amount: int) -> void:
if item_id == "S" or item_id == 'H':
entities[item_id].amount += 1
var entity: OwniverseEntity = entities[item_id]
_process_enitity(entity, amount)
_process_enitity(item_id, amount)
func automated_production() -> void:
pass
func automated_production(_delta: float) -> void:
for entity_id in auto_production:
var entity = entities[entity_id]
var amount_to_produce = int(entity.amount * entity.product_per_year_amount * _delta)
# add to all lists
func _process_enitity(item_id: String, amount: int) -> void:
items_to_update[item_id] = entities[item_id].amount
func _process_enitity(entity: OwniverseEntity, amount: int) -> void:
_count_cumulative(entity, amount)
items_to_update[entity.id] = entity.amount
func _count_cumulative(item_id: String, amount: int) -> void:
if cumulative_amount.has(item_id):
cumulative_amount[item_id] += amount
else:
cumulative_amount[item_id] = amount
func _count_cumulative(entity: OwniverseEntity, amount: int) -> void:
cumulative_amount[entity.id] += amount
if entity.group_as != "":
cumulative_amount[entity.group_as] += amount
func _check_locked_items() -> void:
for entity:OwniverseEntity in locked_entities:
for elem in entity.unlock:
if cumulative_amount[elem] < entity.unlock[elem]:
continue
print(entity.id, " ", elem, ":", entity.unlock[elem])
unlock_item.emit(entity.id)
locked_entities.erase(entity)
# unlock here
return
#endregion
@@ -44,10 +72,19 @@ func init_enitities() -> void:
var entity = OwniverseEntity.new()
entity.init_entity(Global.OwniverseEntities[entity_id])
entities[entity_id] = entity
cumulative_amount[entity_id] = 0
cumulative_amount[entity.group_as] = 0
if entity.unlock != {}:
locked_entities.append(entity)
if entity.product_per_year_type != "":
auto_production.append(entity)
func add_time_delta(delta) -> float:
var year_delta:float = delta * g_speed_in_years
var year_delta: float = delta * g_speed_in_years
# all ownivewrse activity should be called here
@@ -57,5 +94,5 @@ func add_time_delta(delta) -> float:
func change_speed(delta: int) -> int:
g_speed = clamp(g_speed + delta, 1, Global.SPEED_LIMIT)
g_speed_in_years = pow(10, g_speed-1)
g_speed_in_years = pow(10, g_speed - 1)
return g_speed