90 lines
2.1 KiB
C#
90 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEditorInternal;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.SocialPlatforms.Impl;
|
|
|
|
public class PacMan : MonoBehaviour
|
|
{
|
|
public PacManInput pacManInputControl;//输入系统
|
|
|
|
public Rigidbody2D rb;
|
|
public Animator anim;
|
|
|
|
public Vector2 inputDirection;//输入方向的数值
|
|
public float speed;//速度
|
|
public bool die =false;//主角死亡
|
|
|
|
public bool invincible;//无敌
|
|
public float invincibleTime;//无敌时间
|
|
public float timer;//计时器
|
|
public float remainingInvincibleTime { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
pacManInputControl = new PacManInput();//new一个新的输入系统
|
|
rb = GetComponent<Rigidbody2D>();
|
|
anim = GetComponent<Animator>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
pacManInputControl.Enable();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
pacManInputControl.Disable();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
inputDirection = pacManInputControl.PacManInputSystem.Move.ReadValue<Vector2>();//读取数值
|
|
if (invincible)
|
|
{
|
|
PacManInvincible();
|
|
}
|
|
|
|
if (remainingInvincibleTime > 0)
|
|
{
|
|
remainingInvincibleTime -= Time.deltaTime;
|
|
}
|
|
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
Move();
|
|
PacManAnimation();
|
|
}
|
|
|
|
public void Move()
|
|
{
|
|
rb.velocity = new Vector2(inputDirection.x * speed * Time.deltaTime, inputDirection.y * speed * Time.deltaTime);
|
|
}
|
|
|
|
public void PacManAnimation()
|
|
{
|
|
anim.SetFloat("H",inputDirection.x);
|
|
anim.SetFloat("V", inputDirection.y);
|
|
anim.SetBool("invincible", invincible);//无敌动画
|
|
}
|
|
|
|
public void PacManInvincible()
|
|
{
|
|
timer += Time.deltaTime;
|
|
GameManager.instance.invincibleTime = (int)(invincibleTime-timer+1);
|
|
if (timer >= invincibleTime)
|
|
{
|
|
invincible = false;
|
|
timer = 0f; // 重置计时器
|
|
|
|
}
|
|
}
|
|
|
|
public void ActivateInvincible()
|
|
{
|
|
remainingInvincibleTime = invincibleTime; // 重置剩余时间
|
|
}
|
|
}
|