2021-04-19 00:07:40 +02:00
|
|
|
import operator
|
|
|
|
|
2021-03-28 18:05:52 +02:00
|
|
|
from survival import esper
|
2021-06-19 00:04:34 +02:00
|
|
|
from survival.components.on_collision_component import OnCollisionComponent
|
2021-03-28 18:05:52 +02:00
|
|
|
from survival.components.moving_component import MovingComponent
|
|
|
|
from survival.components.position_component import PositionComponent
|
2021-06-19 00:09:14 +02:00
|
|
|
from survival.game.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):
|
2021-05-23 14:00:30 +02:00
|
|
|
for ent, (pos, moving, onCol) in self.world.get_components(PositionComponent, MovingComponent,
|
|
|
|
OnCollisionComponent):
|
2021-04-19 00:07:40 +02:00
|
|
|
if moving.target is not None:
|
2021-03-28 18:05:52 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
moving.checked_collision = True
|
2021-04-19 00:07:40 +02:00
|
|
|
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-06-19 00:04:34 +02:00
|
|
|
onCol.call_all()
|
2021-05-23 14:00:30 +02:00
|
|
|
colliding_object: int = self.map.get_entity(moving.target)
|
2021-05-23 14:33:21 +02:00
|
|
|
|
|
|
|
if colliding_object is None or not self.world.entity_exists(colliding_object):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if self.world.has_component(colliding_object, OnCollisionComponent):
|
2021-06-19 00:04:34 +02:00
|
|
|
self.world.component_for_entity(colliding_object, OnCollisionComponent).call_all()
|
2021-05-21 20:14:00 +02:00
|
|
|
|
2021-03-28 18:05:52 +02:00
|
|
|
else:
|
2021-04-19 00:07:40 +02:00
|
|
|
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)
|