当前位置: 首页 > news >正文

企业网站目的湖南营销型网站建设公司

企业网站目的,湖南营销型网站建设公司,工业产品设计软件,张家港杨舍网站建设1、模式标准 模式名称#xff1a;装饰器模式 模式分类#xff1a;结构型 模式意图#xff1a;动态地给一个对象添加一些额外的职责。就增加功能来说#xff0c;装饰器模式比生成子类更为灵活。 结构图#xff1a; 适用于#xff1a; 当需要给一个对象在运行时添加更…1、模式标准 模式名称装饰器模式 模式分类结构型 模式意图动态地给一个对象添加一些额外的职责。就增加功能来说装饰器模式比生成子类更为灵活。 结构图 适用于 当需要给一个对象在运行时添加更多的责任时。当需要通过组合和封装来添加功能而不是通过继承来添加功能时。 主要成员 抽象组件Component这是一个接口定义了一个对象可以接受额外责任的方式。它是所有对象主对象以及装饰器对象共享的接口。 具体组件Concrete Component这是抽象组件的具体实现。它定义了一个具体的对象可以给这个对象添加一些职责。 抽象装饰类Decorator这是所有装饰器类的基类它定义了一个与抽象组件接口一致的接口这样它就可以装饰所有的具体组件。抽象装饰类通常有一个成员变量用来保存一个抽象组件的引用。 具体装饰类Concrete Decorator具体装饰类是抽象装饰类的子类它定义了具体的装饰效果。每个具体装饰类都增加了一些新的行为或状态。具体装饰类可以通过调用抽象组件的方法并添加新的行为或状态来扩展抽象组件的功能。 2、分析与设计 在开发web应用时用惯了mvc但开发游戏时才发现mvc不够用了。web开发只要处理完用户的当前请求就完事了。但游戏开发不同游戏里是一个迷你世界和我们现实有很多相似之处。现实世界里面有各种各样的系统每个生物都借助外部系统或内部系统进行着各项活动。前辈们把它们抽象成 ecs(  entity实体  component组件 system系统 )这的确大大简化了游戏开发。比如给玩家添加一个登录组件玩家就有了登录功能登录完成后卸载组件释放资源。给玩家添加一个遥感组件玩家就有了操控摇杆的功能游戏结束时移除摇杆组件等等。 回到我们的设计模式ecs算装饰器模式吗看适用性上是非常相似的 看看GPT是如何回答的 实体组件系统Entity-Component-SystemECS模式和装饰器模式在某些方面有一些相似之处尤其是在它们都允许在运行时动态添加和删除对象的行为方面。然而它们的核心目标和实现方式是不同的所以它们并不完全相同。 ECS模式是一种常用于游戏开发的架构模式它的核心思想是通过组合而非继承来实现对象的行为。在ECS模式中实体Entity是一个标识符组件Component包含数据系统System则包含行为。实体通过添加或删除组件来改变其行为而系统则负责处理具有特定组件的实体。这种模式的主要优点在于它的灵活性和可扩展性因为你可以通过简单地添加或删除组件来改变实体的行为。 装饰器模式的目标是允许我们在运行时向对象添加新的行为而不改变其类的定义。它通过将对象封装在装饰器对象中来实现这一点装饰器对象实现了与原对象相同的接口并在调用原对象的方法之前或之后添加新的行为。 所以尽管ECS模式和装饰器模式都允许动态改变对象的行为但它们的实现方式和目标是不同的。在ECS模式中实体没有固定的行为而是由其组件和系统决定的。而在装饰器模式中对象的基本行为是由其类定义的装饰器只是添加或修改这些行为。此外ECS模式中的实体并不需要实现与其组件一致的接口这是它与装饰器模式的另一个主要区别。 从GPT的分析中已经给出了ecs模式是给一个实体标识符添加和删除组件来改变其行为。装饰器模式对象本身是一个类且有其基本的行为装饰器只是在其基础上拓展功能。 细心点的人可能早已发现装饰器模式在cocos creator中其实已有通过的node,component为其实现了需要什么功能挂载一个组件这不就是装饰器吗接下来就修改一下我们的意图。 意图动态地给一个对象cc.node添加一些额外的职责(功能component)。相关的功能也可以随着需求的变化而从对象cc.node中删除。 3、开始打造 export namespace CCObject {export enum Flags {....}export let __props__: string[];export let __values__: string[]; } export class Node extends CCObject implements ISchedulable, CustomSerializable {addComponentT extends Component(classConstructor: __private._types_globals__ConstructorT): T;} export class Component extends CCObject {node: Node;addComponentT extends Component(classConstructor: __private._types_globals__ConstructorT): T | null;} 4、开始使用  用过cocos creator 相信大家都应该很熟悉cocos的组件使用了 这里举几个和设计模式结构图类似的例子 this.node.getComponent(Label).string 123 this.node.getChildByName(progress).getComponent(ProgressBar).progress 456 this.getComponent(UnitItem).addComponent(Label).string 789 5、其他-打造ECS 因为是游戏框架且打算使用ecs模式就在装饰器模式的下面贴出一个简易的ecs虽然ecs和装饰器模式不完全相同只是相似 在ECS模式中实体Entity是一个标识符组件Component包含数据系统System则包含行为。 从概念上实体只是一个标识符没有具体的行为实体通过添加或删除组件来改变其行为而系统则负责处理具有特定组件的实体。 import { Comp } from ./Comp;export class Entity {private static _entities: Mapnumber, Entity new Map();private static nextEntityId 0;// 添加一个公共的 getter 方法来获取 entitiesstatic get entities(): Mapnumber, Entity {return this._entities;}static createEntity(): Entity {const entity new Entity();this.entities.set(entity.id, entity);return entity;}static removeEntity(entity: Entity): void {for (let component of entity.components.values()) {Comp.removeComp(component)}this.entities.delete(entity.id);}static getEntity(entityId: number): Entity | undefined {return this.entities.get(entityId);}static generateEntityId(): number {return this.nextEntityId;}/** 单实体上挂载的组件 */public components: Mapnew () any, Comp new Map();/** 实体id */public readonly id: number;constructor() {this.id Entity.generateEntityId();this.components new Map();}/** 单实体上挂载组件 */attachComponentT extends Comp(componentClass: new () T): T {const hascomponent this.components.get(componentClass) as T;if (hascomponent) {console.error(已存在组件,不会触发挂载事件)return hascomponent;} else {const component Comp.createComp(componentClass, this);this.components.set(componentClass, component);// console.log(实体挂载了组件, this.components, this)return component;}}/** 单实体上卸载组件 */detachComponentT extends Comp(componentClass: new () T): void {const component this.components.get(componentClass);if (component) {this.components.delete(componentClass);Comp.removeComp(component)// console.log(实体卸载了组件, this.components, this)}}getComponentT extends Comp(componentClass: new () T): T | undefined {return this.components.get(componentClass) as T;}} import { Entity } from ./Entity;/*** 组件*/ export abstract class Comp {/*** 组件池*/private static compsPool: Mapnew () any, Comp[] new Map();/*** 创建组件* param compClass * returns */public static createCompT extends Comp(compClass: new () T, entity: Entity): T {// 获取对应组件类的池子let pool this.compsPool.get(compClass);// 如果池子不存在为组件类创建一个新的空池子if (!pool) {pool [];this.compsPool.set(compClass, pool);}// 如果池子中有实例则取出并返回否则创建一个新实例并返回let comp pool.length 0 ? pool.pop() as T : new compClass();comp.entity entitysetTimeout(() {comp.onAttach(entity); // 延迟0,防止属性数据未初始化赋值就已经执行挂载}, 0)return comp}static removeComp(comp: Comp) {comp.onDetach(comp.entity);comp.entity nullcomp.reset();// 获取组件实例的构造函数const compClass comp.constructor as new () Comp;// 从组件池中找到对应的构造函数对应的池子const pool this.compsPool.get(compClass);// 如果池子存在将组件实例放回池子中if (pool) {pool.push(comp);} else {// 如果池子不存在创建一个新的池子并将组件实例放入this.compsPool.set(compClass, [comp]);}}/*** 单体组件的实体*/public entity: Entity | null null;/** * 组件挂载并初始化后的回调*/abstract callback: Function;/** 监听挂载到实体 */abstract onAttach(entity: Entity): void/** 监听从实体卸载 */abstract onDetach(entity: Entity): void/** 重置 */abstract reset(): void } export abstract class System {update?(dt: number);// 为了简化系统系统内方法都是静态方法,直接调用 } 6、其他-使用ECS main.ts 挂载在root下面 onLoad() {window[xhgame] xhgame // 方便console中查看全局const gameDesign new GameDesign();switch (this.gameCode) {case demo: // demogameDesign.setGameBuilder(new DemoGameBuilder(this));gameInstance.game gameDesign.buildGameDemoGame()break;}gameInstance.game.start()// 添加有update的系统临时放置xhgame.game.updateSystems.push(GameMoveSystem)}protected update(dt: number): void {if (xhgame.game xhgame.game.updateSystems.length 0) {for (const system of xhgame.game.updateSystems) {const _system system as System_system.update _system.update(dt);}}} // 创建一个player实体const player_entity Entity.createEntity();ooxh.game.playerEntity player_entityplayer_entity.attachComponent(PlayerStateComp) // 状态组件player_entity.attachComponent(PlayerLoginComp) // 登录组件player_entity.attachComponent(PlayerTouchMoveComp) // 触控组件 export class BattleInitSystem extends System {// 开始初始化战役static startInit(comp: BattleInitComp, callback: Function) {this.showUnit() // 单位callback callback()}// 显示各种单位static showUnit() {...} }export class BattleInitComp extends Comp {callback: Function nullreset() {this.callback null}onAttach(entity: Entity) {BattleInitSystem.startInit(this, () {this.callback this.callback()})}onDetach(entity: Entity) {} } ooxh.game.battleEntity.attachComponent(BattleInitComp).callback () {ooxh.game.battleEntity.detachComponent(BattleInitComp) }
http://www.pierceye.com/news/99577/

