123 lines
2.8 KiB
C#
123 lines
2.8 KiB
C#
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using NLDPB;
|
|
using Google.Protobuf.Collections;
|
|
using Gameplay.LegionBattle;
|
|
|
|
public class LBActor {
|
|
public uint MoveTime;
|
|
public float speed;
|
|
public GameObject obj;
|
|
|
|
public bool IsMoving;
|
|
|
|
public int MovingIndex;
|
|
|
|
public List<Vector3> movePaths;
|
|
|
|
public float disAll;
|
|
|
|
public void DoIdle(Vector3 pos) {
|
|
obj.transform.position = pos;
|
|
IsMoving = false;
|
|
}
|
|
|
|
public void DoMove(List<Vector3> paths) {
|
|
IsMoving = true;
|
|
MovingIndex = 1;
|
|
movePaths = paths;
|
|
speed = LegionBattleManager.Instance.CurBattle.Distance/(MoveTime*0.001f);
|
|
Debug.Log("speed=" + speed);
|
|
|
|
var dis = Vector3.Distance(obj.transform.position, movePaths[0]);
|
|
if(dis > 0.0f) {
|
|
//obj.transform.position = movePaths[0];
|
|
//MovingFast = true;
|
|
dis /= movePaths.Count-1;
|
|
speed = (LegionBattleManager.Instance.CurBattle.Distance+dis)/(MoveTime*0.001f);
|
|
}
|
|
}
|
|
|
|
public void OnUpdate() {
|
|
if(IsMoving && MovingIndex < movePaths.Count) {
|
|
var curPos = obj.transform.position;
|
|
var disCheck = Vector3.Distance(obj.transform.position, movePaths[MovingIndex]);
|
|
|
|
var disMove = speed * Time.deltaTime;
|
|
|
|
disAll += disMove;
|
|
if(disAll >= disCheck) {
|
|
disAll -= disCheck;
|
|
|
|
obj.transform.position = movePaths[MovingIndex];
|
|
MovingIndex ++;
|
|
if(MovingIndex >= movePaths.Count) {
|
|
IsMoving = false;
|
|
}
|
|
} else {
|
|
var dir = movePaths[MovingIndex] - curPos;
|
|
dir.Normalize();
|
|
obj.transform.position = curPos + dir * disAll;
|
|
disAll = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class LegionBattleView : MonoBehaviour {
|
|
private Dictionary<uint, LBActor> m_Actors;
|
|
|
|
public void Init() {
|
|
m_Actors = new Dictionary<uint, LBActor>();
|
|
}
|
|
|
|
public void AddActor(uint id, uint moveTime) {
|
|
var go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
|
|
GameObject.DontDestroyOnLoad(go);
|
|
go.name = $"{id}";
|
|
LBActor a = new LBActor();
|
|
a.obj = go;
|
|
a.MoveTime = moveTime;
|
|
|
|
m_Actors.Add(id, a);
|
|
}
|
|
|
|
void Update() {
|
|
foreach(var kv in m_Actors) {
|
|
kv.Value.OnUpdate();
|
|
}
|
|
}
|
|
|
|
public void DoCommand(uint id, NLDPB.BattlefieldActorCommand command, int x, int z, RepeatedField<uint> pathX, RepeatedField<uint> pathZ) {
|
|
if(m_Actors.TryGetValue(id, out LBActor a)) {
|
|
var Map = LegionBattleManager.Instance.CurBattle.Map;
|
|
|
|
switch(command) {
|
|
case BattlefieldActorCommand.Idle:
|
|
{
|
|
var pos = Map.BlockPosition2WorldPosition(new Vector2Int(z, x));
|
|
pos.y = 0;
|
|
a.DoIdle(pos);
|
|
}
|
|
break;
|
|
|
|
case BattlefieldActorCommand.Move:
|
|
case BattlefieldActorCommand.Occupy:
|
|
{
|
|
List<Vector3> paths = new List<Vector3>();
|
|
for(int i = 0; i < pathX.Count; i ++) {
|
|
var pos = Map.BlockPosition2WorldPosition(new Vector2Int((int)pathZ[i], (int)pathX[i]));
|
|
pos.y = 0;
|
|
paths.Add(pos);
|
|
}
|
|
a.DoMove(paths);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
} |