目前枪械系统的功能列表如下

  • 开枪
    • 底层系统层
    • 表现层
  • 填弹
    • 底层系统层
    • 表现层
  • 换枪
    • 底层系统层
    • 表现层
  • 捡枪
    • 底层系统层
    • 表现层
  • 弹药补给
    • 底层系统层
    • 表现层
  • 补给站补给
    • 底层系统层
    • 表现层

功能实现图如下

换枪功能实现图

由于我们已经实现了捡枪的操作,所以换枪系统就比较简单了

修改IGunSystem

直接修改IGunSystem

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using FrameWorkDesign;
using System.Collections.Generic;
using System.Linq;

namespace ShootingEditor2D
{
interface IGunSystem : ISystem
{
GunInfo CurrentGun { get; }
void PickGun(string name, int ammoNumInGun, int bulletCountOutGun);
void ShiftGun();//
}

public class OnCurrrentGunChanged
{
public string Name { get; set; }
}

public class GunSystem : AbstractSystem, IGunSystem
{
public GunInfo CurrentGun { get; } = new GunInfo()
{
AmmoNumInGun = new BindableProperty<int>()
{
Value = 3
},
BulletCountOutGun = new BindableProperty<int>()
{
Value = 1
},
Name = new BindableProperty<string>()
{
Value = "手枪"
},
State = new BindableProperty<GunState>()
{
Value = GunState.Idle
}
};

private Queue<GunInfo> mGunInfos = new Queue<GunInfo>();
//...

public void ShiftGun()//
{
if(mGunInfos.Count > 0)
{
var previousGunInfo = mGunInfos.Dequeue();

var currentGunInfo = new GunInfo()
{
AmmoNumInGun = new BindableProperty<int>()
{
Value = CurrentGun.AmmoNumInGun.Value
},
BulletCountOutGun = new BindableProperty<int>()
{
Value = CurrentGun.BulletCountOutGun.Value
},
Name = new BindableProperty<string>()
{
Value = CurrentGun.Name.Value
},
State = new BindableProperty<GunState>()
{
Value = GunState.Idle
}
};

mGunInfos.Enqueue(currentGunInfo);

CurrentGun.Name.Value = previousGunInfo.Name.Value;
CurrentGun.AmmoNumInGun.Value = previousGunInfo.AmmoNumInGun.Value;
CurrentGun.BulletCountOutGun.Value = previousGunInfo.BulletCountOutGun.Value;

this.SendEvent(new OnCurrrentGunChanged() { Name = previousGunInfo.Name.Value });
}

}

//...
}
}

实现ShiftGunCommand

在Command文件夹里新建ShitGunCommand文件

1
2
3
4
5
6
7
8
9
10
11
12
using FrameWorkDesign;

namespace ShootingEditor2D
{
internal class ShiftGunCommand : AbstractCommand
{
protected override void OnExecute()
{
this.GetSystem<IGunSystem>().ShiftGun();
}
}
}

修改Player

修改Player脚本

1
2
3
4
5
6
7
8
9
private void Update()
{
//...
if (Input.GetKeyDown(KeyCode.Q))
{
this.SendCommand(new ShiftGunCommand());
}

}

运行游戏,按下Q键,就能切换枪了

代码重构

我们发现,在IGunSystem中,PickGunShiftGun的代码有重复,所以我们需要把重复的部分封装成方法。在写代码的过程中,发现有重复的代码就要第一时间封装重构,要养成良好的习惯

我们把重复的代码封装成EnqueueCurrentGun方法,然后修改其他两个方法

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using FrameWorkDesign;
using System.Collections.Generic;
using System.Linq;

namespace ShootingEditor2D
{
//...

public class GunSystem : AbstractSystem, IGunSystem
{
//...

private Queue<GunInfo> mGunInfos = new Queue<GunInfo>();
public void PickGun(string name, int ammoNumInGun, int bulletCountOutGun)
{
if (CurrentGun.Name.Value == name)
{
CurrentGun.BulletCountOutGun.Value += ammoNumInGun;
CurrentGun.BulletCountOutGun.Value += bulletCountOutGun;
}
else if (mGunInfos.Any(gunInfo => gunInfo.Name.Value == name))
{
var gunInfo = mGunInfos.First(gunInfo => gunInfo.Name.Value == name);
gunInfo.BulletCountOutGun.Value += ammoNumInGun;
gunInfo.BulletCountOutGun.Value += bulletCountOutGun;
}
else
{
EnqueueCurrentGun(name, ammoNumInGun, bulletCountOutGun);
}
}

public void ShiftGun()
{
if (mGunInfos.Count > 0)
{
var previousGunInfo = mGunInfos.Dequeue();

EnqueueCurrentGun(previousGunInfo.Name.Value, previousGunInfo.AmmoNumInGun.Value, previousGunInfo.BulletCountOutGun.Value);
}
}

void EnqueueCurrentGun(string nextGunName, int nextAmmoNumInGun, int nextBulletCountOutGun)//
{
var currentGunInfo = new GunInfo()
{
AmmoNumInGun = new BindableProperty<int>()
{
Value = CurrentGun.AmmoNumInGun.Value
},
BulletCountOutGun = new BindableProperty<int>()
{
Value = CurrentGun.BulletCountOutGun.Value
},
Name = new BindableProperty<string>()
{
Value = CurrentGun.Name.Value
},
State = new BindableProperty<GunState>()
{
Value = GunState.Idle
}
};

mGunInfos.Enqueue(currentGunInfo);

CurrentGun.Name.Value = nextGunName;
CurrentGun.AmmoNumInGun.Value = nextAmmoNumInGun;
CurrentGun.BulletCountOutGun.Value = nextBulletCountOutGun;

this.SendEvent(new OnCurrrentGunChanged() { Name = nextGunName });
}

protected override void OnInit()
{

}
}
}