Initial commit

This commit is contained in:
HussienX72u
2026-07-21 08:56:10 +03:00
commit 4017028111
9410 changed files with 2150500 additions and 0 deletions
@@ -0,0 +1,61 @@
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CommandDTO
{
[JsonProperty]
private string commandName;
[JsonProperty]
private Dictionary<string, object> parameters = new Dictionary<string, object>();
public string CommandName => commandName;
public Dictionary<string, object> Parameters => parameters;
public ICommand converToCommand()
{
// Implementation for converting DTO to Command
switch (commandName)
{
case "CreateCommand":
return new CreateBlockCommand(this);
case "MoveCommand":
return new MoveBlockCommand(this);
case "RotateCommand":
return new RotateblockCommand(this);
case "DeleteCommand":
return new DeleteBlockCommand(this);
case "DeleteGroup":
return new DeleteGroupCommand(this);
default:
Debug.LogWarning($"Unknown command name: {commandName}");
return null;
}
}
public class CommandDTOBuilder
{
private string commandName;
private Dictionary<string, object> parameters = new Dictionary<string, object>();
public CommandDTOBuilder SetCommandName(string name)
{
this.commandName = name;
return this;
}
public CommandDTOBuilder AddParameter(string key, object value)
{
parameters[key] = value;
return this;
}
public CommandDTO Build()
{
CommandDTO commandDTO = new CommandDTO();
commandDTO.commandName = this.commandName;
commandDTO.parameters = this.parameters;
return commandDTO;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3c59b88464539e84c87b396c1cae0b89
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,111 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class CommandHandler : MonoBehaviour
{
private static CommandHandler instance;
private CommandManager commandManager;
private Environment environment;
public static CommandHandler getInstance()
{
if (instance == null)
{
instance = FindObjectOfType<CommandHandler>();
if (instance == null)
{
GameObject commandHandlerObject = new GameObject("CommandHandler");
instance = commandHandlerObject.AddComponent<CommandHandler>();
}
}
return instance;
}
private void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
commandManager = CommandManager.getInstance();
environment = Environment.getInstance();
}
private void Update()
{
// Detect Ctrl+Z for undo and Ctrl+Y for redo
bool ctrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
if (ctrl)
{
if (Input.GetKeyDown(KeyCode.Z))
{
Debug.Log("Ctrl+Z detected: Undo action");
undoAction();
}
else if (Input.GetKeyDown(KeyCode.Y))
{
Debug.Log("Ctrl+Y detected: Redo action");
redoAction();
}
}
}
public void createBlock(string prefabId, Vector3 position, Quaternion rotation)
{
ICommand createCommand = new CreateBlockCommand(prefabId, position, rotation);
commandManager.executeCommand(createCommand);
}
public void moveBlock(string blockId, Vector3 prePosition, Vector3 newPosition, Quaternion preRotation, Quaternion newRotation)
{
ICommand moveCommand = new MoveBlockCommand(blockId, prePosition, newPosition, preRotation, newRotation);
commandManager.executeCommand(moveCommand);
}
public void removeBlock(string blockId)
{
ICommand deleteCommand = new DeleteBlockCommand(blockId);
commandManager.executeCommand(deleteCommand);
}
public void removeGroup(List<string> blockIds)
{
ICommand deleteCommand = new DeleteGroupCommand(blockIds);
commandManager.executeCommand(deleteCommand);
}
public void rotateBlock(string blockId, Quaternion newRotation)
{
ICommand rotateCommand = new RotateblockCommand(blockId, newRotation);
commandManager.executeCommand(rotateCommand);
}
public void undoAction()
{
commandManager.undo();
}
public void redoAction()
{
commandManager.redo();
}
public void clearHistory()
{
commandManager.clearHistory();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8802da992aedca34181376075fc0c011
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,73 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CommandManager
{
private static CommandManager instance;
private LinkedList<ICommand> commandStack = new LinkedList<ICommand>();
private LinkedList<ICommand> redoStack = new LinkedList<ICommand>();
private int maxCapacity = 100; // added to prevent memory leak in the long run
private CommandManager()
{
}
public static CommandManager getInstance()
{
if (instance == null)
{
instance = new CommandManager();
}
return instance;
}
public void setMaxCapacity(int capacity)
{
maxCapacity = capacity;
}
public void executeCommand(ICommand command)
{
command.execute();
commandStack.AddFirst(command);
redoStack.Clear();
if (commandStack.Count > maxCapacity)
{
commandStack.RemoveLast();
}
}
public void undo()
{
if (commandStack.Count > 0)
{
ICommand command = commandStack.First.Value;
commandStack.RemoveFirst();
command.undo();
redoStack.AddFirst(command);
}
}
public ICommand redo()
{
if (redoStack.Count > 0)
{
ICommand command = redoStack.First.Value;
redoStack.RemoveFirst();
command.execute();
commandStack.AddFirst(command);
return command;
}
return null;
}
public void clearHistory()
{
commandStack.Clear();
redoStack.Clear();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 55e3134ede92000488aa626c2beb2d62
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,16 @@
{
"name": "CommandSystem",
"rootNamespace": "",
"references": [
"GUID:0f8d877125e73cc40a31b031c2e5fe60"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 87ecf4ad9955b8f49b5bd39d4f087204
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 96d25cd0bae776b4082ba6691f9f0d52
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,65 @@
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<Block>();
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();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6846488550ca76e49ab5a48c410f5f00
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,55 @@
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();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8631dad80687e2b45a2d8ec6ca40f879
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,50 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeleteGroupCommand : ICommand
{
private List<string> blocksId;
private List<DeleteBlockCommand> deleteBlockCommands;
public DeleteGroupCommand(List<string> blocksId)
{
this.blocksId = blocksId;
deleteBlockCommands = new List<DeleteBlockCommand>();
}
public DeleteGroupCommand(CommandDTO commandDTO)
{
this.blocksId = (List<string>)commandDTO.Parameters["blocksId"];
deleteBlockCommands = new List<DeleteBlockCommand>();
}
public void execute()
{
deleteBlockCommands.Clear();
foreach (string id in blocksId)
{
DeleteBlockCommand command = new DeleteBlockCommand(id);
command.execute();
deleteBlockCommands.Add(command);
}
}
public void undo()
{
foreach (DeleteBlockCommand command in deleteBlockCommands)
{
command.undo();
}
}
public CommandDTO toDTO()
{
return new CommandDTO.CommandDTOBuilder()
.SetCommandName("DeleteGroup")
.AddParameter("blocksId", blocksId)
.Build();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e09c34ca40233944e8c2702acea7eb90
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,6 @@
public interface ICommand
{
void execute();
void undo();
CommandDTO toDTO();
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: da8cfd5f6fb13e2479900c9d4caafa4c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,64 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBlockCommand : ICommand
{
private string blockId;
private Vector3 previousPosition;
private Quaternion previousRotation;
private Vector3 newPosition;
private Quaternion newRotation;
private Environment environment;
private Transform blockTransform;
public MoveBlockCommand(string blockId, Vector3 previousPosition, Vector3 newPosition, Quaternion previousRotation, Quaternion newRotation)
{
this.blockId = blockId;
this.newPosition = newPosition;
this.previousPosition = previousPosition;
this.environment = Environment.getInstance();
this.previousRotation = previousRotation;
this.newRotation = newRotation;
}
public MoveBlockCommand(CommandDTO commandDTO)
{
this.blockId = commandDTO.Parameters["blockId"].ToString();
this.newPosition = (Vector3)commandDTO.Parameters["newPosition"];
this.previousPosition = (Vector3)commandDTO.Parameters["previousPosition"];
this.newRotation = (Quaternion)commandDTO.Parameters["newRotation"];
this.previousRotation = (Quaternion)commandDTO.Parameters["previousRotation"];
this.environment = Environment.getInstance();
}
public void execute()
{
Block block = environment.getBlock(blockId);
if (block != null)
environment.moveBlock(block, previousPosition, previousRotation, newPosition, newRotation);
else
Debug.LogWarning("MoveblockCommand: block with ID " + blockId + " not found.");
}
public void undo()
{
Block block = environment.getBlock(blockId);
if (block != null)
environment.moveBlock(block, newPosition, newRotation, previousPosition, previousRotation);
else
Debug.LogWarning("MoveblockCommand: block with ID " + blockId + " not found.");
}
public CommandDTO toDTO()
{
return new CommandDTO.CommandDTOBuilder()
.SetCommandName("MoveCommand")
.AddParameter("blockId", blockId)
.AddParameter("newPosition", newPosition)
.AddParameter("previousPosition", previousPosition)
.AddParameter("newRotation", newRotation)
.AddParameter("previousRotation", previousRotation)
.Build();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: eb9209f3464c8224eb210299f2a742ea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,57 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateblockCommand : ICommand
{
private string blockId;
private Quaternion newRotation;
private Quaternion previousRotation;
private Environment environment;
private Transform blockTransform;
public RotateblockCommand(string blockId, Quaternion newRotation)
{
this.blockId = blockId;
this.newRotation = newRotation;
this.environment = Environment.getInstance();
}
public RotateblockCommand(CommandDTO commandDTO)
{
this.blockId = commandDTO.Parameters["blockId"].ToString();
this.newRotation = (Quaternion)commandDTO.Parameters["newRotation"];
this.environment = Environment.getInstance();
}
public void execute()
{
blockTransform = environment.getBlock(blockId)?.transform;
if (blockTransform != null)
{
previousRotation = blockTransform.rotation;
blockTransform.rotation = newRotation;
}
else
{
Debug.LogWarning("RotateblockCommand: block with ID " + blockId + " not found.");
}
}
public void undo()
{
blockTransform = environment.getBlock(blockId)?.transform;
if (blockTransform != null)
{
blockTransform.rotation = previousRotation;
}
}
public CommandDTO toDTO()
{
return new CommandDTO.CommandDTOBuilder()
.SetCommandName("RotateCommand")
.AddParameter("blockId", blockId)
.AddParameter("newRotation", newRotation)
.Build();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8100c36e75da6e42a8d788015b34543
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: