libgdx ashley框架的讲解

2024-06-08 21:52
文章标签 讲解 框架 libgdx ashley

本文主要是介绍libgdx ashley框架的讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官网:https://github.com/libgdx/ashley

我的libgdx学习代码:nanshaws/LibgdxTutorial: libgdx 教程项目 本项目旨在提供完整的libgdx桌面教程,帮助开发者快速掌握libgdx游戏开发框架的使用。成功的将gdx-ai和ashley的tests从官网剥离出来,并成功运行。libgdx tutorial project This project aims to provide a complete libgdx desktop tutorial to help developers quickly master the use of libgdx game development framework. Successfully separated GDX-AI and Ashley's tests from the official website and ran them (github.com)

引入依赖:

allprojects {apply plugin: "eclipse"version = '1.0'ext {appName = "My GDX Game"gdxVersion = '1.12.1'roboVMVersion = '2.3.21'box2DLightsVersion = '1.5'ashleyVersion = '1.7.4'aiVersion = '1.8.2'gdxControllersVersion = '2.2.1'}repositories {mavenLocal()mavenCentral()google()gradlePluginPortal()maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }maven { url "https://oss.sonatype.org/content/repositories/releases/" }maven { url "https://jitpack.io" }}
}dependencies {implementation "com.badlogicgames.gdx:gdx:$gdxVersion"implementation "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"implementation "com.badlogicgames.ashley:ashley:$ashleyVersion"testImplementation "junit:junit:4.12"
}

在这个ashley框架中,分为

Component
EntitySystem
PooledEngine
EntityListener

好理解吧。以下我用代码来演示

PooledEngine engine = new PooledEngine();MovementSystem movementSystem = new MovementSystem();PositionSystem positionSystem = new PositionSystem();engine.addSystem(movementSystem);engine.addSystem(positionSystem);Listener listener = new Listener();engine.addEntityListener(listener);Entity entity = engine.createEntity();entity.add(new PositionComponent(10, 0));
  1. PooledEngine engine = new PooledEngine();
    这行代码创建了一个 PooledEngine 的实例。PooledEngine 是 Engine 的一个子类,它可以重用实体和组件,从而减少内存分配和垃圾回收,提高性能。

  2. MovementSystem movementSystem = new MovementSystem();
    创建了一个 MovementSystem 的实例,这是一个自定义的系统,用于处理实体的移动逻辑。

  3. PositionSystem positionSystem = new PositionSystem();
    创建了一个 PositionSystem 的实例,这是另一个自定义的系统,用于处理实体的位置更新。

  4. engine.addSystem(movementSystem);
    engine.addSystem(positionSystem);
    这两行代码将 MovementSystem 和 PositionSystem 添加到 PooledEngine 中。这样,当引擎更新时,这些系统也会被更新。

  5. Listener listener = new Listener();
    创建了一个 Listener 的实例,这是一个实体监听器,它会在实体被添加或移除时收到通知。

  6. engine.addEntityListener(listener);
    将 Listener 添加到 PooledEngine 中,使其成为实体事件的监听器。

  7. Entity entity = engine.createEntity();
    创建了一个新的 Entity 实例。在Ashley中,实体是组件的容器,组件用于存储数据。

  8. entity.add(new PositionComponent(10, 0));
    向刚创建的实体添加了一个 PositionComponent 实例,初始化位置为 (10, 0)。PositionComponent 是一个自定义的组件,用于存储实体的位置信息。

每个人物或者标签都可以称之为实体,比如说一个马里奥游戏,马里奥、乌龟和金币都可以被视为实体。每个实体都可以拥有一组组件,这些组件定义了实体的数据和状态。例如,马里奥可能有位置组件(PositionComponent)、移动组件(MovementComponent)和图形组件(GraphicsComponent)等。

