import tkinter as tk import os import glob import time import random from Effects.animated_gif import AnimatedGif from Effects.order_click import OrderClick class Knight: def __init__(self, engine, position=(0,0)): self.engine = engine self.position = position self.target = position self.destination = position gifs = glob.glob("KnightBasic/**/*.gif", recursive=True) self.animation = {os.path.basename(gif): AnimatedGif(gif) for gif in gifs} self.state = "Walk" self.speed = .05 # This is now the delay between updates in seconds self.partial_move = 0 self.direction = 2 self.visited = [self.position] self.starting_position = (position) def ai(self): screen_x, screen_y = self.engine.iso_transform(*self.position) if self.position != self.target: self.direction = self.engine.get_direction(self.position,self.destination) if self.partial_move<1: screen_x_dest, screen_y_dest = self.engine.iso_transform(*self.destination) self.partial_move += self.speed screen_x += (screen_x_dest - screen_x) * self.partial_move screen_y += (screen_y_dest - screen_y) * self.partial_move else: screen_x, screen_y = self.engine.iso_transform(*self.destination) neighbors_list = self.engine.find_neighbors(self.destination) # Get the neighbors of the destination cell # Remove any neighbors that are not walkable neighbors_list = [cell for cell in neighbors_list if self.engine.battlefield[cell[1]][cell[0]].walkable] old_distance_from_target = self.engine.get_distance(self.destination,self.target) old_distance_from_start = self.engine.get_distance(self.destination,self.starting_position) for cell_visited in self.visited: if cell_visited in neighbors_list: neighbors_list.remove(cell_visited) #self.visited = self.visited[-2:] self.position = self.destination self.visited.append(self.position) for cell in neighbors_list: self.engine.effects.append(OrderClick(self.engine.iso_transform(*cell),color="lightblue")) for cell in self.visited: self.engine.effects.append(OrderClick(self.engine.iso_transform(*cell),color="red")) if not neighbors_list: print("No neighbors") #neighbors_list.append(self.visited) #self.target = self.position self.destination = None else: self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target) new_distance_from_target = self.engine.get_distance(self.destination,self.target) new_distance_from_start = self.engine.get_distance(self.destination,self.starting_position) if new_distance_from_target >= old_distance_from_target or new_distance_from_start < old_distance_from_start: print(f"{time.time()} - No progress") new_neighbors_list = [] for neighbor in neighbors_list: neighbor_neighbors_list = self.engine.find_neighbors(neighbor) for neighbor_neighbor in neighbor_neighbors_list: # consider only the neighbors of the neighbors that are walkable if not self.engine.battlefield[neighbor_neighbor[1]][neighbor_neighbor[0]].walkable: if neighbor_neighbor not in self.visited: # if the neighbor of the neighbor is not walkable and has not been visited, add it to the list of visited cells new_neighbors_list.append(neighbor) break # scegli uhn vicino casualmente come destinaziione self.destination = new_neighbors_list[random.randint(0,len(new_neighbors_list)-1)] if self.destination: self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="purple")) self.partial_move = 0 if self.position != self.target: self.direction = self.engine.get_direction(self.position,self.destination) else: print("No destination") self.visited = [self.position] self.destination = self.position #self.target = self.position self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="brown")) else: self.visited = [self.target] self.position = self.target self.starting_position = self.target self.destination = self.target self.state = "Idle" self.partial_move = 1 gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif') return gif, screen_x, screen_y