Initial commit
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user