-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.py
117 lines (101 loc) · 4.54 KB
/
Block.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import settings
from pyglet.gl import *
from pyglet.window import key
import math
import os
import copy
#THIS FILE ADDS A BLOCK TO THE DICTIONARY OF BLOCKS AND ADDS EACH FACE, ALSO DRAWS BLOCKS WHEN CALLED
def cubePoints(x, y, z, n):
return [
(x-n,y,z-n, x-n,y,z, x,y,z, x,y,z-n), # top
(x-n,y-n,z-n, x,y-n,z-n, x,y-n,z, x-n,y-n,z), # bottom
(x-n,y-n,z-n, x-n,y-n,z, x-n,y,z, x-n,y,z-n), # left
(x,y-n,z, x,y-n,z-n, x,y,z-n, x,y,z), # right
(x-n,y-n,z, x,y-n,z, x,y,z, x-n,y,z), # front
(x,y-n,z-n, x-n,y-n,z-n, x-n,y,z-n, x,y,z-n), # back
]
def tuplePoints(x, y, z, n):
return (
(x-n,y,z-n, x-n,y,z, x,y,z, x,y,z-n), # top
(x-n,y-n,z-n, x,y-n,z-n, x,y-n,z, x-n,y-n,z), # bottom
(x-n,y-n,z-n, x-n,y-n,z, x-n,y,z, x-n,y,z-n), # left
(x,y-n,z, x,y-n,z-n, x,y,z-n, x,y,z), # right
(x-n,y-n,z, x,y-n,z, x,y,z, x-n,y,z), # front
(x,y-n,z-n, x-n,y-n,z-n, x-n,y,z-n, x,y,z-n), # back
)
def getTopFace(x,y,z,n):
return (x-n,y,z-n, x-n,y,z, x,y,z, x,y,z-n)
def getTexture(file):
tex = pyglet.image.load(file).get_texture()
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)
class Block(object):
def __init__(self):
self.blue = getTexture(os.path.join(settings.texturesFolder,'BlueTex.png'))
self.red = getTexture(os.path.join(settings.texturesFolder,'RedTex.png'))
self.orange = getTexture(os.path.join(settings.texturesFolder,'OrangeTex.png'))
self.purple = getTexture(os.path.join(settings.texturesFolder,'PurpleTex.png'))
self.texCoords = ('t2f',(0,0,.781,0,.781,.781,0,.781))
def drawBlocks(self,blockDict):
blockDict = copy.copy(blockDict)
self.blocks = pyglet.graphics.Batch()
color = ('c3f',(26/255,124/255,6/255)*4)
self.blocks.add(4,GL_QUADS,None,('v3f',(0,0,0, 0,0,settings.mapSize, settings.mapSize,0,settings.mapSize ,settings.mapSize, 0 ,0)),color)
for block in blockDict:
if blockDict[block] == 1:
x,y,z = block
blockCoords = cubePoints(x,y,z,1)
for face in range(0,6):
self.blocks.add(4,GL_QUADS,self.blue,('v3f',blockCoords[face]),self.texCoords)
if blockDict[block] == 2:
x,y,z = block
blockCoords = cubePoints(x,y,z,1)
for face in range(0,6):
self.blocks.add(4,GL_QUADS,self.red,('v3f',blockCoords[face]),self.texCoords)
if blockDict[block] == 3:
x,y,z = block
blockCoords = cubePoints(x,y,z,1)
for face in range(0,6):
self.blocks.add(4,GL_QUADS,self.purple,('v3f',blockCoords[face]),self.texCoords)
if blockDict[block] == 4:
x,y,z = block
blockCoords = cubePoints(x,y,z,1)
for face in range(0,6):
self.blocks.add(4,GL_QUADS,self.orange,('v3f',blockCoords[face]),self.texCoords)
self.blocks.draw()
class OrangeBlock(object):
def __init__(self,x,y,z):
X,Y,Z = x,y,z
if settings.blockInit.get((X,Y,Z),None) == None:
settings.blockInit[(X,Y,Z)] = 4
if tuplePoints(X,Y,Z,1) not in settings.faces:
settings.faces.add(tuplePoints(X,Y,Z,1))
class BlueBlock(object):
def __init__(self,x,y,z):
X,Y,Z = x,y,z
if settings.blockInit.get((X,Y,Z),None) == None:
settings.blockInit[(X,Y,Z)] = 1
if tuplePoints(X,Y,Z,1) not in settings.faces:
settings.faces.add(tuplePoints(X,Y,Z,1))
class RedBlock(object):
def __init__(self,x,y,z):
X,Y,Z = x,y,z
if settings.blockInit.get((X,Y,Z),None) == None:
settings.blockInit[(X,Y,Z)] = 2
if tuplePoints(X,Y,Z,1) not in settings.faces:
settings.faces.add(tuplePoints(X,Y,Z,1))
class PurpleBlock(object):
def __init__(self,x,y,z):
X,Y,Z = x,y,z
if settings.blockInit.get((X,Y,Z),None) == None:
settings.blockInit[(X,Y,Z)] =3
if tuplePoints(X,Y,Z,1) not in settings.faces:
settings.faces.add(tuplePoints(X,Y,Z,1))
class GroundBlock(object):
def __init__(self,x,z):
X,Y,Z = x,0,z
if settings.blockInit.get((X,Y,Z),None) == None:
settings.blockInit[(X,Y,Z)] =0
if tuplePoints(X,Y,Z,1) not in settings.faces:
settings.faces.add(tuplePoints(X,Y,Z,1)[0])