Minor improvements
This commit is contained in:
@@ -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