private void OnDestroy()
 {
     if (instance == this)
     {
         instance = null;
     }
 }
示例#2
0
    private void TransitionToMap(int mapID, PlayerDirection.FacingDirections playerDirection)
    {
        EnablePlayerColliderTrigger();

        // TODO: Start fade out
        MapFade.Create(true, false);
        DontDestroyOnLoad(MapFade.Instance);

        SetCameraFollow(false);
        LoadMap(mapID);

        // Fade in
        MapFade.Instance.FadeIn          = false;
        MapFade.Instance.DestroyOnFinish = true;
        MapFade.Instance.ResetFade(true);

        SetCameraFollow(true);

        // Get the new map
        Map  map       = Map.Instance;
        Rect mapBounds = map.Bounds.DivideBy(32);

        // Note: The code below doesn't work but will work when we've stopped using scenes to load map data
        PlayerCharacter player = PlayerCharacter.Instance;

        // Used to move the player up one step after transitioning to a new map
        float speed = PlayerCharacter.Instance.BaseMoveSpeed;

        player.Direction.SetDirection(playerDirection);

        // Set player's position to the beginning of the next map
        switch (playerDirection)
        {
        case PlayerDirection.FacingDirections.West:
            player.transform.position = new Vector3(mapBounds.xMax - speed, player.transform.position.y);

            break;

        case PlayerDirection.FacingDirections.East:
            player.transform.position = new Vector3(mapBounds.xMin + speed, player.transform.position.y);

            break;

        case PlayerDirection.FacingDirections.South:
            player.transform.position = new Vector3(player.transform.position.x, mapBounds.yMax - speed);

            break;

        case PlayerDirection.FacingDirections.North:
            player.transform.position = new Vector3(player.transform.position.x, mapBounds.yMin + speed);

            break;
        }
    }
 private void Awake()
 {
     //Avoid duplicates
     if (instance == null)
     {
         instance           = this;
         SpriteRender       = GetComponent <SpriteRenderer>();
         SpriteRender.color = StartColor;
     }
     else
     {
         Destroy(gameObject);
     }
 }
    /// <summary>
    /// Creates and returns a MapFade. If one doesn't exist, the current one is returned.
    /// </summary>
    /// <param name="fadeIn">Whether the MapFade starts fading in or not. Whether the instance exists or not, the fade value will be changed.</param>
    /// <param name="destroyOnFinish">Whether the MapFade object is destroyed when finished or not.</param>
    /// <returns></returns>
    public static MapFade Create(bool fadeIn, bool destroyOnFinish)
    {
        if (HasInstance == true)
        {
            Instance.FadeIn          = fadeIn;
            Instance.DestroyOnFinish = destroyOnFinish;
            Instance.ResetFade(true);
            return(Instance);
        }

        MapFade fade = Resources.Load <MapFade>(ResourcePath.VFX.MapFade);

        if (fade == null)
        {
            Debug.LogError("MapFade object not found at " + ResourcePath.VFX.MapFade);
            return(null);
        }

        MapFade instantiatedFade = Instantiate <MapFade>(fade);

        instantiatedFade.FadeIn          = fadeIn;
        instantiatedFade.DestroyOnFinish = destroyOnFinish;
        return(instantiatedFade);
    }