Initial Commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
using System.Collections.Generic;
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components.Properties;
|
||||
using Pets.Components.Status;
|
||||
using Pets.Enums;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Pets.Systems.Activities
|
||||
{
|
||||
public class PetEatActivity : PetActivitySystem, IModSystem
|
||||
{
|
||||
protected override PetState StateForUpdate => PetState.Eat;
|
||||
|
||||
private EntityQuery _food;
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_food = GetEntityQuery(new QueryHelper().All(typeof(CItemProvider), typeof(CPosition)).None(typeof(COccupiedByPet)));
|
||||
}
|
||||
|
||||
protected override bool IsPossible(ActivityData data)
|
||||
{
|
||||
if (_food.CalculateEntityCount() == 0) return false;
|
||||
|
||||
using NativeArray<Entity> food = _food.ToEntityArray(Allocator.Temp);
|
||||
|
||||
Dictionary<float, Entity> ValidFood = GetAllEntitiesInRange(food, data.PetPosition);
|
||||
|
||||
bool foundFood = false;
|
||||
|
||||
foreach (float distance in ValidFood.Keys)
|
||||
{
|
||||
Entity foodEntity = ValidFood[distance];
|
||||
|
||||
float offset = 0;
|
||||
|
||||
if (Require(data.Pet, out CStandBackFromFood cStandBackFromFood))
|
||||
offset = cStandBackFromFood.Distance;
|
||||
|
||||
if (Require(data.Pet, out CPreferredFoods cPreferredFoods))
|
||||
{
|
||||
if (Require(foodEntity, out CItemProvider cItemProvider))
|
||||
{
|
||||
if (!cPreferredFoods.PreferredFoods.Contains(cItemProvider.ProvidedItem)) continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Require(foodEntity, out CPosition cFoodPosition)) continue;
|
||||
|
||||
Vector3 targetPos = Vector3.zero;
|
||||
|
||||
if (!CanGetTo(data.PetPosition, cFoodPosition, new Vector3(1 + offset, 0, 0), out targetPos))
|
||||
if (!CanGetTo(data.PetPosition, cFoodPosition, new Vector3(-1 - offset, 0, 0), out targetPos))
|
||||
if (!CanGetTo(data.PetPosition, cFoodPosition, new Vector3(0, 0, 1 + offset), out targetPos))
|
||||
if (!CanGetTo(data.PetPosition, cFoodPosition, new Vector3(0, 0, -1 - offset), out targetPos))
|
||||
continue;
|
||||
|
||||
foundFood = true;
|
||||
TargetPosition = targetPos;
|
||||
TargetEntity = foodEntity;
|
||||
TargetForward = cFoodPosition;
|
||||
break;
|
||||
}
|
||||
|
||||
return foundFood;
|
||||
}
|
||||
|
||||
private Entity TargetEntity;
|
||||
private CPosition TargetPosition;
|
||||
private CPosition TargetForward;
|
||||
|
||||
protected override bool Perform(ActivityData data)
|
||||
{
|
||||
if (!Require(TargetEntity, out CPosition cBedPosition)) return false;
|
||||
|
||||
EntityManager.AddComponentData(data.Pet, new CMoveToLocation
|
||||
{
|
||||
Location = TargetPosition,
|
||||
StoppingDistance = 0,
|
||||
DesiredFacing = TargetForward
|
||||
});
|
||||
|
||||
EntityManager.AddComponentData(TargetEntity, new COccupiedByPet());
|
||||
EntityManager.AddComponentData(data.Pet, new CPetInteractingWith
|
||||
{
|
||||
InteractingWith = TargetEntity,
|
||||
TimeToFinish = Random.Range(5, 15),
|
||||
IsWaitingForDestination = true
|
||||
});
|
||||
|
||||
return HasComponent<CPetInteractingWith>(data.Pet) && HasComponent<CMoveToLocation>(data.Pet) && HasComponent<COccupiedByPet>(TargetEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components;
|
||||
using Pets.Components.Properties;
|
||||
using Pets.Components.Status;
|
||||
using Pets.Enums;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Pets.Systems.Activities
|
||||
{
|
||||
public class PetFollowActivity : PetActivitySystem, IModSystem
|
||||
{
|
||||
protected override PetState StateForUpdate => PetState.Follow;
|
||||
|
||||
protected override bool IsPossible(ActivityData data)
|
||||
{
|
||||
maxDistance = 5;
|
||||
|
||||
if (!Require(data.Pet, out CPet cPet)) return false;
|
||||
if (cPet.Owner == Entity.Null) return false;
|
||||
if (!Require(cPet.Owner, out TargetPosition)) return false;
|
||||
if (Require(data.Pet, out CLonelyDistance cLonelyDistance))
|
||||
maxDistance = cLonelyDistance.Distance;
|
||||
|
||||
currentDistance = Vector3.Distance(data.PetPosition, TargetPosition);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private float maxDistance;
|
||||
private float currentDistance;
|
||||
private CPosition TargetPosition;
|
||||
|
||||
protected override bool Perform(ActivityData data)
|
||||
{
|
||||
if (currentDistance > maxDistance)
|
||||
{
|
||||
EntityManager.AddComponentData(data.Pet, new CMoveToLocation
|
||||
{
|
||||
Location = TargetPosition,
|
||||
StoppingDistance = 1,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Require(data.Pet, out CRoamNearOwner cRoamNearOwner)) return true;
|
||||
|
||||
if (!(Random.value <= 0.03f * Time.DeltaTime)) return true;
|
||||
|
||||
if (!Require(data.Pet, out CCurrentSpeed cCurrentSpeed)) return true;
|
||||
if (cCurrentSpeed.speed > Mod.MinimumSpeedThreshold) return true;
|
||||
|
||||
Vector3 randomPosition = new Vector3(Random.Range(-cRoamNearOwner.Distance, cRoamNearOwner.Distance), 0, Random.Range(-cRoamNearOwner.Distance, cRoamNearOwner.Distance));
|
||||
Vector3 newTarget = TargetPosition + randomPosition;
|
||||
float distance = Vector3.Distance(data.PetPosition, newTarget);
|
||||
|
||||
if (!(distance <= maxDistance)) return true;
|
||||
if (!CanGetTo(data.PetPosition, newTarget, Vector3.zero, out newTarget)) return true;
|
||||
|
||||
EntityManager.AddComponentData(data.Pet, new CMoveToLocation
|
||||
{
|
||||
Location = newTarget,
|
||||
StoppingDistance = 0
|
||||
});
|
||||
|
||||
EntityManager.AddComponentData(data.Pet, new CPetInteractingWith
|
||||
{
|
||||
TimeToFinish = 0,
|
||||
IsWaitingForDestination = true
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components;
|
||||
using Pets.Enums;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Pets.Systems.Activities
|
||||
{
|
||||
public class PetNameChangeActivity : PetActivitySystem, IModSystem
|
||||
{
|
||||
protected override PetState StateForUpdate => PetState.NameChange;
|
||||
|
||||
protected override bool IsPossible(ActivityData data)
|
||||
{
|
||||
return Require(data.Pet, out CPet cPet) && cPet.Owner != Entity.Null && Has<CPlayer>(cPet.Owner) && !Has<CRequestNameChange>(data.Pet);
|
||||
}
|
||||
|
||||
protected override bool Perform(ActivityData data)
|
||||
{
|
||||
if (!Require(data.Pet, out CPet cPet)) return false;
|
||||
if (!Require(cPet.Owner, out CPlayer cPlayer)) return false;
|
||||
|
||||
EntityManager.AddComponentData(data.Pet, new CRequestNameChange
|
||||
{
|
||||
Source = cPlayer.InputSource,
|
||||
IsTriggered = false
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components;
|
||||
using Pets.Components.Properties;
|
||||
using Pets.Components.Status;
|
||||
using Pets.Enums;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Pets.Systems.Activities
|
||||
{
|
||||
public class PetSleepActivity : PetActivitySystem, IModSystem
|
||||
{
|
||||
protected override PetState StateForUpdate => PetState.Sleep;
|
||||
|
||||
private EntityQuery _petBeds;
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_petBeds = GetEntityQuery(new QueryHelper().All(typeof(CPetBed), typeof(CPosition)).None(typeof(COccupiedByPet)));
|
||||
}
|
||||
|
||||
protected override bool IsPossible(ActivityData data)
|
||||
{
|
||||
if (_petBeds.CalculateEntityCount() == 0) return false;
|
||||
|
||||
using NativeArray<Entity> petBeds = _petBeds.ToEntityArray(Allocator.Temp);
|
||||
|
||||
Dictionary<float, Entity> ValidBeds = GetAllEntitiesInRange(petBeds, data.PetPosition);
|
||||
|
||||
bool foundBed = false;
|
||||
|
||||
foreach (float distance in ValidBeds.Keys)
|
||||
{
|
||||
Entity bed = ValidBeds[distance];
|
||||
CPosition bedPosition;
|
||||
if (!Require(bed, out bedPosition)) continue;
|
||||
if (Require(data.Pet, out CSleepingPositionOffset cSleepingPositionOffset))
|
||||
if (GetOffsetPosition(bedPosition.Forward(1), cSleepingPositionOffset.Offset, out Vector2 offsetPosition))
|
||||
bedPosition += offsetPosition;
|
||||
|
||||
|
||||
|
||||
if (!CanGetTo(data.PetPosition, bedPosition, Vector3.zero, out Vector3 targetDestination)) continue;
|
||||
|
||||
foundBed = true;
|
||||
TargetPosition = targetDestination;
|
||||
TargetEntity = bed;
|
||||
break;
|
||||
}
|
||||
|
||||
return foundBed;
|
||||
}
|
||||
|
||||
private Entity TargetEntity;
|
||||
private CPosition TargetPosition;
|
||||
|
||||
protected override bool Perform(ActivityData data)
|
||||
{
|
||||
if (!Require(TargetEntity, out CPosition cBedPosition)) return false;
|
||||
|
||||
EntityManager.AddComponentData(data.Pet, new CMoveToLocation
|
||||
{
|
||||
Location = TargetPosition,
|
||||
StoppingDistance = 0,
|
||||
DesiredFacing = cBedPosition.Forward(-10)
|
||||
});
|
||||
|
||||
EntityManager.AddComponentData(TargetEntity, new COccupiedByPet());
|
||||
EntityManager.AddComponentData(data.Pet, new CPetInteractingWith
|
||||
{
|
||||
InteractingWith = TargetEntity,
|
||||
StartTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
|
||||
TimeToFinish = Random.Range(5, 15),
|
||||
IsWaitingForDestination = true
|
||||
});
|
||||
|
||||
return HasComponent<CPetInteractingWith>(data.Pet) && HasComponent<CMoveToLocation>(data.Pet) && HasComponent<COccupiedByPet>(TargetEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using Kitchen;
|
||||
using KitchenMods;
|
||||
using Pets.Components;
|
||||
using Pets.Components.Status;
|
||||
using Pets.Enums;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Pets.Systems.Activities
|
||||
{
|
||||
public class StopInteraction : GameSystemBase, IModSystem
|
||||
{
|
||||
private EntityQuery _pets;
|
||||
|
||||
protected override void Initialise()
|
||||
{
|
||||
base.Initialise();
|
||||
_pets = GetEntityQuery(new QueryHelper().All(typeof(CPet), typeof(CPetInteractingWith)));
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
if (_pets.CalculateEntityCount() == 0) return;
|
||||
|
||||
NativeArray<Entity> pets = _pets.ToEntityArray(Allocator.Temp);
|
||||
|
||||
for (int i = 0; i < pets.Length; i++)
|
||||
{
|
||||
Entity pet = pets[i];
|
||||
if (!Require(pet, out CPetInteractingWith cPetInteractingWith)) continue;
|
||||
if (!Require(pet, out CMoveToLocation cMoveToLocation)) continue;
|
||||
if (!Require(pet, out CPosition cPosition)) continue;
|
||||
if (!Require(pet, out CCurrentSpeed cCurrentSpeed)) continue;
|
||||
if (cPetInteractingWith.IsWaitingForDestination)
|
||||
{
|
||||
if (Vector3.Distance(cPosition, cMoveToLocation.Location) < 0.1f && cCurrentSpeed.speed < Mod.MinimumSpeedThreshold)
|
||||
{
|
||||
cPetInteractingWith.IsWaitingForDestination = false;
|
||||
cPetInteractingWith.StartTime = DateTimeOffset.Now.ToUnixTimeSeconds();
|
||||
EntityManager.AddComponentData(pet, cPetInteractingWith);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Require(pet, out CPet cPet)) continue;
|
||||
if (cPetInteractingWith.StartTime + cPetInteractingWith.TimeToFinish > DateTimeOffset.Now.ToUnixTimeSeconds()) continue;
|
||||
|
||||
EntityManager.RemoveComponent<CMoveToLocation>(pet);
|
||||
EntityManager.RemoveComponent<COccupiedByPet>(cPetInteractingWith.InteractingWith);
|
||||
EntityManager.RemoveComponent<CPetInteractingWith>(pet);
|
||||
cPet.State = PetState.Follow;
|
||||
EntityManager.AddComponentData(pet, cPet);
|
||||
|
||||
}
|
||||
|
||||
pets.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user