Projectdd/world.py

204 lines
6.7 KiB
Python

import pygame
import random
from settings import *
from tiles import get_tile
class World:
def __init__(self):
self.width = WORLD_WIDTH
self.height = WORLD_HEIGHT
# 2D grid: world[y][x]
self.grid = [
[AIR for _ in range(self.width)]
for _ in range(self.height)
]
self.generate_world()
# ==========================================================
# WORLD GENERATION
# ==========================================================
def generate_world(self):
# First generate terrain
for x in range(self.width):
# Simple terrain height variation
surface_height = SURFACE_LEVEL + random.randint(-3, 3)
for y in range(self.height):
if y < surface_height:
self.grid[y][x] = AIR
elif y == surface_height:
self.grid[y][x] = GRASS
elif y < surface_height + 5:
self.grid[y][x] = DIRT
else:
self.grid[y][x] = STONE
# Then add trees + ores
self.generate_trees()
self.generate_ores()
# ==========================================================
# DRAW
# ==========================================================
def draw(self, screen, camera):
# Determine visible tile range
start_x = max(0, camera.offset.x // TILE_SIZE)
end_x = min(self.width, (camera.offset.x + SCREEN_WIDTH) // TILE_SIZE + 2)
start_y = max(0, camera.offset.y // TILE_SIZE)
end_y = min(self.height, (camera.offset.y + SCREEN_HEIGHT) // TILE_SIZE + 2)
for y in range(int(start_y), int(end_y)):
for x in range(int(start_x), int(end_x)):
tile_id = self.grid[y][x]
if tile_id != AIR:
color = get_tile(tile_id).color # FIXED: no more KeyError
world_rect = pygame.Rect(
x * TILE_SIZE,
y * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
)
screen_rect = camera.apply(world_rect)
pygame.draw.rect(screen, color, screen_rect)
# ==========================================================
# COLLISION SUPPORT
# ==========================================================
def get_nearby_tiles(self, rect):
tiles = []
# Determine tile range around player
start_x = max(0, rect.left // TILE_SIZE - 1)
end_x = min(self.width, rect.right // TILE_SIZE + 2)
start_y = max(0, rect.top // TILE_SIZE - 1)
end_y = min(self.height, rect.bottom // TILE_SIZE + 2)
for y in range(start_y, end_y):
for x in range(start_x, end_x):
tile_id = self.grid[y][x]
if tile_id != AIR:
tile_rect = pygame.Rect(
x * TILE_SIZE,
y * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
)
tiles.append({
"rect": tile_rect,
"solid": get_tile(tile_id).collidable,
"id": tile_id,
"x": x,
"y": y
})
return tiles
# ==========================================================
# BLOCK BREAKING
# ==========================================================
def break_block(self, mouse_pos, camera, inventory):
world_x, world_y = camera.screen_to_world(mouse_pos)
tile_x = int(world_x // TILE_SIZE)
tile_y = int(world_y // TILE_SIZE)
if self.in_bounds(tile_x, tile_y):
tile_id = self.grid[tile_y][tile_x]
tile = get_tile(tile_id)
if tile_id != AIR:
if tile.drop:
inventory.add_item(tile.drop, 1)
self.grid[tile_y][tile_x] = AIR
# ==========================================================
# BLOCK PLACING
# ==========================================================
def place_block(self, mouse_pos, camera, block_type=DIRT):
world_x, world_y = camera.screen_to_world(mouse_pos)
tile_x = int(world_x // TILE_SIZE)
tile_y = int(world_y // TILE_SIZE)
if self.in_bounds(tile_x, tile_y):
if self.grid[tile_y][tile_x] == AIR:
self.grid[tile_y][tile_x] = block_type
# ==========================================================
# UTIL
# ==========================================================
def in_bounds(self, x, y):
return 0 <= x < self.width and 0 <= y < self.height
# ==========================================================
# NEW: Get surface height for player spawn
# ==========================================================
def get_surface_height(self, x):
for y in range(self.height):
if self.grid[y][x] != AIR:
return y
return self.height - 1
# ==========================================================
# TREE GENERATION
# ==========================================================
def generate_trees(self):
for x in range(0, self.width, 6):
if random.random() < 0.25: # 25% chance
# find surface
for y in range(self.height):
if self.grid[y][x] == GRASS:
self.spawn_tree(x, y)
break
def spawn_tree(self, x, surface_y):
# trunk height
height = random.randint(4, 7)
for i in range(height):
self.grid[surface_y - 1 - i][x] = WOOD
# leaves
leaf_start = surface_y - height - 1
for y in range(leaf_start, leaf_start - 4, -1):
for lx in range(x - 2, x + 3):
if 0 <= lx < self.width and 0 <= y < self.height:
if random.random() > 0.25:
self.grid[y][lx] = LEAVES
# ==========================================================
# ORE GENERATION
# ==========================================================
def generate_ores(self):
for _ in range(250):
x = random.randint(0, self.width - 1)
y = random.randint(SURFACE_LEVEL + 5, self.height - 1)
if self.grid[y][x] == STONE:
r = random.random()
if r < 0.5:
self.grid[y][x] = COAL_ORE
elif r < 0.75:
self.grid[y][x] = COPPER_ORE
elif r < 0.9:
self.grid[y][x] = IRON_ORE
else:
self.grid[y][x] = GOLD_ORE