Minor improvements
This commit is contained in:
@@ -130,18 +130,19 @@ func get_total_currency_acquired(currency: Resource) -> BigNumber:
|
||||
func get_total_currency_acquired_by_id(currency_id: StringName) -> BigNumber:
|
||||
return _get_total_currency_ref(currency_id)
|
||||
|
||||
func register_generator(generator_id: StringName, unlocked: bool = false, available: bool = false) -> void:
|
||||
func register_generator(generator_id: StringName, unlocked: bool = false, available: bool = false, owned: int = 0) -> void:
|
||||
var key: String = String(generator_id)
|
||||
if key.is_empty():
|
||||
return
|
||||
var default_owned: int = _to_non_negative_int(owned, 0)
|
||||
|
||||
if generator_states.has(key):
|
||||
var existing_raw: Variant = generator_states.get(key)
|
||||
if existing_raw is Dictionary:
|
||||
generator_states[key] = _sanitize_generator_state(existing_raw, unlocked, available)
|
||||
generator_states[key] = _sanitize_generator_state(existing_raw, unlocked, available, default_owned)
|
||||
return
|
||||
|
||||
var new_state: Dictionary = _default_generator_state(unlocked, available)
|
||||
var new_state: Dictionary = _default_generator_state(unlocked, available, default_owned)
|
||||
generator_states[key] = new_state
|
||||
generator_state_changed.emit(StringName(key), new_state.duplicate(true))
|
||||
|
||||
@@ -446,10 +447,11 @@ func _set_total_currency(currency_id: StringName, value: BigNumber) -> void:
|
||||
return
|
||||
_total_currency_acquired[normalized_currency_id] = _copy_big_number(value)
|
||||
|
||||
func _default_generator_state(unlocked: bool, available: bool) -> Dictionary:
|
||||
func _default_generator_state(unlocked: bool, available: bool, owned: int = 0) -> Dictionary:
|
||||
var owned_value: int = _to_non_negative_int(owned, 0)
|
||||
return {
|
||||
GENERATOR_OWNED_KEY: 0,
|
||||
GENERATOR_PURCHASED_COUNT_KEY: 0,
|
||||
GENERATOR_OWNED_KEY: owned_value,
|
||||
GENERATOR_PURCHASED_COUNT_KEY: owned_value,
|
||||
GENERATOR_UNLOCKED_KEY: unlocked,
|
||||
GENERATOR_AVAILABLE_KEY: available,
|
||||
}
|
||||
@@ -457,19 +459,19 @@ func _default_generator_state(unlocked: bool, available: bool) -> Dictionary:
|
||||
func _get_generator_state(generator_id: StringName) -> Dictionary:
|
||||
var key: String = String(generator_id)
|
||||
if key.is_empty():
|
||||
return _default_generator_state(false, false)
|
||||
return _default_generator_state(false, false, 0)
|
||||
|
||||
if not generator_states.has(key):
|
||||
register_generator(generator_id)
|
||||
|
||||
var existing_raw: Variant = generator_states.get(key, _default_generator_state(false, false))
|
||||
var existing_raw: Variant = generator_states.get(key, _default_generator_state(false, false, 0))
|
||||
if existing_raw is Dictionary:
|
||||
var state: Dictionary = existing_raw
|
||||
var sanitized: Dictionary = _sanitize_generator_state(state, false, false)
|
||||
var sanitized: Dictionary = _sanitize_generator_state(state, false, false, 0)
|
||||
generator_states[key] = sanitized
|
||||
return sanitized
|
||||
|
||||
var fallback: Dictionary = _default_generator_state(false, false)
|
||||
var fallback: Dictionary = _default_generator_state(false, false, 0)
|
||||
generator_states[key] = fallback
|
||||
return fallback
|
||||
|
||||
@@ -478,7 +480,7 @@ func _set_generator_state(generator_id: StringName, state: Dictionary) -> void:
|
||||
if key.is_empty():
|
||||
return
|
||||
|
||||
var sanitized: Dictionary = _sanitize_generator_state(state, false, false)
|
||||
var sanitized: Dictionary = _sanitize_generator_state(state, false, false, 0)
|
||||
generator_states[key] = sanitized
|
||||
generator_state_changed.emit(StringName(key), sanitized.duplicate(true))
|
||||
|
||||
@@ -668,8 +670,9 @@ func _deserialize_generator_state_entry(raw_state: Dictionary) -> Dictionary:
|
||||
|
||||
return parsed_state
|
||||
|
||||
func _sanitize_generator_state(raw_state: Dictionary, default_unlocked: bool, default_available: bool) -> Dictionary:
|
||||
var owned_value: int = _to_non_negative_int(raw_state.get(GENERATOR_OWNED_KEY, 0), 0)
|
||||
func _sanitize_generator_state(raw_state: Dictionary, default_unlocked: bool, default_available: bool, default_owned: int = 0) -> Dictionary:
|
||||
var normalized_default_owned: int = _to_non_negative_int(default_owned, 0)
|
||||
var owned_value: int = _to_non_negative_int(raw_state.get(GENERATOR_OWNED_KEY, normalized_default_owned), normalized_default_owned)
|
||||
var purchased_value: int = _to_non_negative_int(raw_state.get(GENERATOR_PURCHASED_COUNT_KEY, owned_value), owned_value)
|
||||
|
||||
return {
|
||||
|
||||
@@ -154,8 +154,12 @@ func can_buy(amount: int = 1) -> bool:
|
||||
if not is_available_to_player():
|
||||
return false
|
||||
|
||||
var purchase_currency_id: StringName = get_purchase_currency_id()
|
||||
if purchase_currency_id == &"":
|
||||
return false
|
||||
|
||||
var cost: BigNumber = get_cost_for_amount(amount)
|
||||
var current: BigNumber = _get_currency_amount()
|
||||
var current: BigNumber = _get_currency_amount_by_id(purchase_currency_id)
|
||||
return current.is_greater_than(cost) or current.is_equal_to(cost)
|
||||
|
||||
## Attempts to buy `amount` units and emits purchase success/failure signals.
|
||||
@@ -164,11 +168,14 @@ func buy(amount: int = 1) -> bool:
|
||||
return false
|
||||
if not is_available_to_player():
|
||||
return false
|
||||
var purchase_currency_id: StringName = get_purchase_currency_id()
|
||||
if purchase_currency_id == &"":
|
||||
return false
|
||||
|
||||
var safe_amount: int = maxi(amount, 1)
|
||||
var total_cost: BigNumber = get_cost_for_amount(safe_amount)
|
||||
if not _spend_currency(total_cost):
|
||||
purchase_failed.emit(safe_amount, total_cost, _get_currency_amount())
|
||||
if not _spend_currency_by_id(purchase_currency_id, total_cost):
|
||||
purchase_failed.emit(safe_amount, total_cost, _get_currency_amount_by_id(purchase_currency_id))
|
||||
return false
|
||||
|
||||
owned += safe_amount
|
||||
@@ -182,8 +189,11 @@ func buy_max() -> int:
|
||||
return 0
|
||||
if not is_available_to_player():
|
||||
return 0
|
||||
var purchase_currency_id: StringName = get_purchase_currency_id()
|
||||
if purchase_currency_id == &"":
|
||||
return 0
|
||||
|
||||
var available_currency: float = _big_number_to_float(_get_currency_amount())
|
||||
var available_currency: float = _big_number_to_float(_get_currency_amount_by_id(purchase_currency_id))
|
||||
var estimated_max: int = data.max_affordable(owned, available_currency)
|
||||
if estimated_max <= 0:
|
||||
return 0
|
||||
@@ -368,6 +378,13 @@ func get_generator_id() -> StringName:
|
||||
func get_currency_id() -> StringName:
|
||||
return GameState.get_currency_id(currency)
|
||||
|
||||
func get_purchase_currency_id() -> StringName:
|
||||
var purchase_currency: Currency = _resolve_purchase_currency()
|
||||
if purchase_currency == null:
|
||||
return &""
|
||||
|
||||
return GameState.get_currency_id(purchase_currency)
|
||||
|
||||
## True when the generator is both unlocked and available.
|
||||
func is_available_to_player() -> bool:
|
||||
return is_unlocked and is_available
|
||||
@@ -406,6 +423,12 @@ func _get_currency_amount() -> BigNumber:
|
||||
return BigNumber.from_float(0.0)
|
||||
return GameState.get_currency_amount(currency)
|
||||
|
||||
func _get_currency_amount_by_id(currency_id: StringName) -> BigNumber:
|
||||
if currency_id == &"":
|
||||
return BigNumber.from_float(0.0)
|
||||
|
||||
return GameState.get_currency_amount_by_id(currency_id)
|
||||
|
||||
## Safely converts finite float values into BigNumber costs.
|
||||
func _float_to_big_number(value: float) -> BigNumber:
|
||||
if value <= 0.0:
|
||||
@@ -439,6 +462,12 @@ func _resolve_buff_cost_currency_id(buff: GeneratorBuffData) -> StringName:
|
||||
|
||||
return GameState.get_currency_id(cost_currency)
|
||||
|
||||
func _resolve_purchase_currency() -> Currency:
|
||||
if data != null and data.purchase_currency != null:
|
||||
return data.purchase_currency
|
||||
|
||||
return currency
|
||||
|
||||
func _resolve_buff_target_currency_id(buff: GeneratorBuffData) -> StringName:
|
||||
if buff == null:
|
||||
return &""
|
||||
@@ -501,7 +530,12 @@ func _ensure_registered() -> void:
|
||||
if _generator_id == &"":
|
||||
return
|
||||
|
||||
GameState.register_generator(_generator_id, _default_unlocked_state(), _default_available_state())
|
||||
GameState.register_generator(
|
||||
_generator_id,
|
||||
_default_unlocked_state(),
|
||||
_default_available_state(),
|
||||
_default_owned_state()
|
||||
)
|
||||
_register_buffs()
|
||||
_is_registered = true
|
||||
|
||||
@@ -580,6 +614,11 @@ func _default_available_state() -> bool:
|
||||
return false
|
||||
return data.starts_available
|
||||
|
||||
func _default_owned_state() -> int:
|
||||
if data == null:
|
||||
return 0
|
||||
return maxi(data.initial_owned, 0)
|
||||
|
||||
## Area2D callback: pointer entered generator interaction region.
|
||||
func _on_area_2d_mouse_entered() -> void:
|
||||
_mouse_entered = true
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
[ext_resource type="Script" uid="uid://dtbxopw6ulhl8" path="res://core/generator/currency_generator.gd" id="1_4n4ca"]
|
||||
[ext_resource type="Resource" uid="uid://brqaojindcxa5" path="res://idles/currencies/magic.tres" id="2_5tmvy"]
|
||||
[ext_resource type="Resource" uid="uid://co0mcc2kvcpo5" path="res://idles/generators/primary_generator.tres" id="3_wx13b"]
|
||||
[ext_resource type="Resource" uid="uid://co0mcc2kvcpo5" path="res://idles/generators/orb.tres" id="3_wx13b"]
|
||||
[ext_resource type="Texture2D" uid="uid://bgtt3wu43tajh" path="res://icon.svg" id="4_ruf1h"]
|
||||
[ext_resource type="PackedScene" uid="uid://ckos7f22pnmyh" path="res://generator_container.tscn" id="5_bb14m"]
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ enum UnlockGoalBehavior {
|
||||
@export var name: String = ""
|
||||
@export var starts_unlocked: bool = true
|
||||
@export var starts_available: bool = true
|
||||
@export var initial_owned: int = 0
|
||||
@export var purchase_currency: Currency
|
||||
@export var unlock_goal: GoalData
|
||||
@export var unlock_goal_behavior: UnlockGoalBehavior = UnlockGoalBehavior.AUTOMATIC
|
||||
|
||||
|
||||
Reference in New Issue
Block a user