在进行GameJam的演习时,我们使用了除了Unity本身的移动方案外的一套方案
该方案使用了喵大提供的框架中的成熟输入系统
1.首先要使用系统需要在LightGame类中去注册
public class LightGame : Architecture<LightGame>
{
protected override void Init()
{
RegisterModel<IGameModel>(new GameModel());
RegisterSystem<ITimerSystem>(new TimerSystem());
RegisterSystem<IAudioMgrSystem>(new AudioMgrSystem());
RegisterSystem<ITransitionSystem>(new TransitionSystem());
RegisterSystem<ISceneMgrSystem>(new SceneMgrSystem());
RegisterSystem<IQInputSystem>(new QInputSystem());//这行为输入系统的注册代码
RegisterSystem<ICameraSystem>(new CameraSystem());
}
}
想要在其中注册传入的泛型必须是继承自QFaramework中ISystem的接口,且参数必须是继承自这个泛型类型的类
public interface IQInputSystem : ISystem
{
}
该类型继承自其中的泛型,且类为喵大提供,我现阶段只需要学会使用即可。
在完成搭建后,声明一个继承自LightGameController的类,用于挂载到角色上。
既然我们需要角色类似于正常的手感,就需要加速度和减速度,所以声明如下字段。
private int mInputX, mInputY;
//移动速度
[SerializeField] private float MaxSpeed;
// 加速和减速增量
[SerializeField] private float mAccDelta, mDecDelta;
//旋转速度
[SerializeField] private float rotateSpeed;
private Vector2 mCurSpeed;
private Timer mHitTimer;
其中还有计时器字段,同样解释一下。
完成字段声明后,在Start函数中接受一下输入
this.RegisterEvent<DirInputEvent>(e =>
{
mInputX = e.hor;
mInputY = e.ver;
});
由于框架以及输入系统对横纵输入获取的值进行了归一化处理,我们得到的mInputX以及mInputY成为了整数。方便与加减速度的核心逻辑处理。
核心:当判断mInputY不为0后,对角色的当前速度mCurSpeed.y进行限制处理,使其在加速过程中上下移动时,时刻返回其速度,返回出来的速度被当做参数传入Translate函数中进行移动。但又因为限制的原因,加速的mCurSpeed.y的速度又不会超过最大速度,保持最大速度一直移动,实现了起步加速的效果。
减速核心与加速同理,当键盘的WS按键不再输入时,检测到mCurSpeed为0,让mCurSpeed.y的速度一直朝着0偏移,每次偏移减速度的单位,并且返回到移动函数中,实现减速效果。
同时Start中存在计时器的代码块。
mHitTimer = this.GetSystem<ITimerSystem>().AddTimer(1f, () =>
{
Debuger.Log("Hit");
this.GetSystem<IAudioMgrSystem>().PlaySound("硬币落地2");
}
,true);
获取脚本上带着的ITimerSystem,至于如何挂载的System,其原理与如何挂载的InputSystem相同,都是声明了一个继承自抽象类AbstractSystem和一个继承自ISystem并在LightGame中注册过了的接口,注册接口的目的其实是为了方便调用,但我不理解其中实现,就死几下。
总之,在这个TimerSystem中,有一个AddTimer的方法,方法返回一个Timer,参数分别为:延迟事件、完成后的委托、以及是否循环。
public Timer AddTimer(float delayTime, Action onFinished, bool isLoop)
{
var timer = mAvailableQueue.Count == 0 ? new Timer() : mAvailableQueue.Dequeue();
timer.Start(onFinished, delayTime, isLoop);
mUpdateList.Add(timer);
return timer;
}
调用这样调用
mHitTimer = this.GetSystem<ITimerSystem>().AddTimer(1f, () =>
{
Debuger.Log("Hit");
this.GetSystem<IAudioMgrSystem>().PlaySound("硬币落地2");
}
,true);