|
package com.carterza.system.ai; |
|
|
|
import com.artemis.ComponentMapper; |
|
import com.artemis.Entity; |
|
import com.badlogic.gdx.ai.pfa.DefaultGraphPath; |
|
import com.badlogic.gdx.ai.pfa.GraphPath; |
|
import com.badlogic.gdx.ai.pfa.Heuristic; |
|
import com.badlogic.gdx.ai.pfa.indexed.IndexedAStarPathFinder; |
|
import com.badlogic.gdx.math.Vector2; |
|
import com.carterza.action.MoveToAction; |
|
import com.carterza.ai.path.GridGraph; |
|
import com.carterza.ai.path.Path; |
|
import com.carterza.component.PositionComponent; |
|
import com.carterza.event.EventBus; |
|
import com.carterza.event.StartWanderingEvent; |
|
import com.carterza.event.StopWanderingEvent; |
|
import com.carterza.event.WanderEvent; |
|
import com.carterza.generation.ship.grid.GridMap; |
|
import com.carterza.system.action.ActionSystem; |
|
import com.carterza.system.generation.ShipGenerationSystem; |
|
import com.stewsters.util.math.Point2i; |
|
import net.mostlyoriginal.api.event.common.Subscribe; |
|
import net.mostlyoriginal.api.system.core.PassiveSystem; |
|
import squidpony.squidgrid.mapping.DungeonUtility; |
|
import squidpony.squidmath.Coord; |
|
import squidpony.squidmath.GreasedRegion; |
|
import squidpony.squidmath.LightRNG; |
|
import squidpony.squidmath.StatefulRNG; |
|
|
|
import java.util.HashMap; |
|
import java.util.Iterator; |
|
import java.util.Map; |
|
|
|
/** |
|
* Created by zachcarter on 11/20/16. |
|
*/ |
|
public class WanderSystem extends PassiveSystem { |
|
private Map<Entity, Path> entityPathMap; |
|
|
|
private EventBus eventBus; |
|
private ActionSystem actionSystem; |
|
private ShipGenerationSystem shipGenerationSystem; |
|
private ComponentMapper<PositionComponent> positionComponentMapper; |
|
|
|
GridMap gridMap; |
|
GreasedRegion floors; |
|
char[][] map; |
|
|
|
|
|
@Override |
|
protected void initialize() { |
|
this.entityPathMap = new HashMap<>(); |
|
this.gridMap = shipGenerationSystem.getSpacecraft().gridMap; |
|
int width = gridMap.getWidthInTiles(), height = gridMap.getHeightInTiles(); |
|
this.map = new char[width][height]; |
|
for(int x = 0; x < width; x++) { |
|
for(int y = 0; y < height; y++) { |
|
switch(gridMap.getTile(x,y)) { |
|
case AETHER: |
|
map[x][y] = '#'; |
|
break; |
|
case VACUUM: |
|
map[x][y] = '#'; |
|
break; |
|
case INTERNALS: |
|
map[x][y] = '#'; |
|
break; |
|
case WALL: |
|
map[x][y] = '#'; |
|
break; |
|
case REINFORCED_WALL: |
|
map[x][y] = '#'; |
|
break; |
|
case ARMOR: |
|
map[x][y] = '#'; |
|
break; |
|
case DOOR: |
|
map[x][y] = '.'; |
|
break; |
|
case FLOOR: |
|
map[x][y] = '.'; |
|
break; |
|
} |
|
} |
|
} |
|
this.floors = new GreasedRegion(map, '.'); |
|
} |
|
|
|
@Subscribe |
|
void handleEvent(StartWanderingEvent event) { |
|
System.out.println("Starting to wander."); |
|
Entity eventEntity = event.getEntity(); |
|
PositionComponent positionComponent = positionComponentMapper.get(eventEntity); |
|
Vector2 position = positionComponent.getPosition(); |
|
int x = (int)position.x, y = (int)position.y; |
|
Point2i pointToWanderTo = shipGenerationSystem.getSpacecraft().findRoomContainingPoint(new Point2i(x,y)).findRandomTileInRoom(); |
|
|
|
IndexedAStarPathFinder<Coord> astar = new IndexedAStarPathFinder<Coord>(new GridGraph(this.map, floors.asCoords(), 1024)); |
|
GraphPath<Coord> dgp = new DefaultGraphPath<Coord>((1024 - 2) * (1024 - 2)); |
|
Heuristic<Coord> heu = new Heuristic<Coord>() { |
|
@Override |
|
public float estimate(Coord node, Coord endNode) { |
|
return Math.abs(node.x - endNode.x) + Math.abs(node.y - endNode.y); |
|
} |
|
}; |
|
|
|
DungeonUtility utility = new DungeonUtility(new StatefulRNG(new LightRNG(0x1337BEEFDEAL))); |
|
dgp.clear(); |
|
astar.searchNodePath(Coord.get(pointToWanderTo.x, pointToWanderTo.y), Coord.get(x,y), heu, dgp); |
|
|
|
// entityPathMap.put(eventEntity, new Path(dijkstraMap.findPath(100, null, null, Coord.get(x,y), Coord.get(pointToWanderTo.x, pointToWanderTo.y)))); |
|
} |
|
|
|
@Subscribe |
|
void handleEvent(WanderEvent event) { |
|
System.out.println("Wandering."); |
|
Entity eventEntity = event.getEntity(); |
|
PositionComponent positionComponent = positionComponentMapper.get(eventEntity); |
|
Vector2 position = positionComponent.getPosition(); |
|
int x = (int)position.x, y = (int)position.y; |
|
Coord nextCoord = null; |
|
Path path = entityPathMap.get(eventEntity); |
|
Iterator<Coord> pathIterator = path.getPath().iterator(); |
|
while(pathIterator.hasNext()) { |
|
Coord coord = pathIterator.next(); |
|
if(coord.x == x && coord.y == y) { |
|
if(pathIterator.hasNext()) |
|
nextCoord = pathIterator.next(); |
|
else |
|
nextCoord = coord; |
|
} |
|
} |
|
if(nextCoord == null) |
|
nextCoord = path.getPath().get(0); |
|
actionSystem.setNextAction(eventEntity, new MoveToAction(nextCoord)); |
|
} |
|
|
|
@Subscribe |
|
void handleEvent(StopWanderingEvent event) { |
|
System.out.println("Stopping wandering."); |
|
Entity eventEntity = event.getEntity(); |
|
entityPathMap.put(eventEntity, null); |
|
} |
|
|
|
public Map<Entity, Path> getEntityDestinationMap() { |
|
return this.entityPathMap; |
|
} |
|
} |