fix di chuyen update maze ui update map
This commit is contained in:
38
Assets/Scripts/GameSetup/Maze/GoalSpot.cs
Normal file
38
Assets/Scripts/GameSetup/Maze/GoalSpot.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
using Fusion;
|
||||
|
||||
namespace OnlyScove.Scripts.GameSetup.Maze
|
||||
{
|
||||
public class GoalSpot : MonoBehaviour
|
||||
{
|
||||
private bool _isActivated = false;
|
||||
|
||||
private void OnTriggerEnter(Collider healthcare)
|
||||
{
|
||||
if (_isActivated) return;
|
||||
|
||||
// Kiểm tra xem đối tượng chạm vào có phải là người chơi không
|
||||
PlayerStateMachine player = healthcare.GetComponentInParent<PlayerStateMachine>();
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
bool isOffline = player.Runner == null || !player.Runner.IsRunning;
|
||||
|
||||
// Nếu có mạng, kiểm tra quyền điều khiển. Nếu offline, mặc định là người chơi cục bộ.
|
||||
bool canActivate = isOffline || (player.Object != null && player.Object.HasInputAuthority);
|
||||
|
||||
if (canActivate)
|
||||
{
|
||||
_isActivated = true;
|
||||
player.CompleteMaze();
|
||||
|
||||
// Hiệu ứng hình ảnh khi chạm đích
|
||||
var renderer = GetComponent<Renderer>();
|
||||
if (renderer != null) renderer.material.color = Color.green;
|
||||
|
||||
Debug.Log("<color=cyan>Goal reached!</color>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/GameSetup/Maze/GoalSpot.cs.meta
Normal file
2
Assets/Scripts/GameSetup/Maze/GoalSpot.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3b0727dd84fbb046ac472bb1404b363
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Collections;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Fusion;
|
||||
using OnlyScove.Scripts.GameSetup.Maze;
|
||||
|
||||
public class MapLocation
|
||||
{
|
||||
@@ -14,7 +16,7 @@ public class MapLocation
|
||||
}
|
||||
}
|
||||
|
||||
public class Maze : MonoBehaviour
|
||||
public class Maze : NetworkBehaviour
|
||||
{
|
||||
public List<MapLocation> directions = new List<MapLocation>() {
|
||||
new MapLocation(1,0),
|
||||
@@ -27,14 +29,52 @@ public class Maze : MonoBehaviour
|
||||
public int scale = 6;
|
||||
public Transform _mapParentObjet;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
[Header("Sync & Materials")]
|
||||
[Networked] public int MazeSeed { get; set; }
|
||||
public Material wallMaterial;
|
||||
public Material floorMaterial;
|
||||
|
||||
[Header("Win Condition")]
|
||||
public GameObject goalPrefab;
|
||||
|
||||
public override void Spawned()
|
||||
{
|
||||
if (Object.HasStateAuthority)
|
||||
{
|
||||
// Nếu chưa có seed thì tạo mới
|
||||
if (MazeSeed == 0) MazeSeed = Random.Range(1, 99999);
|
||||
}
|
||||
|
||||
GenerateMazeWithSeed(MazeSeed);
|
||||
}
|
||||
|
||||
public void GenerateMazeWithSeed(int seed)
|
||||
{
|
||||
if (_mapParentObjet != null)
|
||||
{
|
||||
// Dùng list để tránh lỗi modify collection khi đang duyệt
|
||||
List<GameObject> children = new List<GameObject>();
|
||||
foreach (Transform child in _mapParentObjet) children.Add(child.gameObject);
|
||||
foreach (GameObject child in children) Destroy(child);
|
||||
}
|
||||
|
||||
Random.InitState(seed);
|
||||
InitialiseMap();
|
||||
Generate();
|
||||
|
||||
// Ép ô (1,1) là đường đi cho player spawn
|
||||
map[1, 1] = 0;
|
||||
|
||||
PlaceExit();
|
||||
DrawMap();
|
||||
}
|
||||
|
||||
public Vector3 GetPlayerSpawnPoint()
|
||||
{
|
||||
// Trả về vị trí tại ô (1,1). y = 1.0f cục bộ
|
||||
return transform.TransformPoint(new Vector3(1 * scale, 1.0f, 1 * scale));
|
||||
}
|
||||
|
||||
void InitialiseMap()
|
||||
{
|
||||
map = new byte[width,depth];
|
||||
@@ -55,18 +95,60 @@ public class Maze : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void PlaceExit()
|
||||
{
|
||||
int x = width - 2;
|
||||
int z = depth - 2;
|
||||
map[x, z] = 2;
|
||||
if (CountSquareNeighbours(x, z) == 0) map[x - 1, z] = 0;
|
||||
}
|
||||
|
||||
void DrawMap()
|
||||
{
|
||||
if (_mapParentObjet == null) _mapParentObjet = this.transform;
|
||||
|
||||
// Tạo sàn chống rơi và Z-fighting
|
||||
GameObject floor = GameObject.CreatePrimitive(PrimitiveType.Plane);
|
||||
floor.name = "Floor";
|
||||
floor.transform.SetParent(_mapParentObjet);
|
||||
floor.transform.localPosition = new Vector3((width - 1) * scale / 2f, -0.05f, (depth - 1) * scale / 2f);
|
||||
floor.transform.localScale = new Vector3(width * scale / 10f, 1, depth * scale / 10f);
|
||||
if (floorMaterial != null) floor.GetComponent<Renderer>().material = floorMaterial;
|
||||
|
||||
for (int z = 0; z < depth; z++)
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
Vector3 pos = new Vector3(x * scale, scale / 2f, z * scale);
|
||||
if (map[x, z] == 1)
|
||||
{
|
||||
Vector3 pos = new Vector3(x * scale, 0, z * scale);
|
||||
GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
wall.name = $"Wall_{x}_{z}";
|
||||
wall.transform.localScale = new Vector3(scale, scale, scale);
|
||||
wall.transform.position = pos;
|
||||
wall.transform.SetParent(_mapParentObjet.transform);
|
||||
wall.transform.SetParent(_mapParentObjet);
|
||||
wall.transform.localPosition = pos;
|
||||
if (wallMaterial != null) wall.GetComponent<Renderer>().material = wallMaterial;
|
||||
}
|
||||
else if (map[x, z] == 2)
|
||||
{
|
||||
GameObject goalObj;
|
||||
if (goalPrefab != null)
|
||||
goalObj = Instantiate(goalPrefab, transform.TransformPoint(pos), Quaternion.identity);
|
||||
else
|
||||
{
|
||||
goalObj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
||||
goalObj.GetComponent<Renderer>().material.color = Color.yellow;
|
||||
goalObj.transform.localScale = new Vector3(scale * 0.5f, scale * 0.5f, scale * 0.5f);
|
||||
}
|
||||
|
||||
goalObj.name = "Goal_WinSpot";
|
||||
goalObj.transform.SetParent(_mapParentObjet);
|
||||
goalObj.transform.localPosition = pos;
|
||||
|
||||
Collider col = goalObj.GetComponent<Collider>();
|
||||
if (col == null) col = goalObj.AddComponent<CapsuleCollider>();
|
||||
col.isTrigger = true;
|
||||
|
||||
if (goalObj.GetComponent<GoalSpot>() == null) goalObj.AddComponent<GoalSpot>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,4 +179,4 @@ public class Maze : MonoBehaviour
|
||||
{
|
||||
return CountSquareNeighbours(x,z) + CountDiagonalNeighbours(x,z);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user