上车逻辑

This commit is contained in:
oldpeper 2024-10-23 09:12:15 +08:00
parent e7d6487487
commit 4cea864708
7 changed files with 187 additions and 28 deletions

10
.gitignore vendored
View File

@ -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/

View File

@ -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();
}
}
}

View File

@ -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<CarObject> 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;
/// <summary>
/// 当前选中车辆
/// </summary>
[SerializeField] public CarObject seleCarObject;
/// <summary>
/// 总车辆节点
/// </summary>
[SerializeField] public List<CarObject> carObjects;
/// <summary>
/// 标准格子预制体
/// </summary>
[SerializeField] private GameObject templateGrid;
/// <summary>
/// 等待节点
/// </summary>
public WaitSlot WaitSlot => _waitSlot;
[SerializeField] private WaitSlot _waitSlot;
/// <summary>
/// 地图格子
/// </summary>
public Grid[,] MapGrid ;
/// <summary>
/// 缓存格子
/// </summary>
[SerializeField] private List<Grid> MapGrids;
/// <summary>
/// 地图格子生成节点
/// </summary>
[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<CarObject>());
car.GetComponent<CarObject>().movePostion = startCar;
car.GetComponent<CarObject>().carColor = (ColorEnum)colorIndex;
}
carObjects[0].movePostion = waitCar;
seleCarObject = carObjects[0];
seleCarObject.CarAni();
}

View File

@ -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<PassengerObject> _passengerObjects;
public ColorEnum carColor;
[SerializeField] private List<Grid> _carGrids;
public ColorEnum carColor
{
get { return _colorEnum; }
set
{
_colorEnum = value;
if (_image==null)
{
_image = GetComponent<Image>();
}
_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;
});
}
}

View File

@ -111,12 +111,15 @@ public class Grid : MonoBehaviour
public void PassengerAni()
{
gAniPlay = true;
if (passenger!=null)
{
passenger.transform.DOMove(transform.position, 0.2f).OnComplete(() =>
{
gAniPlay = false;
action?.Invoke();
});
}
}
public void ResetPosition()
{

View File

@ -18,20 +18,14 @@ public class PassengerObject : MonoBehaviour
{
_image = GetComponent<Image>();
}
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]

View File

@ -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<Grid> waits => waitGrids;
[SerializeField]
private List<Grid> waitGrids;
@ -16,7 +18,11 @@ public class WaitSlot : MonoBehaviour
grid.action = AniEndEvent;
}
}
/// <summary>
/// 判断是否有可移动位置
/// </summary>
/// <param name="mGrid"></param>
/// <returns></returns>
public bool MoveOpen(out Grid mGrid)
{
mGrid = null;
@ -28,9 +34,11 @@ public class WaitSlot : MonoBehaviour
return false;
}
/// <summary>
/// 到达等待区域事件
/// </summary>
public void AniEndEvent()
{
GameManager.ins.MoveCarEvent();
}
}