0

更新: WorldConfiguration 和 World 还是一样的,

WorldConfiguration setup = new WorldConfigurationBuilder()
                .with(new HelloSystemArtemis())
                .with(new RenderSystem(batch, environment))
                .with(new BulletSystem(this))
                .with(new PlayerSystem(perspectiveCamera, batch, gameUi, this))
                .with(new AthmosphereSystem(modelComponent,
                        bulletComponent))
                .with(new MovementSystem(this))
                .with(new StatusSystem(this))
                .with(new EnemySystem(this))
                .build();
        World world = new World(setup);
        world.setDelta(delta);
        int entityId = world.create();
        world.edit(entityId).create(HelloComponentArtemis.class).message = "\n\rHello Oxyddians!\n\r";

出于清晰的原因决定发布完整的代码,实际上无法解释问题出在哪里,即使我遇到了指向我的错误

@All({ModelComponentArtemis.class, ModelComponentArtemis.class})
public class AthmosphereSystem extends BaseSystem {
    private static OxyddiA game;
    private static Assets assets = new Assets();
    private ComponentMapper<ModelComponentArtemis> mc;
    private ComponentMapper<BulletComponentArtemis> bc;
    public AthmosphereSystem(ComponentMapper<ModelComponentArtemis> mc,
                             ComponentMapper<BulletComponentArtemis> bc) {
        this.mc = mc;
        this.bc = bc;
    }

    @Override
    protected void processSystem() {
        int entity = world.create();
        ModelComponentArtemis mc = new ModelComponentArtemis(assets.Athmosphere,0,0,0);
        mc.create(entity); //Not working
        BulletComponentArtemis bc = new BulletComponentArtemis();
        btCollisionShape shape = Bullet.obtainStaticNodeShape(assets.Athmosphere.nodes);
        bc.bodyInfo = new   btRigidBody.btRigidBodyConstructionInfo(0, null, shape,   Vector3.Zero);
        bc.body = new btRigidBody(bc.bodyInfo);
        bc.body.userData = entity;
        bc.motionState = new MotionState(mc.instance.transform);
        ((btRigidBody)  bc.body).setMotionState(bc.motionState);
        bc.create(entity); //Not working
        return entity; //Not working as well
    }
}

此方法中使用的 ModelComponent

public class ModelComponentArtemis extends Component {

    public Model model;
    public ModelInstance instance;
    public ModelComponentArtemis(Model model, float x, float y, float z)
    {
        this.model = model;
        this.instance = new ModelInstance(model, new Matrix4().setToTranslation(x, y, z));
    }

    public void reset() {
    }
}

此方法中使用的 BulletComponent

public class BulletComponentArtemis extends Component {
    public MotionState motionState;
    public btRigidBody.btRigidBodyConstructionInfo bodyInfo;
    public btCollisionObject body;
    public Model model;

    public void init() {
        ModelComponentArtemis modelComponent = new ModelComponentArtemis(model,0,0,0);
        BulletComponentArtemis bulletComponent = new BulletComponentArtemis();
        btCollisionShape shape = Bullet.obtainStaticNodeShape(model.nodes);
        bulletComponent.bodyInfo = new   btRigidBody.btRigidBodyConstructionInfo(0, null, shape,   Vector3.Zero);
        bulletComponent.body = new btRigidBody(bulletComponent.bodyInfo);
        Object bCuD = null;
        bulletComponent.body.userData = bCuD;
        bulletComponent.motionState = new MotionState(modelComponent.instance.transform);
        ((btRigidBody)  bulletComponent.body).setMotionState(bulletComponent.motionState);
    }

    public void reset() {
    }
}

当然还有这个项目中使用的 BulletSystem

@All(ModelComponentArtemis.class)
public class BulletSystem extends EntitySystem
{
    public Entity e;
    public final btCollisionConfiguration collisionConfiguration;
    public final btCollisionDispatcher dispatcher;
    public final btBroadphaseInterface broadphase;
    public final btConstraintSolver solver;
    public final btDiscreteDynamicsWorld collisionWorld;
    public GameWorld gameWorld;
    public PreGameWorld preGameWorld;
    private final btGhostPairCallback ghostPairCallback;
    public final int maxSubSteps = 5;
    public final float fixedTimeStep = 0.2f / 60f;
    private EntityContactListener myContactListener;

    public BulletSystem(GameWorld gameWorld)
    {
        super(Aspect.all(ModelComponentArtemis.class));
        this.gameWorld = gameWorld;
        myContactListener = new EntityContactListener();
        myContactListener.enable();

        collisionConfiguration = new btDefaultCollisionConfiguration();
        dispatcher = new btCollisionDispatcher(collisionConfiguration);
        broadphase = new btAxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));
        solver = new btSequentialImpulseConstraintSolver();
        collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
        ghostPairCallback = new btGhostPairCallback();
        broadphase.getOverlappingPairCache().setInternalGhostPairCallback(ghostPairCallback);
        this.collisionWorld.setGravity(new Vector3(0f, -0.5f, 0f));
    }

    public BulletSystem(PreGameWorld preGameWorld)
    {
        this.preGameWorld = preGameWorld;
        myContactListener = new EntityContactListener();
        myContactListener.enable();

        collisionConfiguration = new btDefaultCollisionConfiguration();
        dispatcher = new btCollisionDispatcher(collisionConfiguration);
        broadphase = new btAxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));
        solver = new btSequentialImpulseConstraintSolver();
        collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
        ghostPairCallback = new btGhostPairCallback();
        broadphase.getOverlappingPairCache().setInternalGhostPairCallback(ghostPairCallback);
        this.collisionWorld.setGravity(new Vector3(0f, -0.5f, 0f));
    }

    public void update(float deltaTime)
    {
        collisionWorld.stepSimulation(deltaTime, maxSubSteps, fixedTimeStep);
    }

    protected void processSystem(int entityId) {
        this.world.getEntity(entityId).getComponent(BulletComponentArtemis.class);
    }

    @Override
    protected void processSystem() {
    }

    public void dispose()
    {
        collisionWorld.dispose();
        if (solver != null) solver.dispose();
        if (broadphase != null) broadphase.dispose();
        if (dispatcher != null) dispatcher.dispose();
        if (collisionConfiguration != null)
            collisionConfiguration.dispose();
        ghostPairCallback.dispose();
    }
}

考虑到所有这些,我知道我找不到任何解决方案,也许对于任何希望提供帮助的人来说会更清楚谢谢无论如何。

4

1 回答 1

0

需要注意的几点:

  1. 你不需要注入实体实例
  2. 使用 Entity 和 EntityEdit 比使用 int 和 ComponentMapper/Archetype/EntityTransmuter 慢,但一开始就很好。

您可能想要做的是创建一个 AtmosphereSystem(扩展 BaseSystem)并在初始化方法中创建实体或提供一个公共方法(非静态)。

如果您需要调用这些实体工厂方法,只需通过添加字段“AtmosphereSystem大气系统;”来注入 AtmosphereSystem(或任何扩展 BaseSystem)。在任何其他 BaseSystem 内。

有关类似用例(用于创建实体的工厂方法),请参阅此要点。

于 2020-08-13T13:40:48.743 回答