| Conditions | 3 |
| Total Lines | 147 |
| 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 |
||
| 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 | |||
| 251 |