Files
taptap2024_GJ_chidouren/Assets/Scripts/Game/Component/MapContent.cs
2024-10-25 16:15:25 +08:00

109 lines
3.5 KiB
C#

using System;
using Framework.Timer;
using Game.EventDefine;
using UniFramework.Event;
using UnityEngine;
namespace Game.Component
{
public class MapContent : MonoBehaviour
{
public PlayerEntity PlayerEntity;
[HideInInspector] public ScenePart ScenePart;
public AstarPath Pathfinder;
public bool IsActiveGame;
private float _moveGlobalOffset = 1;
//玩家位置
public Vector2 PlayerPosition { private set; get; }
public bool IsRunaway { get ; private set ; }
public float MoveGlobalOffset => this._moveGlobalOffset;
public bool IsPause
{
get => this._moveGlobalOffset <= 0;
set => this._moveGlobalOffset = value ? 0 : 1;
}
private TimeHandler _RunawayTimeHandler;
//全局特殊单例
public static MapContent Instance;
private EventGroup _eventGroup = new EventGroup ();
private void Awake ()
{
//全局特殊单例
Instance = this;
var componentInChildren = this.transform.GetComponentInChildren<ScenePart> ();
if (componentInChildren != null)
{
SetPart (componentInChildren);
}
}
private void OnEnable ()
{
GameUpdateMgr.Instance.AddUpdater (DoUpdate);
this._eventGroup.AddListener<GameEventDefine.GlobalRunaway> ( OnRunaway );
}
private void OnDisable ()
{
GameUpdateMgr.Instance.RemoveUpdater (DoUpdate);
this._eventGroup.RemoveAllListener ();
}
private void DoUpdate ()
{
this.PlayerPosition = this.PlayerEntity.transform.position;
}
public void SetPart (ScenePart scenePart)
{
if (this.ScenePart != null)
{
GameObject.Destroy (this.ScenePart.gameObject);
}
this.ScenePart = scenePart;
this.ScenePart.InitPart ();
// this.Pathfinder.data.SetData (this.ScenePart.mapCacheData.bytes);
// this.Pathfinder.FlushGraphUpdates ();
CameraManager.Instance.SetBoxCollider (this.ScenePart.cameraCollider);
}
public void ResetGame ()
{
this.PlayerEntity.gameObject.SetActive (false);
this.IsPause = false;
this._RunawayTimeHandler?.Kill ();
this.IsRunaway = false;
this.PlayerEntity.speed = this.ScenePart.playerBaseSpeed;
this.PlayerEntity.transform.position = this.ScenePart.createPos.position;
this.PlayerEntity.transform.rotation = this.ScenePart.createPos.rotation;
this.PlayerEntity.gameObject.SetActive (true);
this.ScenePart.RefreshInit ();
this.PlayerEntity.RefreshInit ();
//播放特效
}
public bool OverlyCoin (int messageOverlyCoin)
{
// ReSharper disable once Unity.NoNullPropagation
return this.ScenePart?.AddConditionNumber (messageOverlyCoin) ?? false;
}
private void OnRunaway (IEventMessage message)
{
GameEventDefine.GlobalRunaway runaway = (GameEventDefine.GlobalRunaway) message ;
this.IsRunaway = true;
this._RunawayTimeHandler?.Kill ();
this._RunawayTimeHandler = GameUpdateMgr.Instance.CreateTimer (runaway.Duration, () => { this.IsRunaway = false; });
}
}
}