WXMC/.svn/pristine/8c/8ca1829ff53419d809068b83456219a27db9588f.svn-base
2024-12-04 16:18:46 +08:00

78 lines
1.9 KiB
Plaintext

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PGem : MonoBehaviour
{
[SerializeField]
private float m_maxSpeed;
[SerializeField]
private float m_pickRadius;
[SerializeField]
private int m_gemCount;
private bool m_flying;
private Vector2 m_refVel;
public float GemSpawnLifeTime;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PHomePlayer playerUnit = PHomeGameMode.main.PlayerUnit;
if (!m_flying)
{
if (playerUnit.HasPickMagnet)
{
m_flying = true;
}
else
{
Vector2 playerPos = playerUnit.transform.position;
Vector2 gemPos = transform.position;
if (Vector2.Distance(playerPos, gemPos) < playerUnit.PickRadius)
{
m_flying = true;
}
}
}
// Fly to player
if (m_flying && playerUnit != null)
{
Vector3 oldPos = transform.position;
Vector2 curPos = transform.position;
Vector2 targetPos = playerUnit.transform.position;
Vector2 offset = targetPos - curPos;
if (offset.magnitude < m_pickRadius)
{
PickGem(playerUnit);
return;
}
Vector2 newPos = Vector2.SmoothDamp(curPos, targetPos, ref m_refVel, 0.2f);
oldPos.x = newPos.x;
oldPos.y = newPos.y;
transform.position = oldPos;
}
}
private void PickGem(PHomePlayer playerUnit)
{
PHomeGameMode.main.AddGem(playerUnit, m_gemCount);
AudioManager.instance.PlaySingleSound(AudioManager.instance.GemCollect);
Destroy(gameObject);
}
}