Files
Lol/input_commands.py

49 lines
1.8 KiB
Python

from typing import List, Dict, Tuple
import pygame
class InputCommands:
def __init__(self):
pass
def generate_commands(self, events: List, game_state) -> List[Dict]:
commands = []
camera = getattr(game_state, 'camera', {})
for event in events:
command = self._process_event(event, camera)
if command:
commands.append(command)
return commands
def _process_event(self, event, camera: Dict) -> Dict:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left click
world_x, world_y = self.screen_to_world(event.pos[0], event.pos[1], camera)
return {'type': 'select', 'world_pos': (world_x, world_y)}
elif event.button == 3: # Right click
world_x, world_y = self.screen_to_world(event.pos[0], event.pos[1], camera)
return {'type': 'move', 'world_pos': (world_x, world_y)}
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
return {'type': 'build', 'building_type': 'barracks'}
elif event.key == pygame.K_s:
return {'type': 'build', 'building_type': 'supply_depot'}
elif event.key == pygame.K_g:
return {'type': 'gather'}
return None
def screen_to_world(self, x: int, y: int, camera: Dict) -> Tuple[float, float]:
camera_x = camera.get('x', 0)
camera_y = camera.get('y', 0)
zoom = camera.get('zoom', 1.0)
world_x = (x / zoom) + camera_x
world_y = (y / zoom) + camera_y
return world_x, world_y