def __init__(self, viewport, topleft, bottomright, pos=(0, 0)): self.viewport = viewport self.topleft = HexGrid.coord_to_screen(topleft) bottomright = HexGrid.coord_to_screen(bottomright) self.bottomright = (bottomright[0] - HEX_WIDTH / 2, bottomright[1] - HEX_HEIGHT) self.pos = pos
def coord_to_viewport(self, coord): """Given a tile coordinate, get its viewport position.""" cx, cy = self.pos wx, wy = HexGrid.coord_to_world(coord) sx, sy = HexGrid.coord_to_screen(coord) return ( sx - cx, self.viewport[1] - sy + cy )
def centre(self, coord): """Re-centre the camera on the given coord.""" sx, sy = HexGrid.coord_to_screen(coord) self.pos = self.clip( sx - self.viewport[0] // 2, sy - self.viewport[1] // 2, )
def group_connected(triggers): """Group the given list of triggers into connected sets.""" groups = [] ungrouped = set( (x, y, difficulty) for (x, y), difficulty in triggers.items()) while ungrouped: x, y, difficulty = ungrouped.pop() pos = x, y locs = [pos] queue = [pos] while queue: pos = queue.pop() for x, y in HexGrid.neighbours(pos): if (x, y, difficulty) in ungrouped: ungrouped.discard((x, y, difficulty)) c = x, y locs.append(c) queue.append(c) groups.append(TriggerArea(locs, difficulty)) groups_by_loc = {} for g in groups: for l in g.locs: groups_by_loc[l] = g return groups, groups_by_loc
def random_sprites(self, num=1): x, y = HexGrid.coord_to_world(self.pos) for _ in range(num): im = random.choice(self.images) dx = random.random() - 0.5 dy = random.random() - 0.5 c = x + dx, y + dy yield c, im
def __init__(self, filename): self.floor = { } # A list of floor graphics in draw order, keyed by coord self.objects = {} # Static images occupying a tile, keyed by coord self.grid = HexGrid() self.images = {} self.pois = {} self.triggers = {} self.load_file(filename)
def create_particles(self, value, max_value): self.particles = [] for points, img in zip(self.POINTS, self.images): if points > max_value: continue num, value = divmod(value, points) x, y = HexGrid.coord_to_world(self.pos) for _ in range(num): v = self.V vx = random.uniform(-v, v) vy = random.uniform(-v, v) z = 30 vz = random.uniform(60, 100) self.particles.append((x + vx * 0.5, y + vy * 0.5, z, vx, vy, vz, img)) if not value: break
def create_particles(self, value, max_value): self.particles = [] for points, img in zip(self.POINTS, self.images): if points > max_value: continue num, value = divmod(value, points) x, y = HexGrid.coord_to_world(self.pos) for _ in range(num): v = self.V vx = random.uniform(-v, v) vy = random.uniform(-v, v) z = 30 vz = random.uniform(60, 100) self.particles.append( (x + vx * 0.5, y + vy * 0.5, z, vx, vy, vz, img)) if not value: break
def chase_closest_enemy(actor, t, dt): if actor.walking_to is None: grid = actor.world.grid enemies = [a.position for a in actor.world.actors if a.faction != actor.faction] distance_to_actor = lambda p: HexGrid.distance(p, actor.position) enemies.sort(key=distance_to_actor) # print([a.faction for a in actor.world.actors]) for e in enemies: if distance_to_actor(e) < 2: # We're already there! adjacent are at distance sqrt3 # Just change facing actor.look(e) return # Find closest unblocked location to attack attackable = [p for p in grid.neighbours(e) if not grid.blocks_movement(p)] if attackable: dest = min(attackable, key=distance_to_actor) actor.walk_to(dest) break
def group_connected(triggers): """Group the given list of triggers into connected sets.""" groups = [] ungrouped = set((x, y, difficulty) for (x, y), difficulty in triggers.items()) while ungrouped: x, y, difficulty = ungrouped.pop() pos = x, y locs = [pos] queue = [pos] while queue: pos = queue.pop() for x, y in HexGrid.neighbours(pos): if (x, y, difficulty) in ungrouped: ungrouped.discard((x, y, difficulty)) c = x, y locs.append(c) queue.append(c) groups.append(TriggerArea(locs, difficulty)) groups_by_loc = {} for g in groups: for l in g.locs: groups_by_loc[l] = g return groups, groups_by_loc
def viewport_to_coord(self, coord): """Given a viewport coordinate, get the tile coordinate.""" return HexGrid.world_to_coord(self.viewport_to_world(coord))
def calc_damage(self, actor, target): range = HexGrid.distance(actor.position, target.position) return random.randint(1, int(self.damage * (self.max_range - range)))
def coord_to_viewport(self, coord): """Given a tile coordinate, get its viewport position.""" cx, cy = self.pos wx, wy = HexGrid.coord_to_world(coord) sx, sy = HexGrid.coord_to_screen(coord) return (sx - cx, self.viewport[1] - sy + cy)
def calc_damage(self,actor, target): range = HexGrid.distance(actor.position, target.position) return random.randint(1,int(self.damage * (self.max_range - range)))