Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.carterza.entity;
import com.artemis.Entity;
import com.carterza.action.Action;
/**
* Created by zachcarter on 11/19/16.
*/
public class FutureEntityTurn implements Comparable<FutureEntityTurn> {
private Entity entity;
private int time;
public FutureEntityTurn(Entity entity, int time) {
this.entity = entity;
this.time = time;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public Entity getEntity() {
return entity;
}
@Override
public int compareTo(FutureEntityTurn o) {
return Integer.compare(this.time, o.time);
}
}
package com.carterza.event;
import com.artemis.Entity;
import com.carterza.action.Action;
import net.mostlyoriginal.api.event.common.Event;
/**
* Created by zachcarter on 11/19/16.
*/
public class PerformActionEvent implements Event {
private Entity entity;
private int actionCost;
private Action action;
public Entity getEntity() {
return entity;
}
public void setEntity(Entity entity) {
this.entity = entity;
}
public int getActionCost() {
return actionCost;
}
public void setActionCost(int actionCost) {
this.actionCost = actionCost;
}
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
}
package com.carterza.component;
import com.artemis.Component;
/**
* Created by zachcarter on 11/9/16.
*/
public class TurnComponent extends Component {
private int speed;
public TurnComponent(int speed) {
this.speed = speed;
}
public int getSpeed() {
return speed;
}
public TurnComponent() {
}
}
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.carterza.component.TurnComponent;
import com.carterza.entity.FutureEntityTurn;
import com.carterza.event.PerformActionEvent;
import net.mostlyoriginal.api.event.common.Subscribe;
import java.util.Iterator;
import java.util.PriorityQueue;
@Wire
public class TurnSystem extends BaseEntitySystem {
private static final int BASE_DELAY_VALUE = 10;
PriorityQueue<FutureEntityTurn> turnQueue;
private Entity entityWhoseTurnItIs;
ComponentMapper<TurnComponent> turnComponentMapper;
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());
}
}
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);
turnQueue.add(new FutureEntityTurn(world.getEntity(entityId), BASE_DELAY_VALUE/turnComponent.getSpeed()));
}
@Subscribe
void handleEvent(PerformActionEvent event) {
if(entityWhoseTurnItIs != null) {
if (entityWhoseTurnItIs.equals(event.getEntity())) {
event.getAction().execute();
entityWhoseTurnItIs = null;
turnQueue.add(new FutureEntityTurn(event.getEntity(), event.getActionCost()));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment