Search Unity

Several Rooms\locations in one scene Technic

Discussion in '2D' started by ATH1, Oct 10, 2019.

  1. ATH1

    ATH1

    Joined:
    Jul 2, 2019
    Posts:
    4
    I want to implement the technic with several rooms\locations in one scene (where camera is not following you but switching of locations leads to disappearence of first and appearance of a new location. Example here:


    I have an object for each room that contains corresponding items of the room.
    The question is - what is the best way to switch rooms:

    My current variant is to deactivate (roomObject.SetActive(false);) current room and activate next one. It will help save all objects left in the location saved.
    The minus is I dont know how to hide each room except the first one on the start

    Base room class:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public abstract class Room : Element
    4. {
    5.     public  GameObject roomObject;
    6.     protected  Room NextRoom;
    7.     protected Room PreviousRoom;
    8.     protected GameObject rightDoor;
    9.     protected GameObject leftDoor;
    10.  
    11.     public virtual Room MoveRight()
    12.     {
    13.         this.Deactivate();
    14.         return NextRoom;
    15.     }
    16.  
    17.     public virtual Room MoveLeft()
    18.     {
    19.         this.Deactivate();
    20.         return PreviousRoom;
    21.     }
    22.  
    23.     public virtual void Deactivate()
    24.     {
    25.         roomObject.SetActive(false);
    26.     }
    27.  
    28.     public virtual void Activate()
    29.     {
    30.         roomObject.SetActive(true);
    31.     }
    32. }
    Example of room code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Room2 : Room
    4. {
    5.     void Start()
    6.     {
    7.         this.roomObject = GameObject.Find("Room2");
    8.         this.NextRoom = GameObject.FindObjectOfType<Room3>();
    9.         this.PreviousRoom = GameObject.FindObjectOfType<Room1>();
    10.     }
    11. }
    Controller that is helping to change a room
    Code (CSharp):
    1. public class RoomController : Element
    2. {
    3.     private Room currentRoom
    4.     {
    5.         get { return app.model.room.currentRoom; }
    6.         set { app.model.room.currentRoom = value; }
    7.     }
    8.  
    9.     void Awake()
    10.     {
    11.         Messenger<bool>.AddListener(GameEvent.DOOR_ENTERED, ChangeRoom);
    12.     }
    13.     void OnDestroy()
    14.     {
    15.         Messenger<bool>.RemoveListener(GameEvent.DOOR_ENTERED, ChangeRoom);
    16.     }
    17.  
    18.     private void ChangeRoom(bool isRightDoor)
    19.     {
    20.         this.currentRoom = isRightDoor ? currentRoom.MoveRight() : currentRoom.MoveLeft();
    21.         this.currentRoom.Activate();
    22.         app.controller.character.ChangePlayerPosition(isRightDoor);
    23.     }
    24. }