// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } if (actionQueue.Count > 0) { Hashtable data = actionQueue[0]; switch (data["action"].ToString()) { case "myinfo": generateEnemy( System.Int32.Parse(data["xpos"].ToString()), System.Int32.Parse(data["ypos"].ToString()), data["username"].ToString() ); GameObject peer1 = new GameObject("peer UDP client"); UDPClient peerClient = peer1.AddComponent <UDPClient>(); peerClient.InitiateSocket(data["ip"].ToString(), 12346); peerUDPClients.Add(peer1); Hashtable data2 = new Hashtable(); data2.Add("action", "myinfo2"); data2.Add("hash", currentStageHash); data2.Add("username", SystemInfo.deviceUniqueIdentifier); data2.Add("ip", getMyIP()); data2.Add("xpos", player.m_posX); data2.Add("ypos", player.m_posY); peerClient.sendJSONObject(data2); break; case "myinfo2": generateEnemy( System.Int32.Parse(data["xpos"].ToString()), System.Int32.Parse(data["ypos"].ToString()), data["username"].ToString() ); break; case "move": foreach (PeerPlayer peer in peerPlayers) { Debug.Log("peer username = "******"username"].ToString()) { peer.Move( System.Int32.Parse(data["xpos"].ToString()), System.Int32.Parse(data["ypos"].ToString()) ); } } break; } actionQueue.Remove(data); } }
void generateMapAndPlayer(Hashtable mapInfo, PlayerSpawnDir spawnDir) { // get map information from the hash table string mapString = (string)mapInfo["map"]; rows = (int)mapInfo["row"]; columns = (int)mapInfo["column"]; currentStageHash = (string)mapInfo["hash"]; int HPStatus, APStatus; if (player != null) { HPStatus = player.m_HP; APStatus = player.m_AP; } else { HPStatus = (int)mapInfo["hp"]; APStatus = (int)mapInfo["ap"]; } string[] arrayMap = mapString.Split(','); // get item information from the hash table int[] itemCounts = new int[(int)(itemID.END + 1)]; int[] itemValues = new int[(int)(itemID.END + 1)]; for (int i = 0; i <= (int)(itemID.END); i++) { if (mapInfo.ContainsKey(i.ToString())) { Hashtable itemTable = (Hashtable)mapInfo[i.ToString()]; itemCounts[i] = (int)itemTable["items"]; itemValues[i] = (int)itemTable["value"]; } else { itemCounts[i] = 0; } } // find the gates int northGate = -111, southGate = -111, eastGate = -111, westGate = -111; for (int i = 0; i < columns; i++) { if (arrayMap[i] == "N") { northGate = i; break; } } for (int i = 0; i < columns; i++) { if (arrayMap[columns * (rows - 1) + i] == "S") { southGate = i; break; } } for (int i = 0; i < rows; i++) { if (arrayMap[columns - 1 + columns * (rows - 1 - i)] == "E") { eastGate = i; break; } } for (int i = 0; i < rows; i++) { if (arrayMap[columns * (rows - 1 - i)] == "W") { westGate = i; break; } } // floor generation for (int x = -2; x < columns + 2; x++) { for (int y = -3; y < rows + 3; y++) { int tileIndex = 0; if (x <= 0 || y <= 0 || x >= columns - 1 || y >= rows - 1) { tileIndex = 9; } else if (x == 1) { if (y == 1) { tileIndex = 6; } else if (y == rows - 2) { tileIndex = 0; } else { tileIndex = 3; } } else if (x == columns - 2) { if (y == 1) { tileIndex = 8; } else if (y == rows - 2) { tileIndex = 2; } else { tileIndex = 5; } } else { if (y == 1) { tileIndex = 7; } else if (y == rows - 2) { tileIndex = 1; } else { tileIndex = 4; } } instantiateAndAdd(floorTiles[tileIndex], x, y, holders[(int)(holderID.FLOOR)]); } } // cliff generation List <Vector3> rockTilePositions = new List <Vector3>(); List <Vector3> rockPositions = new List <Vector3>(); // cliff, SW side of the map instantiateAndAdd(cliffTiles[3], -1, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) { for (int x = -2; x <= 0; x++) { rockTilePositions.Add(new Vector3(x, y, 0)); } } for (int y = -1; y <= 0; y++) { rockTilePositions.Add(new Vector3(-2, y, 0)); } // cliff, SE side of the map instantiateAndAdd(cliffTiles[15], columns, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) { for (int x = columns - 1; x <= columns + 1; x++) { rockTilePositions.Add(new Vector3(x, y, 0)); } } for (int y = -1; y <= 0; y++) { rockTilePositions.Add(new Vector3((columns + 1), y, 0)); } // cliff, NW side of the map instantiateAndAdd(cliffTiles[7], -1, rows + 1, holders[(int)(holderID.CLIFF)]); for (int x = -2; x <= 0; x++) { rockTilePositions.Add(new Vector3(x, rows + 2, 0)); } for (int y = rows - 1; y <= rows + 1; y++) { rockTilePositions.Add(new Vector3(-2, y, 0)); } // cliff, NE side of the map instantiateAndAdd(cliffTiles[11], columns, rows + 1, holders[(int)(holderID.CLIFF)]); for (int x = columns - 1; x <= columns + 1; x++) { rockTilePositions.Add(new Vector3(x, rows + 2, 0)); } for (int y = rows - 1; y <= rows + 1; y++) { rockTilePositions.Add(new Vector3((columns + 1), y, 0)); } // cliff, south side for (int x = 1; x < columns - 1; x++) { if (x == southGate - 2) { instantiateAndAdd(cliffTiles[2], x, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) { instantiateAndAdd(cliffTiles[4 + Random.Range(0, 2)], x, y, holders[(int)(holderID.CLIFF)]); } } else if (x == southGate + 2) { instantiateAndAdd(cliffTiles[14], x, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) { instantiateAndAdd(cliffTiles[12 + Random.Range(0, 2)], x, y, holders[(int)(holderID.CLIFF)]); } } else if (Mathf.Abs(x - southGate) >= 3) { instantiateAndAdd(cliffTiles[Random.Range(0, 2)], x, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) { rockTilePositions.Add(new Vector3(x, y, 0)); } } } // cliff, north side for (int x = 1; x < columns - 1; x++) { if (x == northGate - 2) { instantiateAndAdd(cliffTiles[6], x, rows + 1, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[4 + Random.Range(0, 2)], x, rows + 2, holders[(int)(holderID.CLIFF)]); } else if (x == northGate + 2) { instantiateAndAdd(cliffTiles[10], x, rows + 1, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[12 + Random.Range(0, 2)], x, rows + 2, holders[(int)(holderID.CLIFF)]); } else if (Mathf.Abs(x - northGate) >= 3) { instantiateAndAdd(cliffTiles[8 + Random.Range(0, 2)], x, rows + 1, holders[(int)(holderID.CLIFF)]); rockTilePositions.Add(new Vector3(x, rows + 2, 0)); } } // cliff, west side for (int y = 1; y <= rows - 1; y++) { if (y == westGate + 3) { instantiateAndAdd(cliffTiles[6], -1, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[8 + Random.Range(0, 2)], -2, y, holders[(int)(holderID.CLIFF)]); } else if (y == westGate - 1) { instantiateAndAdd(cliffTiles[2], -1, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[Random.Range(0, 2)], -2, y, holders[(int)(holderID.CLIFF)]); } else if (Mathf.Abs(y - (westGate + 1)) >= 3) { instantiateAndAdd(cliffTiles[4 + Random.Range(0, 2)], -1, y, holders[(int)(holderID.CLIFF)]); rockTilePositions.Add(new Vector3(-2, y, 0)); } } // cliff, east side for (int y = 1; y <= rows - 1; y++) { if (y == eastGate + 3) { instantiateAndAdd(cliffTiles[10], columns, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[8 + Random.Range(0, 2)], columns + 1, y, holders[(int)(holderID.CLIFF)]); } else if (y == eastGate - 1) { instantiateAndAdd(cliffTiles[14], columns, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[Random.Range(0, 2)], columns + 1, y, holders[(int)(holderID.CLIFF)]); } else if (Mathf.Abs(y - (eastGate + 1)) >= 3) { instantiateAndAdd(cliffTiles[12 + Random.Range(0, 2)], columns, y, holders[(int)(holderID.CLIFF)]); rockTilePositions.Add(new Vector3(columns + 1, y, 0)); } } // random generation of rock tiles foreach (Vector3 pos in rockTilePositions) { instantiateAndAdd( rockFloorTiles[Random.Range(0, rockFloorTiles.Length)], Mathf.FloorToInt(pos.x), Mathf.FloorToInt(pos.y), holders[(int)(holderID.CLIFF)] ); } // box colliders for the borders for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { if (arrayMap[x + (rows - 1 - y) * columns] == "#") { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(x, y, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent <BoxCollider2D>(); } } } if (northGate >= 1 && northGate < columns - 1) { for (int x = northGate - 1; x <= northGate + 1; x += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(x, rows + 1f, 0); border.localScale += new Vector3(0, 2.0f, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent <BoxCollider2D>(); } } if (southGate >= 1 && southGate < columns - 1) { for (int x = southGate - 1; x <= southGate + 1; x += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(x, -2f, 0); border.localScale += new Vector3(0, 2.0f, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent <BoxCollider2D>(); } } if (eastGate >= 1 && eastGate < rows - 1) { for (int y = eastGate - 1; y <= eastGate + 1; y += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(columns + 0.5f, y, 0); border.localScale += new Vector3(1.0f, 0, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent <BoxCollider2D>(); } } if (westGate >= 1 && westGate < rows - 1) { for (int y = westGate - 1; y <= westGate + 1; y += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(-1.5f, y, 0); border.localScale += new Vector3(1.0f, 0, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent <BoxCollider2D>(); } } // block generation for (int y = rows - 1; y >= 0; y--) { for (int x = 0; x < columns; x++) { if (arrayMap[x + (rows - 1 - y) * columns] == "b") { instantiateAndAdd(blockTiles, x, y, 0, holders[(int)(holderID.BLOCK)]); } } } // item generation for (int y = rows - 1; y >= 0; y--) { for (int x = 0; x < columns; x++) { for (int itemIdx = (int)(itemID.START); itemIdx <= (int)(itemID.END); itemIdx++) { if (arrayMap[x + (rows - 1 - y) * columns] == itemIdx.ToString() && itemCounts[itemIdx] > 0) { switch ((itemID)itemIdx) { case itemID.POTION1: Potion potion1 = ObjectFactory.createPotion(x, y, itemValues[itemIdx], (int)(itemID.POTION1)); potion1.transform.SetParent(holders[(int)(holderID.POTION)]); break; case itemID.POTION2: Potion potion2 = ObjectFactory.createPotion(x, y, itemValues[itemIdx], (int)(itemID.POTION2)); potion2.transform.SetParent(holders[(int)(holderID.POTION)]); break; case itemID.WEAPON: Weapon weapon = ObjectFactory.createWeapon(x, y, itemValues[itemIdx], (int)(itemID.WEAPON)); weapon.transform.SetParent(holders[(int)(holderID.WEAPON)]); break; } } } } } // player generation int playerX = 0; int playerY = 0; switch (spawnDir) { case PlayerSpawnDir.SPAWN_EAST: playerX = columns - 2; playerY = eastGate; break; case PlayerSpawnDir.SPAWN_NORTH: playerX = northGate; playerY = rows - 2; break; case PlayerSpawnDir.SPAWN_WEST: playerX = 1; playerY = westGate; break; case PlayerSpawnDir.SPAWN_SOUTH: playerX = southGate; playerY = 1; break; case PlayerSpawnDir.SPAWN_NONE: bool playerPlaced = false; bool hasEmptySpace = false; for (int y = rows - 1; y >= 0; y--) { for (int x = 0; x < columns; x++) { if (arrayMap[x + (rows - 1 - y) * columns] == "u") { if (!playerPlaced) { playerPlaced = true; playerX = x; playerY = y; } } else if (arrayMap[x + (rows - 1 - y) * columns] == "f") { hasEmptySpace = true; } } } while (!playerPlaced && hasEmptySpace) { playerX = Random.Range(1, columns - 1); playerY = Random.Range(1, columns - 1); if (arrayMap[playerX + (rows - 1 - playerY) * columns] == "f") { playerPlaced = true; } } break; } if (player == null) { player = ObjectFactory.createPlayer(playerX, playerY, HPStatus, APStatus); player.transform.SetParent(holders[(int)(holderID.PLAYER)]); } else { player.Initialize(playerX, playerY, HPStatus, APStatus); } // get peer's ip addresses who are in this area! peerIPList.Clear(); ArrayList listIPs = (ArrayList)mapInfo["ips"]; foreach (string ip in listIPs) { if (ip != getMyIP()) { peerIPList.Add(ip); } } // peer player initialization peerUDPClients.Clear(); foreach (string ip in peerIPList) { GameObject peer = new GameObject("peer UDP client"); UDPClient peerClient = peer.AddComponent <UDPClient>(); peerClient.InitiateSocket(ip, 12346); peerUDPClients.Add(peer); Hashtable data = new Hashtable(); data.Add("action", "myinfo"); data.Add("hash", currentStageHash); data.Add("username", SystemInfo.deviceUniqueIdentifier); data.Add("ip", getMyIP()); data.Add("xpos", playerX); data.Add("ypos", playerY); peerClient.sendJSONObject(data); } }