First select cubes, then perform a bunch of stuff

This commit is contained in:
Norbi Peti 2020-06-05 01:00:03 +02:00
parent d3ac5455df
commit dbb65ac65a
2 changed files with 83 additions and 75 deletions

View file

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Gamecraft.Wires.ChannelsCommon;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Commands;
using GamecraftModdingAPI.Engines;
using GamecraftModdingAPI.Players;
@ -8,10 +11,12 @@ using GamecraftModdingAPI.Utility;
using HarmonyLib;
using IllusionPlugin;
using RobocraftX.Character.Weapons;
using RobocraftX.CommandLine.Custom;
using RobocraftX.Common.Players;
using Svelto.ECS;
using Svelto.ECS.Internal;
using Unity.Mathematics;
using Main = GamecraftModdingAPI.Main;
namespace BlockMod
{
@ -22,18 +27,18 @@ namespace BlockMod
Main.Init();
GameClient.SetDebugInfo("BlockModInfo", GetBlockInfo);
Block[] blocks = new Block[0];
CommandBuilder.Builder("scaleBlocksRelative",
Block refBlock = null;
CommandBuilder.Builder("scaleBlocks",
"Scales the blocks you're looking at, relative to current size (current scale * new scale)." +
" The block you're looking at stays where it is, everything else is moved next to it." +
" The command remembers the cluster of blocks, use forgetBlocks to forget it.")
.Action<float, float, float>((scaleX, scaleY, scaleZ) =>
{
var bl = new Player(PlayerType.Local).GetBlockLookedAt();
var cubes = GetCubes(ref blocks, bl);
if (cubes == null) return;
float3 reference = bl.Position;
if(CheckNoBlocks(blocks)) return;
// ReSharper disable once PossibleNullReferenceException
float3 reference = refBlock.Position;
float3 scale = new float3(scaleX, scaleY, scaleZ);
foreach (var block in cubes)
foreach (var block in blocks)
{
block.Scale *= scale;
block.Position = reference + (block.Position - reference) * scale;
@ -45,22 +50,74 @@ namespace BlockMod
"The scale is relative, 1 means no change. Look at a block previously scaled to scale all of the blocks that were connected to it.")
.Action<float, float, float>((scaleX, scaleY, scaleZ) =>
{
var cubes = GetCubesLookedAt(ref blocks);
if (cubes == null) return;
if(CheckNoBlocks(blocks)) return;
float3 scale = new float3(scaleX, scaleY, scaleZ);
foreach (var block in cubes)
foreach (var block in blocks)
block.Scale *= scale;
}).Build();
CommandBuilder.Builder("moveBlocks", "Moves the blocks around.")
.Action<float, float, float>((x, y, z) =>
{
var cubes = GetCubesLookedAt(ref blocks);
if (cubes == null) return;
foreach (var block in cubes)
block.Position += new float3(x, y, z);
if (CheckNoBlocks(blocks)) return;
if (GameState.IsBuildMode())
foreach (var block in blocks)
block.Position += new float3(x, y, z);
else if (GameState.IsSimulationMode())
foreach (var body in GetSimBodies(blocks))
body.Position += new float3(x, y, z);
}).Build();
CommandBuilder.Builder("colorBlocks", "Colors the blocks you're looking at")
.Action<string, byte>((color, darkness) =>
{
if(CheckNoBlocks(blocks)) return;
if (!Enum.TryParse(color, true, out BlockColors clr))
{
Logging.CommandLogWarning("Color " + color + " not found");
}
foreach (var block in blocks)
block.Color = new BlockColor {Color = clr, Darkness = darkness};
}).Build();
CommandBuilder.Builder("selectBlocksLookedAt", "Selects blocks (1 or more) to change. Only works in build mode, however the blocks can be changed afterwards." +
" Paramter: whether one (true) or all connected (false) blocks should be selected.")
.Action<bool>(single =>
{
refBlock = new Player(PlayerType.Local).GetBlockLookedAt();
blocks = single ? new[] {refBlock} : refBlock?.GetConnectedCubes() ?? new Block[0];
Logging.CommandLog(blocks == null ? "Selection cleared." : blocks.Length + " blocks selected.");
}).Build();
CommandBuilder.Builder("selectBlocksWithID", "Selects blocks with a specific object ID.")
.Action<char>(id =>
blocks = (refBlock = ObjectIdentifier.GetByID(id).FirstOrDefault())?.GetConnectedCubes() ??
new Block[0]).Build();
ConsoleCommands.RegisterWithChannel("selectBlocksFromChannel", id =>
{
blocks = ObjectIdentifier.GetBySimID(id).SelectMany(block => block.GetConnectedCubes()).ToArray();
},
BinaryChannelUtilities.ChannelType.Object);
CommandBuilder.Builder("pushBlocks", "Adds velocity to the selected blocks. Only works in simulation.")
.Action<float, float, float>((x, y, z) =>
{
foreach (var block in GetSimBodies(blocks))
block.Velocity += new float3(x, y, z);
}).Build();
CommandBuilder.Builder("pushRotateBlocks", "Adds angular velocity to the selected blocks. Only works in simulation.")
.Action<float, float, float>((x, y, z) =>
{
foreach (var block in GetSimBodies(blocks))
block.AngularVelocity += new float3(x, y, z);
}).Build();
CommandBuilder.Builder("pushPlayer", "Adds velocity to the player.")
.Action<float, float, float>((x, y, z) =>
{
new Player(PlayerType.Local).Velocity += new float3(x, y, z);
}).Build();
CommandBuilder.Builder("pushRotatePlayer", "Adds angular velocity to the player.")
.Action<float, float, float>((x, y, z) =>
{
new Player(PlayerType.Local).AngularVelocity += new float3(x, y, z);
}).Build();
CommandBuilder.Builder("forgetBlocks", "Forgets the cluster of blocks that we're changing.")
.Action(() => blocks = new Block[0]).Build();
GameEngineManager.AddGameEngine(new Test());
}
@ -153,22 +210,19 @@ namespace BlockMod
return "Switching modes...";
}
private bool SameCluster(Block[] bs, Block block)
private bool CheckNoBlocks(Block[] blocks)
{
var id = block.Id;
return bs.Any(b => b.Id == id);
if (blocks.Length == 0)
{
Logging.CommandLogWarning("No blocks selected. Use selectBlocks first.");
return true;
}
return false;
}
private Block[] GetCubes(ref Block[] bs, Block block) =>
SameCluster(bs, block) ? bs : bs = block.GetConnectedCubes();
private Block[] GetCubesLookedAt(ref Block[] bs)
{
var bl = new Player(PlayerType.Local).GetBlockLookedAt();
if (bl == null) return null;
var cubes = GetCubes(ref bs, bl);
return cubes;
}
public IEnumerable<SimBody> GetSimBodies(Block[] blocks)
=> blocks.Select(block => block.GetSimBody()).Distinct();
public void OnApplicationQuit()
{

View file

@ -138,7 +138,7 @@
<Reference Include="Gamecraft.Wires.Mockup, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\ref\Gamecraft_Data\Managed\Gamecraft.Wires.Mockup.dll</HintPath>
</Reference>
<Reference Include="GamecraftModdingAPI, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null">
<Reference Include="GamecraftModdingAPI">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll</HintPath>
</Reference>
<Reference Include="GameState, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
@ -345,52 +345,6 @@
<Reference Include="Svelto.Tasks, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\ref\Gamecraft_Data\Managed\Svelto.Tasks.dll</HintPath>
</Reference>
<Reference Include="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.ComponentModel.Composition.dll</HintPath>
</Reference>
<Reference Include="System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Configuration.dll</HintPath>
</Reference>
<Reference Include="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Core.dll</HintPath>
</Reference>
<Reference Include="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Data.dll</HintPath>
</Reference>
<Reference Include="System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.EnterpriseServices.dll</HintPath>
</Reference>
<Reference Include="System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Numerics.dll</HintPath>
</Reference>
<Reference Include="System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Runtime.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
<Reference Include="System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Runtime.Serialization.dll</HintPath>
</Reference>
<Reference Include="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Security.dll</HintPath>
</Reference>
<Reference Include="System.ServiceModel.Internals, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.ServiceModel.Internals.dll</HintPath>
</Reference>
<Reference Include="System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Transactions.dll</HintPath>
</Reference>
<Reference Include="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Xml.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<HintPath>..\..\ref\Gamecraft_Data\Managed\System.Xml.Linq.dll</HintPath>
</Reference>
<Reference Include="Unity.Addressables, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\ref\Gamecraft_Data\Managed\Unity.Addressables.dll</HintPath>
</Reference>