Files
Lol/entity_creation.py

141 lines
2.3 KiB
Python

class EntityCreation:
def __init__(self):
self.entity_templates = {
'worker': {
'type': 'unit',
'name': 'Worker',
'health': 100,
'movable': True,
'speed': 40.0,
'gather_rate': 10,
'build_power': 5
},
'barracks': {
'type': 'building',
'name': 'Barracks',
'health': 500,
'movable': False,
'build_time': 30.0
},
'supply_depot': {
'type': 'building',
'name': 'Supply Depot',
'health': 400,
'movable': False,
'build_time': 20.0
},
'mineral_field': {
'type': 'resource',
'name': 'Mineral Field',
'resources': {'minerals': 1500}
}
}
def create_entity(self, state, create_cmd: dict) -> dict:
entity_type = create_cmd.get('entity_type')
position = create_cmd.get('position', (0, 0))
if entity_type not in self.entity_templates:
return None
entity_def = self.entity_templates[entity_type]
entity = self.init_entity(entity_def, position)
return {
'type': 'add_entity',
'entity': entity
}
def init_entity(self, entity_def: dict, position: tuple) -> dict:
entity = entity_def.copy()
entity['x'] = position[0]
entity['y'] = position[1]
if entity['type'] == 'unit':
entity['id'] = self._generate_id()
entity['state'] = 'idle'
elif entity['type'] == 'building':
entity['id'] = self._generate_id()
entity['build_progress'] = 0.0
entity['completed'] = False
elif entity['type'] == 'resource':
entity['id'] = self._generate_id()
return entity
def _generate_id(self) -> int:
return id(self)