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

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();
}

1.gif

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();
}

1.gif

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");
    }

}

推荐阅读