215 lines
6.2 KiB
GDScript
215 lines
6.2 KiB
GDScript
extends Node
|
|
class_name Owniverse
|
|
|
|
signal unlock_item(item_id: String)
|
|
|
|
var current_year: float = 0
|
|
var current_speed: int = 1
|
|
var current_speed_in_years:int = 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()
|
|
|
|
#print(_get_distribution(current_amount))
|
|
|
|
|
|
#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 _entity_evolution(year_delta: float) -> void:
|
|
pass
|
|
|
|
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)
|
|
|
|
|
|
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 = clamp(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"]):
|
|
return false
|
|
|
|
entity_batches = game_data["entity_batches"]
|
|
cumulative_amount = game_data["cumulative_amount"]
|
|
current_amount = game_data["current_amount"]
|
|
|
|
for item in current_amount:
|
|
items_to_update[item] = current_amount[item]
|
|
|
|
return true
|
|
|
|
|
|
func save_data() -> void:
|
|
var game_data: Dictionary = {}
|
|
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:
|
|
for entity_id in Global.OwniverseEntities:
|
|
var entity = OwniverseEntity.new()
|
|
entity.init_entity(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)
|
|
|
|
if entity.product_per_year_type != "":
|
|
auto_production.append(entity)
|
|
|
|
# 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)
|
|
|
|
#if entity.event_group != "":
|
|
#entity_event_groups.append(entity.id)
|
|
print(entity_groups)
|
|
print(entity_event_groups)
|
|
#endregion
|