925236118/alphaagent/godot-weapon-system
Claude Code and agent skill by 925236118/alphaagent
Trust tier 3
- community
- By 925236118
- Coding
- 925236118/alphaagent
- addons/agent/skills/default_skills/godot-weapon-system/SKILL.md
- Updated 2 Oct 2026
- Version 1.0.01 version recorded
Source check pending
Checking that the SKILL.md file is still in the repository.
Godot 4武器切换系统,支持武器类型枚举、武器基类设计、多种切换逻辑、输入绑定和锁定机制。
Install
npx vskill@latest install 925236118/alphaagent/godot-weapon-systemSKILL.md
Godot 4 武器切换系统
完整的武器管理系统,支持多类型武器、快速切换、冷却锁定、输入绑定。
何时使用此技能
- 实现玩家武器切换
- 近战/远程武器系统
- 武器冷却与锁定
- 武器类型分类管理
武器类型枚举
gdscript
# weapon_type.gdclass_name WeaponTypeextends RefCounted## 武器类型枚举enum Type {NONE = -1,SWORD = 0, # 近战 - 剑AXE = 1, # 近战 - 斧HAMMER = 2, # 近战 - 锤BOW = 3, # 远程 - 弓CROSSBOW = 4, # 远程 - 弩STAFF = 5, # 法杖SHIELD = 6, # 盾牌THROWING = 7, # 投掷武器}## 类型名称映射const TYPE_NAMES: Dictionary = {Type.SWORD: "剑",Type.AXE: "斧",Type.HAMMER: "锤",Type.BOW: "弓",Type.CROSSBOW: "弩",Type.STAFF: "法杖",Type.SHIELD: "盾牌",Type.THROWING: "投掷武器"}## 获取类型名称static func get_name(type: Type) -> String:return TYPE_NAMES.get(type, "未知")## 是否是近战武器static func is_melee(type: Type) -> bool:return type in [Type.SWORD, Type.AXE, Type.HAMMER, Type.SHIELD]## 是否是远程武器static func is_ranged(type: Type) -> bool:return type in [Type.BOW, Type.CROSSBOW, Type.STAFF, Type.THROWING]
武器基类设计
WeaponData 武器数据资源
gdscript
# weapon_data.gdclass_name WeaponDataextends Resource## 武器唯一标识符@export var id: StringName## 武器名称@export var weapon_name: String## 武器类型@export var weapon_type: WeaponType.Type = WeaponType.Type.SWORD## 武器 Prefab 场景@export var weapon_scene: PackedScene## 武器图标(用于UI)@export var icon: Texture2D## 基础属性@export var base_damage: int = 10@export var attack_speed: float = 1.0@export var attack_range: float = 1.0## 冷却时间@export var cooldown: float = 0.5## 是否需要弹药@export var needs_ammo: bool = false@export var max_ammo: int = 0@export var ammo_type: StringName = &""## 特殊属性@export var can_block: bool = false@export var block_ratio: float = 0.5## 动画@export var attack_animation: StringName = &"attack"@export var idle_animation: StringName = &"idle"## 获取武器类型名称func get_type_name() -> String:return WeaponType.get_name(weapon_type)## 是否是近战武器func is_melee() -> bool:return WeaponType.is_melee(weapon_type)## 是否是远程武器func is_ranged() -> bool:return WeaponType.is_ranged(weapon_type)
Weapon 基类
gdscript
# weapon.gdclass_name Weaponextends Node2D## 信号:攻击开始signal attack_started## 信号:攻击结束signal attack_finished## 信号:攻击命中signal attack_hit(target: Node)## 信号:弹药变化signal ammo_changed(current: int, max: int)@export var weapon_data: WeaponDatavar _owner: CharacterBody2Dvar _current_ammo: intvar _cooldown_timer: float = 0.0var _is_attacking: bool = falsefunc _ready() -> void:_owner = get_owner() as CharacterBody2Dif weapon_data and weapon_data.needs_ammo:_current_ammo = weapon_data.max_ammoammo_changed.emit(_current_ammo, weapon_data.max_ammo)func _process(delta: float) -> void:if _cooldown_timer > 0:_cooldown_timer -= deltafunc _physics_process(delta: float) -> void:pass## 使用武器攻击func attack() -> bool:if not can_attack():return falseif weapon_data.needs_ammo and _current_ammo <= 0:return false_is_attacking = true_start_attack()attack_started.emit()if weapon_data.needs_ammo:_current_ammo -= 1ammo_changed.emit(_current_ammo, weapon_data.max_ammo)# 重置冷却_cooldown_timer = weapon_data.cooldownreturn truefunc _start_attack() -> void:# 播放攻击动画if _owner.has_method("play_animation"):_owner.play_animation(weapon_data.attack_animation)func _end_attack() -> void:_is_attacking = falseattack_finished.emit()## 停止攻击func stop_attack() -> void:_is_attacking = false_end_attack()## 是否可以攻击func can_attack() -> bool:if _is_attacking:return falseif _cooldown_timer > 0:return falsereturn true## 获取剩余冷却时间func get_cooldown_remaining() -> float:return maxf(_cooldown_timer, 0.0)## 装填弹药func reload(amount: int = 0) -> void:if not weapon_data.needs_ammo:returnif amount <= 0:_current_ammo = weapon_data.max_ammoelse:_current_ammo = mini(_current_ammo + amount, weapon_data.max_ammo)ammo_changed.emit(_current_ammo, weapon_data.max_ammo)## 命中处理(供Hitbox调用)func on_hit(target: Node) -> void:attack_hit.emit(target)
具体武器实现
gdscript
# sword.gdclass_name Swordextends Weapon@export var slash_effect_scene: PackedScenefunc _start_attack() -> void:super._start_attack()# 生成斩击特效if slash_effect_scene:var effect := slash_effect_scene.instantiate()get_tree().current_scene.add_child(effect)effect.global_position = global_positioneffect.rotation = _owner.rotationfunc on_hit(target: Node) -> void:if target.has_method("take_damage"):target.take_damage(weapon_data.base_damage)# 击退效果if target.has_method("apply_knockback"):var direction := (target.global_position - _owner.global_position).normalized()target.apply_knockback(direction, 200.0)
gdscript
# bow.gdclass_name Bowextends Weapon@export var arrow_scene: PackedScene@export var arrow_spawn_offset: Vector2 = Vector2(30, 0)func _start_attack() -> void:super._start_attack()# 发射箭矢if arrow_scene:var arrow := arrow_scene.instantiate()get_tree().current_scene.add_child(arrow)var spawn_pos := global_position + arrow_spawn_offset.rotated(global_rotation)arrow.initialize(spawn_pos, _owner.rotation, weapon_data.base_damage)
切换逻辑
WeaponSwitcher 武器切换器
gdscript
# weapon_switcher.gdclass_name WeaponSwitcherextends Node## 信号:武器切换signal weapon_switched(from_weapon: Weapon, to_weapon: Weapon)## 信号:武器被锁定signal weapon_locked(weapon_type: WeaponType.Type, unlock_time: float)## 信号:切换冷却开始signal switch_cooldown_started(duration: float)@export var weapon_slots: Array[WeaponData] = []@export var max_secondary_weapons: int = 2## 当前激活的武器索引var current_weapon_index: int = -1## 当前武器引用var current_weapon: Weapon = null## 武器节点字典var _weapon_nodes: Dictionary = {}## 切换冷却var _switch_cooldown: float = 0.3var _is_locked: bool = falsevar _lock_until: float = 0.0## 武器类型到索引的映射var _type_to_index: Dictionary = {}func _ready() -> void:_initialize_weapons()func _process(delta: float) -> void:_update_cooldown(delta)_update_lock(delta)func _update_cooldown(delta: float) -> void:if _switch_cooldown > 0:_switch_cooldown -= deltafunc _update_lock(delta: float) -> void:if _is_locked and Time.get_ticks_msec() / 1000.0 >= _lock_until:_is_locked = falsefunc _initialize_weapons() -> void:# 创建所有武器节点for i in weapon_slots.size():var data: WeaponData = weapon_slots[i]if data.weapon_scene == null:continuevar weapon: Weapon = data.weapon_scene.instantiate()weapon.weapon_data = dataweapon.set_process(false)weapon.visible = falseadd_child(weapon)_weapon_nodes[i] = weapon# 建立类型映射_type_to_index[data.weapon_type] = i# 默认装备第一把武器if weapon_slots.size() > 0:_equip_weapon(0)## 切换到指定索引的武器func switch_to_index(index: int) -> bool:if index < 0 or index >= weapon_slots.size():return falseif index == current_weapon_index:return trueif not can_switch():return false_switch_cooldown = 0.3switch_cooldown_started.emit(_switch_cooldown)var previous := current_weapon_unequip_current()_equip_weapon(index)weapon_switched.emit(previous, current_weapon)return true## 切换到指定类型func switch_to_type(weapon_type: WeaponType.Type) -> bool:if _type_to_index.has(weapon_type):return switch_to_index(_type_to_index[weapon_type])return false## 切换到下一把武器(循环)func switch_to_next() -> bool:if weapon_slots.size() <= 1:return falsevar next_index := (current_weapon_index + 1) % weapon_slots.size()return switch_to_index(next_index)## 切换到上一把武器(循环)func switch_to_previous() -> bool:if weapon_slots.size() <= 1:return falsevar prev_index := (current_weapon_index - 1 + weapon_slots.size()) % weapon_slots.size()return switch_to_index(prev_index)## 卸下当前武器func _unequip_current() -> void:if current_weapon != null:current_weapon.set_process(false)current_weapon.visible = falsecurrent_weapon.stop_attack()## 装备武器func _equip_weapon(index: int) -> void:current_weapon_index = indexcurrent_weapon = _weapon_nodes.get(index)if current_weapon != null:current_weapon.visible = truecurrent_weapon.set_process(true)## 是否可以切换func can_switch() -> bool:if _switch_cooldown > 0:return falseif _is_locked:return falseif current_weapon != null and current_weapon._is_attacking:return falsereturn true## 锁定特定类型武器func lock_weapon_type(weapon_type: WeaponType.Type, duration: float) -> void:_is_locked = true_lock_until = Time.get_ticks_msec() / 1000.0 + durationweapon_locked.emit(weapon_type, duration)## 解锁武器类型func unlock_weapon_type(weapon_type: WeaponType.Type) -> void:_is_locked = false## 获取当前武器数据func get_current_weapon_data() -> WeaponData:if current_weapon_index >= 0 and current_weapon_index < weapon_slots.size():return weapon_slots[current_weapon_index]return null## 获取当前武器类型func get_current_weapon_type() -> WeaponType.Type:var data := get_current_weapon_data()if data != null:return data.weapon_typereturn WeaponType.Type.NONE## 添加武器到槽位func add_weapon(data: WeaponData) -> bool:if weapon_slots.size() >= max_secondary_weapons + 1:return falseweapon_slots.append(data)var index := weapon_slots.size() - 1var weapon: Weapon = data.weapon_scene.instantiate()weapon.weapon_data = dataweapon.set_process(false)weapon.visible = falseadd_child(weapon)_weapon_nodes[index] = weapon_type_to_index[data.weapon_type] = indexreturn true## 移除武器func remove_weapon(index: int) -> bool:if index < 0 or index >= weapon_slots.size():return false# 如果是当前武器,切换到另一把if index == current_weapon_index:var next_index := (index + 1) % weapon_slots.size()if next_index != index:switch_to_index(next_index)else:_unequip_current()current_weapon_index = -1current_weapon = nullweapon_slots.remove_at(index)_weapon_nodes.erase(index)return true
输入绑定
输入映射配置
gdscript
# weapon_input_handler.gdclass_name WeaponInputHandlerextends Node@export var weapon_switcher: WeaponSwitcher## 输入映射名称const INPUT_SLOT_1 := "weapon_slot_1"const INPUT_SLOT_2 := "weapon_slot_2"const INPUT_SLOT_3 := "weapon_slot_3"const INPUT_SLOT_4 := "weapon_slot_4"const INPUT_SLOT_5 := "weapon_slot_5"const INPUT_NEXT_WEAPON := "next_weapon"const INPUT_PREV_WEAPON := "prev_weapon"const INPUT_ATTACK := "attack"const INPUT_BLOCK := "block"const INPUT_RELOAD := "reload"func _ready() -> void:_setup_input_actions()
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingaddons/agent/skills/default_skills/godot-weapon-system/SKILL.md
Popularity
- Repository stars
- 103
- Repository forks
- 14
- 7-day trend score
- 23
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/925236118/alphaagent/godot-weapon-system)Something wrong with this skill or its page? Tell us.