def setup(self): """Initialize the cache of obstacles near the base.""" self._obstacles = [] for o in self.map.obstacles: c = self.base.center r = self.tanks_radius + constants.TANKRADIUS if collisiontest.circle_to_circle((o.center, o.radius), (c, r)): self._obstacles.append(o)
def check_position(self, pos, rad): """Check a position to see if it is safe to spawn a tank there.""" for o in self._obstacles: if collisiontest.circle_to_poly((pos,rad), o.shape): return False for s in self.map.shots(): shot = (s.pos, constants.SHOTRADIUS) if collisiontest.circle_to_circle((pos,rad), shot): return False for t in self.map.tanks(): tank = (t.pos, constants.TANKRADIUS) if collisiontest.circle_to_circle((pos,rad), tank): return False off_map_left = pos[0]-rad < -self.config.world.size[0]/2 off_map_bottom = pos[1]-rad < -self.config.world.size[1]/2 off_map_right = pos[0]+rad > self.config.world.size[0]/2 off_map_top = pos[1]+rad > self.config.world.size[1]/2 if off_map_left or off_map_bottom or off_map_right or off_map_top: return False return True
def check_position(self, pos, rad): """Check a position to see if it is safe to spawn a tank there.""" for o in self._obstacles: if collisiontest.circle_to_poly((pos, rad), o.shape): return False for s in self.map.shots(): shot = (s.pos, constants.SHOTRADIUS) if collisiontest.circle_to_circle((pos, rad), shot): return False for t in self.map.tanks(): tank = (t.pos, constants.TANKRADIUS) if collisiontest.circle_to_circle((pos, rad), tank): return False off_map_left = pos[0] - rad < -self.config.world.size[0] / 2 off_map_bottom = pos[1] - rad < -self.config.world.size[1] / 2 off_map_right = pos[0] + rad > self.config.world.size[0] / 2 off_map_top = pos[1] + rad > self.config.world.size[1] / 2 if off_map_left or off_map_bottom or off_map_right or off_map_top: return False return True
def collision_at(self, pos): """Return True if collision at given position, and False otherwise.""" rad = constants.TANKRADIUS for obs in self.team.map.obstacles: if collisiontest.circle_to_poly(((pos),rad), obs.shape): return True for tank in self.team.map.tanks(): if tank is self: continue if collisiontest.circle_to_circle((tank.pos, rad), (pos, rad)): self.collide_tank(tank) return True at_left_wall = pos[0]-rad < -self.config.world.size[0]/2 at_bottem_wall = pos[1]-rad < -self.config.world.size[1]/2 at_right_wall = pos[0]+rad > self.config.world.size[0]/2 at_top_wall = pos[1]+rad > self.config.world.size[1]/2 if at_left_wall or at_bottem_wall or at_right_wall or at_top_wall: return True return False
def collision_at(self, pos): """Return True if collision at given position, and False otherwise.""" rad = constants.TANKRADIUS for obs in self.team.map.obstacles: if collisiontest.circle_to_poly(((pos), rad), obs.shape): return True for tank in self.team.map.tanks(): if tank is self: continue if collisiontest.circle_to_circle((tank.pos, rad), (pos, rad)): self.collide_tank(tank) return True at_left_wall = pos[0] - rad < -self.config.world.size[0] / 2 at_bottem_wall = pos[1] - rad < -self.config.world.size[1] / 2 at_right_wall = pos[0] + rad > self.config.world.size[0] / 2 at_top_wall = pos[1] + rad > self.config.world.size[1] / 2 if at_left_wall or at_bottem_wall or at_right_wall or at_top_wall: return True return False
def update(self, dt): """Update the flag's position.""" f_rad = constants.FLAGRADIUS t_rad = constants.TANKRADIUS x, y = self.pos if self.tank is not None: self.pos = self.tank.pos rect = self.tank.team.base.rect if collisiontest.circle_to_rect((self.pos, f_rad), rect): self.tank.team.map.scoreFlag(self) else: for tank in self.team.map.tanks(): if collisiontest.circle_to_circle((self.pos, f_rad), (tank.pos, t_rad)): if tank.team is self.team: self.team.map.returnFlag(self) else: self.tank = tank tank.flag = self return
def check_collisions(self): """Check for collisions.""" s_rad = constants.SHOTRADIUS t_rad = constants.TANKRADIUS for obs in self.team.map.obstacles: if collisiontest.circle_to_poly(((self.pos),s_rad), obs.shape): return self.kill() for tank in self.team.map.tanks(): if self in tank.shots: continue if collisiontest.circle_to_circle((tank.pos, t_rad), (self.pos, s_rad)): if tank.team == self.team and not self.config['friendly_fire']: continue tank.kill() return self.kill() at_left_wall = self.pos[0]-s_rad < -self.config.world.size[0]/2 at_bottem_wall = self.pos[1]-s_rad < -self.config.world.size[1]/2 at_right_wall = self.pos[0]+s_rad > self.config.world.size[0]/2 at_top_wall = self.pos[1]+s_rad > self.config.world.size[1]/2 if at_left_wall or at_bottem_wall or at_right_wall or at_top_wall: return self.kill()
def check_collisions(self): """Check for collisions.""" s_rad = constants.SHOTRADIUS t_rad = constants.TANKRADIUS for obs in self.team.map.obstacles: if collisiontest.circle_to_poly(((self.pos), s_rad), obs.shape): return self.kill() for tank in self.team.map.tanks(): if self in tank.shots: continue if collisiontest.circle_to_circle((tank.pos, t_rad), (self.pos, s_rad)): if tank.team == self.team and not self.config['friendly_fire']: continue tank.kill() return self.kill() at_left_wall = self.pos[0] - s_rad < -self.config.world.size[0] / 2 at_bottem_wall = self.pos[1] - s_rad < -self.config.world.size[1] / 2 at_right_wall = self.pos[0] + s_rad > self.config.world.size[0] / 2 at_top_wall = self.pos[1] + s_rad > self.config.world.size[1] / 2 if at_left_wall or at_bottem_wall or at_right_wall or at_top_wall: return self.kill()