public GenBridge genBridge(int x, int y, int direction) { GenBridge gb = new GenBridge(); gb.bridgeCenterLength = Random.Range(1, 4); Pos p = new Pos(0, 0); switch (direction) { case 0: p.y = -1; break; case 1: p.x = 1; break; case 2: p.y = 1; break; case 3: p.x = -1; break; } // each bridge component equal to 2 square, so bridge head + bridge middle * length + bridge end = .... gb.x = x + (1 + gb.bridgeCenterLength + 1) * 2 * p.x; gb.y = y + (1 + gb.bridgeCenterLength + 1) * 2 * p.y; return(gb); }
public GenRoom genNextRoom(int x, int y, bool isMainline, bool isLastLevel = false, int length = 0, int incomeDirection = -1) { GenRoom newRoom = new GenRoom(); newRoom.roomObjId = randomGen(roomIds); Room roomData = new Room(newRoom.roomObjId); newRoom.roomId = roomIdCounter++; int monsterNum = 0; // first calibrate the position if (incomeDirection != -1) { x -= roomData.gates [incomeDirection].pos.x; y -= roomData.gates [incomeDirection].pos.y; } else { x -= 8; y -= 8; } // check collision Rect newArea = new Rect(x, y, 16, 16); if (collisionCheck(newArea)) { return(null); } occupiedArea.Add(newArea); // add monster to room // check if needed (not starting room) if (length == mainlineLength) { // starting room need character starting position //bool validPos = false; newRoom.charPosition = new Pos(5, 5); // while(!validPos) { // newRoom.charPosition.x = Random.Range (0, 13); // newRoom.charPosition.y = Random.Range (0, 13); // if(roomData.landscape[newRoom.charPosition.y][newRoom.charPosition.x] == 1) { // validPos = true; // } // } } else if (length == 1) { // last room, let this be the teleport gate room newRoom.teleportGate = new GenTGate(); newRoom.teleportGate.teleportGateObjId = 1; bool validPos = false; newRoom.teleportGate.position = new Pos(0, 0); while (!validPos) { newRoom.teleportGate.position.x = Random.Range(0, 13); newRoom.teleportGate.position.y = Random.Range(0, 13); if (roomData.landscape[newRoom.teleportGate.position.y][newRoom.teleportGate.position.x] == 1) { validPos = true; } } if (isLastLevel) { newRoom.teleportGate.target = "boss"; } else { newRoom.teleportGate.target = "level"; } } // else { // random number of monsters monsterNum = maxMonsterNum;// randomGen(monsterNums); newRoom.monsters = new GenMonster[monsterNum]; for (int i = 0; i < monsterNum; i++) { // get a position // TODO: stop using silly method bool validPos = false; Pos position = new Pos(0, 0); while (!validPos) { position.x = Random.Range(0, 13); position.y = Random.Range(0, 13); if (roomData.landscape[position.y][position.x] == 1) { // mark area around nolonger avaliable roomData.landscape[position.y - 1][position.x] = 0; roomData.landscape[position.y + 1][position.x] = 0; roomData.landscape[position.y][position.x - 1] = 0; roomData.landscape[position.y][position.x + 1] = 0; roomData.landscape[position.y][position.x] = 0; validPos = true; } } newRoom.monsters[i] = genMonster(i, position); } //} // mark all gate position for (int i = 0; i < 4; i++) { newRoom.gates[i].x = x + roomData.gates[i].pos.x; newRoom.gates[i].y = y + roomData.gates[i].pos.y; } if (isMainline) { // get the next room (recursion), attach to the bridge if (length > 1) { // random a gate int direction; GenBridge newBridge = null; GenRoom nextRoom = null; int[] directions = new int[] { 0, 1, 2, 3 }; List <int> directionList = new List <int>(directions); List <float> ratioList = new List <float>(directionRatio); if (directionList.Remove(incomeDirection) == true) { ratioList.RemoveAt(incomeDirection); } do { if (directionList.Count == 0) { return(null); } direction = randomGen(directionList.ToArray(), ratioList.ToArray()); int directionIndex = directionList.IndexOf(direction); directionList.RemoveAt(directionIndex); ratioList.RemoveAt(directionIndex); newBridge = genBridge(newRoom.gates[direction].x, newRoom.gates[direction].y, direction); nextRoom = genNextRoom(newBridge.x, newBridge.y, isMainline, isLastLevel, length - 1, (direction < 2?direction + 2:direction - 2)); } while (nextRoom == null); // open the gate newRoom.gates[direction].isGateOpen = true; int gateOpenMethod = genGateEvent(newRoom, direction); // TODO: Old open gate method, get ride of this when have time to implement event system on client side newRoom.gates[direction].gateOpenCondId = gateOpenMethod; if (newRoom.monsters == null) { newRoom.gates[direction].gateOpenCondId = 0; } else if (newRoom.gates[direction].gateOpenCondId == 2) { // need to kill specific monster, mark which one // temp all monsterObj = 1 List <int> targetMonsters = new List <int>(); for (int i = 0; i < monsterNum; i++) { if (newRoom.monsters[i].monsterObjId == 1) { targetMonsters.Add(i); } } newRoom.gates[direction].gateOpenCondParam = targetMonsters.ToArray(); } // get a bridge and connect to the gate newRoom.gates[direction].bridge = newBridge; newRoom.gates[direction].bridge.targetRoom = nextRoom; // gather list of unused gate for branching if (length != mainlineLength) { for (int i = 0; i < 4; i++) { if (i != direction && i != incomeDirection) { branchableGates.Add(newRoom.gates[i]); } } } } } else { } return(newRoom); }