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