这里的实体就是Entity entity = engine.createEntity(); 实体添加组件就是entity.add(new PositionComponent(10, 0)); 而PositionSystem就是各个组件合在一起的逻辑原理

  1. private ComponentMapper<PositionComponent> pm = ComponentMapper.getFor(PositionComponent.class);
    这行代码创建了一个 ComponentMapper 对象,专门用于 PositionComponent 类型的组件。这意味着你可以通过这个映射器快速访问任何实体的 PositionComponent

  2. private ComponentMapper<MovementComponent> mm = ComponentMapper.getFor(MovementComponent.class);
    类似地,这行代码创建了一个 ComponentMapper 对象,专门用于 MovementComponent 类型的组件。这使得你可以快速访问任何实体的 MovementComponent

在MovementSystem里面的两行代码,将每个实体里面的MovementComponent和PositionComponent组件都进行移动。这样的例子在我的libgdx学习代码的

gdx-ashley-tests

 

里面的RenderSystemTest文件,运行起来会让一百个硬币移动

@Overridepublic void update (float deltaTime) {for (int i = 0; i < entities.size(); ++i) {Entity e = entities.get(i);PositionComponent p = pm.get(e);MovementComponent m = mm.get(e);p.x += m.velocityX * deltaTime;p.y += m.velocityY * deltaTime;}log(entities.size() + " Entities updated in MovementSystem.");}

演示一个简单案例吧

MovementComponent
package com.badlogic.ashley.tests.components;import com.badlogic.ashley.core.Component;public class MovementComponent implements Component {public float velocityX;public float velocityY;public MovementComponent (float velocityX, float velocityY) {this.velocityX = velocityX;this.velocityY = velocityY;}
}
PositionComponent
package com.badlogic.ashley.tests.components;import com.badlogic.ashley.core.Component;public class PositionComponent implements Component {public float x, y;public PositionComponent (float x, float y) {this.x = x;this.y = y;}
}
MovementSystem
public static class MovementSystem extends EntitySystem {public ImmutableArray<Entity> entities;private ComponentMapper<PositionComponent> pm = ComponentMapper.getFor(PositionComponent.class);private ComponentMapper<MovementComponent> mm = ComponentMapper.getFor(MovementComponent.class);@Overridepublic void addedToEngine (Engine engine) {entities = engine.getEntitiesFor(Family.all(PositionComponent.class, MovementComponent.class).get());log("MovementSystem added to engine.");}@Overridepublic void removedFromEngine (Engine engine) {log("MovementSystem removed from engine.");entities = null;}@Overridepublic void update (float deltaTime) {for (int i = 0; i < entities.size(); ++i) {Entity e = entities.get(i);PositionComponent p = pm.get(e);MovementComponent m = mm.get(e);p.x += m.velocityX * deltaTime;p.y += m.velocityY * deltaTime;}log(entities.size() + " Entities updated in MovementSystem.");}}
PositionSystem
public static class PositionSystem extends EntitySystem {public ImmutableArray<Entity> entities;@Overridepublic void addedToEngine (Engine engine) {entities = engine.getEntitiesFor(Family.all(PositionComponent.class).get());log("PositionSystem added to engine.");}@Overridepublic void removedFromEngine (Engine engine) {log("PositionSystem removed from engine.");entities = null;}}
Listener
public static class Listener implements EntityListener {@Overridepublic void entityAdded (Entity entity) {log("Entity added " + entity);}@Overridepublic void entityRemoved (Entity entity) {log("Entity removed " + entity);}}public static void log (String string) {System.out.println(string);}

主类:

public static void main (String[] args) {PooledEngine engine = new PooledEngine();MovementSystem movementSystem = new MovementSystem();PositionSystem positionSystem = new PositionSystem();engine.addSystem(movementSystem);engine.addSystem(positionSystem);Listener listener = new Listener();engine.addEntityListener(listener);for (int i = 0; i < 10; i++) {Entity entity = engine.createEntity();entity.add(new PositionComponent(10, 0));if (i > 5) entity.add(new MovementComponent(10, 2));engine.addEntity(entity);}log("MovementSystem has: " + movementSystem.entities.size() + " entities.");log("PositionSystem has: " + positionSystem.entities.size() + " entities.");for (int i = 0; i < 10; i++) {engine.update(0.25f);if (i > 5) engine.removeSystem(movementSystem);}engine.removeEntityListener(listener);}

具体代码可以看:

nanshaws/LibgdxTutorial: libgdx 教程项目 本项目旨在提供完整的libgdx桌面教程,帮助开发者快速掌握libgdx游戏开发框架的使用。成功的将gdx-ai和ashley的tests从官网剥离出来,并成功运行。libgdx tutorial project This project aims to provide a complete libgdx desktop tutorial to help developers quickly master the use of libgdx game development framework. Successfully separated GDX-AI and Ashley's tests from the official website and ran them (github.com)

这篇关于libgdx ashley框架的讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF

数据治理框架-ISO数据治理标准

引言 "数据治理"并不是一个新的概念,国内外有很多组织专注于数据治理理论和实践的研究。目前国际上,主要的数据治理框架有ISO数据治理标准、GDI数据治理框架、DAMA数据治理管理框架等。 ISO数据治理标准 改标准阐述了数据治理的标准、基本原则和数据治理模型,是一套完整的数据治理方法论。 ISO/IEC 38505标准的数据治理方法论的核心内容如下: 数据治理的目标:促进组织高效、合理地

ZooKeeper 中的 Curator 框架解析

Apache ZooKeeper 是一个为分布式应用提供一致性服务的软件。它提供了诸如配置管理、分布式同步、组服务等功能。在使用 ZooKeeper 时,Curator 是一个非常流行的客户端库,它简化了 ZooKeeper 的使用,提供了高级的抽象和丰富的工具。本文将详细介绍 Curator 框架,包括它的设计哲学、核心组件以及如何使用 Curator 来简化 ZooKeeper 的操作。 1

【Kubernetes】K8s 的安全框架和用户认证

K8s 的安全框架和用户认证 1.Kubernetes 的安全框架1.1 认证:Authentication1.2 鉴权:Authorization1.3 准入控制:Admission Control 2.Kubernetes 的用户认证2.1 Kubernetes 的用户认证方式2.2 配置 Kubernetes 集群使用密码认证 Kubernetes 作为一个分布式的虚拟

Spring Framework系统框架

序号表示的是学习顺序 IoC(控制反转)/DI(依赖注入): ioc:思想上是控制反转,spring提供了一个容器,称为IOC容器,用它来充当IOC思想中的外部。 我的理解就是spring把这些对象集中管理,放在容器中,这个容器就叫Ioc这些对象统称为Bean 用对象的时候不用new,直接外部提供(bean) 当外部的对象有关系的时候,IOC给它俩绑好(DI) DI和IO

Sentinel 高可用流量管理框架

Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。 Sentinel 具有以下特性: 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应

利用Django框架快速构建Web应用:从零到上线

随着互联网的发展,Web应用的需求日益增长,而Django作为一个高级的Python Web框架,以其强大的功能和灵活的架构,成为了众多开发者的选择。本文将指导你如何从零开始使用Django框架构建一个简单的Web应用,并将其部署到线上,让世界看到你的作品。 Django简介 Django是由Adrian Holovaty和Simon Willison于2005年开发的一个开源框架,旨在简

Yii框架relations的使用

通过在 relations() 中声明这些相关对象,我们就可以利用强大的 Relational ActiveRecord (RAR) 功能来访问资讯的相关对象,例如它的作者和评论。不需要自己写复杂的 SQL JOIN 语句。 前提条件 在组织数据库时,需要使用主键与外键约束才能使用ActiveReocrd的关系操作; 场景 申明关系 两张表之间的关系无非三种:一对多;一对一;多对多; 在