Refactor owniverse state management; replace evolution class with state handling and update cycle logic

This commit is contained in:
Kirill
2026-08-25 00:26:31 +03:00
parent 829aadca2d
commit 23929e3651
5 changed files with 141 additions and 73 deletions
+10 -4
View File
@@ -244,9 +244,12 @@ func _ready() -> void:
func _process(delta: float) -> void:
var year_delta: float = owniverse.add_time_delta(delta)
set_datetime(year_delta)
owniverse.cycle(year_delta)
owniverse = owniverse.evolute(delta)
set_datetime(owniverse.state.year)
owniverse.cycle(delta)
#owniverse.cycle_legacy(year_delta)
var actions = _check_counters(delta)
_do_actions(actions)
@@ -274,7 +277,10 @@ func _page_action_pressed(page_id: int, action_amount: float) -> void:
func _change_speed(delta: int) -> void:
var speed: int = owniverse.change_speed(delta)
owniverse.speed = owniverse.speed + delta
var speed: int = owniverse.speed
var ind: int = 1
for icon in g_speed_icons:
if ind <= speed:
+63 -27
View File
@@ -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)
+33 -33
View File
@@ -3,45 +3,45 @@
[ext_resource type="Script" uid="uid://d2wrwichuoncd" path="res://world/background/background.gd" id="1_ifjjt"]
[ext_resource type="PackedScene" uid="uid://icbk3la71t7h" path="res://world/background/starry_sky_layer.tscn" id="2_behgl"]
[ext_resource type="Script" uid="uid://iiva6x8uo0f2" path="res://stage/stage.gd" id="3_k83x5"]
[ext_resource type="Texture2D" path="res://ui/topmenu/buttons/button.burger.normal.png" id="4_b8tmp"]
[ext_resource type="Texture2D" path="res://ui/topmenu/buttons/button.burger.pressed.png" id="5_en1oy"]
[ext_resource type="Texture2D" path="res://ui/topmenu/time&speed/timex.png" id="6_pfbnl"]
[ext_resource type="Texture2D" uid="uid://dl7xbd4eltxs1" path="res://ui/topmenu/buttons/button.burger.normal.png" id="4_b8tmp"]
[ext_resource type="Texture2D" uid="uid://c3odgsgxm8x14" path="res://ui/topmenu/buttons/button.burger.pressed.png" id="5_en1oy"]
[ext_resource type="Texture2D" uid="uid://bbhvxbpta4yke" path="res://ui/topmenu/time&speed/timex.png" id="6_pfbnl"]
[ext_resource type="FontFile" uid="uid://dtyi81a0phumr" path="res://fonts/Roboto_Condensed/RobotoCondensed-Bold.ttf" id="7_gwdna"]
[ext_resource type="Texture2D" path="res://ui/topmenu/time&speed/speed.png" id="8_h4vgh"]
[ext_resource type="Texture2D" path="res://ui/topmenu/time&speed/time1.png" id="9_kvuv0"]
[ext_resource type="Texture2D" path="res://ui/topmenu/buttons/buton.report.normal.png" id="10_sg5c8"]
[ext_resource type="Texture2D" path="res://ui/topmenu/buttons/buton.report.pressed.png" id="11_vu62d"]
[ext_resource type="Texture2D" uid="uid://cgrpmgnmyycyg" path="res://ui/topmenu/time&speed/speed.png" id="8_h4vgh"]
[ext_resource type="Texture2D" uid="uid://bd0poiwetepki" path="res://ui/topmenu/time&speed/time1.png" id="9_kvuv0"]
[ext_resource type="Texture2D" uid="uid://d2px6au7uyr6w" path="res://ui/topmenu/buttons/buton.report.normal.png" id="10_sg5c8"]
[ext_resource type="Texture2D" uid="uid://dgh4rx0nb4wgi" path="res://ui/topmenu/buttons/buton.report.pressed.png" id="11_vu62d"]
[ext_resource type="PackedScene" uid="uid://dg15dpxiepicb" path="res://ui/border/border.tscn" id="12_58wjt"]
[ext_resource type="Texture2D" path="res://ui/pages/forces.en.png" id="13_dp8ul"]
[ext_resource type="Texture2D" uid="uid://btq70xgqq14od" path="res://ui/pages/forces.en.png" id="13_dp8ul"]
[ext_resource type="FontFile" uid="uid://d3k1ddvv0rmhs" path="res://fonts/Roboto_Condensed/RobotoCondensed-Light.ttf" id="14_o7xkw"]
[ext_resource type="FontFile" uid="uid://8s0fv3gfj317" path="res://fonts/Roboto_Condensed/RobotoCondensed-BoldItalic.ttf" id="15_bfrib"]
[ext_resource type="FontFile" uid="uid://derqj4pbwtae6" path="res://fonts/Roboto_Condensed/RobotoCondensed-Italic.ttf" id="16_mip0j"]
[ext_resource type="PackedScene" uid="uid://dwneq2qq5p8ek" path="res://ui/actions/actions.tscn" id="17_ifjjt"]
[ext_resource type="Texture2D" path="res://ui/indicators/energy/energy.png" id="17_l6135"]
[ext_resource type="Texture2D" path="res://ui/indicators/energy/border-energy.png" id="18_uvuuk"]
[ext_resource type="Texture2D" path="res://ui/pages/resources.en.png" id="19_an6vj"]
[ext_resource type="Texture2D" path="res://ui/indicators/density/density.png" id="20_s3ar6"]
[ext_resource type="Texture2D" path="res://ui/indicators/density/density_crunch.png" id="21_d36xh"]
[ext_resource type="Texture2D" path="res://ui/indicators/density/density_rip.png" id="22_vu83j"]
[ext_resource type="Texture2D" path="res://ui/indicators/density/density_pointer.png" id="23_bcq5b"]
[ext_resource type="Texture2D" path="res://ui/indicators/burnability/burnability.png" id="24_o2i0n"]
[ext_resource type="Texture2D" path="res://ui/indicators/burnability/burnability_pointer.png" id="25_rur7c"]
[ext_resource type="Texture2D" path="res://ui/pages/stars.en.png" id="26_y1rwn"]
[ext_resource type="Texture2D" path="res://ui/indicators/explosiveness/explosiveness.png" id="27_4rajv"]
[ext_resource type="Texture2D" path="res://ui/indicators/explosiveness/explosiveness_pointer.png" id="28_upe80"]
[ext_resource type="Texture2D" path="res://ui/pages/structures.en.png" id="29_0drd1"]
[ext_resource type="Texture2D" path="res://ui/indicators/aggression/aggression.png" id="30_bemti"]
[ext_resource type="Texture2D" path="res://ui/indicators/aggression/aggression_pointer.png" id="31_x6tg5"]
[ext_resource type="Texture2D" path="res://ui/pages/planets.en.png" id="32_xitqn"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_bar1.png" id="33_fprte"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_bar2.png" id="34_lnsl1"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_icon_2.png" id="35_1u5dl"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_bar3.png" id="36_v15xm"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_icon_3.png" id="37_vjhti"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_bar4.png" id="38_ctwlg"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_icon_4.png" id="39_jms70"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_bar5.png" id="40_50rdj"]
[ext_resource type="Texture2D" path="res://ui/indicators/trends/trend_icon_5.png" id="41_mi6gc"]
[ext_resource type="Texture2D" uid="uid://rqk6n3lcxi32" path="res://ui/indicators/energy/energy.png" id="17_l6135"]
[ext_resource type="Texture2D" uid="uid://bef36h52n7lxb" path="res://ui/indicators/energy/border-energy.png" id="18_uvuuk"]
[ext_resource type="Texture2D" uid="uid://brl8ojsykr5ki" path="res://ui/pages/resources.en.png" id="19_an6vj"]
[ext_resource type="Texture2D" uid="uid://d3nhbp7wvctvf" path="res://ui/indicators/density/density.png" id="20_s3ar6"]
[ext_resource type="Texture2D" uid="uid://df2nus1pmcc67" path="res://ui/indicators/density/density_crunch.png" id="21_d36xh"]
[ext_resource type="Texture2D" uid="uid://bbk74pjqgxe32" path="res://ui/indicators/density/density_rip.png" id="22_vu83j"]
[ext_resource type="Texture2D" uid="uid://bm8ae1ffv7xq5" path="res://ui/indicators/density/density_pointer.png" id="23_bcq5b"]
[ext_resource type="Texture2D" uid="uid://c3icljpbnj4fo" path="res://ui/indicators/burnability/burnability.png" id="24_o2i0n"]
[ext_resource type="Texture2D" uid="uid://sry5js57vds4" path="res://ui/indicators/burnability/burnability_pointer.png" id="25_rur7c"]
[ext_resource type="Texture2D" uid="uid://bv68o2unxtljh" path="res://ui/pages/stars.en.png" id="26_y1rwn"]
[ext_resource type="Texture2D" uid="uid://brb3hw23smntm" path="res://ui/indicators/explosiveness/explosiveness.png" id="27_4rajv"]
[ext_resource type="Texture2D" uid="uid://cno3ktmfxdiig" path="res://ui/indicators/explosiveness/explosiveness_pointer.png" id="28_upe80"]
[ext_resource type="Texture2D" uid="uid://coqcg8fo4lttb" path="res://ui/pages/structures.en.png" id="29_0drd1"]
[ext_resource type="Texture2D" uid="uid://dxrnqx16lvca3" path="res://ui/indicators/aggression/aggression.png" id="30_bemti"]
[ext_resource type="Texture2D" uid="uid://dm87u2ku8wec8" path="res://ui/indicators/aggression/aggression_pointer.png" id="31_x6tg5"]
[ext_resource type="Texture2D" uid="uid://gkr4uuva1dmt" path="res://ui/pages/planets.en.png" id="32_xitqn"]
[ext_resource type="Texture2D" uid="uid://bgjcnwl61nu5i" path="res://ui/indicators/trends/trend_bar1.png" id="33_fprte"]
[ext_resource type="Texture2D" uid="uid://caiyhk6vv53iu" path="res://ui/indicators/trends/trend_bar2.png" id="34_lnsl1"]
[ext_resource type="Texture2D" uid="uid://dspjxpbpwq52k" path="res://ui/indicators/trends/trend_icon_2.png" id="35_1u5dl"]
[ext_resource type="Texture2D" uid="uid://dxutg6k0ck226" path="res://ui/indicators/trends/trend_bar3.png" id="36_v15xm"]
[ext_resource type="Texture2D" uid="uid://bp72c0myjnl61" path="res://ui/indicators/trends/trend_icon_3.png" id="37_vjhti"]
[ext_resource type="Texture2D" uid="uid://qrjhpvw8ou3o" path="res://ui/indicators/trends/trend_bar4.png" id="38_ctwlg"]
[ext_resource type="Texture2D" uid="uid://bwqmmmg6w7dho" path="res://ui/indicators/trends/trend_icon_4.png" id="39_jms70"]
[ext_resource type="Texture2D" uid="uid://4tpbmpe6tbk0" path="res://ui/indicators/trends/trend_bar5.png" id="40_50rdj"]
[ext_resource type="Texture2D" uid="uid://cnf3ml5gj2cvl" path="res://ui/indicators/trends/trend_icon_5.png" id="41_mi6gc"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_ifjjt"]