Example #1
0
    //pop room list
    public void OnRecvRoom_Server(NetworkMessage netMsg)
    {
        PopulateRoomListMessage msg = netMsg.ReadMessage <PopulateRoomListMessage>();

        //if(Global.Instance.player.GetComponent<Player>().isServer)
        Debug.Log("Host/ServerRecv_Room_");
        for (int i = 0; i < Global.Instance.roomGen.roomDataList.Count; ++i)
        {
            SendRoom_S2C(Global.Instance.roomGen.roomDataList[i], msg.connectionId);
        }
    }
Example #2
0
    public void OnRecvRoom_Client(NetworkMessage netMsg)
    {
        if (Global.Instance.player.GetComponent <NetworkIdentity>().isServer)
        {
            return;
        }

        PopulateRoomListMessage msg = netMsg.ReadMessage <PopulateRoomListMessage>();

        //make the damn room here
        //roomgen func gen da room,
        GameObject roomType;

        if (msg.roomType == -1)
        {
            roomType = Global.Instance.roomGen.GetDefaultRoom();
        }
        else
        {
            roomType = Global.Instance.roomGen.GetRandomRoomList()[msg.roomType];
        }


        GameObject room = Instantiate(roomType, msg.roomPos, Quaternion.identity);

        room.transform.localScale = msg.roomScale;
        room.GetComponent <RoomScript>().Set(msg.roomID, msg.gridX, msg.gridY,
                                             !msg.isLeft, !msg.isRight, !msg.isUp, !msg.isDown);
        //determine if room is already visited b4 hand
        room.SetActive(msg.isActive);
        if (msg.isActive)
        {
            room.GetComponent <RoomScript>().SetIsComplete(msg.isCompleted);
        }
        Global.Instance.roomGen.StoreRoom(msg.roomID + 1, msg.gridX, msg.gridY, room);
        //if (msg.roomID != 0)
        Global.Instance.roomGen.roomDataList.Add(msg);

        Debug.Log("ClientRecv_Room");
        //Debug.Log("==============================");
        //Debug.Log("ClientRecv_Room_" + msg.connectionId);
        //Debug.Log("ClientRecv_Room_RoomId" + msg.roomID);
        //Debug.Log("ClientRecv_Room_gridX" + msg.gridX);
        //Debug.Log("ClientRecv_Room_gridY" + msg.gridY);
        //Debug.Log("ClientRecv_Room_Pos" + msg.roomPos);
        //Debug.Log("ClientRecv_Room_roomType" + msg.roomType);
        //Debug.Log("ClientRecv_Room_LEFT" + msg.isLeft);
        //Debug.Log("ClientRecv_Room_RIGHT" + msg.isRight);
        //Debug.Log("ClientRecv_Room_UP" + msg.isUp);
        //Debug.Log("ClientRecv_Room_DOWN" + msg.isDown);
        //Debug.Log("==============================");
    }
Example #3
0
    public void SendRoom_S2C(PopulateRoomListMessage _container, int _connectionId)
    {
        PopulateRoomListMessage msg = _container;

        msg.connectionId = _connectionId;

        Debug.Log("SendRoom_S2C");

        if (NetworkServer.active)
        {
            NetworkServer.SendToClient(msg.connectionId, MyMsgType.roomListMsgType_server, msg);
        }
        //NetworkServer.SendToAll(MyMsgType.spawnRoomMsgType_server, msg);
    }
Example #4
0
    public void SendRoom_C2S()
    {
        if (Global.Instance.player.GetComponent <NetworkIdentity>().isServer)
        {
            return;
        }

        Debug.Log("SendRoom_C2S_" + index);
        PopulateRoomListMessage msg = new PopulateRoomListMessage();

        msg.connectionId = index;

        //send to server
        myClient.Send(MyMsgType.roomListMsgType_client, msg);
    }
