Files
JuegoSim/Assets/_Project/Scripts/Code/CommandLayer/Commands/DeleteBlockCommand.cs
T
2026-07-21 08:56:10 +03:00

56 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeleteBlockCommand : ICommand
{
private string blockId;
private string prefabDeletedId;
private Vector3 blockInitialPosition;
private Quaternion blockInitialRotation;
private BlockFactory factory;
private Environment environment;
public DeleteBlockCommand(string blockId)
{
this.blockId = blockId;
this.factory = BlockFactory.getInstance();
this.environment = Environment.getInstance();
}
public DeleteBlockCommand(CommandDTO commandDTO)
{
this.blockId = commandDTO.Parameters["blockId"].ToString();
this.factory = BlockFactory.getInstance();
this.environment = Environment.getInstance();
}
public void execute()
{
Block block = environment.getBlock(blockId);
if (block != null)
{
blockInitialPosition = block.transform.position;
blockInitialRotation = block.transform.rotation;
prefabDeletedId = block.getBlockData()?.blockName;
environment.removeBlock(blockId);
}
else
{
Debug.LogWarning("DeleteblockCommand: block with ID " + blockId + " not found.");
}
}
public void undo()
{
factory.createblock(prefabDeletedId, blockInitialPosition, blockInitialRotation, blockId);
}
public CommandDTO toDTO()
{
return new CommandDTO.CommandDTOBuilder()
.SetCommandName("DeleteCommand")
.AddParameter("blockId", blockId)
.Build();
}
}