Danro/Assets/Scripts/Behaviour/EntitySpawnerZone.cs
Danil Iarosh 28f5409187
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Implement EntitySpawnerZone override system
- Создан скрипт EntitySpawnerZone.cs для выделения зон замены префабов в Unity Editor.
- Модифицирован EntitySpawner.cs для поддержки зон при выборе префаба на основе координат точки спавна.
2026-08-07 14:39:18 +03:00

84 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using UnityEngine;
namespace RR.Game2D.Behaviour
{
[RequireComponent(typeof(Collider2D))]
[DisallowMultipleComponent]
public sealed class EntitySpawnerZone : MonoBehaviour
{
[Header("Override Prefabs")]
[Tooltip("Список префабов, которые будут спавниться вместо оригинальных префабов спавнера внутри этой зоны.")]
[SerializeField] private EntitySpawner.SpawnPrefabEntry[] prefabs;
private Collider2D _collider;
// Статический список всех активных зон на сцене для быстрого поиска
public static readonly List<EntitySpawnerZone> ActiveZones = new();
public EntitySpawner.SpawnPrefabEntry[] Prefabs => prefabs;
private void Awake()
{
_collider = GetComponent<Collider2D>();
// Убеждаемся, что коллайдер является триггером, чтобы не вызывать физических коллизий с персонажами
if (_collider != null)
{
_collider.isTrigger = true;
}
}
private void OnEnable()
{
ActiveZones.Add(this);
}
private void OnDisable()
{
ActiveZones.Remove(this);
}
/// <summary>
/// Проверяет, находится ли мировая точка внутри коллайдера зоны.
/// </summary>
public bool IsPointInside(Vector3 point)
{
if (_collider == null)
{
_collider = GetComponent<Collider2D>();
}
return _collider != null && _collider.OverlapPoint(point);
}
private void OnDrawGizmos()
{
if (_collider == null)
{
_collider = GetComponent<Collider2D>();
}
if (_collider == null) return;
// Рисуем оранжевую рамку зоны замены в редакторе
Gizmos.color = new Color(1f, 0.5f, 0f, 0.2f);
if (_collider is BoxCollider2D box)
{
Vector3 position = transform.TransformPoint(box.offset);
Vector3 size = Vector3.Scale(box.size, transform.lossyScale);
Gizmos.DrawCube(position, size);
Gizmos.color = new Color(1f, 0.5f, 0f, 0.8f);
Gizmos.DrawWireCube(position, size);
}
else
{
// Для других типов коллайдеров рисуем сферу в центре объекта
Gizmos.DrawSphere(transform.position, 0.5f);
}
}
}
}