package electrosphere.server.service; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.concurrent.locks.ReentrantLock; import org.joml.Vector3d; import electrosphere.engine.signal.Signal.SignalType; import electrosphere.engine.signal.SignalServiceImpl; import electrosphere.entity.Entity; import electrosphere.entity.EntityUtils; import electrosphere.entity.state.lod.ServerLODComponent; /** * Manages lod emitters */ public class LODEmitterService extends SignalServiceImpl { /** * The set of LOD emitters */ private List emitters = new LinkedList(); /** * List of temporary vecs for emitter checking - are cleared every frame *

* Intention with this is that it can be used to guarantee a player character has physics generated */ private List tempVecs = new LinkedList(); /** * Lock for thread-safeing the service */ private ReentrantLock lock = new ReentrantLock(); /** * Creates the LOD emitter service */ public LODEmitterService() { super("LODEmitterService", new SignalType[]{ }); } /** * Gets the list of LOD emitters * @return The list of LOD emitters */ public List getEmitters(){ lock.lock(); List rVal = Collections.unmodifiableList(this.emitters); lock.unlock(); return rVal; } /** * Registers a LOD emitter * @param entity The entity */ public void registerLODEmitter(Entity entity){ lock.lock(); this.emitters.add(entity); lock.unlock(); } /** * Deregisters a LOD emitter * @param entity The entity */ public void deregisterLODEmitter(Entity entity){ lock.lock(); this.emitters.remove(entity); lock.unlock(); } /** * Clears the temp vecs */ public void simulate(){ this.tempVecs.clear(); } /** * Adds a temporary vector * @param tempVec The temporary vector */ public void addTempVec(Vector3d tempVec){ this.tempVecs.add(tempVec); } /** * Checks if a given position would be full LOD * @param position The position * @return true if it is full lod, false otherwise */ public boolean isFullLod(Vector3d position){ for(Entity emitter : this.getEmitters()){ Vector3d emitterLoc = EntityUtils.getPosition(emitter); double dist = position.distance(emitterLoc); if(dist < ServerLODComponent.LOD_RADIUS){ return true; } } for(Vector3d tempVec : this.tempVecs){ double dist = position.distance(tempVec); if(dist < ServerLODComponent.LOD_RADIUS){ return true; } } return false; } }