zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Hotfix/Logic/Behaviour/Game/System/PetSystem.cs

64 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace ET
{
public class PetAwakeSystem : AwakeSystem<Pet>
{
public override void Awake(Pet self)
{
self.timerId = TimerComponent.Instance.NewRepeatedTimer(500, self.Update);
}
}
public class PetDestroySystem : DestroySystem<Pet>
{
public override void Destroy(Pet self)
{
TimerComponent.Instance.Remove(ref self.timerId);
}
}
public static class PetSystem
{
const float XDis = 0.5f;
const float yDis = 0.5f;
public static void Update(this Pet self)
{
if (self.petUnitId == 0)
return;
Unit unit = self.ZoneScene().GetComponent<UnitComponent>().Get(self.petUnitId);
if (!unit) return;
Unit player = self.GetParent<Unit>();
Vector3 targetPos = GetTargetPos(unit.Position, player.Position);
using var listComponent = ListComponent<Vector3>.Create();
listComponent.List.Add(unit.Position);
listComponent.List.Add(targetPos);
unit.GetComponent<MoveComponent>().MoveToAsync(listComponent.List, 3.5f).Coroutine();
}
private static Vector3 GetTargetPos(Vector2 position0, Vector2 position1)
{
Vector2 vector = position1 - position0;
float gradient;
if (Mathf.Abs(vector.x) < XDis)
{
if (Mathf.Abs(vector.y) < XDis)
return new Vector3(position0.x, position0.y, PosHelper.PlayerPos_Z);
gradient = Mathf.Abs((vector.y > 0 ? (vector.y - XDis) : (vector.y + XDis)) / vector.y);
}
else
{
gradient = Mathf.Abs((vector.x > 0 ? (vector.x - XDis) : (vector.x + XDis)) / vector.x);
}
float dx = vector.x * gradient;
float x = position0.x + dx;
float dy = vector.y * gradient;
float y = position0.y + dy;
return new Vector3(x, y, PosHelper.PlayerPos_Z);
}
}
}