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文件,运行起来会让一百个硬币移动

@Override
		public 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);

		@Override
		public void addedToEngine (Engine engine) {
			entities = engine.getEntitiesFor(Family.all(PositionComponent.class, MovementComponent.class).get());
			log("MovementSystem added to engine.");
		}

		@Override
		public void removedFromEngine (Engine engine) {
			log("MovementSystem removed from engine.");
			entities = null;
		}

		@Override
		public 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;

		@Override
		public void addedToEngine (Engine engine) {
			entities = engine.getEntitiesFor(Family.all(PositionComponent.class).get());
			log("PositionSystem added to engine.");
		}

		@Override
		public void removedFromEngine (Engine engine) {
			log("PositionSystem removed from engine.");
			entities = null;
		}
	}
Listener
public static class Listener implements EntityListener {

		@Override
		public void entityAdded (Entity entity) {
			log("Entity added " + entity);
		}

		@Override
		public 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nanshaws

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值