游戏开发创建操作之玩家信息系统的建立

游戏一般都需要玩家信息系统,那么我们应该如何搭建玩家信息系统,接下来我将展示一种简单的方法,完整代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UILabel;

public enum HeroType
{
    Swordman,
    Magician,
    Common
}
/// <summary>
/// 玩家状态信息栏
/// </summary>
public class PlayerStatus : MonoBehaviour
{
    public HeroType heroType;
    public int level = 1;//等级  100+level*50
    public string namePlayer = "NicolePotter";
    public int hp = 100;//HP MAX
    public int mp = 100;//MP MAX
    public float hp_remain = 100;
    public float mp_remain = 100;
    public float exp = 0;//目前获得的经验

   // public int coin = 200;//玩家金币数量

    public float attack = 20;
    public int attack_plus = 0;
    public float def = 20;
    public int def_plus = 0;
    public float speed = 20;
    public int speed_plus = 0;
    //剩余点数
    public int point_remain = 0;

    //特效
    public GameObject effect;
   

    private void Start()
    {
        GetExp(0);
        namePlayer = PlayerPrefs.GetString("name");
    }

    //让外部调用获取金币的方法
    //public void GetCoint(int count)
    //{
    //    coin += count;
    //}

    //获取治疗
    public void GetDrug(int hp,int mp)
    {
        hp_remain+= hp;
        mp_remain+= mp;
        if(hp_remain>this.hp) 
            hp_remain=this.hp;
        if(mp_remain>this.mp)
            mp_remain=this.mp;
        HeadStatusUI._instance.UpdateShow();
    }
    //获取点数
    public bool GetPoint(int point=1)
    {
        if(point_remain>=point)
        {
            point_remain -= point;
            return true;
        }
        return false;
    }

    //获取经验
    public void GetExp(int exp)
    {
        this.exp += exp;
        int total_exp = 100 + level * 50;
        while(this.exp>=total_exp)
        {
            //TODO 升级
            //播放升级动画
            //播放特效
            GameObject.Instantiate(effect, this.transform.position, Quaternion.identity);

            this.level++;
            HeadStatusUI._instance.UpdateShow();
            point_remain += this.level * 5;
            this.exp -= total_exp;
            total_exp = 100 + level * 50;
        }
      
     ExpBar._instance.SetValue(this.exp/total_exp);
       
    }

    public bool TakeMp(int count)
    {
        if(mp_remain>=count)
        {
            mp_remain-=count;
            HeadStatusUI._instance.UpdateShow();
            return true;
        }
        else
        {
            return false;
        }
    }
}

这个类中包含玩家的一些技能和经验值加成,包括血量,速度,魔法值等等。(几乎是非常完整了),每次触发只需要在其中调用方法即可。

以上代码有不明白的地方欢迎私聊问我,我很高兴为您解答,如果对您有帮助就点个赞支持一下吧!

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nicole Potter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值