Notice
Recent Comments
Recent Posts
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Link
Today
Total
관리 메뉴

[게임개발자] 레드핑

[TIL] Godot4 GDScript로 구현한 2D Area Zone 적 타겟팅 시스템 본문

TIL

[TIL] Godot4 GDScript로 구현한 2D Area Zone 적 타겟팅 시스템

레드핑(redping) 2025. 2. 1. 17:09

📝 학습 내용

오늘은 Godot 4에서 Area2D를 활용하여 Zone 내 적 캐릭터들을 효과적으로 관리하고 타겟팅하는 시스템을 구현했습니다. Array를 활용하여 여러 적을 순차적으로 처리하는 방법을 학습했습니다.

🔍 구현 목표

  • Area2D를 통해 들어오는 적들을 감지하고 추적
  • 한 번에 한 명의 적만 타겟팅하여 공격
  • 현재 타겟이 제거되면 다음 적으로 타겟 전환

💡 주요 구현 내용

1. 적 감지 및 저장

extends Area2D

var enemies_in_range: Array = []
var current_target = null

func _ready():
    # Area2D 시그널 연결
    body_entered.connect(_on_body_entered)
    body_exited.connect(_on_body_exited)

func _on_body_entered(body: Node2D):
    if body.is_in_group("enemy"):
        enemies_in_range.append(body)
        print("Enemy entered: ", body.name)

2. 적 제거 및 관리

func _on_body_exited(body: Node2D):
    if body.is_in_group("enemy"):
        enemies_in_range.erase(body)
        if current_target == body:
            current_target = null
        print("Enemy exited: ", body.name)

3. 타겟팅 로직

func _process(delta):
    update_target()
    if current_target:
        attack_target()

func update_target():
    # 현재 타겟이 없거나 처치된 경우
    if current_target == null or !is_instance_valid(current_target):
        if enemies_in_range.size() > 0:
            current_target = enemies_in_range[0]

func attack_target():
    if current_target:
        # 공격 로직 구현
        var direction = (current_target.global_position - global_position).normalized()
        # 여기에 공격 관련 코드 추가
        print("Attacking: ", current_target.name)

🎯 개선사항

  • Vector2.distance_to()를 활용한 거리 기반 우선순위 시스템
  • 체력이 낮은 적 우선 타겟팅
  • 타겟 전환 쿨타임 구현
  • RayCast2D를 통한 시야 체크 추가

💭 회고

Godot의 Area2D와 시그널 시스템을 활용하니 다음과 같은 이점이 있었습니다:

  1. 2D 물리 시스템 활용으로 구현이 간편
  2. 시그널을 통한 깔끔한 이벤트 처리
  3. GDScript의 Array 메서드들로 손쉬운 적 관리
  4. Vector2를 활용한 간편한 2D 위치 계산

📚 다음 학습 계획

  • 2D 타일맵 네비게이션 시스템 학습
  • 상태 머신을 활용한 적 AI 개선
  • 2D 파티클을 활용한 공격 이펙트 구현

🔑 Key Point

Godot의 Area2D와 GDScript의 Array는 2D 게임에서 Zone 내 적 관리에 매우 효과적입니다. 특히 시그널 시스템과 Vector2 연산을 활용하면 2D 게임의 타겟팅 시스템을 효율적으로 구현할 수 있습니다.