BuildingTools/BuildingTools/MirrorModeEngine.cs
NorbiPeti 2ec15427d6 Remove noGarage and improve mirror mode
- Mirroring relative to where the player enabled the mode
- Removing blocks that are where the mirrored counterpart of the removed block would be
2021-11-25 02:01:30 +01:00

122 lines
No EOL
3.8 KiB
C#

using System;
using System.Collections.Generic;
using Gamecraft.GUI.Blueprints;
using RobocraftX.Blocks.Ghost;
using RobocraftX.Common;
using RobocraftX.CR.MachineEditing.BoxSelect;
using RobocraftX.Physics;
using Svelto.ECS;
using Svelto.ECS.EntityStructs;
using Svelto.Tasks;
using Svelto.Tasks.Enumerators;
using Svelto.Tasks.Lean;
using Techblox.Blocks;
using TechbloxModdingAPI;
using TechbloxModdingAPI.App;
using TechbloxModdingAPI.Blocks;
using TechbloxModdingAPI.Blocks.Engines;
using TechbloxModdingAPI.Engines;
using TechbloxModdingAPI.Interface.IMGUI;
using TechbloxModdingAPI.Players;
using TechbloxModdingAPI.Tasks;
using TechbloxModdingAPI.Utility;
using Unity.Mathematics;
using UnityEngine;
namespace BuildingTools
{
public class MirrorModeEngine : IApiEngine
{
private Button _button;
private bool _enabled;
private Block _lastPlaced;
private Block _ghostBlock;
private float3 _offset;
private void BlockOnPlaced(object sender, BlockPlacedRemovedEventArgs e)
{
if (!_enabled) return;
if (Player.LocalPlayer.BuildingMode != PlayerBuildingMode.BlockMode) return;
if (e.ID == _lastPlaced?.Id) return;
_lastPlaced = e.Block.Copy();
_lastPlaced.Position = MirrorPos(_lastPlaced.Position);
_lastPlaced.Flipped = !_lastPlaced.Flipped;
if (math.abs(_lastPlaced.Rotation.y - 90) < float.Epsilon ||
math.abs(_lastPlaced.Rotation.y - 270) < float.Epsilon)
_lastPlaced.Rotation += new float3(0, 180, 0);
}
private void BlockOnRemoved(object sender, BlockPlacedRemovedEventArgs e)
{
if (!_enabled) return;
if (Player.LocalPlayer.BuildingMode != PlayerBuildingMode.BlockMode) return;
var newpos = MirrorPos(e.Block.Position);
foreach (var block in Game.CurrentGame().GetBlocksInGame())
{
if (math.all(math.abs(block.Position - newpos) < 0.1f))
block.Remove();
}
}
private float3 MirrorPos(float3 pos)
{
pos -= _offset;
pos *= new float3(-1, 1, 1);
pos += _offset;
return pos;
}
public void Init()
{
Block.Placed += BlockOnPlaced;
Block.Removed+=BlockOnRemoved;
Game.Enter += OnGameOnEnter;
Game.Exit += (sender, args) => _button = null;
}
private void OnGameOnEnter(object sender, GameEventArgs args)
{
_button = new Button("Mirror mode");
_button.OnClick += (_, __) =>
{
_enabled = !_enabled;
if(_enabled)
_offset = new float3((float)(Math.Round(Player.LocalPlayer.Position.x / 2, 1) * 2), 0, 0);
};
//TryCreateGhostBlock().RunOn(Scheduler.leanRunner);
}
private IEnumerator<TaskContract> TryCreateGhostBlock()
{
int c = 0;
while (_ghostBlock == null && c++ < 10)
{
Console.WriteLine($"Ghost block is {_ghostBlock} and c is {c}");
try
{
//_ghostBlock = Block.CreateGhostBlock();
Console.WriteLine($"New block: {_ghostBlock}");
_ghostBlock.Position += 1;
}
catch (Exception e)
{
Console.WriteLine(e);
}
yield return new WaitForSecondsEnumerator(1f).Continue();
}
}
public void Ready()
{
}
public EntitiesDB entitiesDB { get; set; }
public void Dispose()
{
}
public string Name { get; } = "BuildingToolsMirroModeEngine";
public bool isRemovable { get; } = true;
}
}