game: cache some calculations for enemies
This commit is contained in:
@@ -27,6 +27,7 @@ var is_dead: bool = false
|
|||||||
var health: float
|
var health: float
|
||||||
|
|
||||||
var _path_update_timer: float = 0.0
|
var _path_update_timer: float = 0.0
|
||||||
|
var _compute_cache: KeyedCache = KeyedCache.new()
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
@@ -177,6 +178,7 @@ func _on_animation_player_animation_finished(anim_name: StringName) -> void:
|
|||||||
|
|
||||||
func get_calculated(key: String) -> Variant:
|
func get_calculated(key: String) -> Variant:
|
||||||
# set max move speed to players move speed
|
# set max move speed to players move speed
|
||||||
|
var compute_func = func():
|
||||||
if key == "move_speed":
|
if key == "move_speed":
|
||||||
return clampf(
|
return clampf(
|
||||||
EnemyMod.get_calculated(self, key),
|
EnemyMod.get_calculated(self, key),
|
||||||
@@ -185,9 +187,10 @@ func get_calculated(key: String) -> Variant:
|
|||||||
)
|
)
|
||||||
return EnemyMod.get_calculated(self, key)
|
return EnemyMod.get_calculated(self, key)
|
||||||
|
|
||||||
|
return _compute_cache.get_or_compute(key, compute_func)
|
||||||
|
|
||||||
|
|
||||||
func has_property(key: String) -> bool:
|
func has_property(key: String) -> bool:
|
||||||
for prop in get_property_list():
|
var cache_key = "prop_%s" % key
|
||||||
if prop.name == key:
|
var compute_func = func(): return get(key) != null
|
||||||
return true
|
return _compute_cache.get_or_compute(key, compute_func)
|
||||||
return false
|
|
||||||
|
@@ -13,6 +13,8 @@ const ENEMY_BAT = preload("res://scenes/enemies/enemy_bat.tscn")
|
|||||||
const ENEMY_SLIME_SMALL = preload("res://scenes/enemies/enemy_slime_small.tscn")
|
const ENEMY_SLIME_SMALL = preload("res://scenes/enemies/enemy_slime_small.tscn")
|
||||||
const SLIME_COLOR_VARIATIONS: Array[Color] = [Color.CHARTREUSE, Color.FUCHSIA, Color.DARK_ORANGE]
|
const SLIME_COLOR_VARIATIONS: Array[Color] = [Color.CHARTREUSE, Color.FUCHSIA, Color.DARK_ORANGE]
|
||||||
|
|
||||||
|
var _elapsed_time: float = 0.0
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
timer.wait_time = 1 / spawn_rate
|
timer.wait_time = 1 / spawn_rate
|
||||||
@@ -21,7 +23,12 @@ func _ready() -> void:
|
|||||||
GlobalConst.sig_set_spawn_rate.connect(_on_set_spawn_rate)
|
GlobalConst.sig_set_spawn_rate.connect(_on_set_spawn_rate)
|
||||||
|
|
||||||
|
|
||||||
|
func _physics_process(delta: float) -> void:
|
||||||
|
_elapsed_time += delta
|
||||||
|
|
||||||
|
|
||||||
func _on_timer_timeout() -> void:
|
func _on_timer_timeout() -> void:
|
||||||
|
_on_set_spawn_rate(1.0 + (_elapsed_time / 60.0) ** 2)
|
||||||
var enemies = get_tree().get_nodes_in_group(GlobalConst.GROUP_ENEMY)
|
var enemies = get_tree().get_nodes_in_group(GlobalConst.GROUP_ENEMY)
|
||||||
GlobalConst.sig_debug_stats_set.emit("enemy_count", "%s" % len(enemies))
|
GlobalConst.sig_debug_stats_set.emit("enemy_count", "%s" % len(enemies))
|
||||||
var next_enemy: PackedScene
|
var next_enemy: PackedScene
|
||||||
@@ -105,4 +112,7 @@ func _on_stop_spawning(val: bool):
|
|||||||
|
|
||||||
|
|
||||||
func _on_set_spawn_rate(val: float):
|
func _on_set_spawn_rate(val: float):
|
||||||
timer.wait_time = 1 / val
|
timer.stop()
|
||||||
|
timer.wait_time = 1.0 / val
|
||||||
|
timer.start()
|
||||||
|
print_debug("spawn_rate: %s" % timer.wait_time)
|
||||||
|
17
scenes/utils/keyed_cache.gd
Normal file
17
scenes/utils/keyed_cache.gd
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
class_name KeyedCache
|
||||||
|
extends Resource
|
||||||
|
|
||||||
|
var cache = {}
|
||||||
|
|
||||||
|
|
||||||
|
func get_or_compute(key: String, compute_func: Callable):
|
||||||
|
if key in cache:
|
||||||
|
return cache["key"]
|
||||||
|
|
||||||
|
var value = compute_func.call()
|
||||||
|
cache["key"] = value
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
func invalidate_key(key: String):
|
||||||
|
cache.erase(key)
|
1
scenes/utils/keyed_cache.gd.uid
Normal file
1
scenes/utils/keyed_cache.gd.uid
Normal file
@@ -0,0 +1 @@
|
|||||||
|
uid://bbshyok4m8nq3
|
Reference in New Issue
Block a user