From 4cea864708fa2517c1d963b077105cfdad080376 Mon Sep 17 00:00:00 2001 From: oldpeper <96619050+oldpeper@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:12:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E8=BD=A6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 10 +++ d2/Assets/Script/GameManager.cs | 26 +++++- d2/Assets/Script/Map/MapManager.cs | 50 +++++++++-- d2/Assets/Script/ObjectEndity/CarObject.cs | 84 ++++++++++++++++++- d2/Assets/Script/ObjectEndity/Grid.cs | 11 ++- .../Script/ObjectEndity/PassengerObject.cs | 20 ++--- d2/Assets/Script/ObjectEndity/WaitSlot.cs | 14 +++- 7 files changed, 187 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index eac1b512..8d074381 100644 --- a/.gitignore +++ b/.gitignore @@ -77,3 +77,13 @@ d2/Library/ d2/Temp/ d2/obj/ d2/Logs/ +b1/Library/ +b1/Temp/ +b1/obj/ +b1/Logs/ +b1/.idea/ +b1/.vsconfig +box1/Library/ +box1/Temp/Burst/burst-aoty26ykpfz.1k1/lib_burst_generated_part_a6f5259e22ed809ef3937424c4bd686d.gbc +box1/Temp/ +box1/Logs/ diff --git a/d2/Assets/Script/GameManager.cs b/d2/Assets/Script/GameManager.cs index 36168b9a..64285835 100644 --- a/d2/Assets/Script/GameManager.cs +++ b/d2/Assets/Script/GameManager.cs @@ -53,7 +53,31 @@ namespace Script public void MoveCarEvent() { + foreach (var waitSlot in MapManager.ins.WaitSlot.waits) + { + if (MapManager.ins.seleCar.CarMove(out var carGrid)) + { + if (waitSlot.passenger.passColor==MapManager.ins.seleCar.carColor) + { + carGrid.passenger = waitSlot.passenger; + waitSlot.passenger = null; + carGrid.passenger.transform.parent = carGrid.transform; + carGrid.PassengerAni(); + } + } + + } } + + public void NextCar() + { + MapManager.ins.seleCarObject.movePostion = MapManager.ins.endCar; + MapManager.ins.seleCarObject.CarAni(); + MapManager.ins.carObjects.Remove(MapManager.ins.seleCarObject); + MapManager.ins.seleCarObject = MapManager.ins.carObjects[0]; + MapManager.ins.seleCarObject.movePostion = MapManager.ins.waitCar; + MapManager.ins.seleCarObject.CarAni(); + } } -} +} \ No newline at end of file diff --git a/d2/Assets/Script/Map/MapManager.cs b/d2/Assets/Script/Map/MapManager.cs index 1d848b82..043f3a4f 100644 --- a/d2/Assets/Script/Map/MapManager.cs +++ b/d2/Assets/Script/Map/MapManager.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Serialization; +using Random = UnityEngine.Random; namespace Script.Map { @@ -10,15 +11,40 @@ namespace Script.Map public class MapManager : MonoBehaviour { public static MapManager ins; - [SerializeField] private CarObject seleCarObject; - - [SerializeField] private List carObjects; + [SerializeField] private GameObject carObj; + [SerializeField] private Transform carParent; + [SerializeField] public Transform startCar; + [SerializeField] public Transform waitCar; + [SerializeField] public Transform endCar; + public CarObject seleCar => seleCarObject; + /// + /// 当前选中车辆 + /// + [SerializeField] public CarObject seleCarObject; + /// + /// 总车辆节点 + /// + [SerializeField] public List carObjects; + /// + /// 标准格子预制体 + /// [SerializeField] private GameObject templateGrid; + /// + /// 等待节点 + /// public WaitSlot WaitSlot => _waitSlot; [SerializeField] private WaitSlot _waitSlot; - + /// + /// 地图格子 + /// public Grid[,] MapGrid ; + /// + /// 缓存格子 + /// [SerializeField] private List MapGrids; + /// + /// 地图格子生成节点 + /// [SerializeField] private Transform gridParent; public int x => mX; [SerializeField] private int mX; @@ -40,6 +66,20 @@ namespace Script.Map { MapGrid[grid.X, grid.Y] = grid; } + + for (int i = 0; i < 10; i++) + { + var car = Instantiate(carObj, carParent); + car.transform.position = startCar.position; + var colorIndex =Random.Range(0, 3); + carObjects.Add(car.GetComponent()); + car.GetComponent().movePostion = startCar; + car.GetComponent().carColor = (ColorEnum)colorIndex; + } + + carObjects[0].movePostion = waitCar; + seleCarObject = carObjects[0]; + seleCarObject.CarAni(); } @@ -94,4 +134,4 @@ namespace Script.Map MapGrid[_gx,_gy].SetBool(bo); } } -} +} \ No newline at end of file diff --git a/d2/Assets/Script/ObjectEndity/CarObject.cs b/d2/Assets/Script/ObjectEndity/CarObject.cs index e1a51c44..e7ae5126 100644 --- a/d2/Assets/Script/ObjectEndity/CarObject.cs +++ b/d2/Assets/Script/ObjectEndity/CarObject.cs @@ -1,9 +1,89 @@ +using System; using System.Collections; using System.Collections.Generic; +using System.Linq; +using DG.Tweening; +using Script; using UnityEngine; +using UnityEngine.UI; public class CarObject : MonoBehaviour { - [SerializeField] private List _passengerObjects; - public ColorEnum carColor; + [SerializeField] private List _carGrids; + + public ColorEnum carColor + { + get { return _colorEnum; } + set + { + _colorEnum = value; + if (_image==null) + { + _image = GetComponent(); + } + + _image.color = _colorEnum switch + { + ColorEnum.white => Color.white, + ColorEnum.red => Color.red, + ColorEnum.yellow => Color.yellow, + _ => throw new ArgumentOutOfRangeException() + }; + } + } + [SerializeField] private ColorEnum _colorEnum; + public Transform movePostion; + private bool aniPlay=false; + [SerializeField] private Image _image; + private void Awake() + { + foreach (var grid in _carGrids) + { + grid.action = NextEvent; + } + + aniPlay = false; + } + + public bool CarMove(out Grid carGrid) + { + carGrid = null; + foreach (var grid in _carGrids.Where(grid => grid.passenger==null)) + { + carGrid = grid; + return true; + } + return false; + } + + public void NextEvent() + { + bool _lsbo = true; + foreach (var grid in _carGrids) + { + if (grid.passenger==null) + { + _lsbo = false; + } + + if (grid.aniPlay) + { + _lsbo = false; + } + } + + if (_lsbo) + { + GameManager.ins.NextCar(); + } + } + + public void CarAni() + { + aniPlay = true; + transform.DOMove(movePostion.position, 0.2f).OnComplete(() => + { + aniPlay = false; + }); + } } diff --git a/d2/Assets/Script/ObjectEndity/Grid.cs b/d2/Assets/Script/ObjectEndity/Grid.cs index 920d5680..50efde63 100644 --- a/d2/Assets/Script/ObjectEndity/Grid.cs +++ b/d2/Assets/Script/ObjectEndity/Grid.cs @@ -111,11 +111,14 @@ public class Grid : MonoBehaviour public void PassengerAni() { gAniPlay = true; - passenger.transform.DOMove(transform.position, 0.2f).OnComplete(() => + if (passenger!=null) { - gAniPlay = false; - action?.Invoke(); - }); + passenger.transform.DOMove(transform.position, 0.2f).OnComplete(() => + { + gAniPlay = false; + action?.Invoke(); + }); + } } public void ResetPosition() { diff --git a/d2/Assets/Script/ObjectEndity/PassengerObject.cs b/d2/Assets/Script/ObjectEndity/PassengerObject.cs index 27374c02..963b6b61 100644 --- a/d2/Assets/Script/ObjectEndity/PassengerObject.cs +++ b/d2/Assets/Script/ObjectEndity/PassengerObject.cs @@ -18,20 +18,14 @@ public class PassengerObject : MonoBehaviour { _image = GetComponent(); } - switch (objColor) + + _image.color = objColor switch { - case ColorEnum.white: - _image.color = Color.white; - break; - case ColorEnum.red: - _image.color = Color.red; - break; - case ColorEnum.yellow: - _image.color = Color.yellow; - break; - default: - throw new ArgumentOutOfRangeException(); - } + ColorEnum.white => Color.white, + ColorEnum.red => Color.red, + ColorEnum.yellow => Color.yellow, + _ => throw new ArgumentOutOfRangeException() + }; } } [SerializeField] diff --git a/d2/Assets/Script/ObjectEndity/WaitSlot.cs b/d2/Assets/Script/ObjectEndity/WaitSlot.cs index 2c52fdde..479f6623 100644 --- a/d2/Assets/Script/ObjectEndity/WaitSlot.cs +++ b/d2/Assets/Script/ObjectEndity/WaitSlot.cs @@ -2,10 +2,12 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using Script; using UnityEngine; public class WaitSlot : MonoBehaviour { + public List waits => waitGrids; [SerializeField] private List waitGrids; @@ -16,7 +18,11 @@ public class WaitSlot : MonoBehaviour grid.action = AniEndEvent; } } - + /// + /// 判断是否有可移动位置 + /// + /// + /// public bool MoveOpen(out Grid mGrid) { mGrid = null; @@ -28,9 +34,11 @@ public class WaitSlot : MonoBehaviour return false; } - + /// + /// 到达等待区域事件 + /// public void AniEndEvent() { - + GameManager.ins.MoveCarEvent(); } }