qframework 架构 (作者:凉鞋)使用笔记

2024-01-22 00:30

本文主要是介绍qframework 架构 (作者:凉鞋)使用笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一些准则:

根据VIEW->SYSTEM->MODEL的分层架构

初始架构:

app.

using FrameworkDesign;namespace ShootingEditor2D(项目的命名空间)
{public class ShootingEditor2D (游戏名称): Architecture<ShootingEditor2D>{protected override void Init(){}}
}

该脚本放到scripts文件夹下。

其他model\system等,注册在app.

using FrameworkDesign;namespace ShootingEditor2D
{public class ShootingEditor2D : Architecture<ShootingEditor2D>{protected override void Init(){this.RegisterModel<IPlayerModel>(new PlayerModel());this.RegisterSystem<IStatSystem>(new StatSystem());}}
}

功能列表

在VIEW层和SYSTEM层之间通信

创建VIEW

创建VIEW中的关键角色:UIController,方便快速对UI的显示进行处理。

*注意,VIEW脚本可以获取任何来自SYSTEM和MODEL的信息,以此来更新自己,比如监测是否子弹足够。

namespace ShootingEditor2D
{public class Gun : MonoBehaviour,IController // +{private Bullet mBullet;private GunInfo mGunInfo; // +private void Awake(){mBullet = transform.Find("Bullet").GetComponent<Bullet>();mGunInfo = this.GetSystem<IGunSystem>().CurrentGun; // +}public void Shoot(){if (mGunInfo.BulletCount.Value > 0) // +{var bullet = Instantiate(mBullet.transform, mBullet.transform.position, mBullet.transform.rotation);// 统一缩放值bullet.transform.localScale = mBullet.transform.lossyScale;bullet.gameObject.SetActive(true);this.SendCommand<ShootCommand>(); // +}}public IArchitecture GetArchitecture() // +{return ShootingEditor2D.Interface;}private void OnDestroy() // +{mGunInfo = null; //+}}
}

namespace ShootingEditor2D
{public class UIController : MonoBehaviour,IController{private IStatSystem mStatSystem;private IPlayerModel mPlayerModel;private void Awake(){mStatSystem = this.GetSystem<IStatSystem>();mPlayerModel = this.GetModel<IPlayerModel>();}/// <summary>/// 自定义字体大小/// </summary>private readonly Lazy<GUIStyle> mLabelStyle = new Lazy<GUIStyle>(()=>new GUIStyle(GUI.skin.label){fontSize = 40});private void OnGUI(){GUI.Label(new Rect(10,10,300,100),$"生命:{mPlayerModel.HP.Value}/3",mLabelStyle.Value);GUI.Label(new Rect(Screen.width - 10 - 300,10,300,100),$"击杀数量:{mStatSystem.KillCount.Value}",mLabelStyle.Value);}public IArchitecture GetArchitecture(){return ShootingEditor2D.Interface;}}
}

创建VIEW->SYSTEM的通信方式:命令Command

namespace ShootingEditor2D
{public class KillEnemyCommand : AbstractCommand{protected override void OnExecute(){this.GetSystem<IStatSystem>().KillCount.Value++;}}
}
        protected override void OnExecute(){var gunSystem = this.GetSystem<IGunSystem>();gunSystem.CurrentGun.BulletCountInGun.Value--;gunSystem.CurrentGun.GunState.Value = GunState.Shooting;}

确定在什么运行情况下发出该命令Command。比如,一个小小的子弹销毁时,子弹知道要发出。

namespace ShootingEditor2D
{public class Bullet : MonoBehaviour,IController // +{private Rigidbody2D mRigidbody2D;private void Awake(){mRigidbody2D =  GetComponent<Rigidbody2D>();}private void Start(){mRigidbody2D.velocity = Vector2.right * 10;}private void OnCollisionEnter2D(Collision2D other){if (other.gameObject.CompareTag("Enemy")){this.SendCommand<KillEnemyCommand>(); // +Destroy(other.gameObject);}}public IArchitecture GetArchitecture() // +{return ShootingEditor2D.Interface;}}
}

再比如,如果玩家碰到怪物就掉血,那么针对这个功能可以写一个脚本,挂在再玩家身上

namespace ShootingEditor2D
{public class AttackPlayer : MonoBehaviour,IController{private void OnCollisionEnter2D(Collision2D other){if (other.gameObject.CompareTag("Player")){this.SendCommand<HurtPlayerCommand>();}}public IArchitecture GetArchitecture(){return ShootingEditor2D.Interface;}}

VIEW自己分内的逻辑就自己处理了,比如怪物碰到玩家自己消失。如果不需要记录MODEL以数据的话,就自己删除。

可以建立各种各样的VIEW,别客气。

比如碰到了子弹库来补充弹药:

  public class GunPickItem : MonoBehaviour,IController{public string Name;public int BulletCountInGun;public int BulletCountOutGun;private void OnTriggerEnter2D(Collider2D other){if (other.CompareTag("Player")){this.SendCommand(new PickGunCommand(Name,BulletCountInGun,BulletCountOutGun));}}public IArchitecture GetArchitecture(){return ShootingEditor2D.Interface;}}

条件没问题的话,发送就行。至于这个pickgun要如何运作,有点复杂,但由command交给system去处理,处理之后,发送回一个event,表示枪支变化:

command这样写:

 public class PickGunCommand : AbstractCommand{private readonly string mName;private readonly int mBulletInGun;private readonly int mBulletOutGun;public PickGunCommand(string name, int bulletInGun, int bulletOutGun){mName = name;mBulletInGun = bulletInGun;mBulletOutGun = bulletOutGun;}protected override void OnExecute(){this.GetSystem<IGunSystem>().PickGun(mName, mBulletInGun, mBulletOutGun);}}

system这样写

using System.Collections.Generic;
using System.Linq;
using FrameworkDesign;namespace ShootingEditor2D
{public interface IGunSystem : ISystem{GunInfo CurrentGun { get; }void PickGun(string name, int bulletCountInGun, int bulletCountOutGun); //+}public class OnCurrentGunChanged // +{public string Name { get; set; }}public class GunSystem : AbstractSystem, IGunSystem{protected override void OnInit(){}private Queue<GunInfo> mGunInfos = new Queue<GunInfo>(); // +public GunInfo CurrentGun { get; } = new GunInfo(){BulletCountInGun = new BindableProperty<int>(){Value = 3},BulletCountOutGun = new BindableProperty<int>(){Value = 1},Name = new BindableProperty<string>(){Value = "手枪"},GunState = new BindableProperty<GunState>(){Value = GunState.Idle}};public void PickGun(string name, int bulletCountInGun, int bulletCountOutGun) // +{// 当前枪是同类型if (CurrentGun.Name.Value == name){CurrentGun.BulletCountOutGun.Value += bulletCountInGun;CurrentGun.BulletCountOutGun.Value += bulletCountOutGun;}// 已经拥有这把枪了else if (mGunInfos.Any(info => info.Name.Value == name)){var gunInfo = mGunInfos.First(info => info.Name.Value == name);gunInfo.BulletCountOutGun.Value += bulletCountInGun;gunInfo.BulletCountOutGun.Value += bulletCountOutGun;}else{// 复制当前的枪械信息var currentGunInfo = new GunInfo{Name = new BindableProperty<string>(){Value = CurrentGun.Name.Value},BulletCountInGun = new BindableProperty<int>(){Value = CurrentGun.BulletCountInGun.Value},BulletCountOutGun = new BindableProperty<int>(){Value = CurrentGun.BulletCountOutGun.Value},GunState = new BindableProperty<GunState>(){Value = CurrentGun.GunState.Value}};// 缓存mGunInfos.Enqueue(currentGunInfo);// 新枪设置为当前枪CurrentGun.Name.Value = name;CurrentGun.BulletCountInGun.Value = bulletCountInGun;CurrentGun.BulletCountOutGun.Value = bulletCountOutGun;CurrentGun.GunState.Value = GunState.Idle;// 发送换枪事件this.SendEvent(new OnCurrentGunChanged(){Name = name});}}}
}

SYSTEM中的数据变化如何告知VIEW?——为SYSTEM中的数据套用BindableProperty!

namespace ShootingEditor2D
{public enum GunState{Idle,Shooting,Reload,EmptyBullet,CoolDown}public class GunInfo{[Obsolete("请使用 BulletCountInGame",true)] // 第二个参数改成了 truepublic BindableProperty<int> BulletCount{get => BulletCountInGun;set => BulletCountInGun = value;}public BindableProperty<int> BulletCountInGun;public BindableProperty<string> Name;public BindableProperty<GunState> GunState;public BindableProperty<int> BulletCountOutGun;}
}

也可以设好初始值(但这个和架构无关)

   public GunInfo CurrentGun { get; } = new GunInfo(){BulletCountInGun = new BindableProperty<int>(){Value = 3},BulletCountOutGun = new BindableProperty<int>() // +{Value = 1},Name = new BindableProperty<string>() // +{Value = "手枪"},GunState = new BindableProperty<GunState>() // +{Value = GunState.Idle}};

事件EVENT:作为SYSTEM通知VIEW的方式。VIEW要自己Register

比如:这样

   public class UIController : MonoBehaviour, IController{private IStatSystem mStatSystem;private IPlayerModel mPlayerModel;private IGunSystem mGunSystem;private int mMaxBulletCount;private void Awake(){mStatSystem = this.GetSystem<IStatSystem>();mPlayerModel = this.GetModel<IPlayerModel>();mGunSystem = this.GetSystem<IGunSystem>();// 查询代码mMaxBulletCount = this.SendQuery(new MaxBulletCountQuery(mGunSystem.CurrentGun.Name.Value));this.RegisterEvent<OnCurrentGunChanged>(e =>{mMaxBulletCount = this.SendQuery(new MaxBulletCountQuery(e.Name));}).UnRegisterWhenGameObjectDestroyed(gameObject); // +}

MODEL:作为数据记录层

using System.Collections.Generic;
using FrameworkDesign;namespace ShootingEditor2D
{public interface IGunConfigModel : IModel{GunConfigItem GetItemByName(string name);}public class GunConfigItem{public GunConfigItem(string name, int bulletMaxCount, float attack, float frequency, float shootDistance,bool needBullet, float reloadSeconds, string description){Name = name;BulletMaxCount = bulletMaxCount;Attack = attack;Frequency = frequency;ShootDistance = shootDistance;NeedBullet = needBullet;ReloadSeconds = reloadSeconds;Description = description;}public string Name { get; set; }public int BulletMaxCount { get; set; }public float Attack { get; set; }public float Frequency { get; set; }public float ShootDistance { get; set; }public bool NeedBullet { get; set; }public float ReloadSeconds { get; set; }public string Description { get; set; }}public class GunConfigModel : AbstractModel, IGunConfigModel{private Dictionary<string, GunConfigItem> mItems = new Dictionary<string, GunConfigItem>(){{ "手枪", new GunConfigItem("手枪", 7, 1, 1, 0.5f, false, 3, "默认强") },{ "冲锋枪", new GunConfigItem("冲锋枪", 30, 1, 6, 0.34f, true, 3, "无") },{ "步枪", new GunConfigItem("步枪", 50, 3, 3, 1f, true, 1, "有一定后坐力") },{ "狙击枪", new GunConfigItem("狙击枪", 12, 6, 1, 1f, true, 5, "红外瞄准+后坐力大") },{ "火箭筒", new GunConfigItem("火箭筒", 1, 5, 1, 1f, true, 4, "跟踪+爆炸") },{ "霰弹枪", new GunConfigItem("霰弹枪", 1, 1, 1, 0.5f, true, 1, "一次发射 6 ~ 12 个子弹") },};protected override void OnInit(){}public GunConfigItem GetItemByName(string name){return mItems[name];}}
}

其他好东西:

1.时间冷却系统

            timeSystem.AddDelayTask(0.33f, () =>{gunSystem.CurrentGun.GunState.Value = GunState.Idle;});
using System;
using System.Collections.Generic;
using FrameworkDesign;
using UnityEngine;namespace ShootingEditor2D
{public interface ITimeSystem : ISystem{float CurrentSeconds { get; }void AddDelayTask(float seconds, Action onFinish);}public enum DelayTaskState{NotStart,Started,Finish}public class DelayTask{public float Seconds { get; set; }public Action OnFinish { get; set; }public float StartSeconds { get; set; }public float FinishSeconds { get; set; }public DelayTaskState State { get; set; }}public class TimeSystem : AbstractSystem,ITimeSystem{public class TimeSystemUpdateBehaviour : MonoBehaviour{public event Action OnUpdate;private void Update(){OnUpdate?.Invoke();}}protected override void OnInit(){var updateBehaviourGameObj = new GameObject(nameof(TimeSystemUpdateBehaviour));UnityEngine.Object.DontDestroyOnLoad(updateBehaviourGameObj);// 如果需要销毁,可以缓存为成员变量var updateBehaviour = updateBehaviourGameObj.AddComponent<TimeSystemUpdateBehaviour>();updateBehaviour.OnUpdate += OnUpdate;}public float CurrentSeconds { get;private set; } = 0.0f;private LinkedList<DelayTask> mDelayTasks = new LinkedList<DelayTask>();private void OnUpdate(){CurrentSeconds += Time.deltaTime;if (mDelayTasks.Count > 0){var currentNode = mDelayTasks.First;while (currentNode != null){var delayTask = currentNode.Value;var nextNode = currentNode.Next;if (delayTask.State == DelayTaskState.NotStart){delayTask.State = DelayTaskState.Started;delayTask.StartSeconds = CurrentSeconds;delayTask.FinishSeconds = CurrentSeconds + delayTask.Seconds;} else if (delayTask.State == DelayTaskState.Started){if (CurrentSeconds > delayTask.FinishSeconds){delayTask.State = DelayTaskState.Finish;delayTask.OnFinish.Invoke();delayTask.OnFinish = null;mDelayTasks.Remove(currentNode); // 删除节点}}currentNode = nextNode;}}}public void AddDelayTask(float seconds, Action onFinish){var delayTask = new DelayTask(){Seconds = seconds,OnFinish = onFinish,State = DelayTaskState.NotStart,};mDelayTasks.AddLast(new LinkedListNode<DelayTask>(delayTask));}}
}

2、Query查询类

// 查询代码
var gunConfigModel = this.GetModel<IGunConfigModel>();
var gunConfigItem = gunConfigModel.GetItemByName(mGunSystem.CurrentGun.Name.Value);
mMaxBulletCount = gunConfigItem.BulletMaxCount;

上面的查询显得很臃肿

可以这样:

 mGunSystem = this.GetSystem<IGunSystem>();// 查询代码
mMaxBulletCount = new MaxBulletCountQuery(mGunSystem.CurrentGun.Name.Value).Do(); // -+

做法就是:写一个查询类

using FrameworkDesign;namespace ShootingEditor2D
{public class MaxBulletCountQuery : IBelongToArchitecture,ICanGetModel{private readonly string mGunName;public MaxBulletCountQuery(string gunName){mGunName = gunName;}public int Do(){var gunConfigModel = this.GetModel<IGunConfigModel>();var gunConfigItem = gunConfigModel.GetItemByName(mGunName);return gunConfigItem.BulletMaxCount;}public IArchitecture GetArchitecture(){return ShootingEditor2D.Interface;}}
}

通过一些修改可以直接通过架构Arch来发送查询,做到这样(代码略)

mGunSystem = this.GetSystem<IGunSystem>();// 查询代码
mMaxBulletCount = this.SendQuery(newMaxBulletCountQuery(mGunSystem.CurrentGun.Name.Value)); // -+

3.凉鞋的话

(GamePix 独立游戏学院 - 让独立游戏不再难做 - Powered By EduSoho)欢迎来这里购买决定版的QFRAMEWORK课程。

此文为 决定版群里的聊天记录。

说一下,第一季的整体流程。

课程的最开始,是没有用任何架构就做一个《点点点》这样的项目,做的方式就是用拖拽加一点代码的方式。

但是这种方式有一个问题,就是对象和对象之间的相互访问特别乱,没有规则,也没有限制,这样下去当项目有一定规模了就会变得非常乱。于是就引入了一个规则,就是只有自顶向下的时候可以直接访问对象或者调用对象的方法。然后自底向上的时候使用事件或者委托。而在讲这个规则之前还介绍了对象之间的三种交互方式:方法调用、委托、事件。

然后自底向上和自顶向再加上对象之间的三种交互方式这个构成了一个大的前提,后边的比如层级、业务模块等只要有高低之分的我们就都用这套规则去约束了。

再往下就介绍了一个简单的模块化,介绍了一个单例的模块化。我们在做设计的时候经常听到一个原则,就是高内聚松耦合,意高内聚意思是相同的代码放在一个地方去管理,这个是高内聚,低耦合就是对象之间的引用不要太多,最好就是单向引用或者没有引用,或者是有一定的规则去约束如何互相访问。

再往下就引入了一个概念,就是 Model,Model 是因为什么引入的呢?是因为就是有一些数据,它需要在多个地方去共享,比如角色的攻击力,需要在 UI 界面上显示,或者计算一个伤害的时候需要使用,总之需要在多个地方去使用它,而这种数据就是需要共享的数据,甚至需要把攻击力存储起来,而存储也是一种 共享方式,比如上次游戏关闭到了,现在打开游戏之后角色的攻击力不能变成初始攻击力了,所以数据的存储也是一种共享方式。而这些需要存储的数据,就需要放到 Model 里管理起来。而 Model 在决定版架构的引入就是因为有了需要共享的数据才引入的。

引入完 Model 之后就要考虑一个问题,就是其他的地方怎么跟这个 Model 进行交互,然后交互的部分,一般的方式就是用类似 MVC 的方式,然后其中 MVC 中的 Controller ,它所管理的逻辑分成了交互逻辑和表现逻辑。

大家都说 MVC 中的 Controller 代码容易臃肿起来,那么罪魁祸首就是交互逻辑,只要是有数据操作或者变更游戏数据状态的逻辑都是交互逻辑,而这部分逻辑是非常多的,要想解决 Controller 代码臃肿的问题,一般的共识就是引入命令模式,也就是 Command,让 Command 去分担 Controller 的交互逻辑,Controller 仅仅只是调用相应的 Command 即可。

好多的框架或者方案都是用 Command 去分担交互逻辑的,所以这里就不赘述笔者为啥用 Command 了。

引入了 Command 之后,Controller 就变成了比较薄的一层了,而 Command 并不适合负责所有的交互逻辑,比如有的交互逻辑最好还是统一放在一个对象里,比如分数统计、成就检测、任务检测等,如果分数统计这种逻辑分散在各种 Command 里,会非常不好管理,而分数统计实际上是一种规则类的逻辑,比如打一个敌人得多少分等等,最好是统一放在一个对象里管理,于是就引入了 System 层,System 层就是管理需要统一管理的交互逻辑而引入的,比如成就系统、任务系统等,这些系统一般会写很多死代码,那么这些死代码分散到各个 Command 里,想想都觉得恐怖,所以最好要弄脏就弄脏一个对象就是系统对象,这也是高内聚的一种体现。

到这里架构的一些核心概念就有了雏形了,像事件机制、BindableProperty 等都是一些通用的工具。

再接着架构雏形有了之后,就开始不断打磨这套架构的实现,这部分的内容就是一些 C# 的高级使用方法,用各种技巧达成设计目的,就不多说了。

总之架构中的每一个概念的引入都是为了解决特定的架构问题的,并不是为了做成架构而引入的,然后只要不断地去解决这些架构问题,就会慢慢迭代出来一个比较成型的框架/架构。

最后笔者简单再说一点,就是第一季的内容就是迭代这套架构,在迭代过程中不仅仅只有代码实现的部分,更重要的还是引入这些概念解决哪些问题,所以在学习课程的时候要重点放在概念解决哪些问题上,只要这块了解了,就会对各个概念的使用不会出现问题。

到了第二季 对一些架构本身的问题做了一些改进,比如 BindableProperty 去掉 IEquatable 接口,因为没必要,然后实现了完整的 CQRS 机制,也就是引入了 Query,有了 Query 之后实现充血模型不再是难事。

这篇关于qframework 架构 (作者:凉鞋)使用笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/631324

相关文章

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意