From b6b9a29a3c139f14ed7b6d4fae43c41f4aa84dfa Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 12 May 2021 00:49:01 +0200 Subject: [PATCH] Convert more things to use EcsObjectBase Though the major benefit is only for blocks right now (using initializers) --- TechbloxModdingAPI/Block.cs | 2 +- TechbloxModdingAPI/BlockGroup.cs | 9 +++-- TechbloxModdingAPI/FlyCam.cs | 47 +++++++++++----------- TechbloxModdingAPI/Players/FlyCamEngine.cs | 6 +-- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/TechbloxModdingAPI/Block.cs b/TechbloxModdingAPI/Block.cs index 5181723..64b8231 100644 --- a/TechbloxModdingAPI/Block.cs +++ b/TechbloxModdingAPI/Block.cs @@ -431,7 +431,7 @@ namespace TechbloxModdingAPI return; } blockGroup?.RemoveInternal(this); - BlockEngine.GetBlockInfo(this).currentBlockGroup = value?.Id ?? -1; + BlockEngine.GetBlockInfo(this).currentBlockGroup = (int?) value?.Id.entityID ?? -1; value?.AddInternal(this); blockGroup = value; } diff --git a/TechbloxModdingAPI/BlockGroup.cs b/TechbloxModdingAPI/BlockGroup.cs index 5361445..6e4f42f 100644 --- a/TechbloxModdingAPI/BlockGroup.cs +++ b/TechbloxModdingAPI/BlockGroup.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using Gamecraft.Blocks.BlockGroups; +using Svelto.ECS; using Unity.Mathematics; using UnityEngine; using TechbloxModdingAPI.Blocks; @@ -13,10 +14,10 @@ namespace TechbloxModdingAPI /// /// A group of blocks that can be selected together. The placed version of blueprints. Dispose after usage. /// - public class BlockGroup : ICollection, IDisposable + public class BlockGroup : EcsObjectBase, ICollection, IDisposable { internal static BlueprintEngine _engine = new BlueprintEngine(); - public int Id { get; } + public override EGID Id { get; } private readonly Block sourceBlock; private readonly List blocks; private float3 position, rotation; @@ -26,7 +27,7 @@ namespace TechbloxModdingAPI { if (id == BlockGroupUtility.GROUP_UNASSIGNED) throw new BlockException("Cannot create a block group for blocks without a group!"); - Id = id; + Id = new EGID((uint) id, BlockGroupExclusiveGroups.BlockGroupEntityGroup); sourceBlock = block; blocks = new List(GetBlocks()); Block.Removed += OnBlockRemoved; @@ -168,7 +169,7 @@ namespace TechbloxModdingAPI internal void AddInternal(Block item) { blocks.Add(item); - _engine.AddBlockToGroup(item.Id, Id); + _engine.AddBlockToGroup(item.Id, (int) Id.entityID); } /// diff --git a/TechbloxModdingAPI/FlyCam.cs b/TechbloxModdingAPI/FlyCam.cs index b72b174..201fd0a 100644 --- a/TechbloxModdingAPI/FlyCam.cs +++ b/TechbloxModdingAPI/FlyCam.cs @@ -1,4 +1,5 @@ using RobocraftX.Physics; +using Svelto.ECS; using Svelto.ECS.EntityStructs; using Techblox.FlyCam; using TechbloxModdingAPI.Players; @@ -8,13 +9,13 @@ using UnityEngine; namespace TechbloxModdingAPI { - public class FlyCam + public class FlyCam : EcsObjectBase { private static FlyCamEngine Engine = new FlyCamEngine(); - public uint Id { get; } + public override EGID Id { get; } - public FlyCam(uint id) => Id = id; + public FlyCam(uint id) => Id = new EGID(id, Techblox.FlyCam.FlyCam.Group); /// /// The local player's camera. @@ -26,11 +27,11 @@ namespace TechbloxModdingAPI /// public float3 Position { - get => Engine.GetComponent(Id).Get().position; + get => Engine.GetComponent(this).position; set { - Engine.GetComponent(Id).Get().position = value; - Engine.GetComponent(Id).Get().position = value; + Engine.GetComponent(this).position = value; + Engine.GetComponent(this).position = value; } } @@ -39,11 +40,11 @@ namespace TechbloxModdingAPI /// public float3 Rotation { - get => ((Quaternion) Engine.GetComponent(Id).Get().rotation).eulerAngles; + get => ((Quaternion) Engine.GetComponent(this).rotation).eulerAngles; set { - Engine.GetComponent(Id).Get().rotation = Quaternion.Euler(value); - Engine.GetComponent(Id).Get().rotation = Quaternion.Euler(value); + Engine.GetComponent(this).rotation = Quaternion.Euler(value); + Engine.GetComponent(this).rotation = Quaternion.Euler(value); } } @@ -52,8 +53,8 @@ namespace TechbloxModdingAPI /// public float3 MovementDirection { - get => Engine.GetComponent(Id).Get().movementDirection; - set => Engine.GetComponent(Id).Get().movementDirection = value; + get => Engine.GetComponent(this).movementDirection; + set => Engine.GetComponent(this).movementDirection = value; } /// @@ -61,8 +62,8 @@ namespace TechbloxModdingAPI /// public bool Sprinting { - get => Engine.GetComponent(Id).Get().sprinting; - set => Engine.GetComponent(Id).Get().sprinting = value; + get => Engine.GetComponent(this).sprinting; + set => Engine.GetComponent(this).sprinting = value; } /// @@ -70,8 +71,8 @@ namespace TechbloxModdingAPI /// public float Speed { - get => Engine.GetComponent(Id).Get().speed; - set => Engine.GetComponent(Id).Get().speed = value; + get => Engine.GetComponent(this).speed; + set => Engine.GetComponent(this).speed = value; } /// @@ -79,8 +80,8 @@ namespace TechbloxModdingAPI /// public float SpeedSprintMultiplier { - get => Engine.GetComponent(Id).Get().speedSprintMultiplier; - set => Engine.GetComponent(Id).Get().speedSprintMultiplier = value; + get => Engine.GetComponent(this).speedSprintMultiplier; + set => Engine.GetComponent(this).speedSprintMultiplier = value; } /// @@ -88,8 +89,8 @@ namespace TechbloxModdingAPI /// public float Acceleration { - get => Engine.GetComponent(Id).Get().acceleration; - set => Engine.GetComponent(Id).Get().acceleration = value; + get => Engine.GetComponent(this).acceleration; + set => Engine.GetComponent(this).acceleration = value; } /// @@ -97,8 +98,8 @@ namespace TechbloxModdingAPI /// public float3 Velocity { - get => Engine.GetComponent(Id).Get().velocity; - set => Engine.GetComponent(Id).Get().velocity = value; + get => Engine.GetComponent(this).velocity; + set => Engine.GetComponent(this).velocity = value; } /// @@ -106,8 +107,8 @@ namespace TechbloxModdingAPI /// public float3 AngularVelocity { - get => Engine.GetComponent(Id).Get().angularVelocity; - set => Engine.GetComponent(Id).Get().angularVelocity = value; + get => Engine.GetComponent(this).angularVelocity; + set => Engine.GetComponent(this).angularVelocity = value; } public static void Init() diff --git a/TechbloxModdingAPI/Players/FlyCamEngine.cs b/TechbloxModdingAPI/Players/FlyCamEngine.cs index cd52363..2db12e5 100644 --- a/TechbloxModdingAPI/Players/FlyCamEngine.cs +++ b/TechbloxModdingAPI/Players/FlyCamEngine.cs @@ -19,11 +19,9 @@ namespace TechbloxModdingAPI.Players public string Name => "TechbloxModdingAPIFlyCamEngine"; public bool isRemovable => false; - public OptionalRef GetComponent(uint id) where T : unmanaged, IEntityComponent + public ref T GetComponent(FlyCam cam) where T : unmanaged, IEntityComponent { - if (entitiesDB.TryQueryEntitiesAndIndex(id, Techblox.FlyCam.FlyCam.Group, out uint index, out var array)) - return new OptionalRef(array, index); - return new OptionalRef(); + return ref entitiesDB.QueryEntityOrDefault(cam); } } } \ No newline at end of file