-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
92 lines (76 loc) · 3.92 KB
/
world.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import math
import random
import chunks as chunk
import blocktype as block_type
import texturemanager as texture_manager
import models.plant
import models.cactus
class World:
def __init__(self):
self.texture_manager = texture_manager.TextureManager(16, 16, 256)
self.block_types = [None]
self.block_types.append(block_type.BlockType(
self.texture_manager, "cobblestone", {"all": "cobblestone"}))
self.block_types.append(block_type.BlockType(self.texture_manager, "grass", {
"top": "grass", "bottom": "dirt", "sides": "grass_side"}))
self.block_types.append(block_type.BlockType(
self.texture_manager, "grass_block", {"all": "grass"}))
self.block_types.append(block_type.BlockType(
self.texture_manager, "dirt", {"all": "dirt"}))
self.block_types.append(block_type.BlockType(
self.texture_manager, "stone", {"all": "stone"}))
self.block_types.append(block_type.BlockType(
self.texture_manager, "sand", {"all": "sand"}))
self.block_types.append(block_type.BlockType(
self.texture_manager, "planks", {"all": "planks"}))
self.block_types.append(block_type.BlockType(self.texture_manager, "log", {
"top": "log_top", "bottom": "log_top", "sides": "log_side"}))
self.block_types.append(block_type.BlockType(
self.texture_manager, "daisy", {"all": "daisy"}, models.plant))
self.block_types.append(block_type.BlockType(
self.texture_manager, "rose", {"all": "rose"}, models.plant))
self.block_types.append(block_type.BlockType(self.texture_manager, "cactus", {
"top": "cactus_top", "bottom": "cactus_bottom", "sides": "cactus_side"}, models.cactus))
self.block_types.append(block_type.BlockType(
self.texture_manager, "dead_bush", {"all": "dead_bush"}, models.plant))
self.texture_manager.generate_mipmaps()
self.chunks = {}
for x in range(8):
for z in range(8):
chunk_position = (x - 4, -1, z - 4)
current_chunk = chunk.Chunk(self, chunk_position)
for i in range(chunk.CHUNK_WIDTH):
for j in range(chunk.CHUNK_HEIGHT):
for k in range(chunk.CHUNK_LENGTH):
if j == 15:
current_chunk.blocks[i][j][k] = random.choice(
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 11])
elif j > 12:
current_chunk.blocks[i][j][k] = random.choice([
0, 6])
else:
current_chunk.blocks[i][j][k] = random.choice([
0, 0, 5])
self.chunks[chunk_position] = current_chunk
for chunk_position in self.chunks:
self.chunks[chunk_position].update_mesh()
def get_block_number(self, position):
x, y, z = position
chunk_position = (
math.floor(x / chunk.CHUNK_WIDTH),
math.floor(y / chunk.CHUNK_HEIGHT),
math.floor(z / chunk.CHUNK_LENGTH))
if not chunk_position in self.chunks:
return 0
local_x = int(x % chunk.CHUNK_WIDTH)
local_y = int(y % chunk.CHUNK_HEIGHT)
local_z = int(z % chunk.CHUNK_LENGTH)
block_number = self.chunks[chunk_position].blocks[local_x][local_y][local_z]
block_type = self.block_types[block_number]
if not block_type or block_type.transparent:
return 0
else:
return block_number
def draw(self):
for chunk_position in self.chunks:
self.chunks[chunk_position].draw()