def center_camera(self): self.ids.current_pos.text = pos_to_coord(*self.focus_center) x, y = self.focus_center[0] * self.SIZE_MOD, self.focus_center[ 1] * self.SIZE_MOD x, y = self.ids.map.x + x, self.ids.map.y + y offsetx = x - self.center[0] offsety = y - self.center[1] fx = self.ids.map.center[0] - offsetx fy = self.ids.map.center[1] - offsety # self.ids.map.center = (fx, fy) anim = Animation(x=fx, y=fy, duration=self.ANIM_DUR, t=self.MOVE_ANIM) anim.start(self.ids.map) for pos, tile in list(self.tiles.items()): in_range = distance(pos, self.focus_center) <= self.focus_radius if in_range and tile.color != [1, 1, 1, 1]: anim = Animation(color=[1, 1, 1, 1], duration=self.ANIM_DUR, t=self.FOW_ANIM) anim.start(tile) elif not in_range and tile.color == [1, 1, 1, 1]: anim = Animation(color=[.7, .7, .7, .7], duration=self.ANIM_DUR, t=self.FOW_ANIM) anim.start(tile)
def end_turn(self): if self.troop and not self.troop.units: self.stop_actions( ) # ToDo: check if this could potentially be called every turn; if so it's not good return if self.troop_target: if not self.troop_target.units: self.troop_target = None else: pos = self.troop_target.pos distance = maths.distance(pos, self.troop.pos) if math.ceil(distance) == 1: self.walk_path.clear() self.actions.append( Attack(self.troop.id, self.troop_target.id)) else: self.path_to(*pos) if self.walk_path: troop = self.troop if self.walk_path[0] == troop.pos: self.walk_path.pop(0) if self.walk_path: x, y = self.walk_path[0] if (x, y) in [ t.pos for t in self.perception.troops.values() if t.units ]: self.stop_actions() else: self.actions.append(Move(troop.id, pos=(x, y))) else: self.stop_actions() if not self.walk_path and not self.troop_target: self.actions.append(Rest(self.troop.id))
def _discover_tiles(self, actor): troop = self.perception.troops[actor.troop.id] origin = troop.pos tiles = [] for i in range(-troop.view_range, troop.view_range + 1): for j in range(-troop.view_range, troop.view_range + 1): x = origin[0] + i y = origin[1] + j distance = maths.distance(origin, (x, y)) if distance > troop.view_range: continue if (x, y) not in actor.perception.tiles: tile = self.perception.get_tile(x, y) tiles.append(tile) return tiles
def in_view_range(self, x, y): return maths.distance(self.pos, (x, y)) <= self.view_range
def path_to(self, x, y): def get_neighbors(x, y): for a in range(-1, 2): for b in range(-1, 2): if abs(a) + abs(b) != 1: continue yield x + a, y + b troop = self.troop start_pos = troop.x, troop.y end_pos = int(x), int(y) try: tile = self.perception.tiles[end_pos] except KeyError: self.stop_actions() return if not tile.passable(troop): self.stop_actions() return open = { start_pos: { 'parent': None, 'g_cost': 0, 'h_cost': maths.distance(end_pos, start_pos) } } closed = {} while True: if not open: self.stop_actions() return current = sorted(sorted(open.items(), key=lambda t: t[1]['h_cost']), key=lambda t: t[1]['h_cost'] + t[1]['g_cost'])[0] current = current[0] closed[current] = open.pop(current) if current == end_pos: break for pos in get_neighbors(*current): g_cost = maths.distance(start_pos, pos) if pos not in self.perception.tiles: continue elif not self.perception.tiles[pos].passable(troop): continue elif pos in closed: continue elif pos not in open: open[pos] = { 'parent': current, 'g_cost': g_cost, 'h_cost': maths.distance(end_pos, pos) } elif g_cost < open[pos]['g_cost']: open[pos]['g_cost'] = g_cost open[pos]['parent'] = current path = [] while current != start_pos: path.append(current) current = closed[current]['parent'] self.walk_path = path[::-1]