17.子弹数据

17.游戏场景-游戏主逻辑-子弹相关-数据准备


17.1 知识点

在资源文件夹中找到子弹预设体资源。在场景中创建一个空的父对象,重置好角度和位置,把资源子弹预设体拖到空的父对象上,父对象添加球形碰撞器。把他放到Resource文件夹下,做成我们的子弹预设体。多做几个子弹预设体。再在美术资源里找到死亡特效按同样的方式放到Resource文件夹下。


分析子弹中可能要有的数据配置。子弹的数据配置可能包括多种移动方式(直线、曲线、抛物线、跟踪玩家)、x轴和z轴速度、旋转角度速度、资源路径(用于实例化子弹)、销毁特效和存在时间。

创建子弹数据集合类BulletData脚本,创建子弹单条数据类BulletInfo。BulletInfo包括许多子弹需要配置的变量,搞成属性。BulletData包含一个存储BulletInfo的List。

/// <summary>
/// 子弹数据集合
/// </summary>
public class BulletData
{
    public List<BulletInfo> bulletInfoList = new List<BulletInfo>();   
}

/// <summary>
/// 子弹单条数据
/// </summary>
public class BulletInfo
{
    [XmlAttribute]
    public int id; // 子弹数据的ID 方便我们配置的时候修改数据
    [XmlAttribute]
    public int type; // 子弹的移动规则 1~5代表不同的五种移动规则
    [XmlAttribute]
    public float forwardSpeed; // 正朝向移动速度
    [XmlAttribute]
    public float rightSpeed; // 横向移动速度
    [XmlAttribute]
    public float roundSpeed; // 旋转速度
    [XmlAttribute]
    public string resName; // 子弹特效资源路径
    [XmlAttribute]
    public string deadEffRes; // 子弹销毁时创建的死亡特效
    [XmlAttribute]
    public float lifeTime; // 子弹出生到销毁的生命周期时间
}

在StreamingAsset文件夹下创建BulletData.xml的xml文件。配置好多条子弹配置。

<?xml version="1.0" encoding="UTF-8"?>
<BulletData>
    <bulletInfoList>
        <BulletInfo id="1" type="1" forwardSpeed="60" rightSpeed="0" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="2" type="1" forwardSpeed="70" rightSpeed="0" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="3" type="1" forwardSpeed="80" rightSpeed="0" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="4" type="1" forwardSpeed="90" rightSpeed="0" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="5" type="2" forwardSpeed="60" rightSpeed="200" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="6" type="2" forwardSpeed="70" rightSpeed="200" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="7" type="2" forwardSpeed="80" rightSpeed="200" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="8" type="2" forwardSpeed="90" rightSpeed="200" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="9" type="3" forwardSpeed="60" rightSpeed="150" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="10" type="3" forwardSpeed="70" rightSpeed="150" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="11" type="3" forwardSpeed="80" rightSpeed="150" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="12" type="3" forwardSpeed="90" rightSpeed="150" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="13" type="4" forwardSpeed="60" rightSpeed="150" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="14" type="4" forwardSpeed="70" rightSpeed="150" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="15" type="4" forwardSpeed="80" rightSpeed="150" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="16" type="4" forwardSpeed="90" rightSpeed="150" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="17" type="5" forwardSpeed="60" rightSpeed="0" roundSpeed="40" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="18" type="5" forwardSpeed="70" rightSpeed="0" roundSpeed="40" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="19" type="5" forwardSpeed="80" rightSpeed="0" roundSpeed="40" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="20" type="5" forwardSpeed="90" rightSpeed="0" roundSpeed="40" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
    </bulletInfoList>
</BulletData>

在游戏数据管理类脚本中,一开始就用xml数据管理器读取子弹数据。可以断点检查读取的是否成功。

private GameDataMgr()
{
    //一开始 就读取 子弹数据
    bulletData = XmlDataMgr.Instance.LoadData(typeof(BulletData), "BulletData") as BulletData;
}

17.2 知识点代码

BulletData

using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;

/// <summary>
/// 子弹数据集合
/// </summary>
public class BulletData
{
    public List<BulletInfo> bulletInfoList = new List<BulletInfo>();   
}

/// <summary>
/// 子弹单条数据
/// </summary>
public class BulletInfo
{
    [XmlAttribute]
    public int id;//子弹数据的ID 方便我们配置的时候 修改数据
    [XmlAttribute]
    public int type;//子弹的移动规则 1~5代表不同的五种移动规则
    [XmlAttribute]
    public float forwardSpeed;//正朝向 移动速度
    [XmlAttribute]
    public float rightSpeed;//横向移动速度
    [XmlAttribute]
    public float roundSpeed;//旋转速度
    [XmlAttribute]
    public string resName;//子弹特效资源路径
    [XmlAttribute]
    public string deadEffRes;//子弹销毁时 创建的死亡特效
    [XmlAttribute]
    public float lifeTime;//子弹出生到销毁的生命周期时间
}

BulletData.xml

<?xml version="1.0" encoding="UTF-8"?>
<BulletData>
    <bulletInfoList>
        <BulletInfo id="1" type="1" forwardSpeed="60" rightSpeed="0" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="2" type="1" forwardSpeed="70" rightSpeed="0" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="3" type="1" forwardSpeed="80" rightSpeed="0" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="4" type="1" forwardSpeed="90" rightSpeed="0" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="5" type="2" forwardSpeed="60" rightSpeed="200" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="6" type="2" forwardSpeed="70" rightSpeed="200" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="7" type="2" forwardSpeed="80" rightSpeed="200" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="8" type="2" forwardSpeed="90" rightSpeed="200" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="9" type="3" forwardSpeed="60" rightSpeed="150" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="10" type="3" forwardSpeed="70" rightSpeed="150" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="11" type="3" forwardSpeed="80" rightSpeed="150" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="12" type="3" forwardSpeed="90" rightSpeed="150" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="13" type="4" forwardSpeed="60" rightSpeed="150" roundSpeed="0" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="14" type="4" forwardSpeed="70" rightSpeed="150" roundSpeed="0" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="15" type="4" forwardSpeed="80" rightSpeed="150" roundSpeed="0" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="16" type="4" forwardSpeed="90" rightSpeed="150" roundSpeed="0" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="17" type="5" forwardSpeed="60" rightSpeed="0" roundSpeed="40" resName="Bullet/1" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="18" type="5" forwardSpeed="70" rightSpeed="0" roundSpeed="40" resName="Bullet/2" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="19" type="5" forwardSpeed="80" rightSpeed="0" roundSpeed="40" resName="Bullet/3" deadEffRes="DeadEff/1" lifeTime="8"/>
        <BulletInfo id="20" type="5" forwardSpeed="90" rightSpeed="0" roundSpeed="40" resName="Bullet/4" deadEffRes="DeadEff/1" lifeTime="8"/>
    </bulletInfoList>
</BulletData>

GameDataMgr

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

public class GameDataMgr
{
    private static GameDataMgr instance = new GameDataMgr();
    public static GameDataMgr Instance => instance;

    //音乐相关数据
    public MusicData musicData;
    //排行榜数据
    public RankData rankData;
    //角色数据
    public RoleData roleData;
    //子弹数据
    public BulletData bulletData;

    //当前选择的角色 编号
    public int nowSelHeroIndex = 0;

    private GameDataMgr()
    {
        //就是获取本地硬盘中存储的 音乐数据
        musicData = XmlDataMgr.Instance.LoadData(typeof(MusicData), "MusicData") as MusicData;
        //一开始 就读取本地的 排行榜数据
        rankData = XmlDataMgr.Instance.LoadData(typeof(RankData), "RankData") as RankData;
        //一开始就读取 角色数据 
        roleData = XmlDataMgr.Instance.LoadData(typeof(RoleData), "RoleData") as RoleData;
        //一开始 就读取 子弹数据
        bulletData = XmlDataMgr.Instance.LoadData(typeof(BulletData), "BulletData") as BulletData;
    }

    #region 音乐音效相关
    //保存音乐相关数据的方法
    public void SaveMusicData()
    {
        XmlDataMgr.Instance.SaveData(musicData, "MusicData");
    }

    //开关背景音乐的方法
    public void SetMusicIsOpen(bool isOpen)
    {
        //改数据
        musicData.musicIsOpen = isOpen;
        //真正改变背景音乐的开关
        BKMusic.Instance.SetBKMusicIsOpen(isOpen);
    }

    //开关音效的方法
    public void SetSoundIsOpen(bool isOpen)
    {
        //改数据
        musicData.SoundIsOpen = isOpen;
        //真正改变音效的开关
    }

    //设置背景音乐的音效 0~1
    public void SetMusicValue(float value)
    {
        //改数据
        musicData.musicValue = value;
        //真正的改变背景音乐的大小
        BKMusic.Instance.SetBKMusicValue(value);
    }

    //设置音效的音效 0~1
    public void SetSoundValue(float value)
    {
        //改数据
        musicData.soundValue = value;
    }
    #endregion

    #region 排行榜相关
    /// <summary>
    /// 添加排行榜数据
    /// </summary>
    /// <param name="name">玩家名</param>
    /// <param name="time">通关时间</param>
    public void AddRankData(string name, int time)
    {
        //单条数据
        RankInfo rankInfo = new RankInfo();
        rankInfo.name = name;
        rankInfo.time = time;
        rankData.rankList.Add(rankInfo);

        //排序
        rankData.rankList.Sort((a, b)=>
        {
            if (a.time > b.time)
                return -1;
            return 1;
        });

        //移除大于20条的内容
        if (rankData.rankList.Count > 20)
            rankData.rankList.RemoveAt(20);
        //rankData.rankList.RemoveRange(20, rankData.rankList.Count - 20);

        //保存数据
        XmlDataMgr.Instance.SaveData(rankData, "RankData");
    }
    #endregion


    #region 玩家数据相关
    /// <summary>
    /// 提供给外部 获取当前选择的 英雄数据
    /// </summary>
    /// <returns></returns>
    public RoleInfo GetNowSelHeroInfo()
    {
        return roleData.roleList[nowSelHeroIndex];
    }
    #endregion
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