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

相关文章

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

解决若依微服务框架启动报错的问题

《解决若依微服务框架启动报错的问题》Invalidboundstatement错误通常由MyBatis映射文件未正确加载或Nacos配置未读取导致,需检查XML的namespace与方法ID是否匹配,... 目录ruoyi-system模块报错报错详情nacos文件目录总结ruoyi-systnGLNYpe

MySQL连表查询之笛卡尔积查询的详细过程讲解

《MySQL连表查询之笛卡尔积查询的详细过程讲解》在使用MySQL或任何关系型数据库进行多表查询时,如果连接条件设置不当,就可能发生所谓的笛卡尔积现象,:本文主要介绍MySQL连表查询之笛卡尔积查... 目录一、笛卡尔积的数学本质二、mysql中的实现机制1. 显式语法2. 隐式语法3. 执行原理(以Nes

RabbitMQ消费端单线程与多线程案例讲解

《RabbitMQ消费端单线程与多线程案例讲解》文章解析RabbitMQ消费端单线程与多线程处理机制,说明concurrency控制消费者数量,max-concurrency控制最大线程数,prefe... 目录 一、基础概念详细解释:举个例子:✅ 单消费者 + 单线程消费❌ 单消费者 + 多线程消费❌ 多

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、