Created
November 21, 2016 18:56
-
-
Save zacharycarter/6d04d27e1f69da0e32c0f0b30e959fac 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.ai; | |
import com.artemis.Aspect; | |
import com.artemis.ComponentMapper; | |
import com.artemis.Entity; | |
import com.artemis.systems.IteratingSystem; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.ai.GdxAI; | |
import com.badlogic.gdx.math.Vector2; | |
import com.carterza.action.BaseAction; | |
import com.carterza.action.DummyAction; | |
import com.carterza.action.MoveToAction; | |
import com.carterza.component.AIComponent; | |
import com.carterza.component.BehaviorTreeComponent; | |
import com.carterza.component.PositionComponent; | |
import com.carterza.event.*; | |
import com.carterza.generation.ship.grid.GridMap; | |
import com.carterza.system.action.ActionSystem; | |
import com.carterza.system.generation.ShipGenerationSystem; | |
import com.carterza.system.player.PlayerSystem; | |
import com.stewsters.util.math.Point2i; | |
import net.mostlyoriginal.api.event.common.Subscribe; | |
import squidpony.squidai.DijkstraMap; | |
import squidpony.squidmath.Coord; | |
import squidpony.squidmath.LightRNG; | |
import squidpony.squidmath.RNG; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by zachcarter on 11/9/16. | |
*/ | |
public class AISystem extends IteratingSystem { | |
private List<Entity> ai; | |
private ActionSystem actionSystem; | |
private PlayerSystem playerSystem; | |
private EventBus eventBus; | |
private ComponentMapper<BehaviorTreeComponent> behaviorTreeComponentMapper; | |
private int delta; | |
public AISystem(int logicFPS) { | |
super(Aspect.all(AIComponent.class, PositionComponent.class)); | |
delta = logicFPS; | |
} | |
@Override | |
protected void initialize() { | |
this.ai = new ArrayList<>(); | |
} | |
@Override | |
protected void inserted(int entityId) { | |
ai.add(world.getEntity(entityId)); | |
} | |
@Override | |
protected void end() { | |
GdxAI.getTimepiece().update(Gdx.graphics.getDeltaTime()); | |
} | |
@Override | |
protected void process(int entityId) { | |
} | |
@Subscribe | |
void handleEvent(TurnEvent event) { | |
Entity eventEntity = event.getEntity(); | |
if(!eventEntity.equals(playerSystem.getPlayer())) { | |
BaseAction actionToPerform = (BaseAction) actionSystem.getNextAction(eventEntity); | |
if (actionToPerform != null) { | |
switch (actionToPerform.getActionName()) { | |
case MOVE_TO: | |
MoveToAction moveToAction = (MoveToAction) actionToPerform; | |
eventBus.dispatch(new MoveToEvent(eventEntity, moveToAction.getTileToMoveTo(), moveToAction.getActionCost())); | |
actionSystem.setNextAction(eventEntity, null); | |
break; | |
case DUMMY: | |
DummyAction dummyAction = (DummyAction) actionToPerform; | |
eventBus.dispatch(new DummyEvent(event.getEntity(), dummyAction.getActionCost())); | |
actionSystem.setNextAction(eventEntity, null); | |
break; | |
} | |
} | |
BehaviorTreeComponent behaviorTreeComponent = behaviorTreeComponentMapper.get(eventEntity); | |
behaviorTreeComponent.getBehaviorTree().step(); | |
} | |
} | |
public List<Entity> getAi() { | |
return ai; | |
} | |
public void setAi(List<Entity> ai) { | |
this.ai = ai; | |
} | |
} |
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.player; | |
import com.artemis.Aspect; | |
import com.artemis.ComponentMapper; | |
import com.artemis.Entity; | |
import com.artemis.annotations.Wire; | |
import com.artemis.systems.IteratingSystem; | |
import com.carterza.action.Actions; | |
import com.carterza.action.BaseAction; | |
import com.carterza.action.MoveAction; | |
import com.carterza.component.InventoryComponent; | |
import com.carterza.component.PlayerComponent; | |
import com.carterza.component.PositionComponent; | |
import com.carterza.event.*; | |
import com.carterza.system.generation.ShipGenerationSystem; | |
import com.carterza.system.action.ActionSystem; | |
import com.stewsters.shipwright.Room; | |
import com.stewsters.util.math.Point2i; | |
import net.mostlyoriginal.api.event.common.Subscribe; | |
import static com.carterza.action.Actions.*; | |
@Wire | |
public class PlayerSystem extends IteratingSystem { | |
ShipGenerationSystem shipGenerationSystem; | |
ActionSystem actionSystem; | |
ComponentMapper<InventoryComponent> inventoryComponentMapper; | |
ComponentMapper<PositionComponent> positionComponentMapper; | |
EventBus eventBus; | |
private Entity player; | |
/** | |
* Creates a new EntityProcessingSystem. | |
* | |
* @param aspect the aspect to match entities | |
*/ | |
public PlayerSystem() { | |
super(Aspect.all(InventoryComponent.class, PositionComponent.class, PlayerComponent.class)); | |
} | |
@Subscribe | |
void handleEvent(TurnEvent event) { | |
Entity eventEntity = event.getEntity(); | |
if(eventEntity.equals(player)) { | |
BaseAction actionToPerform = (BaseAction) actionSystem.getNextAction(eventEntity); | |
if (actionToPerform != null) { | |
switch (actionToPerform.getActionName()) { | |
case MOVE: | |
MoveAction moveAction = (MoveAction) actionToPerform; | |
eventBus.dispatch(new MoveEvent(event.getEntity(), moveAction.getDirection(), moveAction.getActionCost())); | |
actionSystem.setNextAction(eventEntity, null); | |
break; | |
} | |
} | |
} | |
} | |
@Subscribe | |
void handleEvent(EndTurnEvent event) { | |
if (event.getEntity() != null) { | |
PositionComponent positionComponent = positionComponentMapper.get(event.getEntity()); | |
if (positionComponent == null) return; | |
Room roomStandingIn = shipGenerationSystem | |
.getSpacecraft() | |
.findRoomContainingPoint( | |
new Point2i( | |
(int) positionComponent.getPosition().x / 24 | |
, (int) positionComponent.getPosition().y / 24 | |
) | |
); | |
if (roomStandingIn == null) return; | |
if (roomStandingIn.getItem() != null) { | |
InventoryComponent inventoryComponent = inventoryComponentMapper.get(event.getEntity()); | |
if (inventoryComponent != null) { | |
String item = roomStandingIn.getItem().toString(); | |
if (item.equals("A") || item.equals("B") || item.equals("C") || item.equals("D")) | |
inventoryComponent.getKeys().add(item); | |
else if (item.equals("SW")) | |
eventBus.dispatch(new SwitchTriggeredEvent()); | |
} | |
} | |
} | |
} | |
@Override | |
protected void inserted(int entityId) { | |
this.player = world.getEntity(entityId); | |
} | |
@Override | |
protected void process(int entityId) { | |
} | |
public Entity getPlayer() { | |
return this.player; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment