Files
owniverse/world/owniverse/owniverse.gd
T
2026-08-02 22:57:58 +03:00

224 lines
6.6 KiB
GDScript

extends Node
class_name Owniverse
var evolution: OwniverseEvolution = OwniverseEvolution.new()
signal unlock_item(item_id: String)
var current_year: float = 0
var current_speed: int = 1
var current_speed_in_years: float = 1
var entities: Dictionary[String, OwniverseEntity] = {}
var entity_groups: Dictionary[String, Array] = {}
var entity_event_groups: Dictionary[String, Array] = {}
var items_to_update: Dictionary[String, float] = {}
var auto_production: Array = []
var locked_entities: Array = []
var cumulative_amount: Dictionary[String, float] = {}
var current_amount: Dictionary[String, float] = {}
#var batch_count: int = 40
#var entity_batches = {}
func cycle(year_delta: float) -> void:
# all owniverse activity should be called here
_auto_production(year_delta)
#_entity_evolution(year_delta)
_check_locked_items()
#var v = _get_distribution(current_amount)
evolution.update_distribution(current_amount)
#for k in current_amount:
#if current_amount[k] != 0:
#print(k, "|", current_amount[k])
#region auto production and evolution
func _auto_production(year_delta: float) -> void:
var _cycle_production = {"S": 0.0, "H": 0.0}
for entity: OwniverseEntity in auto_production:
if current_amount[entity.id] == 0:
continue
_cycle_production[entity.product_per_year_type] += \
current_amount[entity.id] * entity.product_per_year_amount * year_delta
for item in _cycle_production:
_count_amount(item, _cycle_production[item])
func _get_distribution(source: Dictionary) -> Dictionary:
var distribution: Dictionary = {}
var total: float = 0
for id in source:
if source[id] != 0:
total += source[id]
distribution[id] = source[id]
for id in distribution:
distribution[id] /= total
return distribution
#endregion
#region Production
func produce(item_id: String, amount: float) -> void:
var _entity: OwniverseEntity = entities[item_id]
var entity_transaction = {}
for elem in _entity.cost:
if current_amount[elem] < amount * _entity.cost[elem]:
return
if _entity.cost[elem] == 0: continue
entity_transaction[elem] = - amount * _entity.cost[elem]
for elem in entity_transaction:
_process_enitity(elem, entity_transaction[elem])
_process_enitity(item_id, amount)
evolution.add(_entity, current_year, amount)
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)
func get_available_to_produce(item_id: String) -> float:
var entity: OwniverseEntity = entities[item_id]
if entity.cost == {}: return 0
var amount: float = 1E50
for elem in entity.cost:
amount = min(floorf(current_amount[elem] / entity.cost[elem]), amount)
return amount
# add to all lists
func _process_enitity(entity_id: String, amount: float) -> void:
if amount == 0: return
_count_amount(entity_id, amount)
func _count_amount(entity_id: String, amount: float) -> void:
if amount == 0: return
current_amount[entity_id] += amount
var entity: OwniverseEntity = entities[entity_id]
if entity.group_as != "":
current_amount[entity.group_as] += amount
if amount < 0: return
cumulative_amount[entity_id] += amount
if entity.group_as != "":
cumulative_amount[entity.group_as] += amount
items_to_update[entity_id] = current_amount[entity_id]
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)
return # SIC!
#endregion
#region Time
func add_time_delta(delta) -> float:
var year_delta: float = delta * current_speed_in_years
current_year += year_delta
return current_year
func change_speed(delta: int) -> int:
current_speed = clampi(current_speed + delta, 1, Global.SPEED_LIMIT)
current_speed_in_years = pow(10, current_speed - 1)
return current_speed
#endregion
#region Init save load routines
func load_data() -> bool:
#var _is_succesfull = false
var game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
if game_data == {}:
return false
if !game_data.has_all(["cumulative_amount", "current_amount"
#, "entity_batches"
, "year",
]):
return false
#entity_batches = game_data["entity_batches"]
cumulative_amount = game_data["cumulative_amount"]
current_amount = game_data["current_amount"]
current_year = game_data["year"]
for item in current_amount:
items_to_update[item] = current_amount[item]
return true
func save_data() -> void:
var game_data: Dictionary = {}
game_data["year"] = current_year
game_data["cumulative_amount"] = cumulative_amount
game_data["current_amount"] = current_amount
#game_data["entity_batches"] = entity_batches
SaveManager.save_data("owniverse", "qwerty", game_data)
func init_enitities() -> void:
var _auto_production := []
for _entity_id in Global.OwniverseEntities:
var _entity = OwniverseEntity.new(Global.OwniverseEntities[_entity_id])
entities[_entity_id] = _entity
current_amount[_entity_id] = 0
cumulative_amount[_entity_id] = 0
if _entity.group_as != "":
current_amount[_entity.group_as] = 0
cumulative_amount[_entity.group_as] = 0
if _entity.unlock != {}:
locked_entities.append(_entity)
evolution.init_entity(_entity)
if _entity.product_per_year_type != "":
auto_production.append(_entity)
_auto_production.append(_entity.id)
# groups
if _entity.group_as != "":
if !entity_groups.has(_entity.group_as):
entity_groups[_entity.group_as] = []
entity_groups[_entity.group_as].append(_entity_id)
if _entity.event_group != "":
if !entity_event_groups.has(_entity.event_group):
entity_event_groups[_entity.event_group] = []
entity_event_groups[_entity.event_group].append(_entity_id)
#evolution.init_batches(_auto_production)
#endregion