Загрузить файлы в «/»
This commit is contained in:
92
input_commands.py
Normal file
92
input_commands.py
Normal file
@@ -0,0 +1,92 @@
|
||||
|
||||
|
||||
from typing import List, Dict, Tuple
|
||||
|
||||
|
||||
|
||||
class InputCommands:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def generate_commands(self, events: List, snapshot: Dict) -> List[Dict]:
|
||||
|
||||
commands = []
|
||||
|
||||
camera = snapshot.get('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 == event.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 == event.KEYDOWN:
|
||||
|
||||
if event.key == event.K_b:
|
||||
|
||||
return {'type': 'build', 'building_type': 'barracks'}
|
||||
|
||||
elif event.key == event.K_s:
|
||||
|
||||
return {'type': 'build', 'building_type': 'supply_depot'}
|
||||
|
||||
elif event.key == event.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
|
||||
|
||||
Reference in New Issue
Block a user