PacMan/Assets/Scripts/MapDot.cs
2025-03-17 21:56:02 +08:00

55 lines
1.6 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.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class MapDot : MonoBehaviour
{
public GameObject pelletPrefab;
public LayerMask ObstadeLayer;
public GameObject startPosGameObject;
public GameObject endPosGameObject;
public Vector2 startPos;
public Vector2 endPos;
private void Awake()
{
startPos = startPosGameObject.transform.position;
endPos = endPosGameObject.transform.position;
GeneratePellets();
}
void GeneratePellets()
{
/*int minX = Mathf.FloorToInt(Mathf.Min(startPos.x, endPos.x));
int maxX = Mathf.CeilToInt(Mathf.Max(startPos.x, endPos.x));
int minY = Mathf.FloorToInt(Mathf.Min(startPos.y, endPos.y));
int maxY = Mathf.CeilToInt(Mathf.Max(startPos.y, endPos.y));*/
float minX = Mathf.Min(startPos.x, endPos.x);
float maxX = Mathf.Max(startPos.x, endPos.x);
float minY = Mathf.Min(startPos.y, endPos.y);
float maxY = Mathf.Max(startPos.y, endPos.y);
for (float x = minX; x <= maxX; x++)
{
for (float y = minY; y <= maxY; y++)
{
Vector2 checkPos = new Vector2(x, y);
// ¼ì²âWall²ãÅöײÌå
Collider2D hit = Physics2D.OverlapBox(checkPos, Vector2.one * 0.1f, 0, ObstadeLayer);
if (hit == null)
{
Instantiate(pelletPrefab, checkPos, Quaternion.identity, transform);
GameManager.instance.AddDotNum(1);
}
}
}
}
}