using System.Collections; using System.Collections.Generic; using UnityEngine; public class CreateBlockCommand : ICommand { private string prefabId; private Vector3 position; private Quaternion rotation; private GameObject objectCreated; private string blockId; private bool layerDiff; private BlockFactory factory; private Environment environment; public CreateBlockCommand(string prefabId, Vector3 position, Quaternion rotation, bool LayerDiff = false) { this.prefabId = prefabId; this.position = position; this.rotation = rotation; this.layerDiff = LayerDiff; this.factory = BlockFactory.getInstance(); this.environment = Environment.getInstance(); } public CreateBlockCommand(CommandDTO commandDTO) { this.prefabId = commandDTO.Parameters["prefabId"].ToString(); this.position = (Vector3)commandDTO.Parameters["position"]; this.rotation = (Quaternion)commandDTO.Parameters["rotation"]; this.layerDiff = (bool)commandDTO.Parameters["layerDiff"]; this.factory = BlockFactory.getInstance(); this.environment = Environment.getInstance(); } public void execute() { objectCreated = factory.createblock(prefabId, position, rotation, blockId, layerDiff); if (objectCreated != null) { Block blockComponent = objectCreated.GetComponent(); blockId = blockComponent.getBlockId(); } else { Debug.LogWarning("CreateblockCommand: Could not create block with prefab ID " + prefabId); } } public void undo() { environment.removeBlock(blockId); } public CommandDTO toDTO() { return new CommandDTO.CommandDTOBuilder() .SetCommandName("CreateCommand") .AddParameter("prefabId", prefabId) .AddParameter("position", position) .AddParameter("rotation", rotation) .AddParameter("layerDiff", layerDiff) .Build(); } }