AI-Project/survival/systems/pathfinding_movement_system.py

61 lines
2.8 KiB
Python
Raw Normal View History

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.game.enums import Direction
from survival.ai.graph_search import graph_search, Action
2021-04-19 01:41:02 +02:00
from survival.systems.input_system import PathfindingComponent
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-06-06 19:55:55 +02:00
pathfinding.path, cost = 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-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())