public Block AddBlock(MazeNode node) { if (node is MazeRoom) { MazeRoom room = node as MazeRoom; if (room.HasCreated) { return null; } else { room.HasCreated = true; } } Block block = Block.Create(node); int key = Block.GetBlockKey(node.Col, node.Row); if (blockDic.ContainsKey(key)) { BaseLogger.LogFormat("Add same block: {0}", key.ToString()); } blockDic.Add(key, block); node.AboveBlock = block; AddMockNode(node); return block; }
private MazeNode CreateMazeNode(int col, int row) { MazeNode node = new MazeNode(); node.Col = col; node.Row = row; return node; }
public void Init() { mazeData = MazeDataManager.Instance.CurrentMazeData; MazeBuilder builder = new MazeBuilder(mazeData); mazeTable = builder.Build(); StartNode = mazeTable.GetNode(mazeData.StartCol, mazeData.StartRow); InitGlobalExplorationPositions(); // TestGlobalExplorationPositions(); }
public void Dispose() { blockDic.Clear(); mockNodeSet.Clear(); globalExplorationNodeList.Clear(); ToDeleteNodeList.Clear(); ToCreateNodeList.Clear(); prevAroundList.Clear(); mazeTable = null; StartNode = null; mazeData = null; }
public static Block Create(MazeNode node) { if (node == null) { BaseLogger.Log("null node"); return null; } ResourceManager resManager = ResourceManager.Instance; Block block = new Block(); block.Uid = Guid.NewGuid().ToString(); PassageType passageType = MazeUtil.GetPassageType(node); if (node is MazeRoom) { MazeRoom room = node as MazeRoom; block.Data = room.Data; } else { block.Data = BlockDataManager.Instance.GetRandomPassageData(passageType); } block.Script = resManager.LoadAsset<BlockScript>(ObjectType.GameObject, block.Data.GetResPath()); block.Script.Uid = block.Uid; block.Script.transform.parent = RootTransform.Instance.BlockRoot; if (node is MazeRoom) { MazeRoom room = node as MazeRoom; block.SetRotation(room.Direction); block.SetPosition(room.Col, room.Row); } else { block.SetPosition(node.Col, node.Row); int direction = MazeUtil.GetPassageDirection(node, passageType); block.SetRotation(direction); block.InitRandomDecorations(); } block.ExplorationType = node.ExplorationType; return block; }
private void CreateIndicator(MazeNode node) { float cubeSize = GlobalConfig.BlockConfig.MockCubeSize; float posY = GlobalConfig.BlockConfig.MockBlockPosY; GameObject indicator = ResourceManager.Instance.LoadGameObject(ObjectType.GameObject, GlobalConfig.BlockConfig.IndicatorPath); indicator.transform.position = new Vector3(node.Col * cubeSize, posY + 2f, node.Row * cubeSize); indicator.transform.parent = RootTransform.Instance.MockBlockRoot; }
private void CreateMockPassage(MazeNode node) { float cubeSize = GlobalConfig.BlockConfig.MockCubeSize; float posY = GlobalConfig.BlockConfig.MockBlockPosY; GameObject cube = ResourceManager.Instance.LoadGameObject(ObjectType.GameObject, GlobalConfig.BlockConfig.MockPassagePath); cube.transform.position = new Vector3(node.Col * cubeSize, posY, node.Row * cubeSize); cube.transform.parent = RootTransform.Instance.MockBlockRoot; cube.GetComponent<Sparking>().IsEnabled = node.ExplorationType != ExplorationType.Common; GameObject link = null; float offsetSize = cubeSize * 0.5f; if (node.LinkList[0] != null) { link = ResourceManager.Instance.LoadGameObject(ObjectType.GameObject, GlobalConfig.BlockConfig.MockLinkPath); link.transform.localEulerAngles = Vector3.up * 90; link.transform.position = new Vector3(node.Col * cubeSize, posY, node.Row * cubeSize + offsetSize); link.transform.parent = RootTransform.Instance.MockBlockRoot; } if (node.LinkList[1] != null) { link = ResourceManager.Instance.LoadGameObject(ObjectType.GameObject, GlobalConfig.BlockConfig.MockLinkPath); link.transform.position = new Vector3(node.Col * cubeSize + offsetSize, posY, node.Row * cubeSize); link.transform.parent = RootTransform.Instance.MockBlockRoot; } if (node.LinkList[2] != null) { link = ResourceManager.Instance.LoadGameObject(ObjectType.GameObject, GlobalConfig.BlockConfig.MockLinkPath); link.transform.localEulerAngles = Vector3.up * 90; link.transform.position = new Vector3(node.Col * cubeSize, posY, node.Row * cubeSize - offsetSize); link.transform.parent = RootTransform.Instance.MockBlockRoot; } if (node.LinkList[3] != null) { link = ResourceManager.Instance.LoadGameObject(ObjectType.GameObject, GlobalConfig.BlockConfig.MockLinkPath); link.transform.position = new Vector3(node.Col * cubeSize - offsetSize, posY, node.Row * cubeSize); link.transform.parent = RootTransform.Instance.MockBlockRoot; } }
private void ExpandNode(MazeNode node) { int col, row; int baseCol, baseRow; nodeList.Remove(node); for (int direction = 0; direction < 4; ++direction) { if (node.LinkList[direction] != null) { continue; } if (RandomUtils.Value() < mazeData.LinkRate) { int oppositeDirection = MazeUtil.GetOppositeDirection(direction); MazeUtil.GetNeighbor(direction, node.Col, node.Row, out col, out row); if (mazeTable.CheckRange(col, row)) { if (RandomUtils.Value() < mazeData.PassageRate) { //Node if (!mazeTable.CheckOccupied(col, row)) { MazeNode newNode = CreateMazeNode(col, row); node.LinkList[direction] = newNode; newNode.LinkList[oppositeDirection] = node; nodeList.Add(newNode); mazeTable.SetNode(newNode, newNode.Col, newNode.Row); mazeTable.AddMazeNode(newNode); } else { node.LinkList[direction] = null; } } else { //Room BlockData roomData = BlockDataManager.Instance.GetRandomRoomData(); MazeUtil.GetRoomBasePos(direction, col, row, roomData.Cols, roomData.Rows, roomData.LeftOffset, out baseCol, out baseRow); if (mazeTable.CheckRoom(baseCol, baseRow, roomData.Cols, roomData.Rows)) { MazeRoom room = CreateMazeRoom(roomData, direction, col, row); for (int i = baseCol; i < baseCol + roomData.Cols; ++i) { for (int j = baseRow; j < baseRow + roomData.Rows; ++j) { mazeTable.SetNode(room, i, j); } } node.LinkList[direction] = room; room.LinkList[oppositeDirection] = node; mazeTable.AddMazeNode(room); } else { node.LinkList[direction] = null; } } } else { node.LinkList[direction] = null; } } } }
public void RemoveBlock(MazeNode node) { if (node is MazeRoom) { MazeRoom room = node as MazeRoom; //Prevent deleting same block twice. if (room.HasCreated) { room.HasCreated = false; } else { return; } } int key = Block.GetBlockKey(node.Col, node.Row); if (!blockDic.ContainsKey(key)) { BaseLogger.LogFormat("Remove none-exist block: {0}", key.ToString()); } Block block = blockDic[key]; blockDic.Remove(key); node.AboveBlock = null; Block.Recycle(block); }
private void UpdateSearchIndexRecurrsively(MazeNode startNode, MazeNode searchNode, int distance) { int scope = GlobalConfig.BlockConfig.RefreshScope; if (Mathf.Abs(searchNode.Col - startNode.Col) > scope || Mathf.Abs(searchNode.Row - startNode.Row) > scope ) { return; } searchNode.SearchIndex = distance++; // Logger.Log(searchNode.Col, searchNode.Row, searchNode.SearchIndex); for (int i = 0; i < searchNode.LinkList.Length; ++i) { MazeNode nextNode = searchNode.LinkList[i]; if (nextNode != null && nextNode.SearchIndex == -1) { UpdateSearchIndexRecurrsively(startNode, nextNode, distance); } } }
private void AddMockNode(MazeNode node) { if (!mockNodeSet.Contains(node)) mockNodeSet.Add(node); }
public void SetNode(MazeNode node, int col, int row) { if (!CheckRange(col, row)) { BaseLogger.LogFormat("Maze node out of range: {0}, {1}", node.Col, node.Row); } allNodes[col, row] = node; }
public void AddMazeNode(MazeNode node) { mazeNodesList.Add(node); }
public static int GetPassageDirection(MazeNode node, PassageType type) { if (type == PassageType.One) { for (int i = 0; i < 4; ++i) { if (node.LinkList[i] != null) { return GetOppositeDirection(i); } } } else if (type == PassageType.TwoLine) { for (int i = 0; i < 4; ++i) { if (node.LinkList[i] != null) { return i; } } } else if (type == PassageType.TwoTurn) { for (int i = 0; i < 4; ++i) { if (node.LinkList[i] == null && node.LinkList[GetNextDirection(i)] != null) { return i; } } } else if (type == PassageType.Three) { for (int i = 0; i < 4; ++i) { if (node.LinkList[i] == null) { return i; } } } return 0; }
public static PassageType GetPassageType(MazeNode node) { int linkCount = 0; for (int i = 0; i < 4; ++i) { linkCount += node.LinkList[i] != null ? 1 : 0; } if (linkCount == 1) { return PassageType.One; } else if (linkCount == 2) { if (node.LinkList[0] == null && node.LinkList[2] == null || node.LinkList[1] == null && node.LinkList[3] == null) { return PassageType.TwoLine; } else { return PassageType.TwoTurn; } } else if (linkCount == 3) { return PassageType.Three; } else { return PassageType.Four; } }
public static int GetNodeLinkCount(MazeNode node) { int sum = 0; for (int i = 0; i < 4; ++i) { sum += node.LinkList[i] != null ? 1 : 0; } return sum; }