123 lines
3.5 KiB
GDScript
123 lines
3.5 KiB
GDScript
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
|
|
|
|
var entities: Dictionary[String, OwniverseEntity] = {}
|
|
|
|
var items_to_update: Dictionary[String, float] = {}
|
|
|
|
var cumulative_amount: Dictionary[String, float] = {}
|
|
var current_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: 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)
|
|
#print(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)
|
|
items_to_update[entity_id] = current_amount[entity_id]
|
|
|
|
|
|
func _count_amount(entity_id: String, amount: float) -> void:
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
|
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)
|
|
|
|
|
|
func add_time_delta(delta) -> float:
|
|
var year_delta: float = delta * g_speed_in_years
|
|
|
|
# all ownivewrse activity should be called here
|
|
|
|
g_datetime += year_delta
|
|
return g_datetime
|
|
|
|
|
|
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)
|
|
return g_speed
|