-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
163 lines (108 loc) · 5.16 KB
/
main.py
File metadata and controls
163 lines (108 loc) · 5.16 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import math # always useful to have
import ctypes # needed to interact with python at a lower level
import pyglet
pyglet.options["shadow_window"] = False # no need for shadow window and also bcz may cause problem on some os
pyglet.options["debug_gl"] = False # makes things slow, so disable it
import pyglet.gl as gl
import shader
import matrix # import matrix.py file
import block_type
import texture_manager
import camera
#import chunk
import world
import hit
class Window(pyglet.window.Window): # create a class extending pyglet.window.Window
def __init__(self, **args): #**args means all the argumenats are passed ot this function
super().__init__(**args) # pass on arguments to pyglet.window.Window.__init__ function
#create world
self.world = world.World()
# creating shader
self.shader = shader.Shader("vert.glsl", "frag.glsl")
self.shader_sampler_location = self.shader.find_uniform(b"texture_array_sampler") # find our texture array sampler's uniform
self.shader.use()
# pyglet things
pyglet.clock.schedule_interval(self.update, 1.0 / 10000) # call update function every 60th of a second
self.mouse_captured = False
# camera stuff
self.camera = camera.Camera(self.shader, self.width, self.height)
# misc stuff
self.holding = 7
def update(self, delta_time):
print(f"FPS :: {1.0 / delta_time}")
if not self.mouse_captured:
self.camera.input = [0, 0, 0]
self.camera.update_camera(delta_time)
#self.world.set_block(self.camera.position, 7)
def on_draw(self):
self.camera.update_matrices()
# bind textures
gl.glActiveTexture(gl.GL_TEXTURE0) # set our active texture unit to the first texture unit
gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.world.texture_manager.texture_array) # bind our texture manager's texture
gl.glUniform1i(self.shader_sampler_location, 0) # tell our sampler our texture is bound to the first texture unit
#draw stuff
gl.glEnable(gl.GL_DEPTH_TEST) # enable depth testing so faces are drawn in the right order
gl.glEnable(gl.GL_CULL_FACE)
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
#gl.glClearColor(1.0, 0.5, 1.0, 1.0) # set clear colour
self.clear() # clear screen
self.world.draw()
gl.glFinish() # there seems to be a bit of a bug in Pyglet which makes this call necessary
# input functions
def on_resize(self, width, height):
print(f"Resize {width} * {height}")
gl.glViewport(0, 0, width, height)
self.camera.width = width
self.camera.height = height
def on_mouse_press(self, x, y, button, modifiers):
if not self.mouse_captured:
self.mouse_captured = True
self.set_exclusive_mouse(True)
return
# handle breaking/placing blocks
def hit_callback(current_block, next_block):
if button == pyglet.window.mouse.RIGHT: self.world.set_block(current_block, self.holding)
elif button == pyglet.window.mouse.LEFT: self.world.set_block(next_block, 0)
elif button == pyglet.window.mouse.MIDDLE: self.holding = self.world.get_block_number(next_block)
hit_ray = hit.Hit_ray(self.world, self.camera.rotation, self.camera.position)
while hit_ray.distance < hit.HIT_RANGE:
if hit_ray.step(hit_callback):
break
def on_mouse_motion(self, x, y, delta_x, delta_y):
if self.mouse_captured:
sensitivity = 0.004
self.camera.rotation[0] += delta_x * sensitivity
self.camera.rotation[1] += delta_y * sensitivity
self.camera.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.camera.rotation[1]))
def on_mouse_drag(self, x, y, delta_x, delta_y, buttons, modifiers):
self.on_mouse_motion(x, y, delta_x, delta_y)
def on_key_press(self, key, modifiers):
if not self.mouse_captured:
return
if key == pyglet.window.key.D: self.camera.input[0] += 1
elif key == pyglet.window.key.A: self.camera.input[0] -= 1
elif key == pyglet.window.key.W: self.camera.input[2] += 1
elif key == pyglet.window.key.S: self.camera.input[2] -= 1
elif key == pyglet.window.key.SPACE : self.camera.input[1] += 1
elif key == pyglet.window.key.LSHIFT: self.camera.input[1] -= 1
elif key == pyglet.window.key.ESCAPE:
self.mouse_captured = False
self.set_exclusive_mouse(False)
def on_key_release(self, key, modifiers):
if not self.mouse_captured:
return
if key == pyglet.window.key.D: self.camera.input[0] -= 1
elif key == pyglet.window.key.A: self.camera.input[0] += 1
elif key == pyglet.window.key.W: self.camera.input[2] -= 1
elif key == pyglet.window.key.S: self.camera.input[2] += 1
elif key == pyglet.window.key.SPACE : self.camera.input[1] -= 1
elif key == pyglet.window.key.LSHIFT: self.camera.input[1] += 1
class Game:
def __init__(self):
self.config = gl.Config(double_buffer = True, major_version = 3, minor_version = 3, depth_size = 16) # use modern opengl, changeing depth buffers to 16 bits (24 bits is the defalut) 24 bit causes problem on some hardware
self.window = Window(config = self.config, width = 800, height = 600, caption = "Sasta Minecraft", resizable = True, vsync = False) # vsync with pyglet causes problems on some computers, so disable it
def run(self):
pyglet.app.run() # run our application
if __name__ == "__main__": # only run the game if source file is the one run
game = Game() # create game object
game.run()