131 lines
4.4 KiB
Python
131 lines
4.4 KiB
Python
import cv2
|
|
import numpy as np
|
|
import os
|
|
import random
|
|
|
|
def create_project_assets():
|
|
# Directories
|
|
repo_root = '/Users/davidkotnik/repos/novafarma'
|
|
target_dir = '/Users/davidkotnik/nova farma/main/assets'
|
|
|
|
# Ensure target directory exists
|
|
os.makedirs(target_dir, exist_ok=True)
|
|
|
|
# Source Paths (Repositories)
|
|
src_grass = os.path.join(repo_root, 'assets/DEMO_FAZA1/Ground/tla_trava_tekstura.png')
|
|
src_water = os.path.join(repo_root, 'assets/DEMO_FAZA1/Environment/stream_water.png')
|
|
src_high = os.path.join(repo_root, 'assets/DEMO_FAZA1/Vegetation/visoka_trava.png')
|
|
src_dense = os.path.join(repo_root, 'assets/DEMO_FAZA1/Vegetation/grass_cluster_dense.png')
|
|
|
|
# Load and Process Images
|
|
def load_resize_clean(path, clean_pink=False):
|
|
if not os.path.exists(path):
|
|
print(f"Missing source: {path}")
|
|
return np.zeros((256, 256, 4), dtype=np.uint8) # Fallback black square
|
|
|
|
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
|
|
|
|
# Ensure RGBA
|
|
if img.shape[2] == 3:
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
|
|
|
|
# Clean Pink (#FF00FF)
|
|
if clean_pink:
|
|
# Mask pink range
|
|
lower = np.array([250, 0, 250])
|
|
upper = np.array([255, 10, 255])
|
|
mask = cv2.inRange(img[:,:,:3], lower, upper)
|
|
img[mask > 0] = [0, 0, 0, 0] # Set transparent
|
|
|
|
# Resize to 256x256
|
|
return cv2.resize(img, (256, 256), interpolation=cv2.INTER_AREA)
|
|
|
|
# 1. Create Tileset Image (ground_tileset.png)
|
|
img_grass = load_resize_clean(src_grass)
|
|
img_water = load_resize_clean(src_water)
|
|
img_high = load_resize_clean(src_high, clean_pink=True)
|
|
img_dense = load_resize_clean(src_dense, clean_pink=True)
|
|
|
|
# Combine horizontally
|
|
tileset_img = np.hstack((img_grass, img_water, img_high, img_dense))
|
|
|
|
out_img_path = os.path.join(target_dir, 'ground_tileset.png')
|
|
cv2.imwrite(out_img_path, tileset_img)
|
|
print(f"Created {out_img_path}")
|
|
|
|
# 2. Create Tileset Definition (nova_farma.tsx)
|
|
# 4 tiles, 256x256
|
|
tsx_content = f"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<tileset version="1.10" tiledversion="1.11.0" name="nova_farma" tilewidth="256" tileheight="256" tilecount="4" columns="4">
|
|
<image source="ground_tileset.png" width="{tileset_img.shape[1]}" height="{tileset_img.shape[0]}"/>
|
|
</tileset>
|
|
"""
|
|
out_tsx_path = os.path.join(target_dir, 'nova_farma.tsx')
|
|
with open(out_tsx_path, 'w') as f:
|
|
f.write(tsx_content)
|
|
print(f"Created {out_tsx_path}")
|
|
|
|
# 3. Create Map (mapa.tmx)
|
|
# Size 10x10
|
|
width, height = 10, 10
|
|
|
|
# GIDs:
|
|
# 1: Grass
|
|
# 2: Water
|
|
# 3: High Grass
|
|
# 4: Dense Grass
|
|
|
|
# Layer 0: Ground (All Grass)
|
|
layer0_data = [1] * (width * height)
|
|
|
|
# Layer 1: Water (Stream in middle)
|
|
# Let's say row 4 and 5 are water
|
|
layer1_data = [0] * (width * height)
|
|
for y in range(4, 6):
|
|
for x in range(width):
|
|
layer1_data[y * width + x] = 2
|
|
|
|
# Layer 2: Foliage (Random High/Dense Grass)
|
|
layer2_data = [0] * (width * height)
|
|
for i in range(len(layer2_data)):
|
|
# Only place on grass (not where water is)
|
|
if layer1_data[i] == 0:
|
|
if random.random() < 0.2: # 20% chance
|
|
layer2_data[i] = random.choice([3, 4])
|
|
|
|
# function to format CSV
|
|
def to_csv(data):
|
|
lines = []
|
|
for y in range(height):
|
|
row = data[y*width : (y+1)*width]
|
|
lines.append(",".join(map(str, row)))
|
|
return ",\n".join(lines)
|
|
|
|
tmx_content = f"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="{width}" height="{height}" tilewidth="256" tileheight="256" infinite="0" nextlayerid="4" nextobjectid="1">
|
|
<tileset firstgid="1" source="nova_farma.tsx"/>
|
|
<layer id="1" name="Plata" width="{width}" height="{height}">
|
|
<data encoding="csv">
|
|
{to_csv(layer0_data)}
|
|
</data>
|
|
</layer>
|
|
<layer id="2" name="Voda" width="{width}" height="{height}">
|
|
<data encoding="csv">
|
|
{to_csv(layer1_data)}
|
|
</data>
|
|
</layer>
|
|
<layer id="3" name="Foliage" width="{width}" height="{height}">
|
|
<data encoding="csv">
|
|
{to_csv(layer2_data)}
|
|
</data>
|
|
</layer>
|
|
</map>
|
|
"""
|
|
out_tmx_path = os.path.join(target_dir, 'mapa.tmx')
|
|
with open(out_tmx_path, 'w') as f:
|
|
f.write(tmx_content)
|
|
print(f"Created {out_tmx_path}")
|
|
|
|
if __name__ == "__main__":
|
|
create_project_assets()
|