Initial commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class Block : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private BlockData blockData;
|
||||
private string blockId;
|
||||
|
||||
|
||||
public abstract void setupBlock();
|
||||
public abstract void simulate();
|
||||
|
||||
public BlockData getBlockData()
|
||||
{
|
||||
return blockData;
|
||||
}
|
||||
|
||||
public void SetBlockData(BlockData BD) => blockData = BD;
|
||||
|
||||
public void setBlockId(string id)
|
||||
{
|
||||
blockId = id;
|
||||
}
|
||||
public string getBlockId()
|
||||
{
|
||||
return blockId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92f0bbe7028183946b24328df29a379b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "BlockData", menuName = "Blocks/Block Data", order = 51)]
|
||||
public class BlockData : ScriptableObject
|
||||
{
|
||||
[Header("Block Info")]
|
||||
public string blockName;
|
||||
public GameObject blockPrefab;
|
||||
public CategoryData associatedCategory;
|
||||
|
||||
[Header("Icon")]
|
||||
public Texture2D icon;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0f036c260429df458bc55fbece6c077
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,70 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class BlockFactory : MonoBehaviour
|
||||
{
|
||||
private static BlockFactory Instance;
|
||||
|
||||
private Environment environment;
|
||||
private List<BlockData> blockDataList;
|
||||
private Dictionary<string, BlockData> blocksData = new Dictionary<string, BlockData>();
|
||||
public static BlockFactory getInstance()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
GameObject factoryObject = new GameObject("blockFactory");
|
||||
Instance = factoryObject.AddComponent<BlockFactory>();
|
||||
}
|
||||
return Instance;
|
||||
}
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
environment = Environment.getInstance();
|
||||
blockDataList = Resources.LoadAll<BlockData>("BlocksData").ToList();
|
||||
|
||||
foreach (BlockData blockData in blockDataList)
|
||||
{
|
||||
blocksData.Add(blockData.blockName, blockData);
|
||||
}
|
||||
}
|
||||
|
||||
public GameObject createblock(string prefabId, Vector3 position, Quaternion rotation, string id = null, bool layerDiff = false)
|
||||
{
|
||||
Debug.Log(prefabId);
|
||||
|
||||
if (blocksData.ContainsKey(prefabId))
|
||||
{
|
||||
GameObject blockPrefab = blocksData[prefabId].blockPrefab;
|
||||
GameObject block = Instantiate(blockPrefab, position, rotation);
|
||||
if (layerDiff)
|
||||
{
|
||||
block.gameObject.layer = LayerMask.NameToLayer("Ignore Interaction");
|
||||
Debug.Log($"Set layer of {block} to {LayerMask.LayerToName(block.layer)}");
|
||||
}
|
||||
// the only blocks added to the environment is created with BlockFactory
|
||||
environment.addBlock(block.GetComponent<Block>(), id);
|
||||
return block;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"block with PrefabId {prefabId} not found!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6119bef9c1e159341b755fad6f48329c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "ItemSystem",
|
||||
"rootNamespace": "",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f8d877125e73cc40a31b031c2e5fe60
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "Category", menuName = "YoussefScripts/Create a Category")]
|
||||
public class CategoryData : ScriptableObject
|
||||
{
|
||||
public string ID;
|
||||
public List<BlockData> blocks;
|
||||
public Sprite icon;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec390037e79ae134b9fe859322a98be8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CodingBlock : Block
|
||||
{
|
||||
public override void setupBlock()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public override void simulate()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1aca36b1d6467654a9a832b13390ed0e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ElectricalBlock : Block
|
||||
{
|
||||
public override void setupBlock()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public override void simulate()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10902277cb1a7434ebcb6c50edc00c52
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class Environment : MonoBehaviour
|
||||
{
|
||||
private static Environment instance;
|
||||
|
||||
private Dictionary<string, Block> blockRegistry = new Dictionary<string, Block>();
|
||||
|
||||
public UnityEvent<Block> onBlockAddedEvent;
|
||||
public UnityEvent<Block, Vector3, Quaternion> onBlockMovedEvt;
|
||||
public UnityEvent<Block> onBlockRemovedEvent;
|
||||
public UnityEvent onChangeEvent;
|
||||
|
||||
public static Environment getInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = FindObjectOfType<Environment>();
|
||||
if (instance == null)
|
||||
{
|
||||
GameObject environmentObject = new GameObject("Environment");
|
||||
instance = environmentObject.AddComponent<Environment>();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public Dictionary<String, Block> getBlockRegistry()
|
||||
{
|
||||
return blockRegistry;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void addBlock(Block block, string existingUUID = null)
|
||||
{
|
||||
// 1. Assign ID: Use existing (Undo/Redo) or generate a new one (Fresh placement)
|
||||
string uuid = string.IsNullOrEmpty(existingUUID) ? Guid.NewGuid().ToString() : existingUUID;
|
||||
|
||||
block.setBlockId(uuid);
|
||||
|
||||
// 2. Register in the dictionary
|
||||
if (!blockRegistry.ContainsKey(uuid))
|
||||
{
|
||||
blockRegistry.Add(uuid, block);
|
||||
}
|
||||
else
|
||||
{
|
||||
blockRegistry[uuid] = block; // Update reference if already exists
|
||||
}
|
||||
applyChanges();
|
||||
onBlockAddedEvent.Invoke(block);
|
||||
}
|
||||
|
||||
public void moveBlock(Block block, Vector3 oldPos, Quaternion oldRot, Vector3 newPos, Quaternion newRot)
|
||||
{
|
||||
block.transform.SetPositionAndRotation(newPos, newRot);
|
||||
onBlockMovedEvt.Invoke(block, oldPos, oldRot);
|
||||
applyChanges();
|
||||
}
|
||||
|
||||
public void removeBlock(string uuid)
|
||||
{
|
||||
if (blockRegistry.TryGetValue(uuid, out Block block))
|
||||
{
|
||||
onBlockRemovedEvent.Invoke(block);
|
||||
blockRegistry.Remove(uuid);
|
||||
applyChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void applyChanges()
|
||||
{
|
||||
onChangeEvent.Invoke();
|
||||
}
|
||||
public void completeDestroyBlock(Block block)
|
||||
{
|
||||
if (block != null)
|
||||
Destroy(block.gameObject);
|
||||
}
|
||||
|
||||
public Block getBlock(string uuid)
|
||||
{
|
||||
if (blockRegistry.TryGetValue(uuid, out Block block))
|
||||
{
|
||||
return block;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void clearEnvironment()
|
||||
{
|
||||
foreach (var blockEntry in blockRegistry)
|
||||
{
|
||||
if (blockEntry.Value != null)
|
||||
{
|
||||
Destroy(blockEntry.Value.gameObject);
|
||||
}
|
||||
}
|
||||
blockRegistry.Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8e7f129da58b9746a67217bcf1a4aa9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MechanicalBlock : Block
|
||||
{
|
||||
|
||||
public override void setupBlock()
|
||||
{
|
||||
}
|
||||
|
||||
public override void simulate()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c4a1be166f8c7184ab860f9a010f5764
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MechanicalJoint : Block
|
||||
{
|
||||
public override void setupBlock()
|
||||
{
|
||||
// unimplemented as it's a Joint
|
||||
}
|
||||
|
||||
public override void simulate()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a09709a730033f4986af4e51fe685b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user