Refactor state structure; encapsulate datetime and year within a time dictionary and update related logic
This commit is contained in:
+76
-5
@@ -264,9 +264,9 @@ func _page_action_pressed(page_id: int, action_amount: float) -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
var inited_result = owniverse.init_enitities()
|
var inited_result = init_enitities()
|
||||||
locked_items = inited_result.locked_items
|
locked_items = inited_result.locked_items
|
||||||
owniverse.load_data()
|
load_data()
|
||||||
_init_owniverse_items()
|
_init_owniverse_items()
|
||||||
|
|
||||||
g_counters.swipe.sec = 0.2
|
g_counters.swipe.sec = 0.2
|
||||||
@@ -279,7 +279,7 @@ func _ready() -> void:
|
|||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
|
|
||||||
owniverse.evolute(delta)
|
owniverse.evolute(delta)
|
||||||
set_datetime(owniverse.state.year)
|
set_datetime(owniverse.state.time.year)
|
||||||
|
|
||||||
#owniverse.cycle(delta)
|
#owniverse.cycle(delta)
|
||||||
|
|
||||||
@@ -324,6 +324,77 @@ func _on_topmenu_button_pressed(ButtonName) -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _on_unlock_and_save_timeout() -> void:
|
func _on_unlock_and_save_timeout() -> void:
|
||||||
owniverse.save_data()
|
save_data()
|
||||||
|
|
||||||
_unlock_items()
|
_unlock_items()
|
||||||
|
|
||||||
|
|
||||||
|
#region Init save load routines
|
||||||
|
|
||||||
|
|
||||||
|
func load_data() -> bool:
|
||||||
|
# Получаем версию приложения
|
||||||
|
var app_version = ProjectSettings.get_setting("application/config/version")
|
||||||
|
print("Версия приложения: ", app_version)
|
||||||
|
|
||||||
|
var game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
|
||||||
|
if game_data == {}: return false
|
||||||
|
#if game_data.get('version', '0.0.1') != 'app_version': return false
|
||||||
|
if game_data.get('version', str(app_version)) != '0.1.0':
|
||||||
|
print(game_data.get('version', 7))
|
||||||
|
print('wrong load version')
|
||||||
|
return false
|
||||||
|
|
||||||
|
owniverse.state = game_data["state"]
|
||||||
|
|
||||||
|
for item in owniverse.state.amount:
|
||||||
|
owniverse.items_to_update[item] = owniverse.state.amount[item]
|
||||||
|
return true
|
||||||
|
|
||||||
|
|
||||||
|
func save_data() -> void:
|
||||||
|
var game_data: Dictionary = {}
|
||||||
|
game_data["state"] = owniverse.state
|
||||||
|
|
||||||
|
SaveManager.save_data("owniverse", "qwerty", game_data)
|
||||||
|
print("saved ", game_data.version, "\n\n\n")
|
||||||
|
|
||||||
|
|
||||||
|
func init_enitities() -> Dictionary:
|
||||||
|
var _result := {
|
||||||
|
'locked_items': [],
|
||||||
|
}
|
||||||
|
for entity_id in Global.OwniverseEntities:
|
||||||
|
var entity = OwniverseEntity.new(Global.OwniverseEntities[entity_id])
|
||||||
|
owniverse.entities[entity_id] = entity
|
||||||
|
|
||||||
|
owniverse.state.stats.total_amount[entity_id] = 0
|
||||||
|
owniverse.state.amount[entity_id] = 0
|
||||||
|
if entity.lifespan > 0:
|
||||||
|
owniverse.state.batches[entity.id] = []
|
||||||
|
owniverse.add_entity_to_batch(entity_id, 0, 0)
|
||||||
|
if entity.group_as != "":
|
||||||
|
owniverse.state.amount[entity.group_as] = 0
|
||||||
|
|
||||||
|
owniverse.state.stats.total_amount[entity.group_as] = 0
|
||||||
|
|
||||||
|
if entity.unlock != {}:
|
||||||
|
owniverse.locked_entities.append(entity)
|
||||||
|
_result.locked_items.append(entity.id)
|
||||||
|
|
||||||
|
if entity.product_per_year_type != "":
|
||||||
|
owniverse.auto_production.append(entity)
|
||||||
|
|
||||||
|
# groups
|
||||||
|
if entity.group_as != "":
|
||||||
|
if !owniverse.entity_groups.has(entity.group_as):
|
||||||
|
owniverse.entity_groups[entity.group_as] = []
|
||||||
|
owniverse.entity_groups[entity.group_as].append(entity_id)
|
||||||
|
|
||||||
|
if entity.event_group != "":
|
||||||
|
if !owniverse.entity_event_groups.has(entity.event_group):
|
||||||
|
owniverse.entity_event_groups[entity.event_group] = []
|
||||||
|
owniverse.entity_event_groups[entity.event_group].append(entity_id)
|
||||||
|
|
||||||
|
return _result
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|||||||
+22
-107
@@ -20,8 +20,11 @@ var locked_entities: Array = []
|
|||||||
|
|
||||||
|
|
||||||
var state: Dictionary = {
|
var state: Dictionary = {
|
||||||
'datetime': 0.0,
|
'version': '0.1.0',
|
||||||
'year': 0.0,
|
'time': {
|
||||||
|
'datetime': 0.0,
|
||||||
|
'year': 0.0,
|
||||||
|
},
|
||||||
'speed': 1,
|
'speed': 1,
|
||||||
'speed_in_year': 1,
|
'speed_in_year': 1,
|
||||||
'energy': 1E14,
|
'energy': 1E14,
|
||||||
@@ -42,13 +45,16 @@ var speed: int:
|
|||||||
set(value):
|
set(value):
|
||||||
state.speed = clampi(value, 1, Global.SPEED_LIMIT)
|
state.speed = clampi(value, 1, Global.SPEED_LIMIT)
|
||||||
state.speed_in_years = pow(10, state.speed - 1)
|
state.speed_in_years = pow(10, state.speed - 1)
|
||||||
|
print(state.speed, " | ", state.speed_in_years)
|
||||||
|
|
||||||
|
|
||||||
func evolute(delta: float):
|
func evolute(delta: float):
|
||||||
|
|
||||||
_add_time_delta(delta)
|
var new_time: Dictionary = _add_time_delta(delta)
|
||||||
# produce and evolute
|
# produce and evolute
|
||||||
|
|
||||||
|
state.time = new_time
|
||||||
|
|
||||||
#new_owniverse.state = state
|
#new_owniverse.state = state
|
||||||
#new_owniverse.stats = stats
|
#new_owniverse.stats = stats
|
||||||
#new_owniverse.entities = entities
|
#new_owniverse.entities = entities
|
||||||
@@ -58,12 +64,16 @@ func evolute(delta: float):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
func _add_time_delta(delta: float):
|
func _add_time_delta(delta: float) -> Dictionary:
|
||||||
pass
|
var new_datetime = state.time.datetime + delta * 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)
|
#new_owniverse.state.year = floorf(new_owniverse.state.datetime)
|
||||||
|
var new_time:= {
|
||||||
|
'datetime': new_datetime,
|
||||||
|
'year': floorf(new_datetime),
|
||||||
|
}
|
||||||
|
#print(new_time)
|
||||||
|
return new_time
|
||||||
|
|
||||||
|
|
||||||
# ----------
|
# ----------
|
||||||
@@ -242,8 +252,8 @@ func consume_from_batches(entity: OwniverseEntity, current_year: float) -> float
|
|||||||
## @param entity_id - id of entity to add
|
## @param entity_id - id of entity to add
|
||||||
## @param amount - quantity of entity being added
|
## @param amount - quantity of entity being added
|
||||||
## @param current_year - current year in universe
|
## @param current_year - current year in universe
|
||||||
func add_entity_to_batch(entity: OwniverseEntity, amount: float, current_year: float) -> void:
|
func add_entity_to_batch(entity_id: String, amount: float, current_year: float) -> void:
|
||||||
var batches = state.batches[entity]
|
var batches = state.batches[entity_id]
|
||||||
|
|
||||||
# Check if we can add to last batch
|
# Check if we can add to last batch
|
||||||
var last_batch: Dictionary
|
var last_batch: Dictionary
|
||||||
@@ -255,7 +265,7 @@ func add_entity_to_batch(entity: OwniverseEntity, amount: float, current_year: f
|
|||||||
# Create new batch based on current_year
|
# Create new batch based on current_year
|
||||||
last_batch = {
|
last_batch = {
|
||||||
"start_year": current_year,
|
"start_year": current_year,
|
||||||
"end_year": current_year + entity.lifespan_delta,
|
"end_year": current_year + entities[entity_id].lifespan_delta,
|
||||||
"amount": amount
|
"amount": amount
|
||||||
}
|
}
|
||||||
batches.append(last_batch)
|
batches.append(last_batch)
|
||||||
@@ -308,6 +318,7 @@ func _process_enitity(entity_id: String, amount: float) -> void:
|
|||||||
|
|
||||||
# Update state amount
|
# Update state amount
|
||||||
state.amount[entity_id] = state.amount.get(entity_id, 0.0) + amount
|
state.amount[entity_id] = state.amount.get(entity_id, 0.0) + amount
|
||||||
|
state.stats.total_amount[entity_id] += amount
|
||||||
|
|
||||||
# Update items to display
|
# Update items to display
|
||||||
items_to_update[entity_id] = state.amount[entity_id]
|
items_to_update[entity_id] = state.amount[entity_id]
|
||||||
@@ -317,23 +328,9 @@ func _process_enitity(entity_id: String, amount: float) -> void:
|
|||||||
if entity.group_as != "":
|
if entity.group_as != "":
|
||||||
state.amount[entity.group_as] = state.amount.get(entity.group_as, 0.0) + amount
|
state.amount[entity.group_as] = state.amount.get(entity.group_as, 0.0) + amount
|
||||||
items_to_update[entity.group_as] = state.amount[entity.group_as]
|
items_to_update[entity.group_as] = state.amount[entity.group_as]
|
||||||
|
state.stats.total_amount[entity.group_as] += 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]
|
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -342,85 +339,3 @@ func _process_enitity(entity_id: String, amount: float) -> void:
|
|||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Init save load routines
|
|
||||||
|
|
||||||
|
|
||||||
func load_data() -> bool:
|
|
||||||
var game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
|
|
||||||
if game_data == {}:
|
|
||||||
return false
|
|
||||||
|
|
||||||
if !game_data.has_all(["state"
|
|
||||||
, "stats"
|
|
||||||
#, "entity_batches"
|
|
||||||
#, "year",
|
|
||||||
]):
|
|
||||||
return false
|
|
||||||
|
|
||||||
#entity_batches = game_data["entity_batches"]
|
|
||||||
#cumulative_amount = _game_data["cumulative_amount"]
|
|
||||||
#current_amount = _game_data["current_amount"]
|
|
||||||
#print("loaded ", _game_data)
|
|
||||||
state = game_data["state"]
|
|
||||||
#state2 = game_data["state2"]
|
|
||||||
|
|
||||||
for item in state.amount:
|
|
||||||
items_to_update[item] = state.amount[item]
|
|
||||||
#print("state: ", state)
|
|
||||||
#print("stats: ", stats)
|
|
||||||
return true
|
|
||||||
|
|
||||||
|
|
||||||
func save_data() -> void:
|
|
||||||
var game_data: Dictionary = {}
|
|
||||||
game_data["state"] = state
|
|
||||||
|
|
||||||
SaveManager.save_data("owniverse", "qwerty", game_data)
|
|
||||||
#print("saved ", game_data, "\n\n\n")
|
|
||||||
|
|
||||||
|
|
||||||
func init_enitities() -> Dictionary:
|
|
||||||
var _result := {
|
|
||||||
'locked_items': [],
|
|
||||||
}
|
|
||||||
#var _auto_production := []
|
|
||||||
for entity_id in Global.OwniverseEntities:
|
|
||||||
var entity = OwniverseEntity.new(Global.OwniverseEntities[entity_id])
|
|
||||||
entities[entity_id] = entity
|
|
||||||
|
|
||||||
#current_amount[entity_id] = 0
|
|
||||||
state.stats.total_amount[entity_id] = 0
|
|
||||||
state.amount[entity_id] = 0
|
|
||||||
if entity.lifespan > 0:
|
|
||||||
state.batches[entity] = []
|
|
||||||
add_entity_to_batch(entity, 0, 0)
|
|
||||||
if entity.group_as != "":
|
|
||||||
#current_amount[entity.group_as] = 0
|
|
||||||
state.amount[entity.group_as] = 0
|
|
||||||
|
|
||||||
state.stats.total_amount[entity.group_as] = 0
|
|
||||||
|
|
||||||
if entity.unlock != {}:
|
|
||||||
locked_entities.append(entity)
|
|
||||||
_result.locked_items.append(entity.id)
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
#print("ns: ", new_state)
|
|
||||||
return _result
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|||||||
Reference in New Issue
Block a user