Refactor owniverse state management; replace evolution class with state handling and update cycle logic
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
extends Node
|
||||
class_name Owniverse
|
||||
|
||||
var evolution: OwniverseEvolution = OwniverseEvolution.new()
|
||||
#var state: State = State.new()
|
||||
|
||||
signal unlock_item(item_id: String)
|
||||
|
||||
var current_year: float = 0.0
|
||||
var current_speed: int = 1
|
||||
var current_speed_in_years: float = 1.0
|
||||
|
||||
var entities: Dictionary[String, OwniverseEntity] = {}
|
||||
var entity_groups: Dictionary[String, Array] = {}
|
||||
var entity_event_groups: Dictionary[String, Array] = {}
|
||||
@@ -22,18 +18,63 @@ var cumulative_amount: Dictionary[String, float] = {}
|
||||
var current_amount: Dictionary[String, float] = {}
|
||||
|
||||
|
||||
func cycle(year_delta: float) -> void:
|
||||
# ----
|
||||
|
||||
var state: Dictionary = {
|
||||
'datetime': 0.0,
|
||||
'year': 0.0,
|
||||
'speed': 1,
|
||||
'speed_in_year': 1,
|
||||
'energy': 1E14,
|
||||
}
|
||||
|
||||
var speed: int:
|
||||
get:
|
||||
return state.speed
|
||||
set(value):
|
||||
state.speed = clampi(value, 1, Global.SPEED_LIMIT)
|
||||
state.speed_in_years = pow(10, state.speed - 1)
|
||||
|
||||
|
||||
func evolute(delta: float) -> Owniverse:
|
||||
var new_owniverse := Owniverse.new()
|
||||
_add_time_delta(new_owniverse, delta)
|
||||
|
||||
return new_owniverse
|
||||
|
||||
|
||||
func _add_time_delta(new_owniverse: Owniverse, delta: float):
|
||||
new_owniverse.state.speed_in_year = state.speed_in_year
|
||||
new_owniverse.state.datetime = state.datetime \
|
||||
+ delta * new_owniverse.state.speed_in_year
|
||||
|
||||
new_owniverse.state.year = floorf(new_owniverse.state.datetime)
|
||||
|
||||
|
||||
|
||||
# ----------
|
||||
|
||||
func cycle(delta: float):
|
||||
var new_state:State = State.new()
|
||||
#new_state.evolute(state, delta, current_speed_in_years)
|
||||
|
||||
|
||||
# TODO review
|
||||
|
||||
func cycle_legacy(year_delta: float) -> void:
|
||||
# all owniverse activity should be called here
|
||||
_auto_production(year_delta)
|
||||
_produce_automatically(year_delta)
|
||||
|
||||
_check_locked_items()
|
||||
|
||||
evolution.update_distribution(current_amount)
|
||||
#state.update_distribution(current_amount)
|
||||
|
||||
|
||||
|
||||
|
||||
#region auto production and evolution
|
||||
|
||||
func _auto_production(year_delta: float) -> void:
|
||||
func _produce_automatically(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:
|
||||
@@ -75,7 +116,7 @@ func produce(item_id: String, amount: float) -> void:
|
||||
for elem in entity_transaction:
|
||||
_process_enitity(elem, entity_transaction[elem])
|
||||
_process_enitity(item_id, amount)
|
||||
evolution.add(_entity, current_year, amount)
|
||||
#state.add(_entity, current_year, amount)
|
||||
|
||||
|
||||
func automated_production(_delta: float) -> void:
|
||||
@@ -128,16 +169,6 @@ func _check_locked_items() -> void:
|
||||
|
||||
#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
|
||||
|
||||
@@ -146,35 +177,40 @@ func change_speed(delta: int) -> int:
|
||||
|
||||
|
||||
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"
|
||||
if !game_data.has_all(["cumulative_amount"
|
||||
, "current_amount"
|
||||
#, "entity_batches"
|
||||
, "year",
|
||||
#, "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"]
|
||||
state = game_data["state"]
|
||||
|
||||
|
||||
print(state)
|
||||
|
||||
for item in current_amount:
|
||||
items_to_update[item] = current_amount[item]
|
||||
|
||||
print("loaded")
|
||||
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["state"] = state
|
||||
|
||||
#game_data["entity_batches"] = entity_batches
|
||||
SaveManager.save_data("owniverse", "qwerty", game_data)
|
||||
print("saved: ", game_data)
|
||||
|
||||
|
||||
func init_enitities() -> void:
|
||||
@@ -192,7 +228,7 @@ func init_enitities() -> void:
|
||||
if _entity.unlock != {}:
|
||||
locked_entities.append(_entity)
|
||||
|
||||
evolution.init_entity(_entity)
|
||||
#state.init_entity(_entity)
|
||||
|
||||
if _entity.product_per_year_type != "":
|
||||
auto_production.append(_entity)
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
extends Node
|
||||
|
||||
class_name OwniverseEvolution
|
||||
class_name State
|
||||
|
||||
const FLOAT_THRESHOLD: float = 1.0E14
|
||||
# TODO to save !!!
|
||||
var state: Dictionary = {
|
||||
|
||||
var data: Dictionary = {
|
||||
'time': {
|
||||
'timestamp': 0.0,
|
||||
'year': 0.0,
|
||||
},
|
||||
'timestamp': 0.0,
|
||||
'year': 0.0,
|
||||
'speed_in_year': 0,
|
||||
'energy': 1E14,
|
||||
}
|
||||
|
||||
@@ -25,12 +31,32 @@ var evolving_entities: Array[OwniverseEntity] = []
|
||||
# TODO to save !!!
|
||||
var batches: Dictionary[String, Array] = {}
|
||||
# TODO to save !!!
|
||||
var amount: Dictionary[String, float] = {}
|
||||
var gamount: Dictionary[String, float] = {}
|
||||
|
||||
|
||||
var distribution: Dictionary[String, float] = {}
|
||||
|
||||
|
||||
# new functions
|
||||
func evolute(prev_state: State, delta: float, current_speed_in_year: float) -> float:
|
||||
var _new_year: float = _add_year_delta(delta, current_speed_in_year)
|
||||
if data.year == _new_year:
|
||||
return data.year
|
||||
|
||||
return _new_year
|
||||
|
||||
|
||||
func _add_year_delta(delta: float, current_speed_in_year: float) -> float:
|
||||
var _new_year:float = floorf(data.year + delta * current_speed_in_year)
|
||||
#print(data.year, " | ", _new_year, " | ", data)
|
||||
return _new_year
|
||||
|
||||
|
||||
|
||||
# end of new functions
|
||||
|
||||
|
||||
|
||||
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
|
||||
@@ -106,11 +132,11 @@ func produce(from_year: float, to_year: float):
|
||||
|
||||
func produce_hs(entity_id: String, value: float) -> float:
|
||||
var _to_produce: float = floorf(value)
|
||||
amount[entity_id] += _to_produce
|
||||
gamount[entity_id] += _to_produce
|
||||
# TODO double double math
|
||||
if _to_produce > state["energy"]:
|
||||
_to_produce = state["energy"]
|
||||
state["energy"] -= _to_produce
|
||||
if _to_produce > data["energy"]:
|
||||
_to_produce = data["energy"]
|
||||
data["energy"] -= _to_produce
|
||||
return value - _to_produce
|
||||
|
||||
func produce_auto() -> float:
|
||||
@@ -118,7 +144,7 @@ func produce_auto() -> float:
|
||||
|
||||
func init_entity(entity: OwniverseEntity) -> void:
|
||||
entities[entity.id] = entity
|
||||
amount[entity.id] = 0.0
|
||||
gamount[entity.id] = 0.0
|
||||
if entity.product_per_year_type != "":
|
||||
producing_entities.append(entity)
|
||||
|
||||
Reference in New Issue
Block a user