Example #5
0
    public void GenerateRoom(int currRoomID, DIRECTION side)
    {
        if (DEBUG_ROOMGEN)
        {
            Debug.Log("Generating Room for " + side + " at " + currRoomID + "  roomListSize: " + roomList.Count);
        }
        GameObject currentRoom    = roomList[currRoomID];
        Vector3    currPos        = currentRoom.transform.position;
        RoomScript currRoomScript = currentRoom.GetComponent <RoomScript>();

        DIRECTION forceTrueDir = this.GetOppositeDir(side);
        int       newGridX     = currRoomScript.GetGridX() + (side == DIRECTION.LEFT ? -1 : (side == DIRECTION.RIGHT ? 1 : 0));
        int       newGridY     = currRoomScript.GetGridY() + (side == DIRECTION.DOWN ? -1 : (side == DIRECTION.UP ? 1 : 0));

        if (roomMap.ContainsKey(newGridY))
        {
            if (roomMap[newGridY].ContainsKey(newGridX))
            {
                return;
            }
        }

        Dictionary <DIRECTION, RANDACTION> boolArray = new Dictionary <DIRECTION, RANDACTION>();//= GetDoorOpenBooleans(forceTrueDir, true);
        int numOfPotentialOpen = 0;

        boolArray[DIRECTION.NONE]  = 0;
        boolArray[DIRECTION.LEFT]  = IsNeighbourHaveUnlockedDoor(newGridX, newGridY, DIRECTION.LEFT);
        boolArray[DIRECTION.RIGHT] = IsNeighbourHaveUnlockedDoor(newGridX, newGridY, DIRECTION.RIGHT);
        boolArray[DIRECTION.UP]    = IsNeighbourHaveUnlockedDoor(newGridX, newGridY, DIRECTION.UP);
        boolArray[DIRECTION.DOWN]  = IsNeighbourHaveUnlockedDoor(newGridX, newGridY, DIRECTION.DOWN);

        List <DIRECTION> dirToOffTrigger = new List <DIRECTION>();
        List <DIRECTION> openDoors       = new List <DIRECTION>();

        //pre calculate the chances to spawn doors
        foreach (KeyValuePair <DIRECTION, RANDACTION> pair in boolArray)
        {
            if (pair.Key == DIRECTION.NONE)
            {
                continue;
            }
            if (pair.Value == RANDACTION.MUSTOPEN)
            {
                //This side of the new room MUST open
                --numOfOpenedDoors;
                //connect the doors
                dirToOffTrigger.Add(pair.Key);
                openDoors.Add(pair.Key);
            }
            else if (pair.Value == RANDACTION.CANCHOOSE)
            {
                //THIS Side of the new room will undergo calculation
                ++numOfPotentialOpen;
            }
            else
            {
                if (DEBUG_ROOMGEN)
                {
                    //lock this door
                    Debug.Log("MustLock activated");
                }
            }
        }

        //calculate the opened door chances based on current situation
        foreach (KeyValuePair <DIRECTION, RANDACTION> pair in boolArray)
        {
            if (pair.Value == RANDACTION.CANCHOOSE)
            {
                bool openDaDoor = UnityEngine.Random.value < (0.5f * Mathf.Log(1 + estTotalRooms - currID, estTotalRooms)
                                                              + (1.0f / Mathf.Max(10.0f * (currID / (float)estTotalRooms), numOfOpenedDoors + numOfPotentialOpen)));
                if (openDaDoor)
                {
                    openDoors.Add(pair.Key);
                    ++numOfOpenedDoors;
                }
            }
        }

        GameObject room;
        float      offsetX = (side == DIRECTION.LEFT ? -scaleX : (side == DIRECTION.RIGHT ? scaleX : 0));
        float      offsetY = (side == DIRECTION.DOWN ? -scaleY : (side == DIRECTION.UP ? scaleY : 0));
        Vector3    tempRoomPos;
        int        randIndex = -1;

        if (!generatedBossRoom)
        {
            //attempt to generate bossroom
            if (numOfOpenedDoors == 0 || 0.25f * ((currID + 1) / estTotalRooms) > UnityEngine.Random.value)
            {
                tempRoomPos       = new Vector3(currPos.x + offsetX, currPos.y + offsetY, zOffset);
                room              = Instantiate(bossRoom, tempRoomPos, Quaternion.identity);
                generatedBossRoom = true;

                //NetworkServer.Spawn(room);
            }
            else
            {
                randIndex   = UnityEngine.Random.Range(0, randomRooms.Count - 1);
                tempRoomPos = new Vector3(currPos.x + offsetX, currPos.y + offsetY, zOffset);
                room        = Instantiate(randomRooms[randIndex], tempRoomPos, Quaternion.identity);

                //here is network spawn the room for sync
                //NetworkServer.Spawn(room);
            }
        }
        else
        {
            tempRoomPos = new Vector3(currPos.x + offsetX, currPos.y + offsetY, zOffset);
            room        = Instantiate(defaultRoom, tempRoomPos, Quaternion.identity);
            //NetworkServer.Spawn(room);
        }
        room.transform.localScale.Set(scaleX, scaleY, 1);
        RoomScript roomScript = room.GetComponent <RoomScript>();

        roomScript.Set(currID, newGridX, newGridY,
                       openDoors.Contains(DIRECTION.LEFT), openDoors.Contains(DIRECTION.RIGHT),
                       openDoors.Contains(DIRECTION.UP), openDoors.Contains(DIRECTION.DOWN));


        //here is network spawn the room for sync

        //foreach (DIRECTION dir in dirToOffTrigger)
        //{
        //    roomScript.OffTriggerBox(dir);
        //    RoomScript neighbourRS = GetNeighbourRoomScript(newGridX, newGridY, dir);
        //    DIRECTION oppoSide = GetOppositeDir(dir);
        //    neighbourRS.OffTriggerBox(oppoSide);
        //}
        //StoreRoom(currID, newGridX, newGridY, room);


        //foreach (DIRECTION dir in dirToOffTrigger)
        //{
        //    roomScript.OffTriggerBox(dir);
        //    RoomScript neighbourRS = GetNeighbourRoomScript(newGridX, newGridY, dir);
        //    DIRECTION oppoSide = GetOppositeDir(dir);
        //    neighbourRS.OffTriggerBox(oppoSide);
        //}

        StoreRoom(currID, newGridX, newGridY, room);
        room.SetActive(false);
        //RoomStruct temp = new RoomStruct();
        //temp.room = room;
        //syncListRooomStruct.Add(temp);
        //NETWORK
        //NetworkServer.Spawn(room);



        PopulateRoomListMessage popRoomMsg = new PopulateRoomListMessage();

        //Debug.Log(currID);
        popRoomMsg.roomID      = room.GetComponent <RoomScript>().GetRoomID();
        popRoomMsg.gridX       = newGridX;
        popRoomMsg.gridY       = newGridY;
        popRoomMsg.roomPos     = tempRoomPos;
        popRoomMsg.roomScale   = room.transform.localScale;
        popRoomMsg.roomType    = randIndex;
        popRoomMsg.isLeft      = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.LEFT);
        popRoomMsg.isRight     = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.RIGHT);
        popRoomMsg.isUp        = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.UP);
        popRoomMsg.isDown      = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.DOWN);
        popRoomMsg.isActive    = false;
        popRoomMsg.isCompleted = false;
        roomDataList.Add(popRoomMsg);
    }
