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

com.codeheadsystems.gamelib.hex.manager.HexComponentManager 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.hex.manager;

import static com.codeheadsystems.gamelib.core.util.LoggerHelper.logger;

import com.badlogic.gdx.utils.Logger;
import com.codeheadsystems.gamelib.core.util.Pooler;
import com.codeheadsystems.gamelib.hex.component.HexComponent;
import com.codeheadsystems.gamelib.hex.model.HexFieldLayout;
import com.codeheadsystems.gamelib.hex.model.Layout;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * Provides a way to generate a hex field. This class 'self-pools' and should not be used
 * if you are using an entity system.
 */
@Singleton
public class HexComponentManager {

    private static final Logger LOGGER = logger(HexComponentManager.class);
    private final Pooler pool;
    private final HexManager hexManager;
    private final LayoutManager layoutManager;

    @Inject
    public HexComponentManager(final HexManager hexManager,
                               final LayoutManager layoutManager) {
        this.hexManager = hexManager;
        this.layoutManager = layoutManager;
        pool = Pooler.of(HexComponent::new);
        LOGGER.debug("HexComponentManager()");
    }

    public Set generate(final HexFieldLayout hexFieldLayout) {
        return generate(hexFieldLayout.cols(), hexFieldLayout.rows(), hexFieldLayout.layout());
    }

    public Set generate(final int cols, final int rows, final Layout layout) {
        return hexManager.generate(cols, rows)
                .stream()
                .map(h -> pool.obtain().initialize(h, layout, layoutManager))
                .collect(Collectors.toSet());
    }

    public void release(final Set field) {
        field.forEach(this::release);
    }

    public void release(final HexComponent component) {
        hexManager.free(component.hex());
        pool.free(component);
    }

    public int poolSize() {
        return pool.poolSize();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy