这一节我们来实现在刚开始设计时提出的IGunConfigModel

枪械系统需求初步分析

层级一览

IGunConfigModel

我们在之前提到过,GunConfigModel是用来读取数据表的,里面是一些只读数据。我们在前几节写了GunInfoGunInfo才是在运行时主要的枪械数据来源。

GunInfo

之前在设计GunInfo类的时候,已经设计了一个Name字段,这个Name字段就是用来查询配置相关数据的

所以我们需要让IGunConfigModel提供一个根据名字返回GunConfigItem的方法

IGunConfigModel

在Scripts——Model文件夹内新建IGunConfigModel脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Collections.Generic;
using FrameWorkDesign;


namespace ShootingEditor2D
{
public interface IGunConfigModel : IModel
{
GunConfigItem GetItemByName(string gunName);
}

public class GunConfigModel : AbstractModel, IGunConfigModel
{
//注意这里的字典的初始化方式,把键值对放在中括号内即可
private Dictionary<string,GunConfigItem> mItems = new Dictionary<string, GunConfigItem>()
{
{ "手枪",new GunConfigItem("手枪",7,1,1,0.5f,false,3,"默认枪") },
{ "冲锋枪",new GunConfigItem("冲锋枪",30,1,6,0.34f,true,3,"无") },
{ "步枪",new GunConfigItem("步枪",50,3,3,1f,true,1,"有一定后坐力") },
{ "狙击枪",new GunConfigItem("狙击枪",12,6,1,1f,true,5,"红外瞄准+后坐力大") },
{ "火箭筒",new GunConfigItem("火箭筒",1,5,1,1f,true,4,"跟踪+爆炸") },
{ "霰弹枪",new GunConfigItem("霰弹枪",1,1,1,0.5f,true,1,"一次发射6~12颗子弹") },
};
protected override void OnInit()
{
//可以在这里加入读表的代码,将代码加入到mItems里面
}

public GunConfigItem GetItemByName(string gunName)
{
GunConfigItem item;
if(mItems.TryGetValue(gunName, out item))
{
return item;
}else
return null;
}
}

public class GunConfigItem
{
public GunConfigItem(string name,int bulletMaxCount,float attack,float frequency,float shootDistance,bool needBullet,float reloadSeconds,string description)
{
Name = name;
BulletMaxCount = bulletMaxCount;
Attack = attack;
Frequency = frequency;
ShootDistance = shootDistance;
NeedBullet = needBullet;
ReloadSeconds = reloadSeconds;
Description = description;
}

public string Name { get; set; }
public int BulletMaxCount { get; set; }
public float Attack { get; set; }
public float Frequency { get; set; }
public float ShootDistance { get; set; }
public bool NeedBullet { get; set; }
public float ReloadSeconds { get; set; }
public string Description { get; set; }

}
}

C#支持通过构造代码进行初始化,可以像填表一样,将数据填充到C#提供的数据结构中。

在一个项目的初始阶段,假如策划没有把配置表给开发者,我们可以使用以上的方式在代码中配置表,然后等以后策划给到配置表或者配置表格斯定下来后,我们还是可以在IGunConfigModel中去写配置表解析相关代码,然后去填充GunConfigItem对象

IGunConfigModel注册到架构中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using FrameWorkDesign;

namespace ShootingEditor2D
{
public class ShootingEditor2D : Architecture<ShootingEditor2D>
{
protected override void Init()
{
this.RegisterSystem<IStatSystem>(new StatSystem());
this.RegisterSystem<IGunSystem>(new GunSystem());
this.RegisterSystem<ITimeSystem>(new TimeSystem());

this.RegisterModel<IGunConfigModel>(new GunConfigModel());//配置表相关的Model都要放在前面
this.RegisterModel<IPlayerModel>(new PlayerModel());
}
}
}

读取GunConfig

修改ShootCommand

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using FrameWorkDesign;

namespace ShootingEditor2D
{
public class ShootCommand : AbstractCommand
{
public static readonly ShootCommand Instance = new ShootCommand();
protected override void OnExecute()
{
var gunSystem = this.GetSystem<IGunSystem>();
gunSystem.CurrentGun.AmmoNumInGun.Value--;
gunSystem.CurrentGun.State.Value = GunState.Shooting;

var gunConfigItem = this.GetModel<IGunConfigModel>().GetItemByName(gunSystem.CurrentGun.Name.Value);//
var timeSystem = this.GetSystem<ITimeSystem>();
timeSystem.AddTaskDelay( 1/gunConfigItem.Frequency, () =>//
{
gunSystem.CurrentGun.State.Value = GunState.Idle;
});
}
}
}