153 lines
5.0 KiB
C#
153 lines
5.0 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.SceneManagement;
|
|||
|
|
|||
|
public class PlayerController : ActorController
|
|||
|
{
|
|||
|
private Rigidbody2D rigidbody2d;
|
|||
|
|
|||
|
private bool walking = false;
|
|||
|
private EDirectionType direction;
|
|||
|
|
|||
|
protected new void Awake()
|
|||
|
{
|
|||
|
base.Awake();
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҿ<EFBFBD><D2BF><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GameManager.Instance.PlayerManager.BindPlayer(this);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
|
|||
|
GameManager.Instance.EventManager.OnMoveInput = OnMoveInputEvent;
|
|||
|
|
|||
|
rigidbody2d = GetComponent<Rigidbody2D>();
|
|||
|
}
|
|||
|
|
|||
|
private void OnEnable()
|
|||
|
{
|
|||
|
walking = false;
|
|||
|
}
|
|||
|
|
|||
|
void Update()
|
|||
|
{
|
|||
|
CheckAnimator();
|
|||
|
}
|
|||
|
|
|||
|
private void OnTriggerEnter2D(Collider2D collision)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (collision.CompareTag("Item"))
|
|||
|
{
|
|||
|
// <20><>ʯ<EFBFBD>Զ<EFBFBD>ʹ<EFBFBD><CAB9>
|
|||
|
if (collision.GetComponent<ItemController>().ID == 7)
|
|||
|
{
|
|||
|
GameManager.Instance.PlayerManager.PlayerInfo.Attack += 3;
|
|||
|
GameManager.Instance.UIManager.ShowInfo($"<22><><EFBFBD><EFBFBD> {GameManager.Instance.ResourceManager.GetResourceInfo(EResourceType.Item, 7).Name} 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20>㹥<EFBFBD><E3B9A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
GameManager.Instance.PoolManager.RecycleResource(collision.gameObject);
|
|||
|
GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "PickUp");
|
|||
|
}
|
|||
|
else if (collision.GetComponent<ItemController>().ID == 8)
|
|||
|
{
|
|||
|
GameManager.Instance.PlayerManager.PlayerInfo.Defence += 3;
|
|||
|
GameManager.Instance.UIManager.ShowInfo($"<22><><EFBFBD><EFBFBD> {GameManager.Instance.ResourceManager.GetResourceInfo(EResourceType.Item, 8).Name} 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
GameManager.Instance.PoolManager.RecycleResource(collision.gameObject);
|
|||
|
GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "PickUp");
|
|||
|
}
|
|||
|
else GameManager.Instance.BackpackManager.PickUp(collision.GetComponent<ItemController>());
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
else if (collision.CompareTag("Enemy")) GameManager.Instance.CombatManager.StartFight(collision.GetComponent<EnemyController>());
|
|||
|
}
|
|||
|
|
|||
|
private void OnDestroy()
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
|
|||
|
GameManager.Instance.PlayerManager.UnbindPlayer();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD><EFBFBD><E2B6AF>״̬<D7B4><CCAC>
|
|||
|
/// </summary>
|
|||
|
private void CheckAnimator()
|
|||
|
{
|
|||
|
_animator.SetBool("attacking", GameManager.Instance.CombatManager.Fighting);
|
|||
|
_animator.SetBool("walking", walking);
|
|||
|
_animator.SetFloat("direction", (int)direction);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><>ȡ<EFBFBD><C8A1>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
/// <param name="inputType"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
private void OnMoveInputEvent(EDirectionType inputType)
|
|||
|
{
|
|||
|
if (walking || GameManager.Instance.CombatManager.Fighting) return;
|
|||
|
// ״̬<D7B4><CCAC>ֵ
|
|||
|
direction = GameManager.Instance.CombatManager.Fighting ? direction : inputType;
|
|||
|
// <20><><EFBFBD>Ի<EFBFBD>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Vector2 targetPoint = Vector2.zero;
|
|||
|
switch (direction)
|
|||
|
{
|
|||
|
case EDirectionType.UP:
|
|||
|
targetPoint.y = 1;
|
|||
|
break;
|
|||
|
case EDirectionType.DOWN:
|
|||
|
targetPoint.y = -1;
|
|||
|
break;
|
|||
|
case EDirectionType.LEFT:
|
|||
|
targetPoint.x = -1;
|
|||
|
break;
|
|||
|
case EDirectionType.RIGHT:
|
|||
|
targetPoint.x = 1;
|
|||
|
break;
|
|||
|
default:
|
|||
|
break;
|
|||
|
}
|
|||
|
targetPoint += (Vector2)transform.position;
|
|||
|
RaycastHit2D[] hits = Physics2D.RaycastAll(targetPoint, (targetPoint - (Vector2)transform.position), .1f);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>пɽ<D0BF><C9BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (hits.Length > 0)
|
|||
|
{
|
|||
|
// ɸѡ<C9B8><D1A1><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int maxOrder = -100;
|
|||
|
GameObject obj = null;
|
|||
|
foreach (var hit in hits)
|
|||
|
{
|
|||
|
if (hit.collider.GetComponent<SpriteRenderer>().sortingOrder > maxOrder)
|
|||
|
{
|
|||
|
maxOrder = hit.collider.GetComponent<SpriteRenderer>().sortingOrder;
|
|||
|
obj = hit.collider.gameObject;
|
|||
|
}
|
|||
|
}
|
|||
|
if (null != obj.GetComponent<IInteraction>())
|
|||
|
{
|
|||
|
// <20><><EFBFBD>н<EFBFBD><D0BD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹͣ<CDA3>ƶ<EFBFBD>
|
|||
|
if (!obj.GetComponent<IInteraction>().Interaction()) return;
|
|||
|
}
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
|
|||
|
StartCoroutine(Moving(transform.position, targetPoint));
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20>ƶ<EFBFBD>
|
|||
|
/// </summary>
|
|||
|
IEnumerator Moving(Vector2 oldPoint, Vector2 newPoint)
|
|||
|
{
|
|||
|
float speed = 8f;
|
|||
|
float timer = 0;
|
|||
|
walking = true;
|
|||
|
while ((Vector2)transform.position != newPoint)
|
|||
|
{
|
|||
|
timer += Time.deltaTime;
|
|||
|
float t = timer / (oldPoint - newPoint).sqrMagnitude * speed;
|
|||
|
Vector2 point = Vector2.Lerp(oldPoint, newPoint, t);
|
|||
|
rigidbody2d.MovePosition(point);
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
walking = false;
|
|||
|
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ɺ<C9BA><F3B4A5B7>¼<EFBFBD>
|
|||
|
GameManager.Instance.EventManager.OnPlayerArrive?.Invoke(newPoint);
|
|||
|
}
|
|||
|
}
|