65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import random
|
|
|
|
def generate_tmx():
|
|
width = 30
|
|
height = 20
|
|
tile_width = 256
|
|
tile_height = 256
|
|
|
|
# GIDs based on the tileset we created:
|
|
# 1: Grass (Base)
|
|
# 2: Water
|
|
# 3: Tall Grass
|
|
# 4: Dense Grass
|
|
|
|
# 1. Create Data CSV
|
|
# Initialize with Grass (1)
|
|
grid = [[1 for _ in range(width)] for _ in range(height)]
|
|
|
|
# Add a River (Water = 2)
|
|
# Simple sine wave river
|
|
import math
|
|
for x in range(width):
|
|
# vary y around center (10)
|
|
y_center = 10 + int(3 * math.sin(x * 0.2))
|
|
if 0 <= y_center < height:
|
|
grid[y_center][x] = 2
|
|
# Make river wider?
|
|
if y_center + 1 < height: grid[y_center+1][x] = 2
|
|
|
|
# Add random Foliage (3 or 4) on Grass only
|
|
for y in range(height):
|
|
for x in range(width):
|
|
if grid[y][x] == 1: # If grass
|
|
if random.random() < 0.1: # 10% chance
|
|
grid[y][x] = random.choice([3, 4])
|
|
|
|
# Convert to CSV string
|
|
data_lines = []
|
|
for row in grid:
|
|
data_lines.append(",".join(map(str, row)))
|
|
csv_data = ",\n".join(data_lines)
|
|
|
|
# 2. Construct TMX Content
|
|
# defined relative path to tileset: assets/ground_tileset.tsx (assuming level1.tmx is in main/, and assets is main/assets)
|
|
|
|
tmx_content = f"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<map version="1.10" tiledversion="1.11.2" orientation="orthogonal" renderorder="right-down" width="{width}" height="{height}" tilewidth="{tile_width}" tileheight="{tile_height}" infinite="0" nextlayerid="2" nextobjectid="1">
|
|
<tileset firstgid="1" source="assets/ground_tileset.tsx"/>
|
|
<layer id="1" name="Tile Layer 1" width="{width}" height="{height}">
|
|
<data encoding="csv">
|
|
{csv_data}
|
|
</data>
|
|
</layer>
|
|
</map>
|
|
"""
|
|
|
|
output_path = 'main/level1.tmx'
|
|
with open(output_path, 'w') as f:
|
|
f.write(tmx_content)
|
|
|
|
print(f"Generated {output_path}")
|
|
|
|
if __name__ == "__main__":
|
|
generate_tmx()
|