相关文章:

  • 静海集团网站建设住房城乡建设网站
  • 个人备案挂企业网站网站开发公司照片
  • 网站建设课程体会国内最新新闻简短
  • 网站开发大概价格最常用的网页制作软件
  • 商务网站模块设计时前台基础设施建设免费网站建设空间
  • 青海省公路工程建设总公司网站饮料公司网站模板
  • 建设部网站刘赵云网页版邮箱
  • 免费扑克网站企业网站怎么搜索优化
  • 做网站导航的厦门网站建设制作多少钱
  • 怎样免费注册网站域名鹤城建设集团网站
  • 3合1网站建设价格网站建设论坛快速建站
  • 怎样做钓鱼网站上海网站关键词排名优化报价
  • 昆明专业网站设计公司电商类网站设计模板
  • 网站流量用完了重庆网站推广
  • 网站管理助手数据库网站在建设中无法访问
  • 网站标题格式建设网站南昌
  • wordpress作企业网站好吗沈阳短视频制作公司
  • 表格网站怎么做的作文网站大全
  • 比特币网站建设专业网站建设企业网站制作
  • 故宫博物院官网网站咋做的山东省济宁市最新消息
  • 天河营销型网站建设html网页设计代码作业正能量
  • 国外网站设计欣赏智能获客系统
  • 济南网站建设599网站建设完工后在什么科目核算
  • 学校网站的作用app营销推广方式
  • 怎么做网站互换链接重庆工程建设信息网官网查询
  • 刚开始做网站要传数据库吗赛迪建设网站
  • 网站网络推广教程手机html网站开发视频
  • 网站弹出广告代码口碑好的龙岗网站建设
  • 东莞网站建设-搜盟网电商的运营推广
  • 美发网站 源代码网站建设的博客