forked from decaprime/LeadAHorseToWater
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHorseUtil.cs
105 lines (88 loc) · 3.07 KB
/
HorseUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using BepInEx.Logging;
namespace LeadAHorseToWater.VCFCompat
{
using ProjectM;
using Unity.Entities;
using Unity.Transforms;
using Bloodstone.API;
using Unity.Collections;
using Unity.Mathematics;
using System.Collections.Generic;
internal static class HorseUtil
{
private static ManualLogSource _log => Plugin.LogInstance;
private static Entity empty_entity = new Entity();
internal static void SpawnHorse(int countlocal, float3 localPos)
{
// TODO: Cache and Improve
var prefabCollectionSystem = VWorld.Server.GetExistingSystem<PrefabCollectionSystem>();
var entityName = "char_mount_horse";
foreach (var kv in prefabCollectionSystem._SpawnableNameToPrefabGuidDictionary)
{
if (kv.Key.ToLower() != entityName.ToLower()) continue;
var usus = VWorld.Server.GetExistingSystem<UnitSpawnerUpdateSystem>();
usus.SpawnUnit(empty_entity, kv.Value, new float3(localPos.x, 0, localPos.z), countlocal, 1, 2, -1);
break;
}
}
internal static NativeArray<Entity> GetHorses()
{
var horseQuery = VWorld.Server.EntityManager.CreateEntityQuery(new EntityQueryDesc()
{
All = new[] { ComponentType.ReadWrite<FeedableInventory>(),
ComponentType.ReadWrite<NameableInteractable>(),
ComponentType.ReadWrite<Mountable>(),
ComponentType.ReadOnly<LocalToWorld>(),
ComponentType.ReadOnly<Team>()
},
None = new[] { ComponentType.ReadOnly<Dead>(), ComponentType.ReadOnly<DestroyTag>() }
});
return horseQuery.ToEntityArray(Allocator.Temp);
}
internal static Entity? GetClosetHorse(Entity e)
{
var horseEntityQuery = GetHorses();
var origin = VWorld.Server.EntityManager.GetComponentData<LocalToWorld>(e).Position;
var closest = float.MaxValue;
Entity? closestHorse = null;
foreach (var horse in horseEntityQuery)
{
var position = VWorld.Server.EntityManager.GetComponentData<LocalToWorld>(horse).Position;
var distance = UnityEngine.Vector3.Distance(origin, position); // wait really?
if (distance < closest)
{
closest = distance;
closestHorse = horse;
}
}
return closestHorse;
}
internal static bool isTamed(Entity e)
{
EntityManager em = VWorld.Server.EntityManager;
ComponentDataFromEntity<Team> getTeam = VWorld.Server.EntityManager.GetComponentDataFromEntity<Team>();
if (!em.HasComponent<Team>(e)) return false;
var teamhorse = getTeam[e];
var isUnit = Team.IsInUnitTeam(teamhorse);
// Wild horses are Units, appear to no longer be units after you ride them.
return !isUnit;
}
internal static List<Entity> ClosestHorses(Entity e, float radius = 5f)
{
var horses = GetHorses();
var results = new List<Entity>();
var origin = VWorld.Server.EntityManager.GetComponentData<LocalToWorld>(e).Position;
foreach (var horse in horses)
{
var position = VWorld.Server.EntityManager.GetComponentData<LocalToWorld>(horse).Position;
var distance = UnityEngine.Vector3.Distance(origin, position); // wait really?
if (distance < radius)
{
results.Add(horse);
}
}
return results;
}
}
}