def init(): # Load characteristics files info = json.load(open("game/resources/shader/shaders.json")) for e in info["reloading"]: attribute = info["reloading"][e] if attribute == "camView": ShaderManager.addReloading(e, gm.GameManager.cam.getView) # Load each shader registered for shad in info["shaders"]: infos = info["shaders"][shad].copy() ShaderManager.shaders[shad] = shader.Shader( infos["files"][0], infos["files"][1]) ShaderManager.shaders[shad].load() ShaderManager.shaders[shad].use() ShaderManager.shaders[shad].addLink("projection") glU.glUniformv(ShaderManager.shaders[shad], "projection", gm.GameManager.cam.getProjection()) ShaderManager.shaders[shad].addLink("view") ShaderManager.shaders[shad].addLink("model") for reload in infos["mode"]: if reload == "camView": ShaderManager.addToReload("view", shad) elif reload == "static": matrix = matrix4f.Matrix4f(True) matrix.matrix[3][2] = -8.572 glU.glUniformv(ShaderManager.shaders[shad], "view", matrix.matrix) ShaderManager.dispose()
def init(): path_hudSet = "game/resources/textures/hud/hudset.json" Hud.hudSet = json.load(open(path_hudSet)) Hud.shape = shape.Shape("texture-hud", True) Hud.shape.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) Hud.shape.setReading([3, 2, 1]) Hud.model = matrix4f.Matrix4f(True) Hud.model.matrix[3][0] -= 9 Hud.model.matrix[3][1] -= 6 Hud.ebo = [] for index in range(Hud.ELEMENT_TO_DRAW): Hud.ebo.append(index * 4) Hud.ebo.append(index * 4 + 1) Hud.ebo.append(index * 4 + 3) Hud.ebo.append(index * 4 + 1) Hud.ebo.append(index * 4 + 2) Hud.ebo.append(index * 4 + 3) Hud.shape.setEbo(Hud.ebo) Hud.itemName = ["null", "null"] Hud.subItemName = ["null", "null"] Hud.playerLife = [0, 0] Hud.playerInvincibility = [False, False] Hud.playerInteraction = [False, False] Hud.loadCharacteristiques() Hud.constructHud()
def __init__(self, fov, pos): self.pos = pos from game.main.config import Config self.projection = pyrr.matrix44.create_perspective_projection_matrix(fov, Config.ratio, Camera.NEAR, Camera.FAR) self.view = matrix4f.Matrix4f(True) self.updateView() # Track self.track = [False, False] self.entityId = 0 self.posMax = [0, 0] self.setMaximum([18, 12])
def __init__(self, font): self.font = font self.color = [0, 0, 0, 1] self.numberLetter = 0 self.vbo = [] self.ebo = [] self.text = "" self.size = 0 self.centering = "center" self.shape = shape.Shape("text-hud", True) self.shape.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) self.shape.setReading([3, 2, 4]) self.model = matrix4f.Matrix4f(True) self.model.matrix[3][0] -= 9 self.model.matrix[3][1] -= 6
def __init__(self): self.size = [0, 0] self.hotPoint = [0, 0] quad = [ 0, 0, 0.0, 0.0, 0.0, 1, 0, 0.0, 1.0, 0.0, 1, 1, 0.0, 1.0, 1.0, 0, 1, 0.0, 0.0, 1.0 ] indices = [0, 1, 2, 2, 3, 0] self.shape = shape.Shape("texture", True) self.shape.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) self.shape.setEbo(indices) self.shape.setVbo(quad) self.shape.setReading([3, 2]) self.setKey("error") self.model = matrix4f.Matrix4f(True)
def init(): # Images and json loading MapRender.loadTileSets() # Render's shapes loading MapRender.shapeUp = shape.Shape("texture", True) MapRender.shapeUp.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) MapRender.shapeUp.setReading([3, 2]) MapRender.shapeDown = shape.Shape("texture", True) MapRender.shapeDown.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) MapRender.shapeDown.setReading([3, 2]) MapRender.model = matrix4f.Matrix4f(True) sm.updateLink("texture", "model", MapRender.model.matrix) MapRender.transitionShape = guirenderer.GuiRenderer() MapRender.transitionShape.setImage([18, 12], "transition") MapRender.transitionShape.updateModel([9, 6])
def __init__(self, size, color, shader=False): self.size = size self.color = color quad = [ 0.0, 0.0, 0.0, self.color[0], self.color[1], self.color[2], self.color[3], size[0], 0.0, 0.0, self.color[0], self.color[1], self.color[2], self.color[3], size[0], size[1], 0.0, self.color[0], self.color[1], self.color[2], self.color[3], 0.0, size[1], 0.0, self.color[0], self.color[1], self.color[2], self.color[3] ] indices = [0, 1, 2, 2, 3, 0] self.setShader(shader) self.shape = shape.Shape(self.shaderName, True) self.shape.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) self.shape.setEbo(indices) self.shape.setVbo(quad) self.shape.setReading([3, 4]) self.model = matrix4f.Matrix4f(True)
class MapRender: GROUND = 0 GROUND2 = 1 BASE_WALL = 2 BASE_WALL2 = 3 HIGH_WALL = 4 HIGH_WALL2 = 5 SUPP = 6 SUPP2 = 7 tileSets = {} currentTileSet = "tuto" transitionImage = None transitionPos = matrix4f.Matrix4f() transitionShape = None tWidth = 0 tHeight = 0 mapValues = [[[]]] # Position per tile in vbo (object to display) tilesPosition = [[[]]] # Two part off map are displayed # Layer 0 -> 3 and layer 4 -> 7 (entities are displayed between) shapeUp = None shapeDown = None vbo = [[], []] vboCount = [0, 0] ebo = [[], []] eboCount = [0, 0] model = matrix4f.Matrix4f() change = [False, False] ### Methods ### # 4 vertices for a square with information of position, texture position on each vertix @staticmethod def addVertex(stage, posX, posY, tposX, tposY, vboPos): vboPos *= 20 posY += 1 MapRender.vbo[stage].insert(vboPos, float(posX)) MapRender.vbo[stage].insert(vboPos + 1, float(posY)) MapRender.vbo[stage].insert(vboPos + 2, 0.0) MapRender.vbo[stage].insert( vboPos + 3, round( tposX / MapRender.tileSets[MapRender.currentTileSet]["info"] ["size"][0], 4)) MapRender.vbo[stage].insert( vboPos + 4, MapRender.tileSets[MapRender.currentTileSet]["info"]["size"][1] - round( tposY / MapRender.tileSets[MapRender.currentTileSet]["info"] ["size"][1], 4)) # Add a tile without knowing its place in the tile set @staticmethod def addTile(floor, posX, posY, textName, rotate=1): texPos = MapRender.tileSets[ MapRender.currentTileSet]["decor"][textName]["pos"] MapRender.addTileTexPos(floor, posX, posY, texPos[0], texPos[1], rotate) # Add a tile knowing its place in the tile set @staticmethod def addTileTexPos(floor, posX, posY, texPosX, texPosY, rotate=1): if posX < 0 or posX >= len(MapRender.mapValues[0][0]): return if posY < 0 or posY >= len(MapRender.mapValues[0]): return # Choose the part if floor <= 3: stage = 0 else: stage = 1 # Take the vbo in this tile vboCount = MapRender.tilesPosition[floor][MapRender.tHeight - posY - 1][posX] # If the tile is free if vboCount == -1: vboCount = MapRender.reserveNextVbo(floor, posX, posY) MapRender.shiftVboIndex(floor, posX, posY) # If there is already a tile else: MapRender.deleteTile(floor, posX, posY) MapRender.shiftVboIndex(floor, posX, posY) # Create the tile to the position MapRender.addTile2(vboCount, floor, stage, posX, posY, texPosX, texPosY, rotate) MapRender.change[stage] = True @staticmethod def addTile2(vboCount, floor, stage, posX, posY, tposX, tposY, rotate=1): # Securities if posX < 0 or posX >= len(MapRender.mapValues[0][0]): return if posY < 0 or posY >= len(MapRender.mapValues[0]): return eboIndex = MapRender.eboCount[stage] * 4 MapRender.ebo[stage].append(eboIndex) MapRender.ebo[stage].append(eboIndex + 1) MapRender.ebo[stage].append(eboIndex + 3) MapRender.ebo[stage].append(eboIndex + 1) MapRender.ebo[stage].append(eboIndex + 2) MapRender.ebo[stage].append(eboIndex + 3) MapRender.eboCount[stage] += 1 MapRender.tilesPosition[floor][MapRender.tHeight - posY - 1][posX] = vboCount # Construct the tile differently depending on the rotation (base is 1) if rotate == 0: # To left MapRender.addVertex(stage, posX, posY - 1, tposX, tposY, vboCount) MapRender.addVertex(stage, posX + 1, posY - 1, tposX, tposY + 1, vboCount) MapRender.addVertex(stage, posX + 1, posY, tposX + 1, tposY + 1, vboCount) MapRender.addVertex(stage, posX, posY, tposX + 1, tposY, vboCount) elif rotate == 2: # To right MapRender.addVertex(stage, posX, posY - 1, tposX + 1, tposY + 1, vboCount) MapRender.addVertex(stage, posX + 1, posY - 1, tposX + 1, tposY, vboCount) MapRender.addVertex(stage, posX + 1, posY, tposX, tposY, vboCount) MapRender.addVertex(stage, posX, posY, tposX, tposY + 1, vboCount) elif rotate == 1: # To up (normal) MapRender.addVertex(stage, posX, posY - 1, tposX, tposY + 1, vboCount) MapRender.addVertex(stage, posX + 1, posY - 1, tposX + 1, tposY + 1, vboCount) MapRender.addVertex(stage, posX + 1, posY, tposX + 1, tposY, vboCount) MapRender.addVertex(stage, posX, posY, tposX, tposY, vboCount) elif rotate == 3: # To down MapRender.addVertex(stage, posX, posY - 1, tposX + 1, tposY, vboCount) MapRender.addVertex(stage, posX + 1, posY - 1, tposX, tposY, vboCount) MapRender.addVertex(stage, posX + 1, posY, tposX, tposY + 1, vboCount) MapRender.addVertex(stage, posX, posY, tposX + 1, tposY + 1, vboCount) # Add a decor with its name @staticmethod def addDecor(decor, posX, posY): # Securities if decor == "delete" or decor == "null": return layer = MapRender.tileSets[ MapRender.currentTileSet]["decor"][decor]["layer"] # Position of the decor in the tile set texPos = MapRender.tileSets[ MapRender.currentTileSet]["decor"][decor]["pos"] # Add the decor left to right and top to bottom for y in range( MapRender.tileSets[MapRender.currentTileSet]["decor"][decor] ["size"][1] - 1, -1, -1): for x in range(MapRender.tileSets[MapRender.currentTileSet] ["decor"][decor]["size"][0]): MapRender.addTileTexPos(layer, posX + x, posY + y, texPos[0] + x, texPos[1] - y) # Delete a decor with its name @staticmethod def deleteDecor(oldDecor, posX, posY): # Securities if oldDecor == "delete" or oldDecor == "null": return layer = MapRender.tileSets[ MapRender.currentTileSet]["decor"][oldDecor]["layer"] # Delete the decor right to left and bottom to up for y in range(MapRender.tileSets[MapRender.currentTileSet]["decor"] [oldDecor]["size"][1]): for x in range( MapRender.tileSets[MapRender.currentTileSet]["decor"] [oldDecor]["size"][0] - 1, -1, -1): MapRender.deleteTile(layer, posX + x, posY + y) # First method called for the first map construction, this construct is based on map json table @staticmethod def constructMap(): height = len(MapRender.mapValues[0]) width = len(MapRender.mapValues[0][0]) MapRender.tWidth = width MapRender.tHeight = height # Set values MapRender.tilesPosition = [[[-1 for x in range(width)] for y in range(height)] for z in range(8)] MapRender.vbo = [[], []] MapRender.ebo = [[], []] MapRender.eboCount = [0, 0] MapRender.vboCount = [0, 0] # 7 layers for floor in range(8): # Choose the part if floor <= 3: stage = 0 else: stage = 1 for y in range(len(MapRender.mapValues[floor])): for x in range(len(MapRender.mapValues[floor][y])): # With the id, fill a tile with position in vbo id = MapRender.mapValues[floor][y][x] if id != 0: pos = MapRender.tileSets[ MapRender.currentTileSet]["id"][id - 1] MapRender.addTile2(MapRender.vboCount[stage], floor, stage, x, height - y - 1, pos[0], pos[1]) MapRender.vboCount[stage] += 1 MapRender.change = [True, True] @staticmethod def deleteTile(floor, posX, posY): # Securities if posX < 0 or posX >= len(MapRender.mapValues[0][0]): return if posY < 0 or posY >= len(MapRender.mapValues[0]): return # Choose the part if floor <= 3: stage = 0 else: stage = 1 vbo = MapRender.tilesPosition[floor][MapRender.tHeight - posY - 1][posX] # Can't delete a not existing tile if vbo == -1: return else: MapRender.tilesPosition[floor][MapRender.tHeight - posY - 1][posX] = -1 size = len(MapRender.ebo[stage]) for i in range(6): del MapRender.ebo[stage][size - 1 - i] for i in range(20): del MapRender.vbo[stage][vbo * 20] MapRender.vboCount[stage] -= 1 MapRender.eboCount[stage] -= 1 MapRender.change[stage] = True MapRender.shiftVboIndex(floor, posX, posY, -1) @staticmethod def display(transition): # Displays two parts for maps: # Displays fist layer 0 -> 3, then answers EntityManager to display entities and finally display layer 4 -> 7 sm.updateLink("texture", "model", MapRender.model.matrix) tm.bind(MapRender.currentTileSet) MapRender.shapeDown.display() em.EntityManager.display() sm.updateLink("texture", "model", MapRender.model.matrix) tm.bind(MapRender.currentTileSet) MapRender.shapeUp.display() if transition: MapRender.transitionShape.display() @staticmethod def dispose(): # Reapply values for shape with the new version of vbo and ebo if MapRender.change[0]: MapRender.shapeDown.setEbo(MapRender.ebo[0]) MapRender.shapeDown.setVbo(MapRender.vbo[0]) MapRender.change[0] = False if MapRender.change[1]: MapRender.shapeUp.setEbo(MapRender.ebo[1]) MapRender.shapeUp.setVbo(MapRender.vbo[1]) @staticmethod def init(): # Images and json loading MapRender.loadTileSets() # Render's shapes loading MapRender.shapeUp = shape.Shape("texture", True) MapRender.shapeUp.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) MapRender.shapeUp.setReading([3, 2]) MapRender.shapeDown = shape.Shape("texture", True) MapRender.shapeDown.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) MapRender.shapeDown.setReading([3, 2]) MapRender.model = matrix4f.Matrix4f(True) sm.updateLink("texture", "model", MapRender.model.matrix) MapRender.transitionShape = guirenderer.GuiRenderer() MapRender.transitionShape.setImage([18, 12], "transition") MapRender.transitionShape.updateModel([9, 6]) @staticmethod def loadTileSets(): # List of tileSet list = json.load( open("game/resources/textures/tiles/list.json")).copy() list = list["tileSets"] # Load json tileSet files for tileSet in list: curTileSet = tm.textures[tileSet] MapRender.tileSets.update({ tileSet: json.load( open("game/resources/textures/tiles/" + tileSet + ".json")) }) MapRender.tileSize = MapRender.tileSets[tileSet]["info"][ "tilesize"] MapRender.tileSets[tileSet]["info"]["size"] = [ int(curTileSet.width / MapRender.tileSize), int(curTileSet.height / MapRender.tileSize) ] MapRender.tileSets[tileSet]["id"] = [] for y in range(0, int(curTileSet.height / MapRender.tileSize)): for x in range(0, int(curTileSet.width / MapRender.tileSize)): MapRender.tileSets[tileSet]["id"].append([x, y]) @staticmethod def reserveNextVbo(layer, posX, posY): # Choose the part if layer <= 3: end = 4 stage = 0 else: end = 8 stage = 1 # Invert position of posY posY = MapRender.tHeight - posY - 1 posX += 1 # Check next tiles in the same row for x in range(posX, len(MapRender.tilesPosition[layer][posY])): if not MapRender.tilesPosition[layer][posY][x] == -1: vbo = MapRender.tilesPosition[layer][posY][x] return vbo # Check next tiles in the same layer for y in range(posY + 1, len(MapRender.tilesPosition[layer])): for x in range(len(MapRender.tilesPosition[layer][y])): if not MapRender.tilesPosition[layer][y][x] == -1: vbo = MapRender.tilesPosition[layer][y][x] return vbo # Check next tiles after its layer for floor in range(layer + 1, end): for y in range(len(MapRender.tilesPosition[floor])): for x in range(len(MapRender.tilesPosition[floor][y])): if not MapRender.tilesPosition[floor][y][x] == -1: vbo = MapRender.tilesPosition[floor][y][x] return vbo vbo = MapRender.vboCount[stage] MapRender.vboCount[stage] += 1 return vbo # MapTemporarySave send values to apply during the map changing @staticmethod def setMapValues(vbo, ebo, mapValue, tilesPositon, vboCount, eboCount, tileSet): MapRender.mapValues = mapValue MapRender.tilesPosition = tilesPositon MapRender.vbo = vbo MapRender.ebo = ebo MapRender.vboCount = vboCount MapRender.eboCount = eboCount MapRender.currentTileSet = tileSet MapRender.tWidth = len(MapRender.mapValues[0][0]) MapRender.tHeight = len(MapRender.mapValues[0]) width = MapRender.tWidth height = MapRender.tHeight # Set the camera position from game.screen import gamemanager cam = gamemanager.GameManager.cam cam.setPos([0, 0, cam.pos[2]]) cam.setMaximum([width, height]) # Set camera position according to the size of map # In x if width > 18: # Follow the player cam.track[0] = True else: # Don't move cam.track[0] = False cam.addPos([-width / 2, 0, 0]) # In y if height > 12: # Follow the player cam.track[1] = True else: # Don't move cam.track[1] = False cam.addPos([0, -height / 2, 0]) cam.goToEntity() sm.dispose() MapRender.change = [True, True] MapRender.dispose() @staticmethod def setTransitionPos(pos): MapRender.transitionShape.updateModel(pos) # Add a value (indent) for every tiles after one @staticmethod def shiftVboIndex(layer, posX, posY, indent=1): # Choose the part if layer <= 3: end = 4 else: end = 8 # Invert position of posY posY = MapRender.tHeight - posY - 1 posX += 1 # Check next tile in the same row for x in range(posX, len(MapRender.tilesPosition[layer][posY])): if not MapRender.tilesPosition[layer][posY][x] == -1: MapRender.tilesPosition[layer][posY][x] += indent # Check next tile in the same layer for y in range(posY + 1, len(MapRender.tilesPosition[layer])): for x in range(len(MapRender.tilesPosition[layer][y])): if not MapRender.tilesPosition[layer][y][x] == -1: MapRender.tilesPosition[layer][y][x] += indent # Check next tile after its layer for floor in range(layer + 1, end): for y in range(len(MapRender.tilesPosition[floor])): for x in range(len(MapRender.tilesPosition[floor][y])): if not MapRender.tilesPosition[floor][y][x] == -1: MapRender.tilesPosition[floor][y][x] += indent @staticmethod def unload(): MapRender.shapeDown.unload() MapRender.shapeUp.unload() MapRender.transitionShape.unload() del MapRender.shapeDown del MapRender.shapeUp del MapRender.transitionShape
class Hud: BACK_1 = 1 BACK_2 = 2 PORTRAIT_1 = 3 PORTRAIT_2 = 4 FRAME_ITEM_1 = 5 FRAME_ITEM_2 = 6 ITEM_1 = 7 ITEM_2 = 8 HEARTHS_1 = 9 HEARTHS_2 = 12 SWITCH_1 = 15 SWITCH_2 = 16 ITEM_SWITCH_1 = 17 ITEM_SWITCH_2 = 18 ELEMENT_TO_DRAW = 19 VERTEX_SIZE = 4 * 6 hudSetImage = None hudSet = None hudInfo = None shape = None vbo = [] ebo = [] model = matrix4f.Matrix4f(True) itemName = ["null", "null"] subItemName = ["null", "null"] playerLife = [0, 0] playerInvincibility = [False, False] playerInteraction = [False, False] @staticmethod def init(): path_hudSet = "game/resources/textures/hud/hudset.json" Hud.hudSet = json.load(open(path_hudSet)) Hud.shape = shape.Shape("texture-hud", True) Hud.shape.setStorage(shape.Shape.STATIC_STORE, shape.Shape.STATIC_STORE) Hud.shape.setReading([3, 2, 1]) Hud.model = matrix4f.Matrix4f(True) Hud.model.matrix[3][0] -= 9 Hud.model.matrix[3][1] -= 6 Hud.ebo = [] for index in range(Hud.ELEMENT_TO_DRAW): Hud.ebo.append(index * 4) Hud.ebo.append(index * 4 + 1) Hud.ebo.append(index * 4 + 3) Hud.ebo.append(index * 4 + 1) Hud.ebo.append(index * 4 + 2) Hud.ebo.append(index * 4 + 3) Hud.shape.setEbo(Hud.ebo) Hud.itemName = ["null", "null"] Hud.subItemName = ["null", "null"] Hud.playerLife = [0, 0] Hud.playerInvincibility = [False, False] Hud.playerInteraction = [False, False] Hud.loadCharacteristiques() Hud.constructHud() # Load characteristics because this document control positions and sizes of each element of hud @staticmethod def loadCharacteristiques(): path_hudInfo = "game/resources/textures/hud/hudcharacteristics.json" Hud.hudInfo = json.load(open(path_hudInfo)) # Construct the hud @staticmethod def constructHud(): Hud.vbo = [0 for a in range(Hud.VERTEX_SIZE * Hud.ELEMENT_TO_DRAW)] Hud.itemName1 = "" Hud.itemName2 = "" Hud.constructElement(Hud.hudInfo["position"]["back-1"], Hud.hudInfo["size"]["back-1"], "back", Hud.BACK_1, Hud.hudInfo["opacity"]["back-1"]) Hud.constructElement(Hud.hudInfo["position"]["back-2"], Hud.hudInfo["size"]["back-2"], "back", Hud.BACK_2, Hud.hudInfo["opacity"]["back-2"]) Hud.constructElement(Hud.hudInfo["position"]["portrait-1"], Hud.hudInfo["size"]["portrait-1"], "portrait-1", Hud.PORTRAIT_1, Hud.hudInfo["opacity"]["portrait-1"]) Hud.constructElement(Hud.hudInfo["position"]["portrait-2"], Hud.hudInfo["size"]["portrait-2"], "portrait-2", Hud.PORTRAIT_2, Hud.hudInfo["opacity"]["portrait-2"]) Hud.constructElement(Hud.hudInfo["position"]["frame-item-1"], Hud.hudInfo["size"]["frame-item-1"], "frame-item-1", Hud.FRAME_ITEM_1, Hud.hudInfo["opacity"]["frame-item-1"]) Hud.constructElement(Hud.hudInfo["position"]["frame-item-2"], Hud.hudInfo["size"]["frame-item-2"], "frame-item-2", Hud.FRAME_ITEM_2, Hud.hudInfo["opacity"]["frame-item-2"], True) Hud.dispose() @staticmethod def display(): sm.updateLink("texture-hud", "model", Hud.model.matrix) tm.bind("hud") Hud.shape.display() # Check state game to change the hud @staticmethod def dispose(): ent = em.EntityManager change = False itemName = [ ent.entities[0].getItemName(), ent.entities[1].getItemName() ] # For both players for i in range(2): # If players change their items hasChange = False if not Hud.itemName[0] == itemName[0] or not Hud.itemName[ 1] == itemName[1]: hasChange = True if itemName[i] == "Weapon": if not Hud.subItemName[i] == ent.entities[i].item.arm: hasChange = True if hasChange: if Hud.itemName[i] == "Weapon": Hud.constructElement([0, 0], [1, 1], "null", Hud.ITEM_SWITCH_1 + i, 0) Hud.constructElement([0, 0], [1, 1], "null", Hud.SWITCH_1 + i, 0) if itemName[i] == "Null": Hud.constructElement([0, 0], [1, 1], "null", Hud.ITEM_1 + i, 0) else: itemType = "item-key" switch = [False] if itemName[i] == "Key": itemType = "item-key" elif itemName[i] == "Weapon": if ent.entities[i].item.arm: itemType = "item-sword" Hud.subItemName[i] = itemType if not itemName[1 - i] == "Weapon": switch = [True, "item-bow"] else: switch = [False] else: itemType = "item-bow" Hud.subItemName[i] = itemType if not itemName[1 - i] == "Weapon": switch = [True, "item-sword"] else: switch = [False] if i == 0: flip = False else: flip = True # Display new Item Hud.constructElement( Hud.hudInfo["position"]["frame-item-" + str(i + 1)], Hud.hudInfo["size"]["frame-item-" + str(i + 1)], itemType, Hud.ITEM_1 + i, Hud.hudInfo["opacity"]["frame-item-" + str(i + 1)], flip) # Display switch state if item with switch if switch[0]: Hud.constructElement( Hud.hudInfo["position"]["switch-" + str(i + 1)], Hud.hudInfo["size"]["switch"], "switch", Hud.SWITCH_1 + i, Hud.hudInfo["opacity"]["switch"], flip) Hud.constructElement( Hud.hudInfo["position"]["item-switch-" + str(i + 1)], Hud.hudInfo["size"]["item-switch"], switch[1], Hud.ITEM_SWITCH_1 + i, Hud.hudInfo["opacity"]["item-switch"], flip) change = True # If players life change if not ent.entities[i].life == Hud.playerLife[i]: if ent.entities[i].takeDamage == Hud.playerInvincibility[i]: Hud.playerInvincibility[i] = not ent.entities[i].takeDamage Hud.setHealthBar(ent.entities[i].life, i) change = True # If players invincibility change elif ent.entities[i].takeDamage == Hud.playerInvincibility[i]: Hud.playerInvincibility[i] = not ent.entities[i].takeDamage Hud.setHealthBar(Hud.playerLife[i], i) change = True # If players interaction change if not Hud.playerInteraction[i] == ent.entities[i].getCanInteract( ): change = True Hud.playerInteraction[i] = ent.entities[i].getCanInteract() if Hud.playerInteraction[i]: type = "portrait-interaction-" else: type = "portrait-" Hud.constructElement( Hud.hudInfo["position"]["portrait-" + str(i + 1)], Hud.hudInfo["size"]["portrait-" + str(i + 1)], type + str(i + 1), Hud.PORTRAIT_1 + i, Hud.hudInfo["opacity"]["portrait-" + str(i + 1)]) # If there is a hud change, apply it if change: Hud.itemName[0] = itemName[0] Hud.itemName[1] = itemName[1] Hud.shape.setVbo(Hud.vbo) # Reconstruct the health bar @staticmethod def setHealthBar(newLife, playerNumber): Hud.playerLife[playerNumber] = newLife # For three hearth for heartIndex in range(3): if Hud.playerLife[playerNumber] >= heartIndex * 2 + 2: texture = "full-heart" elif Hud.playerLife[playerNumber] >= heartIndex * 2 + 1: texture = "half-heart" else: texture = "dead-heart" if Hud.playerInvincibility[playerNumber]: texture = "protect-heart" position = Hud.hudInfo["position"]["health-bar-" + str(playerNumber + 1)].copy() position[0] += (Hud.hudInfo["info"]["heart-gap"] * heartIndex) + ( Hud.hudInfo["size"]["hearth"][0] * heartIndex) if playerNumber == 0: Hud.constructElement(position, Hud.hudInfo["size"]["hearth"], texture, Hud.HEARTHS_1 + heartIndex, Hud.hudInfo["opacity"]["health-bar-1"]) else: Hud.constructElement(position, Hud.hudInfo["size"]["hearth"], texture, Hud.HEARTHS_2 + heartIndex, Hud.hudInfo["opacity"]["health-bar-2"]) # Construct an element of the hud using OpenGL functionality @staticmethod def constructElement(position, size, texture, vboCount, opacity, flip=False): del Hud.vbo[vboCount * Hud.VERTEX_SIZE:(vboCount + 1) * Hud.VERTEX_SIZE] if texture == "null": texPos = [0, 0] texSize = [0, 0] else: texPos = Hud.hudSet["elements"][texture]["pos"] if "size" in Hud.hudSet["elements"][texture]: texSize = Hud.hudSet["elements"][texture]["size"] else: texSize = [1, 1] # Flip some elements for the other play if flip: Hud.addVertice(position[0] + size[0] / 2, position[1] - size[1] / 2, texPos[0], texPos[1] + texSize[1], vboCount, opacity) Hud.addVertice(position[0] - size[0] / 2, position[1] - size[1] / 2, texPos[0] + texSize[0], texPos[1] + texSize[1], vboCount, opacity) Hud.addVertice(position[0] - size[0] / 2, position[1] + size[1] / 2, texPos[0] + texSize[0], texPos[1], vboCount, opacity) Hud.addVertice(position[0] + size[0] / 2, position[1] + size[1] / 2, texPos[0], texPos[1], vboCount, opacity) else: Hud.addVertice(position[0] - size[0] / 2, position[1] - size[1] / 2, texPos[0], texPos[1] + texSize[1], vboCount, opacity) Hud.addVertice(position[0] + size[0] / 2, position[1] - size[1] / 2, texPos[0] + texSize[0], texPos[1] + texSize[1], vboCount, opacity) Hud.addVertice(position[0] + size[0] / 2, position[1] + size[1] / 2, texPos[0] + texSize[0], texPos[1], vboCount, opacity) Hud.addVertice(position[0] - size[0] / 2, position[1] + size[1] / 2, texPos[0], texPos[1], vboCount, opacity) # 4 vertices for a square with information of position, texture position and opacity on each vertices @staticmethod def addVertice(posX, posY, tposX, tposY, vboPos, opacity): vboPos *= Hud.VERTEX_SIZE posY += 1 Hud.vbo.insert(vboPos, float(posX)) Hud.vbo.insert(vboPos + 1, float(posY)) Hud.vbo.insert(vboPos + 2, 0.0) Hud.vbo.insert(vboPos + 3, round(tposX / Hud.hudSet["info"]["size"][0], 4)) Hud.vbo.insert( vboPos + 4, Hud.hudSet["info"]["size"][1] - round(tposY / Hud.hudSet["info"]["size"][1], 4)) Hud.vbo.insert(vboPos + 5, opacity) @staticmethod def unload(): Hud.shape.unload()