TurnBasedMinecraftMod/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleUpdater.java

44 lines
1.1 KiB
Java
Raw Normal View History

package com.seodisparate.TurnBasedMinecraft.common;
2018-09-06 08:08:36 +00:00
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
2018-09-06 08:08:36 +00:00
public class BattleUpdater implements Runnable
{
private BattleManager manager;
private AtomicBoolean isRunning;
public BattleUpdater(BattleManager manager)
{
this.manager = manager;
isRunning = new AtomicBoolean(true);
}
@Override
public void run()
{
2018-09-06 08:08:36 +00:00
Queue<Integer> endedQueue = new ArrayDeque<Integer>();
Integer ended;
while(isRunning.get())
{
for(Battle e : manager.battleMap.values())
{
2018-09-06 08:08:36 +00:00
if(e.update())
{
endedQueue.add(e.getId());
}
}
for(ended = endedQueue.poll(); ended != null; ended = endedQueue.poll())
2018-09-06 08:08:36 +00:00
{
manager.battleMap.remove(ended);
}
try { Thread.sleep(250); } catch (Throwable t) { /* ignored */ }
}
}
public void setIsRunning(boolean isRunning)
{
this.isRunning.set(isRunning);
}
}