这一章,我们需要用到我们框架的知识,我们将人物的血量,攻击力,速度,等一系列的数据统一起来。
首先在让我们来了解一下框架,我们先不去考虑和服务器的调试的部分。
角色的数据我们将之存放在 MODELS之中,然后通过Signal来传递消息。
基础的流程就是:
VIEW中发出一个signal去找MODELS要数据,MODELS接收信号之后,将自己存储的小姐姐数据,传递给VIEW,然后VIEW实装这部分数据。而两者的中间介质就是Command的。

首先我们来创建三个信号。我来创建一个PlayerDataSignal.cs的角色信号类
using strange.extensions.signal.impl;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBaseDataSignal:Signal<float ,float >{//models向playerView发送基础数据。
}
public class CallMadelPlyerBaseDataSignal:Signal {//View向models发送想要获取基础数据的请求
}
public class PlayerCommandSignal:Signal {//用于创建PlayerCommandSignal对象
}
创建一个IPlayerModel的接口。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IPlayerModel {
float speed { get; set; }
float jumpPower { get; set;}
CallMadelPlyerBaseDataSignal callMadelPlyerBaseDataSignal { get;}
}
创建一个继承了IPlayerModel的类PlayerModel。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerModel:IPlayerModel {
// Use this for initialization
public float speed { get; set; } = 1;//暂定数据
public float jumpPower { get; set; } = 3.5f;//暂定数据
[Inject]
public CallMadelPlyerBaseDataSignal callMadelPlyerBaseDataSignal { get; set; }
public PlayerModel() {
}
}
关于中间层PlayerCommand的处理:
using strange.extensions.command.impl;
using strange.extensions.context.api;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//开始命令
public class PlayerCommand:Command {
[Inject(ContextKeys.CONTEXT_VIEW)]
public GameObject contextView { get; set; }//获得上一层的长官
[Inject]
public IPlayerModel model { get; set; }
[Inject]
public PlayerBaseDataSignal playerBaseDataSignal { get; set; }
public override void Execute() {
//Retain();
Debug.Log("PlayerCommand Execute.....");
model.callMadelPlyerBaseDataSignal.AddListener(onComplete);
}
public override void Release() {
Debug.Log("PlayerCommand Release.....");
model.callMadelPlyerBaseDataSignal.RemoveListener(onComplete);
}
private void onComplete() {
Debug.Log("获得通知,开始找寻model中的数据用于回调");
playerBaseDataSignal.Dispatch(model.speed,model.jumpPower);
// Release();
}
}
StartCommand唤醒playerCommand
public class StartCommand:Command {
// Use this for initialization
void Start() {
}
[Inject]
public PlayerCommandSignal playerCommandSignal { get; set; }//用于创建中间层实例
public override void Execute() {
Debug.Log("StartCommand");
playerCommandSignal.Dispatch();//建立palyercommand的实例对象。
}
// Update is called once per frame
}
信号的注册和绑定TestContext:
protected override void mapBindings() {
//model
injectionBinder.Bind<IPlayerModel>().To<PlayerModel>().ToSingleton();
mediationBinder.Bind<PlayerTestView>().To<PlayerTestMediator>();
commandBinder.Bind<StartSignal>().To<StartCommand>();
commandBinder.Bind<PlayerCommandSignal>().To<PlayerCommand>();//绑定回调
injectionBinder.Bind<PlayerBaseDataSignal>().ToSingleton();//注册信号
injectionBinder.Bind<CallMadelPlyerBaseDataSignal>().ToSingleton();//注册信号
}
处理完了这些,我们就要去Mediator层去发送和接受信号了。
using strange.extensions.mediation.impl;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTestMediator:Mediator {
// Use this for initialization
//test
float up = 0;
float right = 0;
Vector3 vec;
[Inject]
public PlayerTestView playerView { get; set; }
[Inject]
public PlayerBaseDataSignal playerBaseDataSignal { get; set; }//返回给我们的带着数据的信号
[Inject]
public CallMadelPlyerBaseDataSignal callMadelPlyerBaseDataSignal { get; set; }//发出我想要数据的信号。
[Inject]
public PlayerCommandSignal playerCommandSignal { get; set; }//用于创建中间层实例
public override void OnRegister() {//绑定成功之后会调用这个API
Debug.Log("OnRegister");
playerView.init();
playerBaseDataSignal.AddListener(setPlayerData);//获得返回数据的监听函数
callMadelPlyerBaseDataSignal.Dispatch();//在playerView基础组件初始化完成之后,开始去想models层获取数据。
UpdateManges.add_playerEventList_(keyController);
UpdateManges.add_playerEventList_(playerAction);
UpdateManges.add_playerEventList_Fix(accurateDetection);
UpdateManges.add_playerEventList_Late(cameraContorller);
}
public override void OnRemove() {//解除绑定之后调用这个API
Debug.Log("OnRemove");
playerBaseDataSignal.RemoveListener(setPlayerData);
UpdateManges.sub_playerEventList_(keyController);
UpdateManges.sub_playerEventList_(playerAction);
UpdateManges.sub_playerEventList_(accurateDetection);
UpdateManges.sub_playerEventList_Late(cameraContorller);
}
bool run = false;
bool jump = false;
void keyController() {
float targetup = (Input.GetKey("w") ? 1 : 0) - (Input.GetKey("s") ? 1 : 0);
float targetright = (Input.GetKey("d") ? 1 : 0) - (Input.GetKey("a") ? 1 : 0);
//up = Mathf.SmoothDamp(up, targetup, ref upVelocity, 0.1f);
//right = Mathf.SmoothDamp(right, targetright, ref rightVelocity, 0.1f);
up = Mathf.Lerp(up, targetup,0.6f);
right = Mathf.Lerp(right, targetright, 0.6f);
if (Input.GetKeyDown(KeyCode.LeftShift)) {
run = !run;
}
jump = Input.GetKey(KeyCode.Space);
}
void cameraContorller() {//加入LateUpdate不断跟新
playerView.cameraFollow();
}
void playerAction() {//加入update不断跟新
playerView.baseWalkRun(right, up, run);
playerView.startJump(jump);
playerView.playerMove();
}
void accurateDetection() {//加入fixUpdate不断跟新
playerView.colliderTesting();
//playerView.playerMove();
//playerView.userGavity();
}
void setPlayerData(float speed,float jumpPower) {
Debug.Log("开始设置角色数据speed:"+speed.ToString()+" jumpPower:" +jumpPower.ToString());
}
}
完成之后让我们运行一下吧。

最后我们只需要将最后得到的数据给小姐姐设置上就OK了,这样就能有效的控制小姐姐的身体机能了,以后的技能,血量,穿着服饰等等的数据都是这样的基础结构上的扩充。
上一节
下一节
网友评论