示例#1
0
        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);
            }
        }
示例#2
0
        // Update is called once per frame
        void Update()
        {
            int horizontal = 0;
            int vertical   = 0;

            // if moving horizontally, do not move vertically
            if (Time.frameCount % 20 == 0)
            {
                canMove = true;
            }

            if (Mathf.Abs(CnInputManager.GetAxis("Horizontal")) > 0 || Mathf.Abs(CnInputManager.GetAxis("Vertical")) > 0)
            {
                count += 1;
                if (count > 60)
                {
                    count = 1;
                }
            }
            else
            {
                count = 0;

                if (dirX == -1)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.WEST)];
                }
                else if (dirX == 1)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.EAST)];
                }
                else if (dirY == -1)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.NORTH)];
                }
                else if (dirY == 1)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.SOUTH)];
                }
            }

            if (canMove)
            {
                canMove = false;
                var movementVector = new Vector3(CnInputManager.GetAxis("Horizontal"), CnInputManager.GetAxis("Vertical"), 0f);
                if (movementVector.sqrMagnitude >= 0.00001f)
                {
                    movementVector.Normalize();
                    double rad    = Mathf.Atan2(movementVector.y, movementVector.x);
                    double degree = rad * Mathf.Rad2Deg;
                    if (degree >= 45 && degree <= 135)
                    {
                        vertical = 1;
                    }
                    else if (degree > 135 || degree < -135)
                    {
                        horizontal = -1;
                    }
                    else if (degree <= -45 && degree >= -135)
                    {
                        vertical = -1;
                    }
                    else if (degree < 45 || degree < -45)
                    {
                        horizontal = 1;
                    }
                }
            }

            RaycastHit2D hit;

            if (TryToMove(horizontal, vertical, out hit))
            {
                transform.Translate(horizontal, vertical, 0f);
                m_posX += horizontal;
                m_posY += vertical;
                if (horizontal != 0 || vertical != 0)
                {
                    if (m_posX == 0)
                    {
                        boardManager.gotoNextStage(Direction.WEST);
                    }
                    else if (m_posX == boardManager.columns - 1)
                    {
                        boardManager.gotoNextStage(Direction.EAST);
                    }
                    else if (m_posY == boardManager.rows - 1)
                    {
                        boardManager.gotoNextStage(Direction.NORTH);
                    }
                    else if (m_posY == 0)
                    {
                        boardManager.gotoNextStage(Direction.SOUTH);
                    }

                    boardManager.sendMove(m_posX, m_posY);
                }
            }
            else
            {
                GameObject hitObject = hit.collider.gameObject;
                if (hitObject.tag == "Potion")
                {
                    // if the hit object was a potion, update this player's HP and position
                    Potion potion = hitObject.GetComponent <Potion>();
                    m_HP = System.Math.Min(m_HP + potion.healAmount, MAX_HP);
                    transform.Translate(horizontal, vertical, 0f);
                    m_posX += horizontal;
                    m_posY += vertical;

                    // send the hitted information to the server
                    Hashtable data = new Hashtable();
                    data.Add("username", SystemInfo.deviceUniqueIdentifier);
                    data.Add("hash", boardManager.currentStageHash);
                    data.Add("action", "consume");
                    data.Add("consume", potion.m_potionID.ToString());

                    client.sendJSONObject(data);

                    // remove the potion
                    Destroy(hitObject);
                    hitObject = null;

                    Debug.Log("HP = " + m_HP + ", AP = " + m_AP);
                }
                else if (hitObject.tag == "Weapon")
                {
                    // if the hit object was a weapon, update this player's information
                    Weapon weapon = hitObject.GetComponent <Weapon>();
                    m_AP = weapon.value;
                    transform.Translate(horizontal, vertical, 0f);
                    m_posX += horizontal;
                    m_posY += vertical;

                    // send the hitted information to the server (TODO)
                    Hashtable data = new Hashtable();
                    data.Add("username", SystemInfo.deviceUniqueIdentifier);
                    data.Add("hash", boardManager.currentStageHash);
                    data.Add("action", "consume");
                    data.Add("consume", weapon.m_itemID.ToString());

                    client.sendJSONObject(data);

                    // remove the potion
                    Destroy(hitObject);
                    hitObject = null;

                    Debug.Log("HP = " + m_HP + ", AP = " + m_AP);
                }
            }

            // count 2++ => run

            if (horizontal > 0)
            {
                if (count < 2)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.EAST)];
                }
                else
                {
                    anim.Play(Animator.StringToHash("Base Layer.LinkMoveR"));
                }
                dirX = 1; dirY = 0;
            }
            else if (horizontal < 0)
            {
                if (count < 2)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.WEST)];
                }
                else
                {
                    anim.Play(Animator.StringToHash("Base Layer.LinkMoveL"));
                }
                dirX = -1; dirY = 0;
            }
            else if (vertical > 0)
            {
                if (count < 2)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.NORTH)];
                }
                else
                {
                    anim.Play(Animator.StringToHash("Base Layer.LinkMoveU"));
                }
                dirX = 0; dirY = -1;
            }
            else if (vertical < 0)
            {
                if (count < 2)
                {
                    GetComponent <SpriteRenderer>().sprite = moveSprite[(int)(Direction.SOUTH)];
                }
                else
                {
                    anim.Play(Animator.StringToHash("Base Layer.LinkMoveD"));
                }
                dirX = 0; dirY = 1;
            }
        }