AI-Project/survival/systems/input_system.py

35 lines
1.6 KiB
Python
Raw Normal View History

2021-03-28 18:05:52 +02:00
import pygame
from survival import esper
2021-04-19 12:05:33 +02:00
from survival.components.direction_component import DirectionChangeComponent
2021-03-28 18:05:52 +02:00
from survival.components.input_component import InputComponent
from survival.components.moving_component import MovingComponent
2021-04-19 01:41:02 +02:00
from survival.components.pathfinding_component import PathfindingComponent
2021-03-28 18:05:52 +02:00
from survival.components.position_component import PositionComponent
class InputSystem(esper.Processor):
2021-04-19 01:41:02 +02:00
def __init__(self, camera):
self.camera = camera
2021-03-28 18:05:52 +02:00
def process(self, dt):
for ent, (inp, pos) in self.world.get_components(InputComponent, PositionComponent):
keys = pygame.key.get_pressed()
2021-04-19 01:41:02 +02:00
mouse = pygame.mouse.get_pressed(3)
if mouse[0] == 1:
pos = pygame.mouse.get_pos()
pos = (pos[0] - self.camera.camera.left, pos[1] - self.camera.camera.top)
2021-04-19 17:20:40 +02:00
if not self.world.has_component(ent, PathfindingComponent):
self.world.add_component(ent, PathfindingComponent(pos))
2021-03-28 18:05:52 +02:00
if self.world.has_component(ent, MovingComponent):
continue
if keys[pygame.K_LEFT]:
2021-04-19 12:05:33 +02:00
if not self.world.has_component(ent, DirectionChangeComponent):
self.world.add_component(ent, DirectionChangeComponent(pos.rotate_left()))
2021-03-28 18:05:52 +02:00
elif keys[pygame.K_RIGHT]:
2021-04-19 12:05:33 +02:00
if not self.world.has_component(ent, DirectionChangeComponent):
self.world.add_component(ent, DirectionChangeComponent(pos.rotate_right()))
2021-03-28 18:05:52 +02:00
elif keys[pygame.K_UP]:
self.world.add_component(ent, MovingComponent())