修正godot代码里的字典 classname RepresentManager extends RefCounted 象征管理器…
- User
修正godot代码里的字典 class_name RepresentManager extends RefCounted
象征管理器
一个静态工具类,用于集中管理和访问所有“象征” (Represent) 资源。
使用字典进行缓存,以提供高效的查找功能。
#-----------------------------------------------------------------------------
象征资源定义
#-----------------------------------------------------------------------------
#region stage static var ARMOR: Represent = preload("res://static/represent/resource/stage/armor.tres") static var ATTACK: Represent = preload("res://static/represent/resource/stage/attack.tres") static var ATTACKED: Represent = preload("res://static/represent/resource/stage/attacked.tres") static var DESTROY: Represent = preload("res://static/represent/resource/stage/destroy.tres") static var DIE: Represent = preload("res://static/represent/resource/stage/die.tres") static var DISCARD: Represent = preload("res://static/represent/resource/stage/discard.tres") static var DRAW: Represent = preload("res://static/represent/resource/stage/draw.tres") static var DRAW_PILE: Represent = preload("res://static/represent/resource/stage/draw_pile.tres") static var ENTER: Represent = preload("res://static/represent/resource/stage/enter.tres") static var EXIT: Represent = preload("res://static/represent/resource/stage/exit.tres") static var EXTRACT: Represent = preload("res://static/represent/resource/stage/extract.tres") static var GET_BUFF: Represent = preload("res://static/represent/resource/stage/get_buff.tres") static var HP_DECREASE: Represent = preload("res://static/represent/resource/stage/HP_decrease.tres") static var HP_INCREASE: Represent = preload("res://static/represent/resource/stage/HP_increase.tres") static var LOSE_BUFF: Represent = preload("res://static/represent/resource/stage/lose_buff.tres") static var MAGIC: Represent = preload("res://static/represent/resource/stage/magic.tres") static var PLAY_CARD: Represent = preload("res://static/represent/resource/stage/play_card.tres") static var PRAYER: Represent = preload("res://static/represent/resource/stage/prayer.tres") static var RELEASE: Represent = preload("res://static/represent/resource/stage/release.tres") static var SEAL: Represent = preload("res://static/represent/resource/stage/seal.tres") static var SHIELD_DECREASE: Represent = preload("res://static/represent/resource/stage/shield_decrease.tres") static var SHIELD_INCREASE: Represent = preload("res://static/represent/resource/stage/shield_increase.tres") static var SHUFFLE: Represent = preload("res://static/represent/resource/stage/shuffle.tres") static var STAGE_BROKEN: Represent = preload("res://static/represent/resource/stage/stage_broken.tres") static var STAGE_COST: Represent = preload("res://static/represent/resource/stage/stage_cost.tres") static var STAGE_DECREASE_DAMAGE: Represent = preload("res://static/represent/resource/stage/stage_decrease_damage.tres") static var STAGE_DEFICIT: Represent = preload("res://static/represent/resource/stage/stage_deficit.tres") static var STAGE_EVIL: Represent = preload("res://static/represent/resource/stage/stage_evil.tres") static var STAGE_HARD: Represent = preload("res://static/represent/resource/stage/stage_hard.tres") static var STAGE_INCREASE_DAMAGE: Represent = preload("res://static/represent/resource/stage/stage_increase_damage.tres") static var STAGE_INVENTORY: Represent = preload("res://static/represent/resource/stage/stage_inventory.tres") static var STAGE_LUSH: Represent = preload("res://static/represent/resource/stage/stage_lush.tres") static var STAGE_POLLUTION: Represent = preload("res://static/represent/resource/stage/stage_pollution.tres") static var STAGE_PURIFICATION: Represent = preload("res://static/represent/resource/stage/stage_purification.tres") static var STAGE_SEED: Represent = preload("res://static/represent/resource/stage/stage_seed.tres") static var STAGE_VITALITY: Represent = preload("res://static/represent/resource/stage/stage_vitality.tres") static var STAGE_WITHERED: Represent = preload("res://static/represent/resource/stage/stage_withered.tres") static var SUMMON: Represent = preload("res://static/represent/resource/stage/summon.tres") static var TONING: Represent = preload("res://static/represent/resource/stage/toning.tres") static var TURN_END: Represent = preload("res://static/represent/resource/stage/turn_end.tres") static var TURN_START: Represent = preload("res://static/represent/resource/stage/turn_start.tres") static var UNSEAL: Represent = preload("res://static/represent/resource/stage/unseal.tres")
#endregion
#region kind static var BLACK: Represent = preload("res://static/represent/resource/kind/black.tres") static var BLUE: Represent = preload("res://static/represent/resource/kind/blue.tres") static var COLOURLESS: Represent = preload("res://static/represent/resource/kind/colourless.tres") static var CYAN: Represent = preload("res://static/represent/resource/kind/cyan.tres") static var DEBUFF: Represent = preload("res://static/represent/resource/kind/debuff.tres") static var GREEN: Represent = preload("res://static/represent/resource/kind/green.tres") static var IRIDESCENCE: Represent = preload("res://static/represent/resource/kind/iridescence.tres") static var MAGENTA: Represent = preload("res://static/represent/resource/kind/magenta.tres") static var MULTIPLE_COLORS: Represent = preload("res://static/represent/resource/kind/multiple_colors.tres") static var RED: Represent = preload("res://static/represent/resource/kind/red.tres") static var SALT_FISH_SYMBOL: Represent = preload("res://static/represent/resource/kind/salt_fish_symbol.tres") static var WHITE: Represent = preload("res://static/represent/resource/kind/white.tres") static var YELLOW: Represent = preload("res://static/represent/resource/kind/yellow.tres") #endregion
#region life static var AUTO: Represent = preload("res://static/represent/resource/life/auto.tres") static var DISSIPATE: Represent = preload("res://static/represent/resource/life/dissipate.tres") static var ENABLE: Represent = preload("res://static/represent/resource/life/enable.tres") static var FRAGILE: Represent = preload("res://static/represent/resource/life/fragile.tres") static var INVARIANT: Represent = preload("res://static/represent/resource/life/invariant.tres") static var LIGHTNING: Represent = preload("res://static/represent/resource/life/lightning.tres") static var LOCK: Represent = preload("res://static/represent/resource/life/lock.tres") static var UNAVAILABLE: Represent = preload("res://static/represent/resource/life/unavailable.tres") static var WEIGHT: Represent = preload("res://static/represent/resource/life/weight.tres") #endregion
#region number static var BROKEN: Represent = preload("res://static/represent/resource/number/broken.tres") static var COST: Represent = preload("res://static/represent/resource/number/cost.tres") static var DECREASE_DAMAGE: Represent = preload("res://static/represent/resource/number/decrease_damage.tres") static var DEFICIT: Represent = preload("res://static/represent/resource/number/deficit.tres") static var EVIL: Represent = preload("res://static/represent/resource/number/evil.tres") static var HARD: Represent = preload("res://static/represent/resource/number/hard.tres") static var INCREASE_DAMAGE: Represent = preload("res://static/represent/resource/number/increase_damage.tres") static var INVENTORY: Represent = preload("res://static/represent/resource/number/inventory.tres") static var LUSH: Represent = preload("res://static/represent/resource/number/lush.tres") static var POLLUTION: Represent = preload("res://static/represent/resource/number/pollution.tres") static var PURIFICATION: Represent = preload("res://static/represent/resource/number/purification.tres") static var SEED: Represent = preload("res://static/represent/resource/number/seed.tres") static var VITALITY: Represent = preload("res://static/represent/resource/number/vitality.tres") static var WITHERED: Represent = preload("res://static/represent/resource/number/withered.tres") #endregion
#region symbol static var BRAVE: Represent = preload("res://static/represent/resource/symbol/brave.tres") static var CHAMELEON: Represent = preload("res://static/represent/resource/symbol/chameleon.tres") static var COCKROACH: Represent = preload("res://static/represent/resource/symbol/cockroach.tres") static var ERROR: Represent = preload("res://static/represent/resource/symbol/error.tres") static var PROHIBIT_BLUE: Represent = preload("res://static/represent/resource/symbol/prohibit_blue.tres") static var PROHIBIT_CYAN: Represent = preload("res://static/represent/resource/symbol/prohibit_cyan.tres") static var PROHIBIT_GREEN: Represent = preload("res://static/represent/resource/symbol/prohibit_green.tres") static var PROHIBIT_MAGENTA: Represent = preload("res://static/represent/resource/symbol/prohibit_magenta.tres") static var PROHIBIT_RED: Represent = preload("res://static/represent/resource/symbol/prohibit_red.tres") static var PROHIBIT_YELLOW: Represent = preload("res://static/represent/resource/symbol/prohibit_yellow.tres") static var RAINBOW: Represent = preload("res://static/represent/resource/symbol/rainbow.tres") static var SALT_FISH: Represent = preload("res://static/represent/resource/symbol/salt_fish.tres") static var SALT_FISH_COUNTER: Represent = preload("res://static/represent/resource/symbol/salt_fish_counter.tres") static var SALT_FISH_KING: Represent = preload("res://static/represent/resource/symbol/salt_fish_king.tres") static var START: Represent = preload("res://static/represent/resource/symbol/start.tres") static var STRATEGY: Represent = preload("res://static/represent/resource/symbol/strategy.tres") static var THINK: Represent = preload("res://static/represent/resource/symbol/think.tres") static var WOUND: Represent = preload("res://static/represent/resource/symbol/wound.tres")
#endregion
#region system static var ANTICLOCKWISE: Represent = preload("res://static/represent/resource/system/anticlockwise.tres") static var CLOCKWISE: Represent = preload("res://static/represent/resource/system/clockwise.tres") static var CONCORD: Represent = preload("res://static/represent/resource/system/concord.tres") static var COUNTER: Represent = preload("res://static/represent/resource/system/counter.tres") static var DAMAGE: Represent = preload("res://static/represent/resource/system/damage.tres") static var DISCORD: Represent = preload("res://static/represent/resource/system/discord.tres") static var DURATION: Represent = preload("res://static/represent/resource/system/duration.tres") static var HAND_LIMIT: Represent = preload("res://static/represent/resource/system/hand_limit.tres") static var HOURGLASS: Represent = preload("res://static/represent/resource/system/hourglass.tres") static var SHIELD: Represent = preload("res://static/represent/resource/system/shield.tres") static var TURN: Represent = preload("res://static/represent/resource/system/turn.tres") static var HP: Represent = preload("res://static/represent/resource/system/HP.tres")
#endregion
#region target
static var ALL: Represent = preload("res://static/represent/resource/target/all.tres") static var ALL_ENEMY: Represent = preload("res://static/represent/resource/target/all_enemy.tres") static var ALL_TEAMMATE: Represent = preload("res://static/represent/resource/target/all_teammate.tres") static var ONE: Represent = preload("res://static/represent/resource/target/one.tres") static var ONE_ENEMY: Represent = preload("res://static/represent/resource/target/one_enemy.tres") static var ONE_MYSELF: Represent = preload("res://static/represent/resource/target/one_myself.tres") static var ONE_TEAMMATE: Represent = preload("res://static/represent/resource/target/one_teammate.tres") static var RANDOM: Represent = preload("res://static/represent/resource/target/random.tres") static var RANDOM_ENEMY: Represent = preload("res://static/represent/resource/target/random_enemy.tres") static var RANDOM_TEAMMATE: Represent = preload("res://static/represent/resource/target/random_teammate.tres")
#endregion
#-----------------------------------------------------------------------------
私有静态变量 (用于缓存和优化)
#-----------------------------------------------------------------------------
使用下划线前缀表示这些是内部变量,不应从外部直接访问。
static var _is_initialized: bool = false static var _represents_by_name: Dictionary[StringName, Represent] = {} static var _opposition_map: Dictionary[Represent, Array] = {}
#-----------------------------------------------------------------------------
静态函数区
#----------------------------------------------------------------------------- #region
根据名称字符串获取象征对象。
@param name: 象征的名称 (例如 "armor", "attack")
@return: 对应的 Represent 对象,如果未找到则返回 null
static func get_represent(name: StringName) -> Represent: _ensure_initialized() return _represents_by_name.get(name, null)
是否有反义词文字版
static func has_opposition_string(name: StringName) -> bool: var new: Represent = get_represent(name) if new: return has_opposition(new) return false
获取反义词文字版
static func get_opposition_string(name: StringName) -> Array[Represent]: var new: Represent = get_represent(name) return _opposition_map.get(new, [])
获取所有已定义的象征。
@return: 包含所有 Represent 对象的数组。
#static func get_all_represents() -> Array[Represent]: #_ensure_initialized() #return _represents_by_name.values()
检查一个象征是否有明确的对立象征。
@param represent: 要检查的 Represent 对象。
@return: 如果存在对立象征,返回 true。
static func has_opposition(represent: Represent) -> bool: _ensure_initialized() return _opposition_map.has(represent)
获取一个象征的所有对立象征。
通常只有一个,但设计为可返回多个以支持未来扩展。
@param represent: 要获取对立面的 Represent 对象。
@return: 包含对立 Represent 对象的数组,如果不存在则返回空数组。
static func get_opposition(represent: Represent) -> Array[Represent]: _ensure_initialized() return _opposition_map.get(represent, [])
[私有] 确保内部数据结构已初始化。
这是一个"懒加载"实现,只在第一次调用公共函数时执行一次初始化。
static func _ensure_initialized() -> void: if _is_initialized: return
# 1. 初始化按名称查找的字典 _represents_by_name = { # region stage &"armor": ARMOR, &"attack": ATTACK, &"attacked": ATTACKED, &"destroy": DESTROY, &"discard": DISCARD, &"draw": DRAW, &"draw_pile": DRAW_PILE, &"extract": EXTRACT, &"hp_decrease": HP_DECREASE, &"hp_increase": HP_INCREASE, &"magic": MAGIC, &"play_card": PLAY_CARD, &"prayer": PRAYER, &"release": RELEASE, &"seal": SEAL, &"shield_decrease": SHIELD_DECREASE, &"shield_increase": SHIELD_INCREASE, &"shuffle": SHUFFLE, &"stage_broken": STAGE_BROKEN, &"stage_cost": STAGE_COST, &"stage_decrease_damage": STAGE_DECREASE_DAMAGE, &"stage_deficit": STAGE_DEFICIT, &"stage_evil": STAGE_EVIL, &"stage_hard": STAGE_HARD, &"stage_increase_damage": STAGE_INCREASE_DAMAGE, &"stage_inventory": STAGE_INVENTORY, &"stage_lush": STAGE_LUSH, &"stage_pollution": STAGE_POLLUTION, &"stage_purification": STAGE_PURIFICATION, &"stage_seed": STAGE_SEED, &"stage_vitality": STAGE_VITALITY, &"stage_withered": STAGE_WITHERED, &"turn_end": TURN_END, &"turn_start": TURN_START, &"unseal": UNSEAL, # region kind &"black": BLACK, &"blue": BLUE, &"colourless": COLOURLESS, &"cyan": CYAN, &"debuff": DEBUFF, &"green": GREEN, &"iridescence": IRIDESCENCE, &"magenta": MAGENTA, &"multiple_colors": MULTIPLE_COLORS, &"red": RED, &"salt_fish_symbol": SALT_FISH_SYMBOL, &"white": WHITE, &"yellow": YELLOW, # region life &"auto": AUTO, &"dissipate": DISSIPATE, &"enable": ENABLE, &"fragile": FRAGILE, &"invariant": INVARIANT, &"lightning": LIGHTNING, &"lock": LOCK, &"unavailable": UNAVAILABLE, &"weight": WEIGHT, # region number &"broken": BROKEN, &"cost": COST, &"decrease_damage": DECREASE_DAMAGE, &"deficit": DEFICIT, &"evil": EVIL, &"hard": HARD, &"increase_damage": INCREASE_DAMAGE, &"inventory": INVENTORY, &"lush": LUSH, &"pollution": POLLUTION, &"purification": PURIFICATION, &"seed": SEED, &"vitality": VITALITY, &"withered": WITHERED, # region symbol &"brave": BRAVE, &"chameleon": CHAMELEON, &"cockroach": COCKROACH, &"error": ERROR, &"prohibit_blue": PROHIBIT_BLUE, &"prohibit_cyan": PROHIBIT_CYAN, &"prohibit_green": PROHIBIT_GREEN, &"prohibit_magenta": PROHIBIT_MAGENTA, &"prohibit_red": PROHIBIT_RED, &"prohibit_yellow": PROHIBIT_YELLOW, &"rainbow": RAINBOW, &"salt_fish": SALT_FISH, &"salt_fish_counter": SALT_FISH_COUNTER, &"salt_fish_king": SALT_FISH_KING, &"start": START, &"strategy": STRATEGY, &"think": THINK, &"wound": WOUND, # region system &"anticlockwise": ANTICLOCKWISE, &"clockwise": CLOCKWISE, &"concord": CONCORD, &"counter": COUNTER, &"damage": DAMAGE, &"discord": DISCORD, &"duration": DURATION, &"hand_limit": HAND_LIMIT, &"hourglass": HOURGLASS, &"shield": SHIELD, &"turn": TURN, &"hp": HP, # region target &"aoe_enemy": AOE_ENEMY, &"aoe_including_self": AOE_INCLUDING_SELF, &"one": ONE, &"one_enemy": ONE_ENEMY, &"one_myself": ONE_MYSELF, &"random_enemy": RANDOM_ENEMY, &"random_including_self": RANDOM_INCLUDING_SELF, } # 2. 初始化对立象征的映射关系 _opposition_map = { # 加减攻 INCREASE_DAMAGE: [INCREASE_DAMAGE, DECREASE_DAMAGE], DECREASE_DAMAGE: [INCREASE_DAMAGE, DECREASE_DAMAGE], # 生机和代价 VITALITY: [VITALITY, COST], COST: [VITALITY, COST], # 库存,亏损 HARD: [HARD, BROKEN], BROKEN: [HARD, BROKEN], # 茂盛,枯萎 LUSH: [LUSH, WITHERED], WITHERED: [LUSH, WITHERED], # 净化,污染, PURIFICATION: [PURIFICATION, POLLUTION], POLLUTION: [PURIFICATION, POLLUTION], #种子,邪恶 SEED: [SEED, EVIL], EVIL: [SEED, EVIL], } # 标记为已初始化 _is_initialized = true#endregion
Assistant
Okay, let's review and refine the
RepresentManagerscript.The primary goals are:
- Centralize access to
Representresources. - Provide efficient lookup by name.
- Manage opposition relationships between
Representresources.
Based on your code, here are some observations and suggestions for improvement:
- Redundancy: You are
preloading everyRepresentresource into its own static variable (e.g.,static var ARMOR: Represent = preload(...)) and then manually populating a dictionary (_represents_by_name) with these same resources keyed by name. This is redundant. If the main access method is intended to beget_represent(name), you can remove the individual static variables entirely. If you also want direct access likeRepresentManager.ARMOR, then you have to maintain both the static variable and the dictionary entry, which is error-prone. The modern approach is often to rely on the dictionary/map. - Hardcoded Dictionary: The
_represents_by_namedictionary is fully hardcoded. This means every time you add or remove aRepresentresource, you must manually edit this script. This is prone to errors (typos, forgetting entries). A more robust solution would be to load resources dynamically from the directories, although this adds complexity. For a fixed, known set of resources, the hardcoded approach is simpler to implement initially but harder to maintain. - Hardcoded Opposition Map: Similar to the dictionary, the
_opposition_mapis hardcoded. This is generally acceptable for defining relationships, but needs careful review.- Symmetry: The way oppositions are defined might need adjustment. For example,
INCREASE_DAMAGE: [INCREASE_DAMAGE, DECREASE_DAMAGE]is problematic. It mapsINCREASE_DAMAGEto an array containing itself andDECREASE_DAMAGE. You likely want a symmetric relationship:INCREASE_DAMAGEopposesDECREASE_DAMAGE, andDECREASE_DAMAGEopposesINCREASE_DAMAGE. - Completeness: Ensure all intended oppositions are listed.
- Symmetry: The way oppositions are defined might need adjustment. For example,
- Naming Consistency (Targets): There's an inconsistency in the
targetsection. Thepreloadstatements use names likeALL_ENEMY,ONE_ENEMY, etc., but the dictionary keys in_ensure_initializeduse different names like&"aoe_enemy",&"one_enemy". You need to ensure the keys in the dictionary accurately represent the intended string identifiers for these resources. Assuming the variable names are closer to the final desired string representation, the dictionary keys should be corrected. get_opposition_stringvs.get_opposition: You have functions to get oppositions by name (_string) and by object. This might be intentional, but consider if a single functionget_oppositions(key)that accepts either a string name or aRepresentobject could simplify the interface.has_opposition_string: This function is slightly redundant. You can achieve the same check by callingget_opposition_string(name).is_empty() == false.@toolAnnotation: Since this script defines constants and manages resources, adding@toolat the top would ensure it runs in the editor, potentially helping with preloading or setup if needed, althoughextends RefCountedalready provides a way to access static members. It's generally good practice for manager scripts like this.- Uncomment
get_all_represents: If this function is intended to be used, uncomment it.
Refined Code Example:
Here's a revised version addressing these points, primarily focusing on removing redundancy and correcting the opposition map and target names. This version keeps the explicit definition (no dynamic loading) but cleans up the structure.
@tool # Ensures script runs in the editor class_name RepresentManager extends RefCounted ## 象征管理器 ## ## 一个静态工具类,用于集中管理和访问所有“象征” (Represent) 资源。 ## 使用字典进行缓存,以提供高效的查找功能。 #----------------------------------------------------------------------------- # 象征资源定义 (已移除,由 _ensure_initialized 中的字典管理) #----------------------------------------------------------------------------- # [注意] 移除了所有单独的 static var preload(...) 行。 # 现在所有资源都通过 _represents_by_name 字典访问。 # 如果需要直接访问(如 RepresentManager.ARMOR),则需要恢复 preload # 并同时更新字典,但这会增加维护成本。 #----------------------------------------------------------------------------- # 私有静态变量 (用于缓存和优化) #----------------------------------------------------------------------------- ## 使用下划线前缀表示这些是内部变量,不应从外部直接访问。 static var _is_initialized: bool = false # 使用 StringName 作为键可以提高查找效率并避免字符串比较开销 static var _represents_by_name: Dictionary[StringName, Represent] = {} # 使用 Dictionary[Represent, Array[Represent]] 来存储反义词关系 static var _opposition_map: Dictionary[Represent, Array[Represent]] = {} #----------------------------------------------------------------------------- # 静态函数区 #----------------------------------------------------------------------------- #region ## 根据名称字符串获取象征对象。 ## @param name: 象征的名称 (例如 "armor", "attack") ## @return: 对应的 Represent 对象,如果未找到则返回 null static func get_represent(name: StringName) -> Represent: _ensure_initialized() # 使用 .get() 避免在键不存在时出错,并提供默认值 null return _represents_by_name.get(name, null) ## 检查一个象征是否有明确的对立象征。 ## @param represent: 要检查的 Represent 对象。 ## @return: 如果存在对立象征,返回 true。 static func has_opposition(represent: Represent) -> bool: _ensure_initialized() # 检查字典中是否存在该 Represent 作为键 return _opposition_map.has(represent) ## 获取一个象征的所有对立象征。 ## 通常只有一个,但设计为可返回多个以支持未来扩展。 ## @param represent: 要获取对立面的 Represent 对象。 ## @return: 包含对立 Represent 对象的数组,如果不存在则返回空数组。 static func get_opposition(represent: Represent) -> Array[Represent]: _ensure_initialized() # 返回该 Represent 对应的数组,如果不存在则返回空数组 return _opposition_map.get(represent, []) ## 根据名称字符串获取其对立象征的数组。 ## @param name: 象征的名称。 ## @return: 对立 Represent 对象的数组,如果名称无效或无对立则返回空数组。 static func get_opposition_by_name(name: StringName) -> Array[Represent]: _ensure_initialized() var represent_obj = get_represent(name) if represent_obj: return get_opposition(represent_obj) else: printerr("RepresentManager: Name '%s' not found when getting opposition." % name) return [] ## 获取所有已定义的象征。 ## @return: 包含所有 Represent 对象的数组。 static func get_all_represents() -> Array[Represent]: _ensure_initialized() # 返回字典中所有值的数组 return _represents_by_name.values() ## [私有] 确保内部数据结构已初始化。 ## 这是一个"懒加载"实现,只在第一次调用公共函数时执行一次初始化。 static func _ensure_initialized() -> void: if _is_initialized: return # 1. 初始化按名称查找的字典 # 注意:键使用 StringName 以获得最佳性能。 # 修正了 target 部分的键名以匹配变量名。 _represents_by_name = { # region stage &"armor": preload("res://static/represent/resource/stage/armor.tres"), &"attack": preload("res://static/represent/resource/stage/attack.tres"), &"attacked": preload("res://static/represent/resource/stage/attacked.tres"), &"destroy": preload("res://static/represent/resource/stage/destroy.tres"), &"die": preload("res://static/represent/resource/stage/die.tres"), &"discard": preload("res://static/represent/resource/stage/discard.tres"), &"draw": preload("res://static/represent/resource/stage/draw.tres"), &"draw_pile": preload("res://static/represent/resource/stage/draw_pile.tres"), &"enter": preload("res://static/represent/resource/stage/enter.tres"), &"exit": preload("res://static/represent/resource/stage/exit.tres"), &"extract": preload("res://static/represent/resource/stage/extract.tres"), &"get_buff": preload("res://static/represent/resource/stage/get_buff.tres"), &"hp_decrease": preload("res://static/represent/resource/stage/HP_decrease.tres"), &"hp_increase": preload("res://static/represent/resource/stage/HP_increase.tres"), &"lose_buff": preload("res://static/represent/resource/stage/lose_buff.tres"), &"magic": preload("res://static/represent/resource/stage/magic.tres"), &"play_card": preload("res://static/represent/resource/stage/play_card.tres"), &"prayer": preload("res://static/represent/resource/stage/prayer.tres"), &"release": preload("res://static/represent/resource/stage/release.tres"), &"seal": preload("res://static/represent/resource/stage/seal.tres"), &"shield_decrease": preload("res://static/represent/resource/stage/shield_decrease.tres"), &"shield_increase": preload("res://static/represent/resource/stage/shield_increase.tres"), &"shuffle": preload("res://static/represent/resource/stage/shuffle.tres"), &"stage_broken": preload("res://static/represent/resource/stage/stage_broken.tres"), &"stage_cost": preload("res://static/represent/resource/stage/stage_cost.tres"), &"stage_decrease_damage": preload("res://static/represent/resource/stage/stage_decrease_damage.tres"), &"stage_deficit": preload("res://static/represent/resource/stage/stage_deficit.tres"), &"stage_evil": preload("res://static/represent/resource/stage/stage_evil.tres"), &"stage_hard": preload("res://static/represent/resource/stage/stage_hard.tres"), &"stage_increase_damage": preload("res://static/represent/resource/stage/stage_increase_damage.tres"), &"stage_inventory": preload("res://static/represent/resource/stage/stage_inventory.tres"), &"stage_lush": preload("res://static/represent/resource/stage/stage_lush.tres"), &"stage_pollution": preload("res://static/represent/resource/stage/stage_pollution.tres"), &"stage_purification": preload("res://static/represent/resource/stage/stage_purification.tres"), &"stage_seed": preload("res://static/represent/resource/stage/stage_seed.tres"), &"stage_vitality": preload("res://static/represent/resource/stage/stage_vitality.tres"), &"stage_withered": preload("res://static/represent/resource/stage/stage_withered.tres"), &"summon": preload("res://static/represent/resource/stage/summon.tres"), &"toning": preload("res://static/represent/resource/stage/toning.tres"), &"turn_end": preload("res://static/represent/resource/stage/turn_end.tres"), &"turn_start": preload("res://static/represent/resource/stage/turn_start.tres"), &"unseal": preload("res://static/represent/resource/stage/unseal.tres"), # region kind &"black": preload("res://static/represent/resource/kind/black.tres"), &"blue": preload("res://static/represent/resource/kind/blue.tres"), &"colourless": preload("res://static/represent/resource/kind/colourless.tres"), &"cyan": preload("res://static/represent/resource/kind/cyan.tres"), &"debuff": preload("res://static/represent/resource/kind/debuff.tres"), &"green": preload("res://static/represent/resource/kind/green.tres"), &"iridescence": preload("res://static/represent/resource/kind/iridescence.tres"), &"magenta": preload("res://static/represent/resource/kind/magenta.tres"), &"multiple_colors": preload("res://static/represent/resource/kind/multiple_colors.tres"), &"red": preload("res://static/represent/resource/kind/red.tres"), &"salt_fish_symbol": preload("res://static/represent/resource/kind/salt_fish_symbol.tres"), &"white": preload("res://static/represent/resource/kind/white.tres"), &"yellow": preload("res://static/represent/resource/kind/yellow.tres"), # region life &"auto": preload("res://static/represent/resource/life/auto.tres"), &"dissipate": preload("res://static/represent/resource/life/dissipate.tres"), &"enable": preload("res://static/represent/resource/life/enable.tres"), &"fragile": preload("res://static/represent/resource/life/fragile.tres"), &"invariant": preload("res://static/represent/resource/life/invariant.tres"), &"lightning": preload("res://static/represent/resource/life/lightning.tres"), &"lock": preload("res://static/represent/resource/life/lock.tres"), &"unavailable": preload("res://static/represent/resource/life/unavailable.tres"), &"weight": preload("res://static/represent/resource/life/weight.tres"), # region number &"broken": preload("res://static/represent/resource/number/broken.tres"), &"cost": preload("res://static/represent/resource/number/cost.tres"), &"decrease_damage": preload("res://static/represent/resource/number/decrease_damage.tres"), &"deficit": preload("res://static/represent/resource/number/deficit.tres"), &"evil": preload("res://static/represent/resource/number/evil.tres"), &"hard": preload("res://static/represent/resource/number/hard.tres"), &"increase_damage": preload("res://static/represent/resource/number/increase_damage.tres"), &"inventory": preload("res://static/represent/resource/number/inventory.tres"), &"lush": preload("res://static/represent/resource/number/lush.tres"), &"pollution": preload("res://static/represent/resource/number/pollution.tres"), &"purification": preload("res://static/represent/resource/number/purification.tres"), &"seed": preload("res://static/represent/resource/number/seed.tres"), &"vitality": preload("res://static/represent/resource/number/vitality.tres"), &"withered": preload("res://static/represent/resource/number/withered.tres"), # region symbol &"brave": preload("res://static/represent/resource/symbol/brave.tres"), &"chameleon": preload("res://static/represent/resource/symbol/chameleon.tres"), &"cockroach": preload("res://static/represent/resource/symbol/cockroach.tres"), &"error": preload("res://static/represent/resource/symbol/error.tres"), &"prohibit_blue": preload("res://static/represent/resource/symbol/prohibit_blue.tres"), &"prohibit_cyan": preload("res://static/represent/resource/symbol/prohibit_cyan.tres"), &"prohibit_green": preload("res://static/represent/resource/symbol/prohibit_green.tres"), &"prohibit_magenta": preload("res://static/represent/resource/symbol/prohibit_magenta.tres"), &"prohibit_red": preload("res://static/represent/resource/symbol/prohibit_red.tres"), &"prohibit_yellow": preload("res://static/represent/resource/symbol/prohibit_yellow.tres"), &"rainbow": preload("res://static/represent/resource/symbol/rainbow.tres"), &"salt_fish": preload("res://static/represent/resource/symbol/salt_fish.tres"), &"salt_fish_counter": preload("res://static/represent/resource/symbol/salt_fish_counter.tres"), &"salt_fish_king": preload("res://static/represent/resource/symbol/salt_fish_king.tres"), &"start": preload("res://static/represent/resource/symbol/start.tres"), &"strategy": preload("res://static/represent/resource/symbol/strategy.tres"), &"think": preload("res://static/represent/resource/symbol/think.tres"), &"wound": preload("res://static/represent/resource/symbol/wound.tres"), # region system &"anticlockwise": preload("res://static/represent/resource/system/anticlockwise.tres"), &"clockwise": preload("res://static/represent/resource/system/clockwise.tres"), &"concord": preload("res://static/represent/resource/system/concord.tres"), &"counter": preload("res://static/represent/resource/system/counter.tres"), &"damage": preload("res://static/represent/resource/system/damage.tres"), &"discord": preload("res://static/represent/resource/system/discord.tres"), &"duration": preload("res://static/represent/resource/system/duration.tres"), &"hand_limit": preload("res://static/represent/resource/system/hand_limit.tres"), &"hourglass": preload("res://static/represent/resource/system/hourglass.tres"), &"shield": preload("res://static/represent/resource/system/shield.tres"), &"turn": preload("res://static/represent/resource/system/turn.tres"), &"hp": preload("res://static/represent/resource/system/HP.tres"), # region target # Corrected keys to match variable names (assuming variable names are desired identifiers) &"all": preload("res://static/represent/resource/target/all.tres"), # Original code didn't have 'all' preload, assuming it should exist &"all_enemy": preload("res://static/represent/resource/target/all_enemy.tres"), &"all_teammate": preload("res://static/represent/resource/target/all_teammate.tres"), &"one": preload("res://static/represent/resource/target/one.tres"), &"one_enemy": preload("res://static/represent/resource/target/one_enemy.tres"), &"one_myself": preload("res://static/represent/resource/target/one_myself.tres"), &"one_teammate": preload("res://static/represent/resource/target/one_teammate.tres"), &"random": preload("res://static/represent/resource/target/random.tres"), # Original code didn't have 'random' preload, assuming it should exist &"random_enemy": preload("res://static/represent/resource/target/random_enemy.tres"), &"random_teammate": preload("res://static/represent/resource/target/random_teammate.tres"), # Add any missing Represents based on your project structure if needed # Example: &"some_new_represent": preload("res://path/to/some_new_represent.tres") } # 2. 初始化对立象征的映射关系 # 使用对称定义,确保 map[A] 包含 B,并且 map[B] 包含 A。 # 注意:键和值都需要是 Represent 对象。 # 为了在初始化时直接使用,这里直接使用 preload 结果。 # 更好的方式可能是先将所有 preload 结果存入临时变量。 # --- Define preloaded resources for mapping --- var INCREASE_DAMAGE = _represents_by_name.get(&"increase_damage") var DECREASE_DAMAGE = _represents_by_name.get(&"decrease_damage") var VITALITY = _represents_by_name.get(&"vitality") var COST = _represents_by_name.get(&"cost") var HARD = _represents_by_name.get(&"hard") var BROKEN = _represents_by_name.get(&"broken") var LUSH = _represents_by_name.get(&"lush") var WITHERED = _represents_by_name.get(&"withered") var PURIFICATION = _represents_by_name.get(&"purification") var POLLUTION = _represents_by_name.get(&"pollution") var SEED = _represents_by_name.get(&"seed") var EVIL = _represents_by_name.get(&"evil") # --- End Define --- # Check if all required resources were loaded before mapping if not [INCREASE_DAMAGE, DECREASE_DAMAGE, VITALITY, COST, HARD, BROKEN, LUSH, WITHERED, PURIFICATION, POLLUTION, SEED, EVIL].has(null): _opposition_map = { # 加减攻 (Damage increase/decrease) # A -> [B], B -> [A] INCREASE_DAMAGE: [DECREASE_DAMAGE], DECREASE_DAMAGE: [INCREASE_DAMAGE], # 生机与代价 (Vitality/Cost) # A -> [B], B -> [A] VITALITY: [COST], COST: [VITALITY], # 坚硬与破碎 (Hard/Broken) # A -> [B], B -> [A] HARD: [BROKEN], BROKEN: [HARD], # 茂盛与枯萎 (Lush/Withered) # A -> [B], B -> [A] LUSH: [WITHERED], WITHERED: [LUSH], # 净化与污染 (Purification/Pollution) # A -> [B], B -> [A] PURIFICATION: [POLLUTION], POLLUTION: [PURIFICATION], # 种子与邪恶 (Seed/Evil) # A -> [B], B -> [A] SEED: [EVIL], EVIL: [SEED], # Add other symmetric relationships here following the same pattern # Example: # SEAL: [UNSEAL], # UNSEAL: [SEAL], } else: printerr("RepresentManager: One or more resources failed to load, skipping opposition map initialization.") # Optionally, still populate map with available resources if desired, or leave empty. # 标记为已初始化 _is_initialized = true print("RepresentManager initialized with %d represents." % _represents_by_name.size()) #endregionKey Changes and Explanations:
@tool: Added for editor compatibility.- Removed Static Variables: All
static var ... = preload(...)lines were removed. Access is now solely throughget_represent(name). - Consolidated Dictionary: The
_represents_by_namedictionary now contains all thepreloadcalls. This is still manual but consolidates the definitions. - Corrected Target Keys: The keys in the
targetsection of the dictionary were updated (e.g.,&"all_enemy",&"one_enemy") to more closely match the corresponding variable names provided in the original code snippet (assuming those variable names represented the desired string identifiers). Addedallandrandombased on common patterns, assuming they should exist. - Refined
_opposition_map:- Defined oppositions symmetrically (e.g.,
INCREASE_DAMAGEmaps toDECREASE_DAMAGE, andDECREASE_DAMAGEmaps back toINCREASE_DAMAGE). - Used temporary variables holding the actual
Representobjects (obtained viaget_representafter the dictionary is populated) as keys and values in the map. This is safer than using thepreloadcalls directly within the map definition itself, especially if loading fails. Added checks to ensure resources loaded correctly before defining the map.
- Defined oppositions symmetrically (e.g.,
- Simplified Functions: Removed
has_opposition_stringas it's easily derived. Renamedget_opposition_stringtoget_opposition_by_namefor clarity. Added error printing if a name is not found. - Uncommented
get_all_represents: Made the function available. - Added Print Statement: Included a print statement upon successful initialization to confirm it works.
How to Use:
- Get a Represent:
var shield_represent = RepresentManager.get_represent("shield") if shield_represent: print("Found shield represent!") var unknown_represent = RepresentManager.get_represent("non_existent_key") if unknown_represent == null: print("Could not find represent for non_existent_key.") - Check/Get Oppositions:
var armor_represent = RepresentManager.get_represent("armor") if armor_represent and RepresentManager.has_opposition(armor_represent): var oppositions = RepresentManager.get_opposition(armor_represent) print("Armor has oppositions: ", oppositions) else: print("Armor has no defined oppositions.") var increase_damage_opposites = RepresentManager.get_opposition_by_name("increase_damage") print("Opposites of increase_damage: ", increase_damage_opposites) # Should show [DECREASE_DAMAGE]
This revised structure is cleaner, less redundant, and slightly more robust. Remember to ensure the file paths in the
_represents_by_namedictionary are correct for your project structure.- Centralize access to
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.