弹药补给
弹药补给功能实现图

修改IGunSystem
由于所有的枪都要增加一个弹夹的子弹,所以我们需要获取IGunSystem
中的mGunInfos
,需要将此队列开放出来
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
| using FrameWorkDesign; using System.Collections.Generic; using System.Linq;
namespace ShootingEditor2D { interface IGunSystem : ISystem { Queue<GunInfo> GunInfos { get; } }
public class OnCurrrentGunChanged { public string Name { get; set; } }
public class GunSystem : AbstractSystem, IGunSystem {
private Queue<GunInfo> mGunInfos = new Queue<GunInfo>();
public Queue<GunInfo> GunInfos { get { return mGunInfos; } }
} }
|
添加AddBulletCommand
在Command文件夹中新建AddBulletCommand
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
| using FrameWorkDesign;
namespace ShootingEditor2D { public class AddBulletCommand : AbstractCommand { protected override void OnExecute() { var gunSystem = this.GetSystem<IGunSystem>(); var gunConfigModel = this.GetModel<IGunConfigModel>();
AddBullet(gunSystem.CurrentGun, gunConfigModel);
foreach (var gunInfo in gunSystem.GunInfos) { AddBullet(gunInfo, gunConfigModel); } }
void AddBullet(GunInfo gunInfo,IGunConfigModel gunConfigModel) { var gunConfigItem = gunConfigModel.GetItemByName(gunInfo.Name.Value); if (gunConfigItem.NeedBullet) { gunInfo.BulletCountOutGun.Value += gunConfigItem.BulletMaxCount; } else { gunInfo.AmmoNumInGun.Value = gunConfigItem.BulletMaxCount; } } } }
|
添加补给物品
新建一个胶囊体(Capsule),命名为BulletPickItem,将它的缩放改为(x:0.2,y:0.2,z:0.2),设成黄色,并放在平台的某个地方。添加Capsule Collider 2D并打开is Trigger
在ViewController——Gameplay文件夹里面新建BulletPickItem
脚本,将它挂载在这个胶囊体上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| using FrameWorkDesign; using UnityEngine;
namespace ShootingEditor2D { public class BulletPickItem : MonoBehaviour, IController { private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { this.SendCommand<AddBulletCommand>();
Destroy(gameObject); } }
public IArchitecture GetArchitecture() { return ShootingEditor2D.Interface; } } }
|
当Player触碰到这个胶囊体时,就会自动填弹了。
补给站补给
功能图如下,弹药补给主要补给的是枪外弹匣的子弹,补给站主要补给的是枪内的

添加FullBulletCommand
在Command文件夹内新建FullBulletCommand
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| using FrameWorkDesign;
namespace ShootingEditor2D { public class FullBulletCommand : AbstractCommand { protected override void OnExecute() { var gunSystem = this.GetSystem<IGunSystem>(); var gunConfigModel = this.GetModel<IGunConfigModel>();
gunSystem.CurrentGun.AmmoNumInGun.Value = gunConfigModel.GetItemByName(gunSystem.CurrentGun.Name.Value).BulletMaxCount;
foreach (var gunInfo in gunSystem.GunInfos) { gunInfo.AmmoNumInGun.Value = gunConfigModel.GetItemByName(gunInfo.Name.Value).BulletMaxCount; } } } }
|
添加补给站
新建一个方块Square,命名为SupplyStation,颜色设为蓝色,添加BoxCollider 2D并勾选is Trigger
在ViewController——Gameplay文件夹里面新建SupplyStation
脚本,将它挂载在这个方体上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| using FrameWorkDesign; using UnityEngine;
namespace ShootingEditor2D { public class SupplyStation : MonoBehaviour, IController {
private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { this.SendCommand<FullBulletCommand>(); } }
public IArchitecture GetArchitecture() { return ShootingEditor2D.Interface; } } }
|
至此枪械系统底层系统层功能就做好了