2021-04-19 01:41:02 +02:00
|
|
|
from survival import esper
|
2021-04-19 12:05:33 +02:00
|
|
|
from survival.components.direction_component import DirectionChangeComponent
|
2021-04-19 01:41:02 +02:00
|
|
|
from survival.components.movement_component import MovementComponent
|
|
|
|
from survival.components.moving_component import MovingComponent
|
|
|
|
from survival.components.position_component import PositionComponent
|
|
|
|
from survival.enums import Direction
|
2021-04-19 17:20:40 +02:00
|
|
|
from survival.graph_search import graph_search, Action
|
2021-04-19 01:41:02 +02:00
|
|
|
from survival.systems.input_system import PathfindingComponent
|
|
|
|
|
|
|
|
|
2021-05-24 17:20:08 +02:00
|
|
|
class CollectingResourceComponent:
|
|
|
|
def __init__(self, action):
|
|
|
|
self.action = action
|
|
|
|
|
|
|
|
|
2021-04-19 01:41:02 +02:00
|
|
|
class PathfindingMovementSystem(esper.Processor):
|
|
|
|
def __init__(self, game_map):
|
|
|
|
self.game_map = game_map
|
|
|
|
|
|
|
|
def process(self, dt):
|
|
|
|
for ent, (pos, pathfinding, movement) in self.world.get_components(PositionComponent, PathfindingComponent,
|
|
|
|
MovementComponent):
|
|
|
|
if pathfinding.path is None:
|
2021-05-24 17:20:08 +02:00
|
|
|
pathfinding.path = graph_search(self.game_map, pos, pathfinding.target_grid_pos, self.world)
|
2021-04-19 01:41:02 +02:00
|
|
|
|
2021-04-19 17:20:40 +02:00
|
|
|
if len(pathfinding.path) < 1:
|
2021-04-19 01:41:02 +02:00
|
|
|
self.world.remove_component(ent, PathfindingComponent)
|
|
|
|
continue
|
|
|
|
|
2021-05-24 17:20:08 +02:00
|
|
|
if pathfinding.searching_for_resource and len(pathfinding.path) == 1:
|
|
|
|
self.world.add_component(ent, CollectingResourceComponent(pathfinding.path.pop(0)))
|
|
|
|
self.world.remove_component(ent, PathfindingComponent)
|
|
|
|
continue
|
|
|
|
|
2021-04-19 17:20:40 +02:00
|
|
|
if self.world.has_component(ent, MovingComponent) or self.world.has_component(ent, DirectionChangeComponent):
|
2021-04-19 01:41:02 +02:00
|
|
|
continue
|
|
|
|
|
2021-04-19 17:20:40 +02:00
|
|
|
action = pathfinding.path.pop(0)
|
2021-04-19 01:41:02 +02:00
|
|
|
|
2021-04-19 17:20:40 +02:00
|
|
|
if action == Action.ROTATE_LEFT:
|
|
|
|
self.world.add_component(ent, DirectionChangeComponent(Direction.rotate_left(pos.direction)))
|
|
|
|
elif action == Action.ROTATE_RIGHT:
|
|
|
|
self.world.add_component(ent, DirectionChangeComponent(Direction.rotate_right(pos.direction)))
|
|
|
|
else:
|
|
|
|
self.world.add_component(ent, MovingComponent())
|
|
|
|
|
|
|
|
# if pathfinding.path is None:
|
|
|
|
# pathfinding.path = breadth_first_search(self.game_map, pos.grid_position, pathfinding.target_grid_pos)
|
|
|
|
#
|
|
|
|
# if len(pathfinding.path) < 1 and pathfinding.current_target is None:
|
|
|
|
# self.world.remove_component(ent, PathfindingComponent)
|
|
|
|
# continue
|
|
|
|
#
|
|
|
|
# if self.world.has_component(ent, MovingComponent):
|
|
|
|
# continue
|
|
|
|
#
|
|
|
|
# if pathfinding.current_target is None:
|
|
|
|
# target = pathfinding.path.pop(0)
|
|
|
|
# else:
|
|
|
|
# target = pathfinding.current_target
|
|
|
|
#
|
|
|
|
# vector = (target[0] - pos.grid_position[0], target[1] - pos.grid_position[1])
|
|
|
|
# direction = Direction.from_vector(vector)
|
|
|
|
# if direction != pos.direction:
|
|
|
|
# pathfinding.current_target = target
|
|
|
|
# self.world.add_component(ent, DirectionChangeComponent(direction))
|
|
|
|
# continue
|
|
|
|
#
|
|
|
|
# pathfinding.current_target = None
|
|
|
|
# self.world.add_component(ent, MovingComponent())
|