Initial Commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components.Creation;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Pets.Systems
|
||||
{
|
||||
[UpdateAfter(typeof(EnsureLinkedPets))]
|
||||
public class CleanPetlessPlayers : GameSystemBase, IModSystem
|
||||
{
|
||||
private EntityQuery _players;
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_players = GetEntityQuery(new QueryHelper().All(typeof(CPlayer), typeof(CLinkedPet)));
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
NativeArray<Entity> players = _players.ToEntityArray(Allocator.Temp);
|
||||
|
||||
for (int i = 0; i < players.Length; i++)
|
||||
{
|
||||
Entity player = players[i];
|
||||
if (!Require(player, out CLinkedPet cLinkedPet)) continue;
|
||||
if (cLinkedPet.PetEntity != Entity.Null) continue;
|
||||
|
||||
Mod.Logger.LogInfo($"Removing petless player");
|
||||
|
||||
EntityManager.RemoveComponent<CLinkedPet>(player);
|
||||
}
|
||||
|
||||
players.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components.Creation;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Pets.Systems
|
||||
{
|
||||
public class EnsureCorrectPet : GameSystemBase, IModSystem
|
||||
{
|
||||
private EntityQuery _players;
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_players = GetEntityQuery(typeof(CPlayer), typeof(CLinkedPet), typeof(CRequiresPet));
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
NativeArray<Entity> players = _players.ToEntityArray(Allocator.Temp);
|
||||
|
||||
for (int i = 0; i < players.Length; i++)
|
||||
{
|
||||
Entity player = players[i];
|
||||
if (!Require(player, out CLinkedPet cLinkedPet)) continue;
|
||||
if (!Require(player, out CRequiresPet cRequiresPet)) continue;
|
||||
|
||||
if (cLinkedPet.PetType != cRequiresPet.PetType && cLinkedPet.PetEntity != Entity.Null)
|
||||
{
|
||||
EntityManager.DestroyEntity(cLinkedPet.PetEntity);
|
||||
EntityManager.RemoveComponent<CLinkedPet>(player);
|
||||
}
|
||||
}
|
||||
|
||||
players.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components;
|
||||
using Pets.Components.Creation;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Pets.Systems
|
||||
{
|
||||
[UpdateBefore(typeof(CleanPetlessPlayers))]
|
||||
public class EnsureLinkedPets : GameSystemBase, IModSystem
|
||||
{
|
||||
private EntityQuery _pets;
|
||||
private EntityQuery _playersWithLink;
|
||||
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_pets = GetEntityQuery(new QueryHelper().All(typeof(CPet)));
|
||||
_playersWithLink = GetEntityQuery(new QueryHelper().All(typeof(CLinkedPet)));
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
NativeArray<Entity> pets = _pets.ToEntityArray(Allocator.Temp);
|
||||
NativeArray<Entity> playersWithLink = _playersWithLink.ToEntityArray(Allocator.Temp);
|
||||
|
||||
foreach (Entity player in playersWithLink)
|
||||
{
|
||||
if (!Require(player, out CLinkedPet cLinkedPet)) continue;
|
||||
if ((cLinkedPet.PetEntity == Entity.Null || cLinkedPet.PetEntity.Index == 0) || (cLinkedPet.PetEntity != Entity.Null && !EntityManager.HasComponent<CPet>(cLinkedPet.PetEntity)))
|
||||
{
|
||||
foreach (Entity pet in pets)
|
||||
{
|
||||
if (!Require(pet, out CPet cPet)) continue;
|
||||
if (cLinkedPet.PetLinkId != cPet.PetLinkId) continue;
|
||||
cLinkedPet.PetEntity = pet;
|
||||
cPet.Owner = player;
|
||||
EntityManager.SetComponentData(player, cLinkedPet);
|
||||
EntityManager.SetComponentData(pet, cPet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Entity pet in pets)
|
||||
{
|
||||
if (!Require(pet, out CPet cPet)) continue;
|
||||
if ((cPet.Owner == Entity.Null || cPet.Owner.Index == 0) || (cPet.Owner != Entity.Null && !EntityManager.HasComponent<CLinkedPet>(cPet.Owner)))
|
||||
{
|
||||
foreach (Entity player in playersWithLink)
|
||||
{
|
||||
if (!Require(player, out CLinkedPet cLinkedPet)) continue;
|
||||
if (cLinkedPet.PetLinkId != cPet.PetLinkId) continue;
|
||||
cLinkedPet.PetEntity = pet;
|
||||
cPet.Owner = player;
|
||||
EntityManager.SetComponentData(player, cLinkedPet);
|
||||
EntityManager.SetComponentData(pet, cPet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pets.Dispose();
|
||||
playersWithLink.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components.Creation;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Pets.Systems
|
||||
{
|
||||
public class PetSelector : GameSystemBase, IModSystem
|
||||
{
|
||||
public static PetSelector Main;
|
||||
|
||||
private EntityQuery _players;
|
||||
private EntityQuery _requestedPets;
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
Main = this;
|
||||
_players = GetEntityQuery(new QueryHelper().All(typeof(CPlayer)));
|
||||
_requestedPets = GetEntityQuery(new QueryHelper().All(typeof(CRequestedPet)));
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
using NativeArray<Entity> requestedPets = _requestedPets.ToEntityArray(Allocator.Temp);
|
||||
using NativeArray<Entity> players = _players.ToEntityArray(Allocator.Temp);
|
||||
|
||||
for (int i = 0; i < requestedPets.Length; i++)
|
||||
{
|
||||
Entity requestedPet = requestedPets[i];
|
||||
if (!Require(requestedPet, out CRequestedPet cRequestedPet)) continue;
|
||||
foreach (Entity player in players)
|
||||
{
|
||||
if (!Require(player, out CPlayer cPlayer)) continue;
|
||||
if (cPlayer.ID != cRequestedPet.player) continue;
|
||||
EntityManager.AddComponentData(player, new CRequiresPet
|
||||
{
|
||||
PetType = cRequestedPet.pet
|
||||
});
|
||||
EntityManager.DestroyEntity(requestedPet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CreatePlayerRequest(int player, int pet)
|
||||
{
|
||||
Entity entity = EntityManager.CreateEntity();
|
||||
EntityManager.AddComponentData(entity, new CRequestedPet
|
||||
{
|
||||
pet = pet,
|
||||
player = player
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using Kitchen;
|
||||
using KitchenData;
|
||||
using KitchenMods;
|
||||
using Pets.Components;
|
||||
using Pets.Components.Creation;
|
||||
using Pets.Customs.Types;
|
||||
using Pets.Enums;
|
||||
using Pets.Interfaces;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Pets.Systems.Creation
|
||||
{
|
||||
[UpdateAfter(typeof(EnsureLinkedPets))]
|
||||
public class SpawnPets : GameSystemBase, IModSystem
|
||||
{
|
||||
private EntityQuery _players;
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_players = GetEntityQuery(new QueryHelper().All(typeof(CPlayer), typeof(CRequiresPet)).None(typeof(CLinkedPet)));
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
NativeArray<Entity> players = _players.ToEntityArray(Allocator.Temp);
|
||||
|
||||
foreach (Entity player in players)
|
||||
{
|
||||
if (!Require(player, out CRequiresPet cRequiresPet)) continue;
|
||||
if (!Require(player, out CPosition cPosition)) continue;
|
||||
if (!GameData.Main.TryGet(cRequiresPet.PetType, out Pet petType)) continue;
|
||||
|
||||
Entity pet = EntityManager.CreateEntity();
|
||||
|
||||
int LinkID = UnityEngine.Random.Range(-100000, 100000);
|
||||
|
||||
EntityManager.AddComponentData(pet, new CPet
|
||||
{
|
||||
Owner = player,
|
||||
State = petType.DefaultState,
|
||||
PetLinkId = LinkID,
|
||||
PetType = petType.ID
|
||||
});
|
||||
EntityManager.AddComponentData(pet, new CPosition(cPosition));
|
||||
|
||||
EntityManager.AddComponentData(pet, new CRequiresView
|
||||
{
|
||||
Type = petType.ViewType,
|
||||
PhysicsDriven = true
|
||||
});
|
||||
EntityManager.AddComponentData(pet, new CDoNotPersist());
|
||||
EntityManager.AddComponentData(pet, new CIsInteractive());
|
||||
EntityManager.AddComponentData(pet, new CCanBePetted());
|
||||
EntityManager.AddComponentData(pet, new CPersistThroughSceneChanges());
|
||||
|
||||
|
||||
EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
|
||||
foreach (IPetProperty property in petType.Properties)
|
||||
{
|
||||
ecb.AddComponent(pet, (dynamic)property);
|
||||
}
|
||||
|
||||
ecb.AddComponent(pet, new CDefaultState(petType.DefaultState));
|
||||
|
||||
ecb.Playback(EntityManager);
|
||||
|
||||
EntityManager.AddComponentData(player, new CLinkedPet
|
||||
{
|
||||
PetType = cRequiresPet.PetType,
|
||||
PetEntity = pet,
|
||||
PetLinkId = LinkID
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
players.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System.Collections.Generic;
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components.Creation;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Pets.Systems
|
||||
{
|
||||
public class TransitionFix : GameSystemBase, IModSystem
|
||||
{
|
||||
private EntityQuery _players;
|
||||
private static Dictionary<int, CRequiresPet> _requiresPet = new Dictionary<int, CRequiresPet>();
|
||||
private static Dictionary<int, CLinkedPet> _linkedPet = new Dictionary<int, CLinkedPet>();
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_players = GetEntityQuery(new QueryHelper().All(typeof(CPlayer)));
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
NativeArray<Entity> players = _players.ToEntityArray(Allocator.Temp);
|
||||
|
||||
_requiresPet.Clear();
|
||||
_linkedPet.Clear();
|
||||
|
||||
foreach (Entity player in players)
|
||||
{
|
||||
if (!Require(player, out CPlayer cPlayer)) continue;
|
||||
if (Require(player, out CRequiresPet cRequiresPet))
|
||||
{
|
||||
_requiresPet.Add(cPlayer.ID, cRequiresPet);
|
||||
}
|
||||
if (Require(player, out CLinkedPet cLinkedPet))
|
||||
{
|
||||
_linkedPet.Add(cPlayer.ID, cLinkedPet);
|
||||
}
|
||||
}
|
||||
|
||||
players.Dispose();
|
||||
}
|
||||
|
||||
public override void AfterLoading(SaveSystemType system_type)
|
||||
{
|
||||
base.AfterLoading(system_type);
|
||||
if (_requiresPet == null) return;
|
||||
|
||||
NativeArray<Entity> players = _players.ToEntityArray(Allocator.Temp);
|
||||
|
||||
for (int i = 0; i < players.Length; i++)
|
||||
{
|
||||
Entity player = players[i];
|
||||
if (Has<CLinkedPet>(player))
|
||||
{
|
||||
EntityManager.RemoveComponent<CLinkedPet>(player);
|
||||
}
|
||||
|
||||
if (!Require(player, out CPlayer cPlayer)) continue;
|
||||
|
||||
if (_linkedPet.TryGetValue(cPlayer.ID, out CLinkedPet cLinkedPet))
|
||||
{
|
||||
EntityManager.AddComponentData(player, cLinkedPet);
|
||||
}
|
||||
|
||||
if (_requiresPet.TryGetValue(cPlayer.ID, out CRequiresPet cRequiresPet))
|
||||
{
|
||||
EntityManager.AddComponentData(player, cRequiresPet);
|
||||
}
|
||||
}
|
||||
|
||||
_requiresPet.Clear();
|
||||
players.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user