Example #6
0
    //init the map
    public void Init()
    {
        biggestX                   = biggestY = smallestY = smallestX = 0;
        numOfOpenedDoors           = 0;
        generatedBossRoom          = false;
        roomList                   = new List <GameObject>();
        roomMap                    = new Dictionary <int, Dictionary <int, GameObject> >();
        currID                     = 0;
        Global.Instance.bossIsDead = false;

        //if (!Global.Instance.player.GetComponent<NetworkIdentity>().isServer)
        //{
        //    Debug.Log("u not the host");
        //    return;
        //}

        if (!GenerateOnStart)
        {
            return;
        }
        //Debug.Log("RoomGenerator Start()");
        //spawn at 0,0, so generate one at 0,0
        GameObject room =
            Instantiate(defaultRoom, new Vector3(0, 0, zOffset), Quaternion.identity);

        room.transform.localScale.Set(scaleX, scaleY, 1);

        RoomScript roomScript = room.GetComponent <RoomScript>();
        Dictionary <DIRECTION, bool> boolArray = new Dictionary <DIRECTION, bool>();
        float incChance = 0.0f;

        for (DIRECTION i = DIRECTION.LEFT; i < DIRECTION.LEFT + 4; ++i)
        {
            boolArray[i] = false;
            if (!boolArray[i])
            {
                boolArray[i] = UnityEngine.Random.value < (0.5f + incChance);
                if (boolArray[i])
                {
                    ++numOfOpenedDoors;
                    incChance -= 0.2f;
                }
                else
                {
                    incChance += 0.25f;
                }
            }
        }

        //Debug.Log("B4_CurrID: " + currID);
        roomScript.Set(currID, 0, 0, boolArray[DIRECTION.LEFT], boolArray[DIRECTION.RIGHT], boolArray[DIRECTION.UP], boolArray[DIRECTION.DOWN]);
        StoreRoom(currID, 0, 0, room);
        //Debug.Log("After_CurrID: " + currID);
        //Debug.Log("roomID" + room.GetComponent<RoomScript>().GetRoomID());

        //NETWORK asdasd
        //RoomStruct temp = new RoomStruct();
        //temp.room = room;
        //syncListRooomStruct.Add(temp);
        //NetworkServer.Spawn(room);

        PopulateRoomListMessage popRoomMsg = new PopulateRoomListMessage();

        popRoomMsg.roomID      = room.GetComponent <RoomScript>().GetRoomID();
        popRoomMsg.gridX       = 0;
        popRoomMsg.gridY       = 0;
        popRoomMsg.roomPos     = new Vector3(0, 0, zOffset);
        popRoomMsg.roomScale   = room.transform.localScale;
        popRoomMsg.roomType    = -1;
        popRoomMsg.isLeft      = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.LEFT);
        popRoomMsg.isRight     = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.RIGHT);
        popRoomMsg.isUp        = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.UP);
        popRoomMsg.isDown      = room.GetComponent <RoomScript>().GetIsLocked(DIRECTION.DOWN);
        popRoomMsg.isActive    = true;
        popRoomMsg.isCompleted = false;
        roomDataList.Add(popRoomMsg);


        while (numOfOpenedDoors > 0)
        {
            DIRECTION[] dirList = new DIRECTION[4];
            //check which door open
            for (int i = 0; i < roomList.Count; ++i)
            {
                GameObject daroom       = roomList[i];
                RoomScript daroomscript = daroom.GetComponent <RoomScript>();

                for (DIRECTION j = DIRECTION.LEFT; j < DIRECTION.LEFT + 4; ++j)
                {
                    if (daroomscript.GetHasTriggerBox(j) && !daroomscript.GetIsLocked(j))
                    {
                        //open tat door
                        GenerateRoom(i, j);
                    }
                }
            }
        }

        Debug.Log("number of rooms: " + roomList.Count);
        //Increase the number of times entered game
        PlayerPrefs.SetInt(PREFTYPE.NUM_OF_ENTERGAME.ToString(), PlayerPrefs.GetInt(PREFTYPE.NUM_OF_ENTERGAME.ToString(), 0) + 1);

        //Debug.Log("LEFT: " + room.GetComponent<RoomScript>().GetIsLocked(DIRECTION.LEFT));
        //Debug.Log("UP: " + room.GetComponent<RoomScript>().GetIsLocked(DIRECTION.UP));
        //Debug.Log("RIGHT: " + room.GetComponent<RoomScript>().GetIsLocked(DIRECTION.RIGHT));
        //Debug.Log("DOWN: " + room.GetComponent<RoomScript>().GetIsLocked(DIRECTION.DOWN));
    }