AgentSkillsCN

Godot Patterns

当用户在 Swarm Dominion 中提出“实现单位选中功能”、“添加寻路机制”、“为单位创建状态机”、“实现战争迷雾效果”、“增加控制点占领机制”、“创建资源采集系统”、“实现战斗系统”,或在 Godot 4.x 中从事 RTS 游戏机制开发时,可选用此技能。它提供了 Godot 4.x 的开发模式与项目专属的规范指导。

SKILL.md
--- frontmatter
description: Use when implementing GDScript code, unit behaviors, game systems, or mechanics in Swarm Dominion. Provides Godot 4.x patterns and project-specific conventions.

Godot Patterns for Swarm Dominion

This skill provides domain knowledge for implementing game features in Swarm Dominion using Godot 4.x and GDScript.

Project Architecture

Node Hierarchy

code
Main (Node2D)
├── Units (Node2D) - Container for all units
│   └── UnitBase (CharacterBody2D) - Base class for units
├── Systems (Node) - Game systems container
└── UI (CanvasLayer) - UI elements

Autoloads (Singletons)

AutoloadPurpose
EventBusSignal-based communication between systems
GameManagerGame state, win conditions, match flow
AudioManagerSound effects and music

Key Base Classes

ClassExtendsPurpose
UnitBaseCharacterBody2DAll game units (drones, hunters, etc.)

GDScript Conventions

Naming

gdscript
class_name UnitBase extends CharacterBody2D

const MAX_HEALTH: int = 100          # UPPER_SNAKE_CASE for constants
@export var move_speed: float = 200.0 # snake_case for variables

var _private_var: String             # Leading underscore for private
var _is_moving: bool = false         # Boolean prefixed with is_/has_/can_

@onready var _sprite: Sprite2D = $Sprite2D  # @onready for node refs

Type Hints

Always use type hints for:

  • Function parameters
  • Function return types
  • Class variables
gdscript
func move_to(target: Vector2) -> void:
    _target_position = target
    _is_moving = true

func get_health() -> int:
    return _current_health

Signal Definitions

Define signals at the top of the class:

gdscript
class_name UnitBase extends CharacterBody2D

signal health_changed(new_health: int)
signal died

@export var max_health: int = 100

Common Patterns

Movement Pattern

Units use CharacterBody2D with move_and_slide() for physics-based movement:

  1. Track target position and moving state
  2. Calculate direction in _physics_process()
  3. Set velocity and call move_and_slide()
  4. Use arrival threshold to prevent jitter

See: examples/unit-movement.md

Input Handling Pattern

Use _unhandled_input() for game commands (allows UI to consume first):

gdscript
func _unhandled_input(event: InputEvent) -> void:
    if event.is_action_pressed("command"):
        var click_position = get_global_mouse_position()
        # Handle command...

Input actions are defined in project.godot:

  • select - Left mouse button
  • command - Right mouse button
  • attack_move - A key
  • camera_up/down/left/right - WASD keys

Group Membership Pattern

Use groups for unit queries:

gdscript
func _ready() -> void:
    add_to_group("units")
    add_to_group("player_units")  # or "enemy_units"

# Query all units
var all_units = get_tree().get_nodes_in_group("units")

Examples Index

Detailed implementation examples with code snippets and rationale:

ExamplePatternSource
unit-movement.mdClick-to-move with arrival detectionSPI-1349

References