AI-Project/survival/systems/input_system.py

27 lines
1.2 KiB
Python
Raw Normal View History

2021-03-28 18:05:52 +02:00
import pygame
from survival import esper
from survival.components.input_component import InputComponent
from survival.components.moving_component import MovingComponent
from survival.components.position_component import PositionComponent
class InputSystem(esper.Processor):
def __init__(self):
self.map = None
def process(self, dt):
for ent, (inp, pos) in self.world.get_components(InputComponent, PositionComponent):
keys = pygame.key.get_pressed()
if self.world.has_component(ent, MovingComponent):
continue
if keys[pygame.K_LEFT]:
self.world.add_component(ent, MovingComponent([-1, 0], [pos.grid_position[0] - 1, pos.grid_position[1]]))
elif keys[pygame.K_RIGHT]:
self.world.add_component(ent, MovingComponent([1, 0], [pos.grid_position[0] + 1, pos.grid_position[1]]))
elif keys[pygame.K_DOWN]:
self.world.add_component(ent, MovingComponent([0, 1], [pos.grid_position[0], pos.grid_position[1] + 1]))
elif keys[pygame.K_UP]:
self.world.add_component(ent, MovingComponent([0, -1], [pos.grid_position[0], pos.grid_position[1] - 1]))