TechbloxModdingAPI/GamecraftModdingAPI/Players/PlayerEngine.cs

401 lines
12 KiB
C#
Raw Normal View History

2020-05-12 00:30:16 +00:00
using System;
using System.Runtime.CompilerServices;
using RobocraftX.Character;
using RobocraftX.Character.Movement;
using RobocraftX.Common.Players;
using RobocraftX.Common.Input;
using RobocraftX.Physics;
2020-05-30 01:30:24 +00:00
using RobocraftX.Blocks.Ghost;
using RobocraftX.Character.Camera;
using RobocraftX.Character.Factories;
using Gamecraft.CharacterVulnerability;
using Gamecraft.CharacterVulnerability.Entities;
2020-05-12 00:30:16 +00:00
using Svelto.ECS;
using Unity.Mathematics;
using Unity.Physics;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Players
{
2020-05-30 01:30:24 +00:00
internal class PlayerEngine : IApiEngine, IFactoryEngine
2020-05-12 00:30:16 +00:00
{
public string Name { get; } = "GamecraftModdingAPIPlayerGameEngine";
public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
2020-05-30 01:30:24 +00:00
public IEntityFactory Factory { set; private get; }
2020-05-12 00:30:16 +00:00
private bool isReady = false;
public void Dispose()
{
isReady = false;
}
public void Ready()
{
isReady = true;
}
public uint GetLocalPlayer()
{
if (!isReady) return uint.MaxValue;
PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
if (count > 0)
{
return localPlayers[0].ID.entityID;
}
return uint.MaxValue;
}
public uint GetRemotePlayer()
{
if (!isReady) return uint.MaxValue;
PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out uint count);
if (count > 0)
{
return localPlayers[0].ID.entityID;
}
return uint.MaxValue;
}
public bool ExistsById(uint playerId)
{
PlayerIDStruct[] players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
for (int i = 0; i < count; i++)
{
if (players[i].ID.entityID == playerId)
{
return true;
}
}
players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out count);
for (int i = 0; i < count; i++)
{
if (players[i].ID.entityID == playerId)
{
return true;
}
}
return false;
}
public float3 GetLocation(uint playerId)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
return rbes.position;
}
return float3.zero;
}
public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
{
ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
for (int i = 0; i < characterGroups.Length; i++)
{
EGID egid = new EGID(playerId, characterGroups[i]);
if (entitiesDB.Exists<RigidBodyEntityStruct>(egid))
{
ref RigidBodyEntityStruct rbes = ref entitiesDB.QueryEntity<RigidBodyEntityStruct>(egid);
if (characterGroups[i] == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
{
entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true;
entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);
}
rbes.position = location;
return true;
}
}
return false;
}
public quaternion GetRotation(uint playerId)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
return rbes.rotation;
}
return quaternion.identity;
}
public bool SetRotation(uint playerId, quaternion value)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
rbes.rotation = value;
return true;
}
return false;
}
public float3 GetLinearVelocity(uint playerId)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
return rbes.velocity;
}
return float3.zero;
}
public bool SetLinearVelocity(uint playerId, float3 value)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
rbes.velocity = value;
return true;
}
return false;
}
public float3 GetAngularVelocity(uint playerId)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
return rbes.angularVelocity;
}
return float3.zero;
}
public bool SetAngularVelocity(uint playerId, float3 value)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
rbes.angularVelocity = value;
return true;
}
return false;
}
public PhysicsMass GetMass(uint playerId)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
return rbes.physicsMass;
}
return default;
}
public bool SetInverseMass(uint playerId, float inverseMass)
{
if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
{
rbes.physicsMass.InverseInertia = inverseMass;
return true;
}
return false;
}
public float? GetLastPingTime(uint playerId, PlayerType type)
{
2020-05-30 01:30:24 +00:00
EGID egid = new EGID(playerId, PlayerGroupFromEnum(type));
2020-05-12 00:30:16 +00:00
if (entitiesDB.Exists<PlayerNetworkStatsEntityStruct>(egid))
{
return entitiesDB.QueryEntity<PlayerNetworkStatsEntityStruct>(egid).lastPingTimeSinceLevelLoad;
}
return null;
}
2020-05-30 01:30:24 +00:00
public float GetInitialHealth(uint playerId)
{
if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
{
return c.initialHealth;
}
return -1f;
}
public bool SetInitialHealth(uint playerId, float val)
{
if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
{
c.initialHealth = val;
return true;
}
return false;
}
public float GetCurrentHealth(uint playerId)
{
if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
{
return c.currentHealth;
}
return -1f;
}
public bool SetCurrentHealth(uint playerId, float val)
{
if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
{
c.currentHealth = val;
return true;
}
return false;
}
public bool DamagePlayer(uint playerId, float amount)
{
Factory.BuildEntity<DamageEntityDescriptor>(
new EGID(CharacterVulnerabilityExclusiveGroups.NextDamageEntityId, CharacterVulnerabilityExclusiveGroups.CharacterDamageExclusiveGroup)
).Init(new DamageEntityStruct
{
damage = amount,
targetPlayerEntityId = playerId,
});
return true;
}
public bool GetDamageable(uint playerId)
{
if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
{
return c.canTakeDamageStat;
}
return false;
}
public bool SetDamageable(uint playerId, bool val)
{
if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct ches))
{
ches.canTakeDamage = val;
ches.canTakeDamage = val;
return true;
}
return false;
}
public uint GetInitialLives(uint playerId)
{
if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
{
return c.initialLives;
}
return uint.MaxValue;
}
public bool SetInitialLives(uint playerId, uint val)
{
if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
{
c.initialLives = val;
return true;
}
return false;
}
public uint GetCurrentLives(uint playerId)
{
if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
{
return c.currentLives;
}
return uint.MaxValue;
}
public bool SetCurrentLives(uint playerId, uint val)
{
if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
{
c.currentLives = val;
return true;
}
return false;
}
public bool GetGameOverScreen(uint playerId)
{
if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
{
return c.gameOverScreen;
}
return false;
}
public bool IsDead(uint playerId)
{
return entitiesDB.Exists<RigidBodyEntityStruct>(playerId, CharacterExclusiveGroups.DeadGroup);
}
public int GetSelectedBlock(uint playerId)
{
if (GetCharacterStruct<EquippedPartStruct>(playerId, out EquippedPartStruct c))
{
return c.SelectedDBPartID;
}
return ushort.MaxValue;
}
public byte GetSelectedColor(uint playerId)
{
if (GetCharacterStruct<EquippedColourStruct>(playerId, out EquippedColourStruct c))
{
return c.indexInPalette;
}
return 255;
}
2020-05-12 00:30:16 +00:00
// reusable methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-05-30 01:30:24 +00:00
private ExclusiveGroup PlayerGroupFromEnum(PlayerType type)
2020-05-12 00:30:16 +00:00
{
return type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool GetCharacterStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
{
ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
for (int i = 0; i < characterGroups.Length; i++)
{
EGID egid = new EGID(playerId, characterGroups[i]);
if (entitiesDB.Exists<T>(egid))
{
s = entitiesDB.QueryEntity<T>(egid);
return true;
}
}
s = default;
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool GetPlayerStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
{
ExclusiveGroup[] playerGroups = PlayersExclusiveGroups.AllPlayers;
for (int i = 0; i < playerGroups.Length; i++)
{
EGID egid = new EGID(playerId, playerGroups[i]);
if (entitiesDB.Exists<T>(egid))
{
s = entitiesDB.QueryEntity<T>(egid);
return true;
}
}
s = default;
return false;
}
2020-05-22 22:06:49 +00:00
public EGID? GetThingLookedAt(uint playerId, float maxDistance = -1f)
{
if (!entitiesDB.TryQueryMappedEntities<CharacterCameraRayCastEntityStruct>(
CameraExclusiveGroups.CameraGroup, out var mapper))
return null;
mapper.TryGetEntity(playerId, out CharacterCameraRayCastEntityStruct rayCast);
float distance = maxDistance < 0
? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast)
: maxDistance;
if (rayCast.hit && rayCast.distance <= distance)
2020-05-22 22:06:49 +00:00
return rayCast.hitEgid;
return null;
}
2020-05-12 00:30:16 +00:00
}
}