94 lines
3.0 KiB
GDScript
94 lines
3.0 KiB
GDScript
extends Node
|
|
|
|
class_name OwniverseEvolution
|
|
|
|
var entities: Dictionary[String, OwniverseEntity] = {}
|
|
var producing_entities: Array[OwniverseEntity] = []
|
|
var evolving_entities: Array[OwniverseEntity] = []
|
|
|
|
var batches: Dictionary[String, Array] = {}
|
|
|
|
var distribution: Dictionary[String, float] = {}
|
|
|
|
|
|
func add(entity: OwniverseEntity, year: float, amount: float):
|
|
var _batch_array:Array = batches[entity.id]
|
|
var _from_year: float = year + entity.lifespan - entity.lifespan_delta
|
|
var _to_year: float = year + entity.lifespan + entity.lifespan_delta
|
|
if _batch_array.size() != 0 and _batch_array[-1].to_year > year + entity.lifespan:
|
|
_batch_array[-1].amount += amount
|
|
else:
|
|
var _batch := {
|
|
'from_year': _from_year,
|
|
'to_year': _to_year,
|
|
'amount': amount,
|
|
}
|
|
_batch_array.append(_batch)
|
|
|
|
|
|
func process(from_year: float, to_year: float) -> void:
|
|
# производим
|
|
# эволюционируем
|
|
# записываем новые
|
|
var _to_produce:= {}
|
|
var _to_evolute:= {}
|
|
for _entity_id in batches:
|
|
_to_produce[_entity_id] = 0
|
|
_to_evolute[_entity_id] = 0
|
|
var _new_batch_array:= []
|
|
for _batch: Dictionary in batches[_entity_id]:
|
|
# production
|
|
_to_produce[_entity_id] += _batch.amount
|
|
# evolution
|
|
if _batch.to_year < to_year:
|
|
_to_produce[_entity_id] += _batch.amount
|
|
else:
|
|
var _max_from_year: float = max(from_year, _batch.from_year)
|
|
var _min_to_year: float = min(to_year, _batch.to_year)
|
|
var _part_of_amount: float = _batch.amount \
|
|
* (_min_to_year - _max_from_year) \
|
|
/ (_batch.to_year - _batch.from_year)
|
|
_to_evolute[_entity_id] += _part_of_amount
|
|
var _rest_of_amount: float = _batch.amount - _part_of_amount
|
|
|
|
if _rest_of_amount > 0:
|
|
_new_batch_array.append({
|
|
'from_year': _batch._from_year,
|
|
'to_year': _batch._to_year,
|
|
'amount': _rest_of_amount,
|
|
})
|
|
|
|
# entity level
|
|
batches[_entity_id] = _new_batch_array
|
|
|
|
|
|
func produce(from_year: float, to_year: float):
|
|
for batch_name in batches:
|
|
pass
|
|
|
|
|
|
func init_entity(entity: OwniverseEntity) -> void:
|
|
entities[entity.id] = entity
|
|
if entity.product_per_year_type != "":
|
|
producing_entities.append(entity)
|
|
|
|
if entity.lifespan > 0:
|
|
evolving_entities.append(entity)
|
|
|
|
batches[entity.id] = []
|
|
|
|
|
|
func update_distribution(_distribute_by: Dictionary[String, float]) -> void:
|
|
distribution = {}
|
|
var _total: float = 0
|
|
for id in _distribute_by:
|
|
if _distribute_by[id] != 0:
|
|
var _value: float = _distribute_by[id]
|
|
_total += _value
|
|
distribution[id] = _value
|
|
|
|
if _total == 0: return
|
|
|
|
for id in distribution:
|
|
distribution[id] /= _total
|