//Prefab脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SavePoint : MonoBehaviour
{
private LayerMask player;
private Light signlLight;
// Start is called before the first frame update
void Start()
{
player = LayerMask.NameToLayer("Player");
signlLight = GetComponentInChildren();
}

//玩家触碰到Point时调用GM中的方法
private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject .layer ==player )
    {
        signlLight.color = Color.green;
        this.gameObject.GetComponent<SpriteRenderer>().material.color = Color.green;
        Debug.Log("Save");
        
        GameManager.instance.SavePoint();
    }
}

}

//GameManager
public Vector2 playerPos ;//存储玩家位置,(通关/返回主界面时职位Vector2.zero
//instance.playerPos = Vector2.zero;//通关后位置初始化)
/存档点位置更新
public void SavePoint()
{
instance.playerPos = instance.playerMove.transform.position;//玩家位置赋值给GM中的Pos
}
//GM中的Save/Load
public GameData Save()
{
GameData date = new GameData();
date.scence = SceneManager.GetActiveScene().buildIndex;
date.deathCount = instance.deathCount;
date.bgmValue = UIManager.instence.GetBGMValue();
date.fxValue = UIManager.instence.GetFXValue();

    if (instance.playerPos == Vector2.zero)
    {
        date.posx = 0;
        date.posy = 0;
    }

    else
    {
        date.posx = instance.playerPos.x;
        date.posy = instance.playerPos.y;
    }

    return date;

}

public void Load(GameData date)
{
    instance.playerPos = new Vector2(date.posx, date.posy);

    SceneManager.LoadScene(date .scence );
    //UIManager.instence.ShowLoadPanel(date.scence);
    instance.deathCount = date.deathCount;
    UIManager.UpdataDeathUI(instance.deathCount);
    GameManager.instance.time = 0;
    UIManager.instence.SetBGMValue(date.bgmValue);
    UIManager.instence.SetFXValue(date.fxValue);
}

//Player控制脚本
Start
{
//玩家未接触到存档点GM中默认Pos=0;此时将默认未值赋值给GM中的Pos。
    //(通关之后GM中的Pos会被PlayerWin()方法赋值为默认值)
    if ( GameManager.instance.playerPos==Vector2.zero)
    {
        GameManager.instance.playerPos = playerPos;
    }else//玩家触碰到存档点时,将GM中的位置赋值给玩家物体的Pos。
    {
        transform.position = GameManager.instance.playerPos;
    }
    }