Created
November 21, 2016 18:52
-
-
Save zacharycarter/6af486c42b5a3675e2bf0f8c978743b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carterza.system.turn; | |
import com.artemis.Aspect; | |
import com.artemis.BaseEntitySystem; | |
import com.artemis.ComponentMapper; | |
import com.artemis.Entity; | |
import com.artemis.annotations.Wire; | |
import com.badlogic.gdx.Gdx; | |
import com.carterza.component.TurnComponent; | |
import com.carterza.entity.FutureEntityTurn; | |
import com.carterza.event.EndTurnEvent; | |
import com.carterza.event.EventBus; | |
import com.carterza.event.PerformActionEvent; | |
import com.carterza.event.TurnEvent; | |
import net.mostlyoriginal.api.event.common.Subscribe; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.PriorityQueue; | |
@Wire | |
public class TurnSystem extends BaseEntitySystem { | |
private static final int BASE_DELAY_VALUE = 10; | |
private PriorityQueue<FutureEntityTurn> turnQueue; | |
private Entity entityWhoseTurnItIs; | |
private EventBus eventBus; | |
ComponentMapper<TurnComponent> turnComponentMapper; | |
long counter = 0; | |
public TurnSystem() { | |
super(Aspect.all(TurnComponent.class)); | |
turnQueue = new PriorityQueue<FutureEntityTurn>(); | |
} | |
@Override | |
protected void processSystem() { | |
if (entityWhoseTurnItIs == null) { | |
FutureEntityTurn futureEntityTurn = turnQueue.remove(); | |
entityWhoseTurnItIs = futureEntityTurn.getEntity(); | |
adjustActionQueue(-futureEntityTurn.getTime()); | |
} | |
eventBus.dispatch(new TurnEvent(entityWhoseTurnItIs)); | |
} | |
private void adjustActionQueue(int time) { | |
Iterator<FutureEntityTurn> actionQueueIterator = turnQueue.iterator(); | |
while(actionQueueIterator.hasNext()) { | |
FutureEntityTurn futureEntityAction = actionQueueIterator.next(); | |
futureEntityAction.setTime(futureEntityAction.getTime()+time); | |
} | |
} | |
public Entity getEntityWhoseTurnItIs() { | |
return entityWhoseTurnItIs; | |
} | |
@Override | |
protected void inserted(int entityId) { | |
TurnComponent turnComponent = turnComponentMapper.get(entityId); | |
int initialActionCost = BASE_DELAY_VALUE/turnComponent.getSpeed(); | |
turnQueue.add(new FutureEntityTurn(world.getEntity(entityId), initialActionCost, counter)); | |
counter++; | |
} | |
@Subscribe | |
void handleEvent(EndTurnEvent event) { | |
Entity e = event.getEntity(); | |
TurnComponent turnComponent = turnComponentMapper.get(e); | |
entityWhoseTurnItIs = null; | |
int actionTime = event.getActionCost()/turnComponent.getSpeed(); | |
turnQueue.add(new FutureEntityTurn(event.getEntity(), actionTime, counter)); | |
counter++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment