AI-Project/survival/systems/collision_system.py

31 lines
1.0 KiB
Python
Raw Normal View History

import operator
2021-03-28 18:05:52 +02:00
from survival import esper
from survival.components.moving_component import MovingComponent
from survival.components.position_component import PositionComponent
from survival.enums import Direction
2021-03-28 18:05:52 +02:00
class CollisionSystem(esper.Processor):
def __init__(self, game_map):
self.map = game_map
def process(self, dt):
for ent, (pos, moving) in self.world.get_components(PositionComponent, MovingComponent):
if moving.target is not None:
2021-03-28 18:05:52 +02:00
continue
moving.checked_collision = True
vector = Direction.get_vector(pos.direction)
moving.target = tuple(map(operator.add, vector, pos.grid_position))
moving.direction_vector = vector
if self.check_collision(moving.target):
self.world.remove_component(ent, MovingComponent)
2021-03-28 18:05:52 +02:00
else:
self.map.move_entity(pos.grid_position, moving.target)
pos.grid_position = moving.target
2021-03-28 18:05:52 +02:00
def check_collision(self, pos):
return self.map.is_colliding(pos)