def update(self, delta): if self.__map == None: return self.__shots = [item for item in self.__shots if not item.isDead()] for s in self.__shots: s.update(delta) self.__shootdelay = max(0.,self.__shootdelay - delta) bbsize = 0.3 ldist = 0.2 noff = [[bbsize,bbsize],[-bbsize,-bbsize],[bbsize,-bbsize],[-bbsize,bbsize]] walls = self.__map.getWalls() props = self.__map.getTile(self.__position + Vector(0,0,self.__map.LAYERHEIGHT*1)) try: if props["stair"] == "ny": walls = [] for off in noff: h = self.__map.getHeight(self.__position + Vector(off[0],off[1],0)) self.__position.z = max(self.__position.z,h) print "height:"+str(self.__position.z) noff = [] except Exception as e: pass epsilon = 0.26 newpos = Vector(self.__position) newpos.x += self.__velocity.x * delta * 0.5 solid = False hit = Vector(0,0,0) for off in noff: checkpos = newpos + Vector(off[0],off[1],self.__map.LAYERHEIGHT) if self.__map.isSolid(checkpos): solid = True for w in walls: p1 = Vector(w[0]) if abs(round(p1.z / self.__map.LAYERHEIGHT) - round(checkpos.z / self.__map.LAYERHEIGHT)) - 1.0 > epsilon: continue p2 = Vector(w[1]) p1.z = 0 p2.z = 0 checkpos2 = Vector(checkpos) checkpos2.z = 0 diff = p2 - p1 length = diff.length dist,ort = minimum_distance(p1,p2,checkpos2) #print dist if dist < ldist: hit = ort solid = True if not solid: self.__position = newpos newpos = Vector(self.__position) newpos.y += self.__velocity.y * delta + hit.y * delta * 0.1 solid = False hit = Vector(0,0,0) for off in noff: checkpos = newpos + Vector(off[0],off[1],self.__map.LAYERHEIGHT) if self.__map.isSolid(checkpos): solid = True for w in walls: p1 = Vector(w[0]) if abs(round(p1.z / self.__map.LAYERHEIGHT) - round(checkpos.z / self.__map.LAYERHEIGHT)) - 1.0 > epsilon: continue p2 = Vector(w[1]) p1.z = 0 p2.z = 0 checkpos2 = Vector(checkpos) checkpos2.z = 0 diff = p2 - p1 length = diff.length dist, ort = minimum_distance(p1,p2,checkpos2) #print dist if dist < ldist: hit = ort solid = True if not solid: self.__position = newpos newpos = Vector(self.__position) newpos.x += self.__velocity.x * delta * 0.5 + hit.x * delta * 0.1 solid = False hit = Vector(0,0,0) for off in noff: checkpos = newpos + Vector(off[0],off[1],self.__map.LAYERHEIGHT) if self.__map.isSolid(checkpos): solid = True for w in walls: p1 = Vector(w[0]) if abs(round(p1.z / self.__map.LAYERHEIGHT) - round(checkpos.z / self.__map.LAYERHEIGHT)) - 1.0 > epsilon: continue p2 = Vector(w[1]) p1.z = 0 p2.z = 0 checkpos2 = Vector(checkpos) checkpos2.z = 0 diff = p2 - p1 length = diff.length dist,ort = minimum_distance(p1,p2,checkpos2) #print dist if dist < ldist: hit = ort solid = True if not solid: self.__position = newpos if len(noff) != 0: newpos = Vector(self.__position) newpos.z += -1.0 * delta solid = False for off in noff: checkpos = self.__position + Vector(off[0],off[1],self.__map.LAYERHEIGHT) if self.__map.isSolid(checkpos): solid = True if not solid: self.__position = newpos print "Player Height:"+str(self.__position.z)
def updateCollision(self, delta): # TODO: make Bounding box more arbritary bbsizex, bbsizey = self.__receipe["bounding_box"] bbsizex /= 2. bbsizey /= 2. ldist = min(bbsizex, bbsizey) noff = [[bbsizex,bbsizey],[-bbsizex,-bbsizey],[bbsizex,-bbsizey],[-bbsizex,bbsizey]] current_map = self.__game.getGameStateManager().getCurrentGameState().getMap() walls = current_map.getWalls() props = current_map.getTile(self.__position + Vector(0,0,current_map.LAYERHEIGHT)) epsilon = 0.26 newpos = Vector(self.__position) newpos.x += self.__velocity.x * delta * 0.5 solid = False impdist = 5 hit = Vector(0,0,0) def nearwall(p1,p2,pos): x1 = p1.x x2 = p2.x if x1 > x2: x1,x2 = x2,x1 y1 = p1.y y2 = p2.y if y1 > y2: y1,y2 = y2,y1 return pos.inPlanarRect([x1-1.5,y1-1.5,x2+1.5,y2+1.5]) for off in noff: checkpos = newpos + Vector(off[0],off[1],current_map.LAYERHEIGHT*1.1) if current_map.isSolid(checkpos): solid = True for w2 in walls: for w in w2["colllines"]: p1 = Vector(w[0]) p2 = Vector(w[1]) if not nearwall(p1,p2,newpos): continue if abs(round(p1.z / current_map.LAYERHEIGHT) - round(checkpos.z / current_map.LAYERHEIGHT)) - 1.0 > epsilon: continue p1.z = 0 p2.z = 0 checkpos2 = Vector(checkpos) checkpos2.z = 0 diff = p2 - p1 length = diff.length dist,ort = minimum_distance(p1,p2,checkpos2) #print dist if dist < ldist: #hit = ort solid = True if not solid: self.__position = newpos else: self.call("onCollision",newpos) newpos = Vector(self.__position) newpos.y += self.__velocity.y * delta + hit.y * delta * 0.1 solid = False hit = Vector(0,0,0) for off in noff: checkpos = newpos + Vector(off[0],off[1],current_map.LAYERHEIGHT) if current_map.isSolid(checkpos): solid = True for w2 in walls: for w in w2["colllines"]: p1 = Vector(w[0]) p2 = Vector(w[1]) if not nearwall(p1,p2,newpos): continue if abs(round(p1.z / current_map.LAYERHEIGHT) - round(checkpos.z / current_map.LAYERHEIGHT)) - 1.0 > epsilon: continue p1.z = 0 p2.z = 0 checkpos2 = Vector(checkpos) checkpos2.z = 0 diff = p2 - p1 length = diff.length dist, ort = minimum_distance(p1,p2,checkpos2) #print dist if dist < ldist: #hit = ort solid = True if not solid: self.__position = newpos else: self.call("onCollision",newpos) newpos = Vector(self.__position) newpos.x += self.__velocity.x * delta * 0.5 + hit.x * delta * 0.1 solid = False hit = Vector(0,0,0) for off in noff: checkpos = newpos + Vector(off[0],off[1],current_map.LAYERHEIGHT) if current_map.isSolid(checkpos): solid = True for w2 in walls: for w in w2["colllines"]: p1 = Vector(w[0]) p2 = Vector(w[1]) if not nearwall(p1,p2,newpos): continue if abs(round(p1.z / current_map.LAYERHEIGHT) - round(checkpos.z / current_map.LAYERHEIGHT)) - 1.0 > epsilon: continue p1.z = 0 p2.z = 0 checkpos2 = Vector(checkpos) checkpos2.z = 0 diff = p2 - p1 length = diff.length dist,ort = minimum_distance(p1,p2,checkpos2) #print dist if dist < ldist: #hit = ort solid = True if not solid: self.__position = newpos else: self.call("onCollision",newpos) gamestate = self.__game.getGameStateManager().getCurrentGameState() entitymanager = gamestate.getEntityManager() for e in entitymanager.getEntities(): if not e.isCollidable(): continue if e == self: continue # todo coll detect improve dist = (e.getPosition() - self.getPosition()).length if dist < 0.5: self.call("onCollision",e) # if len(noff) != 0: # newpos = Vector(self.__position) # newpos.z += -1.0 * delta # solid = False # for off in noff: # checkpos = self.__position + Vector(off[0],off[1],current_map.LAYERHEIGHT*0.5) # if current_map.isSolid(checkpos): # solid = True # if not solid: # self.__position = newpos self.__position.z = 0.0