欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

FXGL 学习心得第二弹:探讨Entity的创建方法

最编程 2024-07-23 21:37:36
...
package com.cah.api.entity; import com.almasb.fxgl.app.GameApplication; import com.almasb.fxgl.app.GameSettings; import com.almasb.fxgl.dsl.FXGL; import com.almasb.fxgl.entity.Entity; import com.almasb.fxgl.entity.SpawnData; import com.almasb.fxgl.physics.BoundingShape; import com.almasb.fxgl.physics.HitBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; /** * <p> 实体创建 </p> * 在游戏中,大部分物体都可以称为实体。角色,敌人,子弹,物品等。 * 实体如下基本属性 * 1.view:显示展示实体内容(图片) * 2.bbox:碰撞体积(用于做物理碰撞事件,如果没有设置,这时获取宽与高都为0) * 3.type:类型,标识该实体属于什么,如主角,敌人,子弹 * 4.transform: 位置 * @author hongtool * @date 2023/8/14 */ public class EntityApp extends GameApplication { public static void main(String[] args) { launch(args); } @Override protected void initSettings(GameSettings gameSettings) { } @Override protected void initGame() { // 创建实体 Entity entity = FXGL.entityBuilder() // 设置实体类型-长方形 .type(EntityTypeEnum.RECTANGLE) // 设置实体初始位置 .at(100, 100) // 黑色的长方形 .view(new Rectangle(20, 40, Color.BLACK)) .build(); // 将实体添加到游戏中(如果没有该方法,则创建的实体不会添加到游戏中,游戏界面看不见) FXGL.getGameWorld().addEntity(entity); System.out.printf("entity-w:%s--h:%s%n", entity.getWidth(), entity.getHeight()); // 创建并添加 buildAndAttach(); Entity entity1 = FXGL.entityBuilder() .type(EntityTypeEnum.SQUARE) .at(200, 100) // 红色的正方形 .view(new Rectangle(20, 20, Color.RED)) .buildAndAttach(); System.out.printf("entity1-w:%s--h:%s%n", entity1.getWidth(), entity1.getHeight()); // 如果.at(x, y) 的方法可以替换成 .entityBuilder(new SpawnData(x, y)) Entity entity2 = FXGL.entityBuilder(new SpawnData(200, 200)) .type(EntityTypeEnum.ROUNDNESS) // 蓝色的圆形 .view(new Circle(20, Color.BLUE)) .buildAndAttach(); System.out.printf("entity2-w:%s--h:%s%n", entity2.getWidth(), entity2.getHeight()); // 以上几个没有设置bbox,则宽与高都为0,这里设置了bbox,宽与高则有值 Entity entity3 = FXGL.entityBuilder(new SpawnData(300, 200)) .type(EntityTypeEnum.ROUNDNESS) // 蓝色的圆形 .view(new Circle(20, Color.PINK)) // 设置碰撞体积(圆形的碰撞体积) .bbox(new HitBox(BoundingShape.circle(20))) .buildAndAttach(); System.out.printf("entity3-w:%s--h:%s%n", entity3.getWidth(), entity3.getHeight()); // 如果显示与碰撞一致,则可以简写 Entity entity4 = FXGL.entityBuilder(new SpawnData(300, 300)) .type(EntityTypeEnum.SQUARE) // 黄色的正方形 .viewWithBBox(new Rectangle(30, 30, Color.YELLOW)) .buildAndAttach(); System.out.printf("entity4-w:%s--h:%s%n", entity4.getWidth(), entity4.getHeight()); // 视图可以有多个组合 Entity entity5 = FXGL.entityBuilder(new SpawnData(400, 300)) .type(EntityTypeEnum.SQUARE) .view(new Text("绿色")) // 绿色的正方形 .viewWithBBox(new Rectangle(30, 30, Color.GREEN)) .buildAndAttach(); System.out.printf("entity5-w:%s--h:%s%n", entity5.getWidth(), entity5.getHeight()); // 通过实体工厂创建,在ShapeEntityFactory中编写方法,并且使用@Spawns指定名称 // 注意,使用实体工厂的时候,需要添加该工厂 FXGL.getGameWorld().addEntityFactory(new ShapeEntityFactory()); // 只创建实体,不添加到游戏世界中 Entity entity10 = FXGL.getGameWorld().create("ROUNDNESS", new SpawnData(400, 100)); // 以spawn方式创建的Entity,默认直接添加到游戏世界中。 // 没有指定位置,默认在左上角 Entity entity6 = FXGL.getGameWorld().spawn("ROUNDNESS"); // 上面方法的简写,并且拥有初始指定位置 Entity entity7 = FXGL.spawn("ROUNDNESS", new SpawnData(300, 100)); // 如果多个相同的实体,但是用途不一样,可以在@Spawns中指定多个名称,用逗号“,”分割 Entity entity8 = FXGL.spawn("RECTANGLE", new SpawnData(400, 200)); Entity entity9 = FXGL.spawn("SQUARE", new SpawnData(500, 200)); Entity entity11 = FXGL.spawn("RECTANGLE1", new SpawnData(500, 300) .put("w", 60D) .put("h", 60) .put("color", Color.BLACK)); // 从 entity 中获取 SpawnData 数据 System.out.println("entity11-->w:" + entity11.getDouble("w")); System.out.println("entity11-->h:" + entity11.getInt("h")); System.out.println("entity11-->color:" + entity11.getObject("color")); // 从 entity 中,获取所有的参数 entity11.getProperties().keys().forEach(key -> // 这里有一个key是type,与 entity中设置的type是不一样的。这里指的就是 spawn 的 entityName System.out.println(key + " : " + entity11.getProperties().getValue(key)) ); } }