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

com.tenio.engine.fsm.EntityManager Maven / Gradle / Ivy

Go to download

TenIO is a java NIO (Non-blocking I/O) based server specifically designed for multiplayer games. It supports UDP and TCP transports which are handled by Netty for high-speed network transmission. This is the engine module for the game related handlers of the framework.

There is a newer version: 0.6.0
Show newest version
/*
The MIT License

Copyright (c) 2016-2021 kong 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package com.tenio.engine.fsm;

import com.tenio.common.logger.SystemLogger;
import com.tenio.engine.exception.DuplicatedEntityException;
import com.tenio.engine.fsm.entity.AbstractEntity;
import java.util.HashMap;
import java.util.Map;

/**
 * This class for managing entities.
 */
public final class EntityManager extends SystemLogger {

  /**
   * The list of entities.
   */
  private final Map entities = new HashMap();

  /**
   * Register an entity to this management.
   *
   * @param entity the desired entity, see {@link AbstractEntity}
   */
  public void register(AbstractEntity entity) {
    try {
      if (contain(entity.getId())) {
        throw new DuplicatedEntityException();
      }
    } catch (DuplicatedEntityException e) {
      // fire an event
      error(e, "entity id: ", entity.getId());
      return;
    }

    entities.put(entity.getId(), entity);
  }

  public boolean contain(String id) {
    return entities.containsKey(id);
  }

  public long count() {
    return entities.size();
  }

  public AbstractEntity get(String id) {
    return entities.get(id);
  }

  /**
   * Need to call update every frame.
   *
   * @param deltaTime the time between two consecutive frames
   */
  public void update(float deltaTime) {
    for (var entity : entities.values()) {
      entity.update(deltaTime);
    }
  }

  /**
   * Retrieves the list of entities.
   *
   * @return the list of entities in this manager
   */
  public Map gets() {
    return entities;
  }

  public void remove(String id) {
    entities.remove(id);
  }

  public void clear() {
    entities.clear();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy