From 6f8241554df4f02a6a5cd3eb735f5bd89e684fef Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 13 May 2020 16:52:21 +0200 Subject: [PATCH] Add block type and color properties --- GamecraftModdingAPI/Block.cs | 62 +++++++++++++++---- GamecraftModdingAPI/Blocks/BlockEngine.cs | 11 +++- .../Blocks/BlockIdentifiers.cs | 9 +-- GamecraftModdingAPI/Blocks/MovementEngine.cs | 17 +---- GamecraftModdingAPI/Blocks/PlacementEngine.cs | 7 --- GamecraftModdingAPI/Blocks/RemovalEngine.cs | 6 -- GamecraftModdingAPI/Blocks/RotationEngine.cs | 16 +---- GamecraftModdingAPI/Blocks/SignalEngine.cs | 23 +------ GamecraftModdingAPI/Blocks/Signals.cs | 8 +-- GamecraftModdingAPI/Blocks/Tweakable.cs | 6 +- GamecraftModdingAPI/Blocks/TweakableEngine.cs | 6 +- GamecraftModdingAPI/Blocks/TweakableStat.cs | 3 +- GamecraftModdingAPI/Main.cs | 1 - GamecraftModdingAPI/Player.cs | 1 - GamecraftModdingAPI/Players/PlayerEngine.cs | 3 - .../Tests/GamecraftModdingAPIPluginTest.cs | 9 ++- 16 files changed, 75 insertions(+), 113 deletions(-) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index d099aba..336215e 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -1,10 +1,11 @@ using System; -using System.Collections.Generic; + +using Svelto.ECS; +using RobocraftX.Common; +using Unity.Mathematics; + using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Utility; -using RobocraftX.Common; -using Svelto.ECS; -using Unity.Mathematics; namespace GamecraftModdingAPI { @@ -80,11 +81,6 @@ namespace GamecraftModdingAPI set => MovementEngine.MoveBlock(Id.entityID, value); } - /// - /// Returns an array of blocks that are connected to this one. - /// - public Block[] ConnectedCubes => BlockEngine.GetConnectedBlocks(Id.entityID); - /// /// The block's current rotation in degrees. /// @@ -94,13 +90,57 @@ namespace GamecraftModdingAPI set => RotationEngine.RotateBlock(Id.entityID, value); } + /// + /// The block's type (ID). Changing from or to a functional part may crash the game. + /// + public BlockIDs Type + { + get => (BlockIDs) BlockEngine.GetBlockInfo(Id).DBID; + set + { + BlockEngine.GetBlockInfo(Id).DBID = (uint) value; + uint prefabId = PrefabsID.GetPrefabId((uint) value, 0); + BlockEngine.GetBlockInfo(Id).prefabID = prefabId; + BlockEngine.GetBlockInfo(Id) = new PhysicsPrefabEntityStruct(prefabId); + } + } + + public BlockColors Color + { + get => (BlockColors) (BlockEngine.GetBlockInfo(Id).indexInPalette % 10); + set + { + ref var color = ref BlockEngine.GetBlockInfo(Id); + color.indexInPalette = (byte) (color.indexInPalette / 10 * 10 + value); + color.needsUpdate = true; + } + } + + public byte ColorDarkness + { + get => (byte) (BlockEngine.GetBlockInfo(Id).indexInPalette / 10); + set + { + ref var color = ref BlockEngine.GetBlockInfo(Id); + color.indexInPalette = (byte) (10 * (byte) value + color.indexInPalette % 10); + color.needsUpdate = true; + } + } + + /// + /// Returns an array of blocks that are connected to this one. + /// + public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id.entityID); + /// /// Removes this block. /// /// True if the block exists and could be removed. - public bool Remove() + public bool Remove() => RemovalEngine.RemoveBlock(Id); + + public override string ToString() { - return RemovalEngine.RemoveBlock(Id); + return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}"; } public static void Init() diff --git a/GamecraftModdingAPI/Blocks/BlockEngine.cs b/GamecraftModdingAPI/Blocks/BlockEngine.cs index ad2af1b..a851643 100644 --- a/GamecraftModdingAPI/Blocks/BlockEngine.cs +++ b/GamecraftModdingAPI/Blocks/BlockEngine.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; -using GamecraftModdingAPI.Engines; + using RobocraftX.Blocks; using RobocraftX.Common; using Svelto.DataStructures; using Svelto.ECS; -using Svelto.ECS.EntityStructs; -using Unity.Mathematics; + +using GamecraftModdingAPI.Engines; namespace GamecraftModdingAPI.Blocks { @@ -35,5 +35,10 @@ namespace GamecraftModdingAPI.Blocks ret[i] = new Block(cubesToProcess[i]); return ret; } + + public ref T GetBlockInfo(EGID blockID) where T : struct, IEntityComponent + { + return ref entitiesDB.QueryEntity(blockID); + } } } \ No newline at end of file diff --git a/GamecraftModdingAPI/Blocks/BlockIdentifiers.cs b/GamecraftModdingAPI/Blocks/BlockIdentifiers.cs index 60819fe..eb479c0 100644 --- a/GamecraftModdingAPI/Blocks/BlockIdentifiers.cs +++ b/GamecraftModdingAPI/Blocks/BlockIdentifiers.cs @@ -1,11 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Reflection; - -using Svelto.ECS; +using Svelto.ECS; using RobocraftX.Common; using HarmonyLib; diff --git a/GamecraftModdingAPI/Blocks/MovementEngine.cs b/GamecraftModdingAPI/Blocks/MovementEngine.cs index 6615949..a887763 100644 --- a/GamecraftModdingAPI/Blocks/MovementEngine.cs +++ b/GamecraftModdingAPI/Blocks/MovementEngine.cs @@ -1,24 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using RobocraftX; -using RobocraftX.Blocks; -using RobocraftX.Blocks.Ghost; -using RobocraftX.Common; -using RobocraftX.Multiplayer; -using RobocraftX.SimulationModeState; +using RobocraftX.Common; using RobocraftX.UECS; -using Unity.Entities; -using Svelto.Context; using Svelto.ECS; using Svelto.ECS.EntityStructs; using Unity.Transforms; using Unity.Mathematics; -using UnityEngine; -using Svelto.DataStructures; using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines; diff --git a/GamecraftModdingAPI/Blocks/PlacementEngine.cs b/GamecraftModdingAPI/Blocks/PlacementEngine.cs index d0a1192..28642e8 100644 --- a/GamecraftModdingAPI/Blocks/PlacementEngine.cs +++ b/GamecraftModdingAPI/Blocks/PlacementEngine.cs @@ -4,21 +4,14 @@ using System.Reflection; using DataLoader; using HarmonyLib; using RobocraftX.Blocks; -using RobocraftX.Blocks.Ghost; using RobocraftX.Blocks.Scaling; using RobocraftX.Character; -using RobocraftX.CommandLine.Custom; using RobocraftX.Common; -using RobocraftX.Common.Input; -using RobocraftX.Common.Utilities; using RobocraftX.CR.MachineEditing; -using RobocraftX.StateSync; using Svelto.ECS; using Svelto.ECS.EntityStructs; -using Unity.Jobs; using Unity.Mathematics; using UnityEngine; -using uREPL; using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines; diff --git a/GamecraftModdingAPI/Blocks/RemovalEngine.cs b/GamecraftModdingAPI/Blocks/RemovalEngine.cs index 823cc86..05ad7c9 100644 --- a/GamecraftModdingAPI/Blocks/RemovalEngine.cs +++ b/GamecraftModdingAPI/Blocks/RemovalEngine.cs @@ -2,15 +2,9 @@ using System.Reflection; using HarmonyLib; using RobocraftX.Blocks; -using RobocraftX.Blocks.Ghost; -using RobocraftX.Character.Camera; -using RobocraftX.Character.Factories; using RobocraftX.Common; -using RobocraftX.Players; using Svelto.ECS; -using uREPL; -using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines; diff --git a/GamecraftModdingAPI/Blocks/RotationEngine.cs b/GamecraftModdingAPI/Blocks/RotationEngine.cs index b295441..88c43d2 100644 --- a/GamecraftModdingAPI/Blocks/RotationEngine.cs +++ b/GamecraftModdingAPI/Blocks/RotationEngine.cs @@ -1,21 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using RobocraftX; -using RobocraftX.Blocks; -using RobocraftX.Blocks.Ghost; -using RobocraftX.Common; -using RobocraftX.Multiplayer; -using RobocraftX.SimulationModeState; +using RobocraftX.Common; using RobocraftX.UECS; -using Unity.Entities; -using Svelto.Context; using Svelto.ECS; using Svelto.ECS.EntityStructs; -using Unity.Transforms; using Unity.Mathematics; using UnityEngine; diff --git a/GamecraftModdingAPI/Blocks/SignalEngine.cs b/GamecraftModdingAPI/Blocks/SignalEngine.cs index 6bac3cb..90343e2 100644 --- a/GamecraftModdingAPI/Blocks/SignalEngine.cs +++ b/GamecraftModdingAPI/Blocks/SignalEngine.cs @@ -1,27 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using RobocraftX; -using RobocraftX.Blocks; -using RobocraftX.Blocks.Ghost; -using RobocraftX.Common; -using RobocraftX.Multiplayer; -using RobocraftX.SimulationModeState; -using RobocraftX.UECS; -using Unity.Entities; -using Svelto.Context; -using Svelto.DataStructures; -using Svelto.ECS; -using Svelto.ECS.EntityStructs; -using Unity.Transforms; -using Unity.Mathematics; -using UnityEngine; +using Svelto.ECS; using Gamecraft.Wires; -using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines; namespace GamecraftModdingAPI.Blocks diff --git a/GamecraftModdingAPI/Blocks/Signals.cs b/GamecraftModdingAPI/Blocks/Signals.cs index c844d3a..6adf670 100644 --- a/GamecraftModdingAPI/Blocks/Signals.cs +++ b/GamecraftModdingAPI/Blocks/Signals.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Svelto.ECS; +using Svelto.ECS; using GamecraftModdingAPI.Utility; diff --git a/GamecraftModdingAPI/Blocks/Tweakable.cs b/GamecraftModdingAPI/Blocks/Tweakable.cs index 920bbb6..6405ac3 100644 --- a/GamecraftModdingAPI/Blocks/Tweakable.cs +++ b/GamecraftModdingAPI/Blocks/Tweakable.cs @@ -1,8 +1,4 @@ -using System; - -using Svelto.ECS; - -namespace GamecraftModdingAPI.Blocks +namespace GamecraftModdingAPI.Blocks { /// /// Common tweakable stats operations. diff --git a/GamecraftModdingAPI/Blocks/TweakableEngine.cs b/GamecraftModdingAPI/Blocks/TweakableEngine.cs index 63b02f7..36ace8a 100644 --- a/GamecraftModdingAPI/Blocks/TweakableEngine.cs +++ b/GamecraftModdingAPI/Blocks/TweakableEngine.cs @@ -1,11 +1,7 @@ -using System; -using System.Reflection; - -using RobocraftX.Blocks; +using RobocraftX.Blocks; using Gamecraft.Wires; using Svelto.ECS; -using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines; diff --git a/GamecraftModdingAPI/Blocks/TweakableStat.cs b/GamecraftModdingAPI/Blocks/TweakableStat.cs index 18f5bad..228c89d 100644 --- a/GamecraftModdingAPI/Blocks/TweakableStat.cs +++ b/GamecraftModdingAPI/Blocks/TweakableStat.cs @@ -1,5 +1,4 @@ -using System; -namespace GamecraftModdingAPI.Blocks +namespace GamecraftModdingAPI.Blocks { public enum TweakableStat { diff --git a/GamecraftModdingAPI/Main.cs b/GamecraftModdingAPI/Main.cs index ea5911f..e8ab256 100644 --- a/GamecraftModdingAPI/Main.cs +++ b/GamecraftModdingAPI/Main.cs @@ -72,7 +72,6 @@ namespace GamecraftModdingAPI // init object-oriented classes Player.Init(); Block.Init(); - RuntimeCommands.Register("getblock", () => new Player(PlayerType.Local).GetBlockLookedAt()); Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized"); } diff --git a/GamecraftModdingAPI/Player.cs b/GamecraftModdingAPI/Player.cs index 3f11cfb..29546da 100644 --- a/GamecraftModdingAPI/Player.cs +++ b/GamecraftModdingAPI/Player.cs @@ -3,7 +3,6 @@ using Unity.Mathematics; using GamecraftModdingAPI.Players; -using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI { diff --git a/GamecraftModdingAPI/Players/PlayerEngine.cs b/GamecraftModdingAPI/Players/PlayerEngine.cs index d1df72e..f8e53f0 100644 --- a/GamecraftModdingAPI/Players/PlayerEngine.cs +++ b/GamecraftModdingAPI/Players/PlayerEngine.cs @@ -248,10 +248,7 @@ namespace GamecraftModdingAPI.Players ? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast) : maxDistance; if (rayCast.hit && rayCast.distance <= distance) - { - Console.WriteLine("Hit block: " + rayCast.hitEgid); return new Block(rayCast.hitEgid); - } return null; } diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 5ea4965..c241159 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -12,6 +12,7 @@ using RobocraftX.SimulationModeState; using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Events; using GamecraftModdingAPI.Utility; +using GamecraftModdingAPI.Blocks; namespace GamecraftModdingAPI.Tests { @@ -100,7 +101,7 @@ namespace GamecraftModdingAPI.Tests .Action((float x, float y, float z) => { if (GameState.IsBuildMode()) - foreach (var block in Block.GetLastPlacedBlock().ConnectedCubes) + foreach (var block in Block.GetLastPlacedBlock().GetConnectedCubes()) block.Position += new Unity.Mathematics.float3(x, y, z); else GamecraftModdingAPI.Utility.Logging.CommandLogError("Blocks can only be moved in Build mode!"); @@ -141,6 +142,12 @@ namespace GamecraftModdingAPI.Tests Tasks.Scheduler.Schedule(task); }).Build(); + CommandBuilder.Builder("getBlock") + .Action(() => uREPL.Log.Output(new Player(Players.PlayerType.Local).GetBlockLookedAt()+"")).Build(); + CommandBuilder.Builder("changeToAluminium") + .Action(() => new Player(Players.PlayerType.Local).GetBlockLookedAt().Type = BlockIDs.AluminiumCube) + .Build(); + /* CommandManager.AddCommand(new SimpleCustomCommandEngine((float d) => { UnityEngine.Camera.main.fieldOfView = d; }, "SetFOV", "Set the player camera's field of view"));