【2d游戏开发】实现生命值、饥饿度的减少和耐力值减少以及恢复

前言

本次文章实现生命值、饥饿度的减少和耐力值减少以及恢复这些内容;同样,因进行多次编写了教程,本次只写出重要的代码步骤。如需要更详细的教学,请到b站搜索我的2d游戏开发-unity实现系列教学视频。

功能说明

饥饿度会随着时间进行减少,当饥饿度为0并且持续保持为0,生命值会随着时间进行扣减;而耐力值的话,当玩家进行奔跑的时候会减少耐力值,当耐力值减少到一定程度,禁止加速,然后不奔跑的情况下,耐力值会随着时间进行恢复
在这里插入图片描述

代码

本次代码更新在本人编写的上次关于玩家移动和奔跑的文章部分

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    // 获取角色得刚体
    private Rigidbody2D playerRid;
    // 实例化二维向量接收角色坐标
    Vector2 playerVector2 = new Vector2();
    // 获取动画组件
    private Animator playerAnimator;


    // 定义速度
    public float speed = 200;
    // 玩家最大生命值
    public float maxHealth = 100;
    // 当前生命值
    public float currentHealth = 100;
    // 饥饿度
    public float maxHunger = 100;
    // 当前饥饿度
    public float currentHunger = 100;
    // 耐力值
    public float maxEndurance = 100;
    public float currentEndurance = 100;

    // 控制耐力值变化速度
    private float totalEnduranceTime = 0;
    // 控制生命值变换速度
    private float totalHealthTime = 0;
    // 控制饥饿度变换时间
    private float totalHungerTime =0;

    void Start()
    {
        playerRid = GetComponent<Rigidbody2D>();
        playerAnimator = GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        ChangePlayerAnim();
    }

    private void FixedUpdate()
    {
        // 角色移动
        playerMove();
        // 恢复和减少耐力
        recoverAndReduceEndurance();
        // 更新饥饿度
        UpdateHunger();
        // 更新生命值
        UpdateHealth();

    }

    // 移动方法
    void playerMove() {
        // 获取水平方向输入坐标
        playerVector2.x = Input.GetAxisRaw("Horizontal");
        // 获取垂直方向输入坐标
        playerVector2.y = Input.GetAxisRaw("Vertical");
        // 标准化坐标
        playerVector2.Normalize();
        // 将向量赋值给刚体
        playerRid.velocity = speed * playerVector2 * Time.fixedDeltaTime;
        // 加速
        UpMove();

    }
    // 加速
    void UpMove()
    {
        if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {

                // 判断 当前耐力是否小于等于10
                if (currentEndurance <= 10) {
                    speed = 200;
                    playerAnimator.speed = 1;
                } else {
                    speed = 400;
                    playerAnimator.speed = 2;
                }
            }
            else
            {
                speed = 200;
                playerAnimator.speed = 1;
            }
        }
    }

    // 耐力恢复和减少
    void recoverAndReduceEndurance() {
        // 设置恢复时间
        totalEnduranceTime += Time.fixedDeltaTime;
        // 判断是否在加速状态
        if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                if (currentEndurance > 0) {
                    if (totalEnduranceTime >= 0.1)
                    {
                        currentEndurance -= 1;
                        totalEnduranceTime = 0;
                    }
                }
            }
        }
        if (currentEndurance < maxEndurance)
        {
            if (totalEnduranceTime >= 0.3)
            {
                currentEndurance += 1;
                totalEnduranceTime = 0;
            }
        }


    }

    // 更新饥饿度
    void UpdateHunger() {
        // 24分钟 / 3    480秒
        totalHungerTime += Time.fixedDeltaTime;
        if (totalHungerTime >= 4.8) {
            if (currentHunger > 0) {
                currentHunger -= 1;
                
            }
            totalHungerTime = 0;
        }
    }

    // 更新生命值
    void UpdateHealth() {
        totalHealthTime += Time.fixedDeltaTime;
        if (totalHealthTime >= 1) {
            if (currentHealth > 0)
            {
                // 饥饿度会不会=0
                if (currentHunger == 0) {
                    currentHealth -= 1;
                }
            }
            else
            {
                // 进入到医院
            }
            totalHealthTime = 0;
        }
    }
    // 变换动画
    void ChangePlayerAnim() {
        // 左
        if (Input.GetKeyDown(KeyCode.A)) {
            playerAnimator.SetBool("idle",false);
            playerAnimator.SetBool("lefting",true);
            playerAnimator.SetBool("righting",false);
            playerAnimator.SetBool("uping",false);
            playerAnimator.SetBool("downing",false);
            playerAnimator.SetBool("faceLeft",false);
            playerAnimator.SetBool("faceRight",false);
            playerAnimator.SetBool("faceUp",false);
            playerAnimator.SetBool("faceDown",false);
        }

        if (Input.GetKeyUp(KeyCode.A)) {
            playerAnimator.SetBool("idle", false);
            playerAnimator.SetBool("lefting", false) ;
            playerAnimator.SetBool("righting", false);
            playerAnimator.SetBool("uping", false);
            playerAnimator.SetBool("downing", false);
            playerAnimator.SetBool("faceLeft", true);
            playerAnimator.SetBool("faceRight", false);
            playerAnimator.SetBool("faceUp", false);
            playerAnimator.SetBool("faceDown", false);
        }
        // 上
        if (Input.GetKeyDown(KeyCode.W))
        {
            playerAnimator.SetBool("idle", false);
            playerAnimator.SetBool("lefting", false);
            playerAnimator.SetBool("righting", false);
            playerAnimator.SetBool("uping", true);
            playerAnimator.SetBool("downing", false);
            playerAnimator.SetBool("faceLeft", false);
            playerAnimator.SetBool("faceRight", false);
            playerAnimator.SetBool("faceUp", false);
            playerAnimator.SetBool("faceDown", false);
        }

        if (Input.GetKeyUp(KeyCode.W))
        {
            playerAnimator.SetBool("idle", false);
            playerAnimator.SetBool("lefting", false);
            playerAnimator.SetBool("righting", false);
            playerAnimator.SetBool("uping", false);
            playerAnimator.SetBool("downing", false);
            playerAnimator.SetBool("faceLeft", false);
            playerAnimator.SetBool("faceRight", false);
            playerAnimator.SetBool("faceUp", true);
            playerAnimator.SetBool("faceDown", false);
        }

        // 右
        if (Input.GetKeyDown(KeyCode.D))
        {
            playerAnimator.SetBool("idle", false);
            playerAnimator.SetBool("lefting", false);
            playerAnimator.SetBool("righting", true);
            playerAnimator.SetBool("uping", false);
            playerAnimator.SetBool("downing", false);
            playerAnimator.SetBool("faceLeft", false);
            playerAnimator.SetBool("faceRight", false);
            playerAnimator.SetBool("faceUp", false);
            playerAnimator.SetBool("faceDown", false);
        }

        if (Input.GetKeyUp(KeyCode.D))
        {
            playerAnimator.SetBool("idle", false);
            playerAnimator.SetBool("lefting", false);
            playerAnimator.SetBool("righting", false);
            playerAnimator.SetBool("uping", false);
            playerAnimator.SetBool("downing", false);
            playerAnimator.SetBool("faceLeft", false);
            playerAnimator.SetBool("faceRight", true);
            playerAnimator.SetBool("faceUp", false);
            playerAnimator.SetBool("faceDown", false);
        }
        // 下
        if (Input.GetKeyDown(KeyCode.S))
        {
            playerAnimator.SetBool("idle", false);
            playerAnimator.SetBool("lefting", false);
            playerAnimator.SetBool("righting", false);
            playerAnimator.SetBool("uping", false);
            playerAnimator.SetBool("downing", true);
            playerAnimator.SetBool("faceLeft", false);
            playerAnimator.SetBool("faceRight", false);
            playerAnimator.SetBool("faceUp", false);
            playerAnimator.SetBool("faceDown", false);
        }

        if (Input.GetKeyUp(KeyCode.S))
        {
            playerAnimator.SetBool("idle", false);
            playerAnimator.SetBool("lefting", false);
            playerAnimator.SetBool("righting", false);
            playerAnimator.SetBool("uping", false);
            playerAnimator.SetBool("downing", false);
            playerAnimator.SetBool("faceLeft", false);
            playerAnimator.SetBool("faceRight", false);
            playerAnimator.SetBool("faceUp", false);
            playerAnimator.SetBool("faceDown", true);
        }

    }


}

结语

以上,为本人实现生命值、饥饿度的减少和耐力值减少以及恢复的代码过程,如需要更详细的教学,请到b站搜索本人的2d游戏开发-unity实现系列教学视频

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值