All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codeheadsystems.gamelib.entity.manager.EngineManager Maven / Gradle / Ivy

/*
 *   Copyright (c) 2022. Ned Wolpert 
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 */

package com.codeheadsystems.gamelib.entity.manager;

import com.badlogic.ashley.core.Component;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.PooledEngine;
import com.badlogic.gdx.Gdx;
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class EngineManager {

    private final Set entitySystems = new HashSet<>();
    private final PooledEngine pooledEngine;

    @Inject
    public EngineManager(final Set entities,
                         final Set entitySystems,
                         final PooledEngine pooledEngine) {
        this.entitySystems.addAll(entitySystems);
        this.pooledEngine = pooledEngine;
        for (Entity entity : entities) {
            Gdx.app.log("PoolEngineManager", "AddEntity: " + entity + ":" + entity.getComponents());
            pooledEngine.addEntity(entity);
        }
        for (EntitySystem entitySystem : entitySystems) {
            Gdx.app.log("PoolEngineManager", "AddSystem: " + entitySystem);
            pooledEngine.addSystem(entitySystem);
        }
    }

    public Entity createEntity() {
        return pooledEngine.createEntity();
    }

    public  T createComponent(Class componentType) {
        return pooledEngine.createComponent(componentType);
    }

    public PooledEngine engine() {
        return pooledEngine;
    }

    public void addSystem(EntitySystem system) {
        pooledEngine.addSystem(system);
        entitySystems.add(system);
    }

    public void removeSystem(EntitySystem system) {
        pooledEngine.addSystem(system);
        entitySystems.remove(system);
    }

    public void addEntity(Entity entity) {
        pooledEngine.addEntity(entity);
    }

    public void removeEntity(Entity entity) {
        entity.removeAll();
        pooledEngine.removeEntity(entity);
    }

    public void update(float delta) {
        pooledEngine.update(delta);
    }

    public void destroy() {
        pooledEngine.removeAllEntities();
        for (EntitySystem entitySystem : entitySystems) {
            Gdx.app.log("PoolEngineManager", "removeSystem: " + entitySystem);
            pooledEngine.removeSystem(entitySystem);
        }
        pooledEngine.removeAllSystems();
        entitySystems.clear();
    }

    public Set getEntitySystems() {
        return entitySystems;
    }

    public void clear() {
      pooledEngine.removeAllEntities();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy