Refactor code to improve knight movement and update maze configuration
This commit is contained in:
+32
-74
@@ -1,8 +1,5 @@
|
||||
import tkinter as tk
|
||||
import os
|
||||
import glob
|
||||
import time
|
||||
import random
|
||||
|
||||
from Effects.animated_gif import AnimatedGif
|
||||
from Effects.order_click import OrderClick
|
||||
@@ -24,81 +21,42 @@ class Knight:
|
||||
|
||||
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:
|
||||
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)
|
||||
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]
|
||||
self.position = self.destination
|
||||
self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target)
|
||||
self.partial_move = 0
|
||||
print(self.partial_move)
|
||||
|
||||
|
||||
gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif')
|
||||
return gif, screen_x, screen_y
|
||||
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 = .15
|
||||
self.target = target
|
||||
self.state = "Run"
|
||||
Reference in New Issue
Block a user