def causeExplosion(self, origin, radius, strength, force=False): logging.debug("Start Explosion %s, %d, %d [%d]", origin, radius, strength, thread.get_ident()) origin = pymunk.Vec2d(origin) for obj in self.__objects: if obj.explodable: for point in wrappos(obj.body.position, radius, self.size): if in_circle(origin, radius, point): logging.debug("Applying Explosion Force to #%d", obj.id) obj.body.apply_impulse((point - origin) * strength, (0,0)) break logging.debug("Done Explosion")
def getObjectsInArea(self, pos, radius, force=False): logging.debug("Get Objects In Area %s %d (%s) [%d]", repr(pos), radius, repr(force), thread.get_ident()) objList = [] for obj in self.__objects.values(): for point in wrappos(obj.body.position, radius, self.size): if in_circle(pos, radius, point): objList.append(obj) break #eif #next #next return objList
def getObjectsInArea(self, pos, radius, radar=False): """ Returns objects within the given radius from the position (even wrapping around edge of world), pass radar = True if for environment """ logging.debug("Get Objects In Area %s %d [%d]", repr(pos), radius, thread.get_ident()) objList = [] for obj in self.__objects.values(): for point in wrappos(obj.body.position, radius, self.size): if in_circle(pos, radius, point): objList.append(obj) break #eif #next #next return objList
def causeExplosion(self, origin, radius, strength, force=False): logging.debug("Start Explosion %s, %d, %d [%d]", origin, radius, strength, thread.get_ident()) origin = pymunk.Vec2d(origin) for obj in self.__objects: if obj.explodable: for point in wrappos(obj.body.position, radius, self.size): if in_circle(origin, radius, point): logging.debug( "Applying Explosion Force of strength %d impulse %s to #%d", strength, repr( (point - origin) * strength), obj.id) obj.body.apply_impulse((point - origin) * strength, (0, 0)) break logging.debug("Done Explosion")
def __THREAD__gameloop(self): lasttime = MINIMUM_GAMESTEP_TIME try: while self.__active: if self.__pys: self.__space.step(MINIMUM_GAMESTEP_TIME) # Advance Physics Engine tstamp = time.time() # find objects in nebulas for neb in self.__nebulas: for shape in self.__space.shape_query(neb.shape): # Set value to two, so that if we're still in the nebula # for another loop, that we don't toggle in/out of nebula between slices # across threads if self.has_key(shape.id): self[shape.id].in_nebula = [2, neb] # update all game objects for obj in self: # self is dictionary # Wrap Object in World if obj.body.position[0] < 0: obj.body.position[0] += self.width elif obj.body.position[0] > self.width: obj.body.position[0] -= self.width if obj.body.position[1] < 0: obj.body.position[1] += self.height elif obj.body.position[1] > self.height: obj.body.position[1] -= self.height # Apply Gravity for Planets if obj.explodable: for planet in self.__planets: for point in wrappos(obj.body.position, planet.gravityFieldLength, self.size): if in_circle(planet.body.position, planet.gravityFieldLength, point): obj.body.apply_impulse((point - planet.body.position) * -planet.pull * lasttime, (0,0)) break # Update and Run Commands obj.update(lasttime) if obj.in_nebula != None: # slow down objects in Nebula if obj.body.velocity.length > 0.1: obj.body.velocity.length -= (obj.in_nebula[1].pull / obj.mass) * lasttime obj.in_nebula[0] -= 1 # decrement count if obj.in_nebula[0] <= 0: obj.in_nebula = None if obj.has_expired(): del self[obj] #next # game time notification self.__game.game_update(lasttime) # update time lasttime = time.time() - tstamp logging.debug("SEMAPHORE ACQ gameloop [%d]", thread.get_ident()) self.__addremovesem.acquire() # NOTE ISSUE #68 - If server is slow, can be adding and removing object in same step... add first, so we can immediately remove instead of crash # TODO: just look for common set between two lists and remove... or have each list check the other first before adding to the lists... # probably best to refactor to PyMunk 4.0.0 and be able to have PyMunk handle adding/removing objects during physics step. # add objects for item in self.__toadd: logging.info("World Adding #%d to Physics Engine %s", item.id, repr(item)) item.addToSpace(self.__space) self.__toadd = [] #remove objects for item in self.__toremove: logging.info("World Removing #%d from Physics Engine %s", item.id, repr(item)) item.removeFromSpace(self.__space) self.__toremove = [] self.__addremovesem.release() logging.debug("SEMAPHORE REL gameloop [%d]", thread.get_ident()) # minimum to conserve resources if lasttime < MINIMUM_GAMESTEP_TIME: time.sleep(MINIMUM_GAMESTEP_TIME - lasttime) lasttime = MINIMUM_GAMESTEP_TIME #else: #logg#ging.debug("Can't keep at 50fps at %dfps", 1.0 / lasttime) #eif #wend except: print "EXCEPTION IN GAMELOOP" logging.exception("FATAL Error in game loop!!!") logging.error(traceback.format_exc()) print traceback.format_exc() self.gameerror = True logging.debug("Gameloop Ended")
def __THREAD__gameloop(self): lasttime = MINIMUM_GAMESTEP_TIME try: while self.__active: if self.__pys: self.__space.step( MINIMUM_GAMESTEP_TIME) # Advance Physics Engine tstamp = time.time() # update all game objects for obj in self: # self is dictionary # Wrap Object in World if obj.body.position[0] < 0: obj.body.position[0] += self.width elif obj.body.position[0] > self.width: obj.body.position[0] -= self.width if obj.body.position[1] < 0: obj.body.position[1] += self.height elif obj.body.position[1] > self.height: obj.body.position[1] -= self.height # Apply Gravity for Planets if obj.gravitable: for influencer in self.__influential: for point in wrappos(obj.body.position, influencer.influence_range, self.size): if in_circle(influencer.body.position, influencer.influence_range, point): influencer.apply_influence( obj, point, lasttime) break # Update and Run Commands obj.update(lasttime) if obj.has_expired() or obj.destroyed: del self[obj] #next # game time notification self.__game.game_update(lasttime) # update time lasttime = time.time() - tstamp logging.debug("SEMAPHORE ACQ gameloop [%d]", thread.get_ident()) self.__addremovesem.acquire() # NOTE ISSUE #68 - If server is slow, can be adding and removing object in same step... add first, so we can immediately remove instead of crash # TODO: just look for common set between two lists and remove... or have each list check the other first before adding to the lists... # probably best to refactor to PyMunk 4.0.0 and be able to have PyMunk handle adding/removing objects during physics step. # add objects for item in self.__toadd: logging.info("World Adding #%d to Physics Engine %s", item.id, repr(item)) item.addToSpace(self.__space) self.__toadd = [] #remove objects for item in self.__toremove: logging.info("World Removing #%d from Physics Engine %s", item.id, repr(item)) item.removeFromSpace(self.__space) self.__toremove = [] self.__addremovesem.release() logging.debug("SEMAPHORE REL gameloop [%d]", thread.get_ident()) # minimum to conserve resources if lasttime < MINIMUM_GAMESTEP_TIME: time.sleep(MINIMUM_GAMESTEP_TIME - lasttime) lasttime = MINIMUM_GAMESTEP_TIME #else: #logg#ging.debug("Can't keep at 50fps at %dfps", 1.0 / lasttime) #eif #wend except: print "EXCEPTION IN GAMELOOP" logging.exception("FATAL Error in game loop!!!") logging.info(traceback.format_exc()) logging.error(traceback.format_exc()) print traceback.format_exc() self.gameerror = True logging.debug("Gameloop Ended")
def __THREAD__gameloop(self): lasttime = MINIMUM_GAMESTEP_TIME try: while self.__active: if self.__pys: self.__space.step(MINIMUM_GAMESTEP_TIME) # Advance Physics Engine tstamp = time.time() # update all game objects for obj in self: # self is dictionary # Wrap Object in World if obj.body.position[0] < 0: obj.body.position[0] += self.width elif obj.body.position[0] > self.width: obj.body.position[0] -= self.width if obj.body.position[1] < 0: obj.body.position[1] += self.height elif obj.body.position[1] > self.height: obj.body.position[1] -= self.height # Apply Gravity for Planets if obj.gravitable: for influencer in self.__influential: for point in wrappos(obj.body.position, influencer.influence_range, self.size): if in_circle(influencer.body.position, influencer.influence_range, point): influencer.apply_influence(obj, point, lasttime) break # Update and Run Commands obj.update(lasttime) if obj.has_expired() or obj.destroyed: del self[obj] #next # game time notification self.__game.game_update(lasttime) # update time lasttime = time.time() - tstamp logging.debug("SEMAPHORE ACQ gameloop [%d]", thread.get_ident()) self.__addremovesem.acquire() # NOTE ISSUE #68 - If server is slow, can be adding and removing object in same step... add first, so we can immediately remove instead of crash # TODO: just look for common set between two lists and remove... or have each list check the other first before adding to the lists... # probably best to refactor to PyMunk 4.0.0 and be able to have PyMunk handle adding/removing objects during physics step. # add objects for item in self.__toadd: logging.info("World Adding #%d to Physics Engine %s", item.id, repr(item)) item.addToSpace(self.__space) self.__toadd = [] #remove objects for item in self.__toremove: logging.info("World Removing #%d from Physics Engine %s", item.id, repr(item)) item.removeFromSpace(self.__space) self.__toremove = [] self.__addremovesem.release() logging.debug("SEMAPHORE REL gameloop [%d]", thread.get_ident()) # minimum to conserve resources if lasttime < MINIMUM_GAMESTEP_TIME: time.sleep(MINIMUM_GAMESTEP_TIME - lasttime) lasttime = MINIMUM_GAMESTEP_TIME #else: #logg#ging.debug("Can't keep at 50fps at %dfps", 1.0 / lasttime) #eif #wend except: print "EXCEPTION IN GAMELOOP" logging.exception("FATAL Error in game loop!!!") logging.info(traceback.format_exc()) logging.error(traceback.format_exc()) print traceback.format_exc() self.gameerror = True logging.debug("Gameloop Ended")