IGunSystem初始值设定 上一节我们实现好了GunInfo
和GunState
,接下来我们在IGunSystem
里面给CurrentGun
设置好初始值
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 using FrameWorkDesign;namespace ShootingEditor2D { interface IGunSystem : ISystem { GunInfo CurrentGun { get ; } } 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 } }; protected override void OnInit () { } } }
初始值设定好后,我们再修改一下ShootCommand
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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; } } }
枪械信息显示 修改UIController
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 using UnityEngine;using FrameWorkDesign;using System;namespace ShootingEditor2D { public class UIController : MonoBehaviour ,IController { private void OnGUI () { GUI.Label(new Rect(10 , 10 , 300 , 100 ), $"生命:{m_PlayerModel.HP.Value} /3" ,m_LableStyle.Value); GUI.Label(new Rect(10 , 60 , 300 , 100 ), $"枪内子弹:{m_GunSystem.CurrentGun.AmmoNumInGun.Value} " ,m_LableStyle.Value); GUI.Label(new Rect(10 , 110 , 300 , 100 ), $"枪外子弹:{m_GunSystem.CurrentGun.BulletCountOutGun.Value} " ,m_LableStyle.Value); GUI.Label(new Rect(10 , 160 , 300 , 100 ), $"枪械名字:{m_GunSystem.CurrentGun.Name.Value} " ,m_LableStyle.Value); GUI.Label(new Rect(10 , 210 , 300 , 100 ), $"枪械状态:{m_GunSystem.CurrentGun.State.Value} " ,m_LableStyle.Value); GUI.Label(new Rect(Screen.width - 10 - 300 , 10 , 300 , 100 ), $"击杀数量:{m_StatSystem.KillCount.Value} " ,m_LableStyle.Value); } } }
在运行时我们发现,枪械虽然变成了Shooting状态,但是并没有方法让枪械的从Shooting状态切换回来。
我们的枪械系统中每一把枪都有一个开枪频率,如手枪1发/秒、冲锋枪6发/秒,枪械的子弹是按秒发射的,所以我们需要一个延时方法,来为枪械从开枪状态切回闲置状态预留一个接口。
在游戏暂停的时候,延时计算就应该暂停下来,继续游戏的时候,延时计算就继续进行,所以我们的延时方法不能是异步计时(如协程、async Task)。下一节将实现一个简单的时间系统。