64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
import sys
|
|
from constants import TILE_WIDTH_HEIGHT
|
|
|
|
ENTITY_TYPE_MAP = {
|
|
'templates/NPC.tx': 'ENTITY_TYPE_NPC',
|
|
}
|
|
|
|
def parseEntity(obj, chunkData):
|
|
if 'type' in obj:
|
|
if obj['type'] not in ENTITY_TYPE_MAP:
|
|
print(f"Unknown entity type: {obj['type']}")
|
|
return None
|
|
|
|
entType = ENTITY_TYPE_MAP[obj['type']]
|
|
|
|
elif 'template' in obj:
|
|
if obj['template'] not in ENTITY_TYPE_MAP:
|
|
print(f"Unknown entity template: {obj['template']}")
|
|
return None
|
|
entType = ENTITY_TYPE_MAP[obj['template']]
|
|
|
|
else:
|
|
return None
|
|
|
|
if 'properties' not in obj:
|
|
obj['properties'] = {}
|
|
|
|
obj['localX'] = obj['x'] - (chunkData['topLeftTileX'] * TILE_WIDTH_HEIGHT)
|
|
obj['localY'] = obj['y'] - (chunkData['topLeftTileY'] * TILE_WIDTH_HEIGHT)
|
|
obj['dir'] = 'ENTITY_DIR_SOUTH'
|
|
obj['type'] = entType
|
|
|
|
def getProperty(propName):
|
|
for prop in obj['properties']:
|
|
if prop['name'] == propName:
|
|
return prop['value']
|
|
return None
|
|
|
|
# Handle per-type properties
|
|
if entType == 'ENTITY_TYPE_NPC':
|
|
interactType = getProperty('interactType')
|
|
if interactType is None:
|
|
print(f"NPC entity missing 'interactType' property: {obj['id']}")
|
|
sys.exit(1)
|
|
|
|
obj['data'] = {}
|
|
obj['data']['npc'] = {}
|
|
obj['data']['npc']['interactType'] = interactType
|
|
|
|
if interactType == 'NPC_INTERACT_TYPE_TEXT':
|
|
text = getProperty('interactText')
|
|
if text is None:
|
|
print(f"NPC entity missing 'interactText' property: {obj['id']}")
|
|
sys.exit(1)
|
|
obj['data']['npc']['text'] = text
|
|
|
|
elif interactType == 'NPC_INTERACT_TYPE_EVENT':
|
|
event = getProperty('interactEvent')
|
|
if event is None:
|
|
print(f"NPC entity missing 'interactEvent' property: {obj['id']}")
|
|
sys.exit(1)
|
|
obj['data']['npc']['eventData'] = f'&EVENT_{event.upper()}'
|
|
|
|
return obj |