You've already forked taptap2024_GJ_chidouren
116 lines
3.9 KiB
C#
116 lines
3.9 KiB
C#
using System;
|
|
using Framework.Audio;
|
|
using Framework.Timer;
|
|
using Game.EventDefine;
|
|
using UniFramework.Event;
|
|
using Unity.Mathematics;
|
|
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;
|
|
|
|
private float _expGlobalOffset = 0;
|
|
|
|
//玩家位置
|
|
public Vector2 PlayerPosition { private set; get; }
|
|
public bool IsRunaway { get ; private set ; }
|
|
public float MoveGlobalOffset => this._moveGlobalOffset;
|
|
public float EnemyMoveGlobalOffset => math.clamp (this._moveGlobalOffset - this._expGlobalOffset , 0 , 1f);
|
|
|
|
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 ();
|
|
AudioManager.Instance.PlayBGM (this.ScenePart.BGM , 1 , 0.65f);
|
|
CameraManager.Instance.SetBoxCollider (this.ScenePart.cameraCollider);
|
|
}
|
|
|
|
public void ResetGame ()
|
|
{
|
|
this._expGlobalOffset = Mathf.Clamp (this.ScenePart.LossCount * 0.025f , 0 , 0.25f);
|
|
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; });
|
|
}
|
|
}
|
|
} |