Update world.py
parent
c0f4872248
commit
e9c27990f1
74
world.py
74
world.py
|
|
@ -1,6 +1,7 @@
|
||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
from settings import *
|
from settings import *
|
||||||
|
from tiles import get_tile
|
||||||
|
|
||||||
|
|
||||||
class World:
|
class World:
|
||||||
|
|
@ -20,6 +21,7 @@ class World:
|
||||||
# WORLD GENERATION
|
# WORLD GENERATION
|
||||||
# ==========================================================
|
# ==========================================================
|
||||||
def generate_world(self):
|
def generate_world(self):
|
||||||
|
# First generate terrain
|
||||||
for x in range(self.width):
|
for x in range(self.width):
|
||||||
|
|
||||||
# Simple terrain height variation
|
# Simple terrain height variation
|
||||||
|
|
@ -39,6 +41,10 @@ class World:
|
||||||
else:
|
else:
|
||||||
self.grid[y][x] = STONE
|
self.grid[y][x] = STONE
|
||||||
|
|
||||||
|
# Then add trees + ores
|
||||||
|
self.generate_trees()
|
||||||
|
self.generate_ores()
|
||||||
|
|
||||||
# ==========================================================
|
# ==========================================================
|
||||||
# DRAW
|
# DRAW
|
||||||
# ==========================================================
|
# ==========================================================
|
||||||
|
|
@ -57,7 +63,7 @@ class World:
|
||||||
tile_id = self.grid[y][x]
|
tile_id = self.grid[y][x]
|
||||||
|
|
||||||
if tile_id != AIR:
|
if tile_id != AIR:
|
||||||
color = TILE_PROPERTIES[tile_id]["color"]
|
color = get_tile(tile_id).color # FIXED: no more KeyError
|
||||||
|
|
||||||
world_rect = pygame.Rect(
|
world_rect = pygame.Rect(
|
||||||
x * TILE_SIZE,
|
x * TILE_SIZE,
|
||||||
|
|
@ -98,7 +104,7 @@ class World:
|
||||||
|
|
||||||
tiles.append({
|
tiles.append({
|
||||||
"rect": tile_rect,
|
"rect": tile_rect,
|
||||||
"solid": TILE_PROPERTIES[tile_id]["solid"],
|
"solid": get_tile(tile_id).collidable,
|
||||||
"id": tile_id,
|
"id": tile_id,
|
||||||
"x": x,
|
"x": x,
|
||||||
"y": y
|
"y": y
|
||||||
|
|
@ -109,14 +115,18 @@ class World:
|
||||||
# ==========================================================
|
# ==========================================================
|
||||||
# BLOCK BREAKING
|
# BLOCK BREAKING
|
||||||
# ==========================================================
|
# ==========================================================
|
||||||
def break_block(self, mouse_pos, camera):
|
def break_block(self, mouse_pos, camera, inventory):
|
||||||
world_x, world_y = camera.screen_to_world(mouse_pos)
|
world_x, world_y = camera.screen_to_world(mouse_pos)
|
||||||
|
|
||||||
tile_x = int(world_x // TILE_SIZE)
|
tile_x = int(world_x // TILE_SIZE)
|
||||||
tile_y = int(world_y // TILE_SIZE)
|
tile_y = int(world_y // TILE_SIZE)
|
||||||
|
|
||||||
if self.in_bounds(tile_x, tile_y):
|
if self.in_bounds(tile_x, tile_y):
|
||||||
if self.grid[tile_y][tile_x] != AIR:
|
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
|
self.grid[tile_y][tile_x] = AIR
|
||||||
|
|
||||||
# ==========================================================
|
# ==========================================================
|
||||||
|
|
@ -137,3 +147,57 @@ class World:
|
||||||
# ==========================================================
|
# ==========================================================
|
||||||
def in_bounds(self, x, y):
|
def in_bounds(self, x, y):
|
||||||
return 0 <= x < self.width and 0 <= y < self.height
|
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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue