You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
3.2 KiB

import os
import glob
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.visited = [self.target]
self.position = self.target
self.starting_position = self.target
self.destination = self.target
self.state = "Idle"
self.partial_move = 1
else:
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
if self.partial_move > 1:
self.partial_move = 1
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)
self.visited = self.visited[-4:]
neighbors_list = self.engine.find_neighbors(self.destination) # Get the neighbors of the destination cell
neighbors_list = [cell for cell in neighbors_list if cell not in self.visited]
# Remove any neighbors that are not walkable
self.best_next = self.engine.get_closest_neighbor(neighbors_list,self.target)
#if best next is not walkable, find the closest walkable cell to best next
neighbors_list = [cell for cell in neighbors_list if self.engine.battlefield[cell[1]][cell[0]].walkable]
if self.engine.battlefield[self.best_next[1]][self.best_next[0]].walkable == False:
self.best_next = self.engine.get_closest_neighbor(neighbors_list,self.best_next)
if not self.best_next:
self.target = self.destination
self.best_next = self.destination
self.position = self.destination
self.destination = self.best_next
self.partial_move = 0
self.visited.append(self.destination)
print(self.partial_move)
gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif')
return gif, screen_x, screen_y
def move_to(self, target):
self.speed = .05
self.target = target
self.state = "Walk"
def run_to(self, target):
self.speed = .13
self.target = target
self.state = "Run"