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

com.kauridev.lunarbase.EntityCollection Maven / Gradle / Ivy

Go to download

Lunar Base is a simple game framework written in Java. It is written using an entity-component-system architecture.

The newest version!
/*
 * This file is part of the lunar-base package.
 *
 * Copyright (c) 2014 Eric Fritz
 *
 * 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.kauridev.lunarbase;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * A collection of entities which specify a given criteria. This collection keeps itself current by
 * listening to entity changed and removed events. If an entity receives a property which allows it
 * to be in the collection, it is added automatically. If an entity removes a property which allows
 * it to be in the collection, it is removed automatically.
 * 

* The criteria by which an entity is allowed in the collection is determined by an entity matcher. * Entity matchers may be composed for greater flexibility. * * @author Eric Fritz */ public class EntityCollection implements Iterable, EntityChangedEventListener, EntityRemovedEventListener { /** * The entity matcher. */ private EntityMatcher matcher; /** * The wrapped entity list. */ private Set actives = new LinkedHashSet<>(); /** * Creates a new EntityCollection. * * @param eventManager The event manager. * @param matcher The entity matcher. */ public EntityCollection(EventManager eventManager, EntityMatcher matcher) { this.matcher = matcher; eventManager.addListener(EntityChangedEvent.class, this); eventManager.addListener(EntityRemovedEvent.class, this); } @Override public Iterator iterator() { return Collections.unmodifiableSet(actives).iterator(); } @Override public EntityChangedEvent onEntityChanged(EntityChangedEvent event) { Entity entity = event.getEntity(); if (matcher.matches(entity)) { actives.add(entity); } else { actives.remove(entity); } return event; } @Override public EntityRemovedEvent onEntityRemoved(EntityRemovedEvent event) { actives.remove(event.getEntity()); return event; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy