AI-Project/survival/systems/input_system.py

41 lines
2.0 KiB
Python
Raw Normal View History

2021-03-28 18:05:52 +02:00
import pygame
2021-05-24 17:20:08 +02:00
from survival import esper, GameMap
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
2021-05-24 17:20:08 +02:00
from survival.components.resource_component import ResourceComponent
2021-03-28 18:05:52 +02:00
class InputSystem(esper.Processor):
2021-05-24 17:20:08 +02:00
def __init__(self, camera, game_map: GameMap):
2021-04-19 01:41:02 +02:00
self.camera = camera
2021-05-24 17:20:08 +02:00
self.game_map = game_map
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):
2021-05-24 17:20:08 +02:00
target_ent = self.game_map.get_entity([int(pos[0] / 32), int(pos[1]/ 32)])
if target_ent is not None and self.world.has_component(target_ent, ResourceComponent):
2021-06-06 19:55:55 +02:00
self.world.add_component(ent, PathfindingComponent(pos))
2021-05-24 17:20:08 +02:00
else:
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())