FXGL17 - 元件组成介绍
最编程
2024-07-23 21:47:22
...
PS:禁止拷贝形式转载,转载请以URL形式
PS:FXGL 准备写成一个系列,所以在该系列未完成前,该系列文章除了目录会被修改其他内容均可能被删改。
1. 简介
Entity 实体可以表示游戏里面任意对象但是对象所包含的不同行为、方法可以封装到不同的 Component 组件里,可以看作 Component 组件是具体行为动作的实现。
Component as Behavior
We talked about how adding a component is like adding a field. Well, adding a component is also similar to adding a method. Components allow us to make an entity do something, essentially defining entity behavior. Let's say we want an entity to be a lift, so that it can carry the player to the top of a mountain.
2. 默认组件
PS:本节整理了一些FXGL提供的常用组件使用方法
FXGL提供的默认 components 可以从以下包中查找
- com.almasb.fxgl.dsl.components.*
- com.almasb.fxgl.entity.components.*
2.1. ExpireCleanComponent
过期清除组件:根据传入的等待时间,完成等待时间后自动删除该Component对应的 Entity
protected void initGame() {
FXGL.entityBuilder()
.view(new Rectangle(20, 20, Color.BLUE))
.with(new ExpireCleanComponent(Duration.seconds(2)))
.buildAndAttach();
}
2.2. KeepOnScreenComponent
*保持在屏幕上组件:该Component对应的 Entity 只能显示在屏幕上,不能移动超过屏幕 *
protected void initGame() {
FXGL.entityBuilder()
.view(new Rectangle(20, 20, Color.BLUE))
.with(new KeepOnScreenComponent())
.buildAndAttach();
}
2.3. LiftComponent
电梯组件:可以控制组件在 X | Y方向上运动
-
xAxisSpeedDuration(double speed, Duration duration)
:控制X方向行进的速度 -
yAxisSpeedDuration(double speed, Duration duration)
:控制Y方向行进的速度
protected void initGame() {
FXGL.entityBuilder()
.viewWithBBox(new Rectangle(20, 20, Color.BLUE))
.at(FXGL.getAppCenter())
.with(new LiftComponent().xAxisSpeedDuration(-150, Duration.seconds(1)))
.buildAndAttach();
FXGL.entityBuilder()
.viewWithBBox(new Rectangle(20, 20, Color.RED))
.at(FXGL.getAppCenter())
.with(new LiftComponent().xAxisSpeedDuration(150, Duration.seconds(1)))
.buildAndAttach();
FXGL.entityBuilder()
.viewWithBBox(new Rectangle(20, 20, Color.GREEN))
.at(FXGL.getAppCenter())
.with(new LiftComponent().yAxisSpeedDuration(-150, Duration.seconds(1)))
.buildAndAttach();
FXGL.entityBuilder()
.viewWithBBox(new Rectangle(20, 20, Color.AQUA))
.at(FXGL.getAppCenter())
.with(new LiftComponent().yAxisSpeedDuration(150, Duration.seconds(1)))
.buildAndAttach();
}
2.4. DraggableComponent
拖动组件:所属组件可以被鼠标选择拖动
protected void initGame() {
FXGL.entityBuilder()
.viewWithBBox(new Rectangle(20, 20, Color.BLUE))
.at(FXGL.getAppCenter())
.with(new DraggableComponent())
.buildAndAttach();
}
3. 自定义组件
-
Component
:组件必须继承该基类 -
onAdded()
:组件被添加时调用方法 -
onUpdate()
:游戏刷新时会调用该方法 -
onRemoved()
:组件被删除时调用方法
public class CustomComponent extends Component{
@Override
public void onAdded() {
System.out.println("CustomComponent.onAdded");
}
@Override
public void onUpdate(double tpf) {
System.out.println("CustomComponent.onUpdate");
}
@Override
public void onRemoved() {
System.out.println("CustomComponent.onRemoved");
}
}
推荐阅读
-
探究大疆无人机的内部构造,揭示其关键组成元件
-
FXGL17 - 元件组成介绍
-
异步编程RxJava-介绍-前言 前段时间写了一篇对协程的一些理解,里面提到了不管是协程还是callback,本质上其实提供的是一种异步无阻塞的编程模式;并且介绍了java中对异步无阻赛这种编程模式的支持,主要提到了Future和CompletableFuture;之后有同学在下面留言提到了RxJava,刚好最近在看微服务设计这本书,里面提到了响应式扩展(Reactive extensions,Rx),而RxJava是Rx在JVM上的实现,所有打算对RxJava进一步了解。 RxJava简介 RxJava的官网地址:https://github.com/ReactiveX/RxJava, 其中对RxJava进行了一句话描述:RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM. 大意就是:一个在Java VM上使用可观测的序列来组成异步的、基于事件的程序的库。 更详细的说明在Netflix技术博客的一篇文章中描述了RxJava的主要特点: 1.易于并发从而更好的利用服务器的能力。 2.易于有条件的异步执行。 3.一种更好的方式来避免回调地狱。 4.一种响应式方法。 与CompletableFuture对比 之前提到CompletableFuture真正的实现了异步的编程模式,一个比较常见的使用场景: CompletableFuture<Integer> future = CompletableFuture.supplyAsync(耗时函数); Future<Integer> f = future.whenComplete((v, e) -> { System.out.println(v); System.out.println(e); }); System.out.println("other..."); 下面用一个简单的例子来看一下RxJava是如何实现异步的编程模式: Observable<Long> observable = Observable.just(1, 2) .subscribeOn(Schedulers.io).map(new Func1<Integer, Long> { @Override public Long call(Integer t) { try { Thread.sleep(1000); //耗时的操作 } catch (InterruptedException e) { e.printStackTrace; } return (long) (t * 2); } }); observable.subscribe(new Subscriber<Long> { @Override public void onCompleted { System.out.println("onCompleted"); } @Override public void onError(Throwable e) { System.out.println("error" + e); } @Override public void onNext(Long result) { System.out.println("result = " + result); } }); System.out.println("other..."); Func1中以异步的方式执行了一个耗时的操作,Subscriber(观察者)被订阅到Observable(被观察者)中,当耗时操作执行完会回调Subscriber中的onNext方法。 其中的异步方式是在subscribeOn(Schedulers.io)中指定的,Schedulers.io可以理解为每次执行耗时操作都启动一个新的线程。 结构上其实和CompletableFuture很像,都是异步的执行一个耗时的操作,然后在有结果的时候主动告诉我结果。那我们还需要RxJava干嘛,不知道你有没有注意,上面的例子中其实提供2条数据流[1,2],并且处理完任何一个都会主动告诉我,当然这只是它其中的一项功能,RxJava还有很多好用的功能,在下面的内容会进行介绍。 异步观察者模式 上面这段代码有没有发现特别像设计模式中的:观察者模式;首先提供一个被观察者Observable,然后把观察者Subscriber添加到了被观察者列表中; RxJava中一共提供了四种角色:Observable、Observer、Subscriber、Subjects Observables和Subjects是两个被观察者,Observers和Subscribers是观察者; 当然我们也可以查看一下源码,看一下jdk中的Observer和RxJava的Observer jdk中的Observer: public interface Observer { void update(Observable o, Object arg); } RxJava的Observer: public interface Observer<T> { void onCompleted; void onError(Throwable e); void onNext(T t); } 同时可以发现Subscriber是implements Observer的: public abstract class Subscriber<T> implements Observer<T>, Subscription 可以发现RxJava中在Observer中引入了2个新的方法:onCompleted和onError onCompleted:即通知观察者Observable没有更多的数据,事件队列完结 onError:在事件处理过程中出异常时,onError会被触发,同时队列自动终止,不允许再有事件发出。 正是因为RxJava提供了同步和异步两种方式进行事件的处理,个人觉得异步的方式更能体现RxJava的价值,所以这里给他命名为异步观察者模式。 好了,下面正式介绍RxJava的那些灵活的操作符,这里仅仅是简单的介绍和简单的实例,具体用在什么场景下,会在以后的文章中介绍 Maven引入
-
触摸屏原理介绍--电极:选用导电性极佳的材料(如银粉油墨)组成,其导电性约为 ITO 的 1000 倍。
-
RCWL-0516/RCWL-9196模块简介 & 微波感应模块简介-前言 RCWL-0516是一款由无锡日晨物联科技有限公司开发的微波感应模块(资料下载),见图0.0、图0.1,用于检测物体(人体)移动,具有以下特征: 1.穿透感应:可穿透适当厚度的玻璃、木板以及墙壁。 2.抗干扰:不受温度、灰尘等环境因素影响。 3.感应距离:5~8m(可调,见后文) 4.可重复触发、触发时间可调(见后文) 5.工作电压:3.3~18V 6.稳压输出:提供3.3V电压输出(最大100mA) 7.夜晚自动工作:外接光敏电阻和一个电阻实现 当模块检测到物体在感应范围内移动时,OUT引脚输出一段时间的高电平(该时间可通过电容“C-TM”调节,见后文);若在输出高电平期间再次检测到物体移动,高电平持续时间将延长一段时间(又称为重复触发),该时间不可叠加。 模块使用的注意事项如下,示意图见图0.2: 1. 感应面正前方不能有金属遮挡。 2. 感应面前后方预留2cm以上空间。若对灵敏度要求很高,应预留4cm以上距离,且模块后方遮挡空间应尽可能小。 3. 模块与安装载体平面尽可能平行。 4. 有元器件面为正感应面,反面为负感应面,负感应面效果略差。 5. 相同模块,单个个体之间间距应大于2m。 图0.0-模块实物图(正) 图0.1-模块实物图(反) 图0.2-感应区域示意图 原理 关于此模块的原理,有2种主流观点,这些观点所争论的焦点在于哪种解释是最主要的: 1. 以Roger Clark为代表的“反射”解释:模块上的振荡器会发射出微波信号,位于模块感应区域内的物体会反射模块所发出的微波信号,这些反射信号又被模块所接收,接收到的反射信号会改变流经晶体管发射极的电流I。外界环境不变的情况下,模块内部的调节电路会稳定振荡器,此时振荡器处于稳定状态,电流I也处于稳定;当外界环境发生变化(例如,有物体进入感应区域),该物体的反射信号会使振荡器暂时失去稳定,从而导致电流I发生变化。模块通过检测该电流I的变化,以检测物体移动。此过程中,发射频率的变化只是由于振荡器受反射信号影响而进入一个“暂稳态”所导致。 2.以Joe Desbonnet为代表的“多普勒效应”解释:位于模块感应区域内的物体会反射模块所发出的微波信号,这些反射信号的频率由于物体移动而发生改变(多普勒效应)。模块通过对比发射与反射频率的差异,以判断是否有物体进入感应区域。 应用 降低感应距离:模块背面丝印“R-GN”处添加1MΩ的电阻,模块的感应距离可降低到5m;如果不接,感应距离为7m。 调节触发时间:模块背面丝印“C-TM”处添加不同容值的电容,可以调节触发时间(“C-TM”电容容值的选择见后文);若不安装电容,触发时间为2~4s。 夜晚自动工作:模块正面丝印“CDS”处添加光敏电阻、模块背面丝印“R-CDS”处添加适当阻值的电阻,可控制模块在夜晚自动工作。“CDS”与“R-CDS”的选择方法见后文。 以上应用的实际电路请参考图1.0、图1.1。 图1.0-测试电路(正) 图1.1-测试电路(反) 测试 测试由5部分组成: 1.测量模块处于不同状态时的功耗,见表0.0。 2.未接入电阻“R-GN”时,测试模块最大感应距离,见表0.1。 3.接入电阻“R-GN”,测试模块最大感应距离,见表0.2。 4.以下步骤将介绍如何根据确定的光敏电阻“CDS”,选择电阻“R-CDS”的阻值,以实现模块夜间自动工作的功能。 1-白天,接入可调电阻“R-CDS”(推荐2MΩ)、光敏电阻“CDS”。 2-触发模块后(在模块面前走动),调节可调电阻,直到触发消失。再次尝试触发模块,正常情况下,模块应该无法被触发(如果可以触发,重复步骤2)。 3-将光敏电阻感光面遮住,尝试触发模块,正常情况下,模块应该可以被触发(如果无法触发,重复步骤3)。 4-此时可调电阻阻值即为电阻“R-CDS”的正确阻值。 5.电容“C-TM”分别接入不同容值的无极电容,测试模块单次触发所持续的时间,见表0.3。 测试条件 总电流(mA) 总功耗(mW) +5V供电电压,模块未触发 3.63 18.15 +5V供电电压,模块被触发 4.33 21.65 表0.0-模块功耗信息 正面最大感应距离(M) 6 反面最大感应距离(M) 2 表0.1-未接入电阻“R-GN”时,模块最大感应距离[1] 正面最大感应距离(M) 5 反面最大感应距离(M) 1 表0.2-接入电阻“R-GN”=1MΩ时,模块最大感应距离[1] 电容“C-TM”容值 悬空 103(10nF) 104(100nF) 224(220nF) 474(470nF) 105(1uF) 理论单次触发时间(s) 2~4 6 30 66 140 300 实际单次触发时间(s) 3 6 32 67 122 210 表0.3-电容“C-TM”容值 vs. 模块单次触发持续时间 结论 RCWL-0516是一款性价比高的人体感应模块,具有以下优缺点: 优点:
-
卷积神经网络的结构组成与解析(详细介绍)
-
PLUG 的 NLP:阿里达摩院发布国内最大中文预训练语言模型 PLUG 的介绍、架构组成、模型训练、使用详解攻略
-
Hadoop 架构原理,三个组成部分的详细介绍(注释)
-
openEuler郑州用户组成立!openEuler与hyperfusion携手共建河南地区用户生态 - 开幕致辞 超融合操作系统业务总经理、openEuler委员会成员蒋振华先生为本次活动致辞。 在本次活动的致辞中,他提到,作为openEuler社区早期的成员,超融合见证了openEuler从成立到在各行业商业落地,再到跨越生态拐点的过程,感谢openEuler提供了一个全产业链共同创新的平台,共同推动创新技术的商业落地。 同时,本次活动得到了郑州市郑东新区大数据管理局、郑州中原科技城投资服务局的大力支持。 郑东新区大数据管理局曹光远 在活动致辞中表示,openEuler的应用和*应用设施的深度优化,为郑东新区数字化转型提供了安全、可靠、高性能的技术基础;郑州中原科技城招商服务局王林表示,郑东新区欢迎所有openEuler生态相关企业扎根当地,围绕openEuler社区共同发展,形成合力。 openEuler社区及运维功能介绍 openEuler技术委员会委员胡峰 openEuler技术委员会委员胡峰先生在本次活动中介绍了openEuler社区目前发展的整体情况,并重点从技术层面介绍了openEuler的运维功能。 openEuler 晚会 胡峰先生介绍智能运维工具 A-Ops 和 openEuler gala、 阿波罗 Apollo、智能漏洞管理解决方案等新功能,以及涵盖各种运维场景的精品运维组件。在*交流环节,许多用户就目前使用的 openEuler 在*交流环节,许多用户就自己在使用openEuler过程中遇到的一些问题与胡峰先生进行了进一步的交流。 软硬结合,构建多样化算力操作系统 Hyperfusion 基于 openEuler 的基础上,结合自身软硬件技术积累,推出了富讯服务器操作系统 FusionOS FusionOS. FusionOS 首席架构师张海亮 分享了 FusionOS FusionOS首席架构师张海亮分享了FusionOS的软硬件协同优势、卓越的性能和可靠性,以及FusionOS在金融、运营商、*、互联网等行业的实践案例,引起了众多用户的兴趣,分享结束后,不少参会者就FusionOS的特点向讲师提问并进行了交流。
-
纯干货分享 | 研发效能提升——敏捷需求篇-而敏捷需求是提升效能的方式中不可或缺的模块之一。 云智慧的敏捷教练——Iris Xu近期在公司做了一场分享,主题为「敏捷需求挖掘和组织方法,交付更高业务价值的产品」。Iris具有丰富的团队敏捷转型实施经验,完成了企业多个团队从传统模式到敏捷转型的落地和实施,积淀了很多的经验。 这次分享主要包含以下2个部分: 第一部分是用户影响地图 第二部分是事件驱动的业务分析Event driven business analysis(以下简称EDBA) 用户影响地图,是一种从业务目标到产品需求映射的需求挖掘和组织的方法。 在软件开发过程中可能会遇到一些问题,比如大家使用不同的业务语言、技术语言,造成角色间的沟通阻碍,还会导致一些问题,比如需求误解、需求传递错误等;这会直接导致产品的功能需求和要实现的业务目标不是映射关系。 但在交付期间,研发人员必须要将这些需求实现交付,他们实则并不清楚这些功能需求产生的原因是什么、要解决客户的哪些痛点。研发人员往往只是拿到了解决方案,需要把它实现,但没有和业务侧一起去思考解决方案是否正确,能否真正的帮助客户解决问题。而用户影响地图通常是能够连接业务目标和产品功能的一种手段。 我们在每次迭代里加入的假设,也就是功能需求。首先把它先实现,再逐步去验证我们每一个小目标是否已经实现,再看下一个目标要是什么。那影响地图就是在这个过程中帮我们不断地去梳理目标和功能之间的关系。 我们在软件开发中可能存在的一些问题 针对这些问题,我们如何避免?先简单介绍做敏捷转型的常规思路: 先做团队级的敏捷,首先把产品、开发、测试人员,还有一些更后端的人员比如交互运维的同学放在一起,组成一个特训团队做交付。这个团队要包含交付过程中所涉及的所有角色。 接着业务敏捷要打通整个业务环节和研发侧的一个交付。上图中可以看到在敏捷中需求是分层管理的,第一层是业务需求,在这个层级是以用户目标和业务目标作为输入进行规划,同时需要去考虑客户的诉求。业务人员通过获取到的业务需求,进一步的和团队一起将其分解为产品需求。所以业务需求其实是我们真正去发布和运营的单元,它可以被独立发布到我们的生产环境上。我们的产品需求其实就是产品的具体功能,它是我们集成和测试的对象,也就是我们最终去部署到系统上的一个基本单元。产品需求再到了我们的开发团队,映射到迭代计划会上要把它分解为相应的技术任务,包括我们平时所说的比如一些前端的开发、后端的开发、测试都是相应的技术任务。所以业务敏捷要达到的目标是需要去持续顺畅高质量的交付业务价值。 将这几个点串起来,形成金字塔结构。最上层我们会把业务目标放在整个金字塔的塔尖。这个业务目标是通过用户的目标以及北极星指标确立的。确认业务目标后再去梳理相应的业务流程,最后生产。另外产品需求包含了操作流程和业务规则,具需求交付时间、工程时间以及我们的一些质量标准的要求。 谈到用户影响的地图,在敏捷江湖上其实有一个传说,大家都有一个说法叫做敏捷需求的“任督二脉”。用户影响地图其实就是任脉,在黑客马拉松上用过的用户故事地图其实叫督脉。所以说用户影响地图是在用户故事地图之前,先帮我们去梳理出我们要做哪些东西。当我们真正识别出我们要实现的业务活动之后,用户故事地图才去梳理我们整个的业务工作流,以及每个工作流节点下所要包含的具体功能和用户故事。所以说用户影响地图需要解决的问题,我们包括以下这些: 首先是范围蔓延,我们在整张地图上,功能和对应的业务目标是要去有一个映射的。这就避免了一些在我们比如有很多干系人参与的会议上,那大家都有不同想法些立场,会提出很多需求(正确以及错误的需求)。这个时候我们会依据目标去看这些需求是否真的是会影响我们的目标。 这里提到的错误需求,比如是利益相关的人提出的、客户认为产品应该有的、某个产品经理需求分析师认为可以有的....但是这些功能在用户影响地图中匹配不到对应目标的话,就需要降低优先级或弃掉。另外,通常我们去制定解决方案的时候,会考虑较完美的实现,导致解决方案括很多的功能。这个时候关键目标至关重要,会帮助我们梳理筛选、确定优先级。 看一下用户影响到地图概貌 总共分为一个三层的结构: 第一层why,你的业务目标哪个是最重要的,为什么?涉及到的角色有哪些? 第二层how ,怎样产生影响?影响用户角色什么样的行为? (不需要去列出所有的影响,基于业务目标) 第三层what,最关键的是在梳理需求时不需一次把所有细节想全,这通常团队中经常遇到的问题。 我们用这个例子来看一下 这是一个客服中心的影响地图,业务目标是 3个月内不增加客服人数的前提下能支持1.5倍的用户数。此业务目标设定是符合 smart 原则的,specific非常的具体,miserable 是可以衡量的,action reoriented是面向活动的, real list 也是很实际的。 量化的目标会指引我们接下来的行动,梳理一个业务目标,尽量去量化,比如 :我们通过打造一条什么样的流水线,能够提高整个部署的效率,时间是原来的 1/2 。这样才是一个能量化的有意义的目标。 回到这幅图, how 层级识别出来的内容,客服角色:想要对它施加的影响,把客户引导到论坛上,帮助客户更容易的跟踪问题,更快速的去定位问题。初级用户:方论坛上找到问题。高级用户:在论坛上回答问题。通过我们这些用户角色,进行活动,完成在不增加客户客服人数的前提下支持更多的用户数量。 最后一个层级,才是我们日常接触比较多的真正的功能的特性和需求,比如引导到客户到论坛上,其实这个产品就需要有一个常见问题的论坛的链接。这个层次需要我们团队进一步地在交付,在每个迭代之前做进一步的梳理,细化成相应的用户故事。 这个是云智慧团队中,自己做的影响地图的范例,可以看下整个的层级结构。序号表示优先级。 那我们用户影响地图可以总结为: