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