Conditions | 3 |
Total Lines | 160 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import random as rnd |
||
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 | |||
288 |