1 | import random as rnd |
||
2 | from ed2d import window |
||
3 | from ed2d import sysevents |
||
4 | from ed2d.events import Events |
||
5 | from ed2d import context |
||
6 | from ed2d import timing |
||
7 | from ed2d import files |
||
8 | from ed2d import shaders |
||
9 | from ed2d.opengl import gl |
||
10 | from ed2d.opengl import pgl |
||
11 | from gem import vector |
||
12 | from gem import matrix |
||
13 | from ed2d import texture |
||
14 | from ed2d import mesh |
||
15 | from ed2d.physics import rectangle |
||
16 | from ed2d.physics import cmodel |
||
17 | from ed2d.physics import physobj |
||
18 | from ed2d.physics import physengine |
||
19 | from ed2d.physics import primitives |
||
20 | from ed2d.physics import gjk |
||
21 | from ed2d.csg import csg |
||
22 | from ed2d import view |
||
23 | from ed2d import text |
||
24 | from ed2d.scenegraph import SceneGraph |
||
25 | from ed2d import audio |
||
26 | from ed2d import cursor |
||
27 | |||
28 | class GameManager(object): |
||
29 | ''' Entry point into the game, and manages the game in general ''' |
||
30 | def __init__(self): |
||
31 | |||
32 | self.width = 800 |
||
33 | self.height = 600 |
||
34 | self.title = "ed2d" |
||
35 | self.running = False |
||
36 | |||
37 | self.fpsTimer = timing.FpsCounter() |
||
38 | self.fpsEstimate = 0 |
||
39 | |||
40 | self.sysEvents = sysevents.SystemEvents() |
||
41 | self.window = window.Window(self.title, self.width, self.height, window.WindowedMode) |
||
42 | self.context = context.Context(3, 3, 2) |
||
43 | self.context.window = self.window |
||
44 | |||
45 | Events.add_listener(self.process_event) |
||
46 | |||
47 | self.keys = [] |
||
48 | self.mouseButtons = [] |
||
49 | self.mousePosX = 0 |
||
50 | self.mousePosY = 0 |
||
51 | cursor.set_relative_mode(True) |
||
52 | cursor.show_cursor() |
||
53 | |||
54 | self.audio = audio.Audio() |
||
55 | audioPath = files.resolve_path('data', 'sound', 'test.wav') |
||
56 | self.audioFile = audio.AudioFile(audioPath) |
||
57 | self.audioFile.play() |
||
58 | print(self.audioFile.get_pos()) |
||
59 | print(self.audioFile.get_pos()) |
||
60 | |||
61 | gl.init() |
||
62 | major = pgl.glGetInteger(gl.GL_MAJOR_VERSION) |
||
63 | minor = pgl.glGetInteger(gl.GL_MINOR_VERSION) |
||
64 | print('OpenGL Version: {}.{}'.format(major, minor)) |
||
65 | |||
66 | gl.glViewport(0, 0, self.width, self.height) |
||
67 | |||
68 | # For CSG to work properly |
||
69 | gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) |
||
70 | gl.glEnable(gl.GL_DEPTH_TEST) |
||
71 | gl.glEnable(gl.GL_CULL_FACE) |
||
72 | |||
73 | vsPath = files.resolve_path('data', 'shaders', 'main.vs') |
||
74 | fsPath = files.resolve_path('data', 'shaders', 'main.fs') |
||
75 | |||
76 | vertex = shaders.VertexShader(vsPath) |
||
77 | fragment = shaders.FragmentShader(fsPath) |
||
78 | self.program = shaders.ShaderProgram(vertex, fragment) |
||
79 | self.program.use() |
||
80 | |||
81 | self.vao = pgl.glGenVertexArrays(1) |
||
82 | |||
83 | self.scenegraph = SceneGraph() |
||
84 | |||
85 | # Load character image into new opengl texture |
||
86 | imagePath = files.resolve_path('data', 'images', 'cubix.png') |
||
87 | self.texAtlas = texture.Texture(imagePath, self.program) |
||
88 | |||
89 | # Physics Test Scene |
||
90 | # Create a physics engine |
||
91 | self.physicsEngineTest = physengine.PhysEngine() |
||
92 | |||
93 | # Player |
||
94 | # Create a rectangle the long way, this will be the player |
||
95 | self.cModelTestRect = rectangle.Rectangle(100.0, 100.0, width=32.0, height=32.0) |
||
96 | self.cModelTestRect.update() |
||
97 | |||
98 | # Creating a object steps: |
||
99 | # Create a collision model object |
||
100 | # Create a physics object to simulate |
||
101 | # Create a mesh object to render |
||
102 | self.cModelTest = cmodel.cModel(self.cModelTestRect) |
||
103 | self.physicsObjectTest = physobj.PhysObj(self.cModelTest, vector.Vector(3, data=[0.0, 0.0, 1.0])) |
||
104 | self.physicsEngineTest.addObject(self.physicsObjectTest) |
||
105 | self.meshObjectTest = mesh.Mesh() |
||
106 | playerACSG = csg.CSG().cube([0, 0, 0], [1, 1, 1]) |
||
107 | playerBCSG = csg.CSG().sphere([0, 0, 0], 1.35, 16, 8) |
||
108 | playerACSG.setColor(0.5, 0.0, 1.0) |
||
109 | playerBCSG.setColor(1.0, 1.0, 0.0) |
||
110 | playerFCSG = playerACSG.subtract(playerBCSG) #change to subtract, union, intersect for different outcome |
||
111 | self.meshObjectTest.fromCSG(playerFCSG) |
||
112 | self.meshObjectTest.addProgram(self.program) |
||
113 | self.meshObjectTest.addTexture(None) |
||
114 | self.meshObjectTest.addPhysicsObject(self.physicsObjectTest) |
||
115 | self.meshObjectTestID = self.scenegraph.establish(self.meshObjectTest) |
||
116 | # End Player |
||
117 | |||
118 | # Scene objects |
||
119 | # For now store all the mesh objects in here |
||
120 | # We need some sort of rendering engine class |
||
121 | |||
122 | print(self.audioFile.get_pos()) |
||
123 | |||
124 | for i in range(20): |
||
125 | xRND = rnd.randrange(1, (self.width-32)) |
||
126 | yRND = rnd.randrange(1, (self.height-32)) |
||
127 | # The creating object stuff from above... One Liner... Yes I know. :| |
||
128 | self.physicsEngineTest.addObject(physobj.PhysObj(cmodel.cModel(rectangle.Rectangle(xRND, yRND, width=32.0, height=32.0)), vector.Vector(3, data=[0.0, 0.0, 1.0]))) |
||
129 | tempObj = self.physicsEngineTest.getObject(i+1) |
||
130 | tempObj.getCollisionModel().getModel().scale(32, 32) |
||
131 | tempObj.getCollisionModel().getModel().update() |
||
132 | tempMesh = mesh.Mesh() |
||
133 | tempMesh.fromData(data=[ |
||
134 | [0.0, 1.0, 0.0], |
||
135 | [1.0, 1.0, 0.0], |
||
136 | [0.0, 0.0, 0.0], |
||
137 | [1.0, 0.0, 0.0]]) |
||
138 | #tempMesh.setColorAll(1.0, 0.0, 0.0) |
||
139 | tempMesh.addProgram(self.program) |
||
140 | tempMesh.addTexture(self.texAtlas) |
||
141 | tempMesh.addPhysicsObject(tempObj) |
||
142 | self.scenegraph.establish(tempMesh) |
||
143 | |||
144 | # End Scene Objects |
||
145 | |||
146 | |||
147 | |||
148 | # Create the collider |
||
149 | gjkTest = gjk.GJK() |
||
150 | |||
151 | # Box A and Box B collistion test, should return False |
||
152 | # Substract the origins and add the two rectangles together to form a bigger one |
||
153 | # If it include the origin, collision happens |
||
154 | boxTestA = primitives.Box(vector.Vector(3, data=[50, 50, 49]), 1, 1, 1, matrix.Matrix(4)) |
||
155 | boxTestB = primitives.Box(vector.Vector(3, data=[50, 50, 51]), 2, 2, 2, matrix.Matrix(4)) |
||
156 | |||
157 | # Rectangle A and Rectangle B collision test, should return False |
||
158 | # Substract the origins and add the two boxes together to form a bigger one |
||
159 | # If it include the origin, collision happens |
||
160 | rectTestA = primitives.Rectangle(vector.Vector(3, data=[10, 10, 0]), 2, 2, matrix.Matrix(4)) |
||
161 | rectTestB = primitives.Rectangle(vector.Vector(3, data=[50, 50, 50]), 2, 2, matrix.Matrix(4)) |
||
162 | |||
163 | # Circle A and Cirlce B collision test, should return False |
||
164 | # Substract the origins and add the radii |
||
165 | # If the new circle includes the origin, collision happens |
||
166 | circleTestA = primitives.Circle(vector.Vector(3, data=[50, 50, 50]), 1) |
||
167 | circleTestB = primitives.Circle(vector.Vector(3, data=[50, 50, 53]), 1) |
||
168 | |||
169 | print("Box A and Box B collision:", gjkTest.intersects(boxTestA, boxTestB)) |
||
170 | print("Rect A and Rect B collision:", gjkTest.intersects(rectTestA, rectTestB)) |
||
171 | print("Circle A and Circle B collision:", gjkTest.intersects(circleTestA, circleTestB)) |
||
172 | |||
173 | # Circle A and Box/Rect B collision detection, 2D object with a 3D/2D object, it combines the two different shapes |
||
174 | # If the new shape includes the origin, collision happens |
||
175 | # Should return true because they are touching, if not interesting each other at a depth |
||
176 | print("Circle A and Box B collision:", gjkTest.intersects(circleTestA, boxTestB)) |
||
177 | print("Circle A and Rect B collision:", gjkTest.intersects(circleTestA, rectTestB)) |
||
178 | |||
179 | |||
180 | self.view = view.View() |
||
181 | self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0) |
||
182 | self.view.new_projection('ortho', self.ortho) |
||
183 | self.view.register_shader('ortho', self.program) |
||
184 | |||
185 | self.loadText() |
||
186 | |||
187 | glerr = gl.glGetError() |
||
188 | if glerr != 0: |
||
189 | print('GLError:', glerr) |
||
190 | |||
191 | def loadText(self): |
||
192 | vsPath = files.resolve_path('data', 'shaders', 'font.vs') |
||
193 | fsPath = files.resolve_path('data', 'shaders', 'font.fs') |
||
194 | |||
195 | vertex = shaders.VertexShader(vsPath) |
||
196 | fragment = shaders.FragmentShader(fsPath) |
||
197 | self.textProgram = shaders.ShaderProgram(vertex, fragment) |
||
198 | |||
199 | fontPath = files.resolve_path('data', 'SourceCodePro-Regular.ttf') |
||
200 | self.font = text.Font(12, fontPath) |
||
201 | self.text = text.Text(self.textProgram, self.font) |
||
202 | |||
203 | self.view.register_shader('ortho', self.textProgram) |
||
204 | |||
205 | def resize(self, width, height): |
||
206 | self.width = width |
||
207 | self.height = height |
||
208 | gl.glViewport(0, 0, self.width, self.height) |
||
209 | self.ortho = matrix.orthographic(0.0, self.width, self.height, 0.0, -1.0, 1.0) |
||
210 | self.view.set_projection('ortho', self.ortho) |
||
211 | |||
212 | View Code Duplication | def process_event(self, event, data): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
213 | if event == 'quit' or event == 'window_close': |
||
214 | self.running = False |
||
215 | elif event == 'window_resized': |
||
216 | winID, x, y = data |
||
217 | self.resize(x, y) |
||
218 | elif event == 'mouse_move': |
||
219 | print (data) |
||
220 | |||
221 | if cursor.is_relative(): |
||
222 | xrel, yrel = data |
||
223 | self.mousePosX += xrel |
||
224 | self.mousePosY += yrel |
||
225 | else: |
||
226 | self.mousePosX, self.mousePosY = data |
||
227 | elif event == 'key_down': |
||
228 | if data[0] == 'r': |
||
229 | cursor.set_relative_mode(True) |
||
230 | elif data[0] == 'd': |
||
231 | cursor.set_relative_mode(False) |
||
232 | self.keys.append(data[0]) |
||
233 | print(self.keys) |
||
234 | elif event == 'key_up': |
||
235 | self.keys.remove(data[0]) |
||
236 | elif event == 'mouse_button_down': |
||
237 | self.mouseButtons.append(data[0]) |
||
238 | print(self.mouseButtons) |
||
239 | elif event == 'mouse_button_up': |
||
240 | self.mouseButtons.remove(data[0]) |
||
241 | |||
242 | def update(self): |
||
243 | self.scenegraph.update() |
||
244 | |||
245 | # Translate and then update it, this can be handled better but for now, this will do |
||
246 | self.physicsObjectTest.translate(self.mousePosX, self.mousePosY) |
||
247 | |||
248 | # Disabled because it can get really annoying, really fast >:[ |
||
249 | # self.physicsEngineTest.simulate(self.fpsTimer.tickDelta) |
||
250 | |||
251 | def render(self): |
||
252 | gl.glClearColor(0.5, 0.5, 0.5, 1.0) |
||
253 | gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) |
||
254 | gl.glEnable(gl.GL_BLEND) |
||
255 | gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) |
||
256 | self.textProgram.use() |
||
257 | self.text.draw_text(str(self.fpsEstimate) + ' FPS', 0, 10) |
||
258 | |||
259 | self.program.use() |
||
260 | gl.glBindVertexArray(self.vao) |
||
261 | |||
262 | #self.meshObjectTest.render() |
||
263 | |||
264 | self.scenegraph.render() |
||
265 | |||
266 | gl.glBindVertexArray(0) |
||
267 | |||
268 | View Code Duplication | def do_run(self): |
|
0 ignored issues
–
show
|
|||
269 | ''' Process a single loop ''' |
||
270 | self.sysEvents.process() |
||
271 | self.update() |
||
272 | self.render() |
||
273 | self.window.flip() |
||
274 | self.fpsTimer.tick() |
||
275 | |||
276 | # print(self.audioFile.get_pos()) |
||
277 | if self.fpsTimer.fpsTime >= 2000: |
||
278 | self.fpsEstimate = self.fpsTimer.get_fps() |
||
279 | print("{:.2f} fps".format(self.fpsEstimate)) |
||
280 | |||
281 | def run(self): |
||
282 | ''' Called from launcher doesnt exit until the game is quit ''' |
||
283 | self.running = True |
||
284 | while self.running: |
||
285 | self.do_run() |
||
286 | self.audioFile.destroy() |
||
287 | self.audio.destroy() |
||
288 |