get path work and printing

This commit is contained in:
Xevion
2020-04-21 17:53:00 -05:00
parent 7948662459
commit 50f56e4190
3 changed files with 22 additions and 7 deletions

View File

@@ -13,7 +13,7 @@ from typing import Tuple, List
import arcade
from config import Config
from map import Dungeon
from mobs import Player
from mobs import Player, Mob
from projectiles import Temp
@@ -76,6 +76,11 @@ class Game(arcade.Window):
self.player.center_x, self.player.center_y = level.center()
# x, y = level.center()
mob = Mob(filename="resources/images/monsters/ghost/ghost1.png", dungeon=self.dungeon)
mob.center_x, mob.center_y = random.choice(self.dungeon.levelList).center()
mob.target = self.player
self.enemy_list.append(mob)
# Setup viewport
self.view_bottom = self.player.center_x - (0.5 * Config.SCREEN_WIDTH) + 300
self.view_left = self.player.center_x - (0.5 * Config.SCREEN_WIDTH)
@@ -116,7 +121,7 @@ class Game(arcade.Window):
# Draw paths for all mobs
for mob in self.enemy_list:
if mob.target is not None:
self.draw_path(mob.mob.get_path())
self.draw_path(mob.get_path())
self.fps.tick()
except Exception:

View File

@@ -14,6 +14,9 @@ import numpy as np
from itertools import chain
from config import Config
from pathfinding.core.diagonal_movement import DiagonalMovement
from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
class Dungeon(object):
@@ -49,6 +52,8 @@ class Dungeon(object):
for yy in range(10):
if level.structure[xx][yy] == 'w':
self.matrix[(level.x * 10) + xx][(level.y * 10) + yy] = 1
self.grid = Grid(matrix=self.matrix)
self.finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
# pprint(self.matrix, width=1000)
def getWalls(self) -> arcade.SpriteList:

View File

@@ -18,7 +18,7 @@ class Mob(arcade.Sprite):
def __init__(self, dungeon: Dungeon, max_health=100, max_armor=0, *args, **kwargs) -> None:
# Set up parent class
super().__init__()
super(Mob, self).__init__(*args, **kwargs)
self.max_health, self.max_armor = max_health, max_armor
self.health, self.armor = max_health, max_armor
@@ -37,8 +37,8 @@ class Mob(arcade.Sprite):
:return: A tuple containing the Mob's dungeon tile position.
"""
return (round(self.center_x / Config.TILE_SIZE) * Config.TILE_SIZE,
round(self.center_y / Config.TILE_SIZE) * Config.TILE_SIZE)
return (round(self.center_x / Config.TILE_SIZE),
round(self.center_y / Config.TILE_SIZE))
def tick(self) -> None:
"""
@@ -54,8 +54,13 @@ class Mob(arcade.Sprite):
:return:
"""
if end is None:
x, y = self.target.position
x, y = (round(x / Config.TILE_SIZE) * Config.TILE_SIZE, round(y / Config.TILE_SIZE) * Config.TILE_SIZE)
end = self.target.position
start, end = self.nearestPosition(), (round(end[0] / Config.TILE_SIZE), round(end[1] / Config.TILE_SIZE))
print(start, end)
start, end = self.dungeon.grid.node(*start), self.dungeon.grid.node(*end)
paths, runs = self.dungeon.finder.find_path(start, end, self.dungeon.grid)
print(paths, runs)
return paths
class Player(Mob):