[integrate_a_star_and_movement] added camera position fix

This commit is contained in:
czorekk 2022-04-28 21:28:03 +02:00
parent ffffa3a308
commit e6448b61cd
4 changed files with 16 additions and 10 deletions

View File

@ -19,8 +19,6 @@ class aiPlayer():
self.game.draw() self.game.draw()
# print(self.player.get_actual_coords()) # print(self.player.get_actual_coords())
def turn_left(self): def turn_left(self):
change = int(self.player.rotation()) - 1 change = int(self.player.rotation()) - 1
if(change == -1): if(change == -1):
@ -42,5 +40,5 @@ class aiPlayer():
self.turn_right() self.turn_right()
if action == 'left': if action == 'left':
self.turn_left() self.turn_left()
print(f'ROT: {self.player.rot}') # print(f'ROT: {self.player.rot}')
print("Agent pos: ", math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE)) # print("Agent pos: ", math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE))

View File

@ -26,15 +26,14 @@ class Player(pg.sprite.Sprite):
def set_rotation(self, rotation): def set_rotation(self, rotation):
self.__rotation = rotation self.__rotation = rotation
if (rotation == a_star_utils.Rotation.UP or rotation == int(a_star_utils.Rotation.UP)): if (rotation == a_star_utils.Rotation.UP or rotation == int(a_star_utils.Rotation.UP)):
self.rot = 90 self.rot = -90
elif (rotation == a_star_utils.Rotation.RIGHT or rotation == int(a_star_utils.Rotation.RIGHT)): elif (rotation == a_star_utils.Rotation.RIGHT or rotation == int(a_star_utils.Rotation.RIGHT)):
self.rot = 0 self.rot = 0
elif (rotation == a_star_utils.Rotation.DOWN or rotation == int(a_star_utils.Rotation.DOWN)): elif (rotation == a_star_utils.Rotation.DOWN or rotation == int(a_star_utils.Rotation.DOWN)):
self.rot = 270 self.rot = 90
elif (rotation == a_star_utils.Rotation.LEFT or rotation == int(a_star_utils.Rotation.LEFT)): elif (rotation == a_star_utils.Rotation.LEFT or rotation == int(a_star_utils.Rotation.LEFT)):
self.rot = 180 self.rot = 180
def get_keys(self): def get_keys(self):
self.rot_speed = 0 self.rot_speed = 0
self.vel = vec(0, 0) self.vel = vec(0, 0)
@ -51,8 +50,12 @@ class Player(pg.sprite.Sprite):
def update(self): def update(self):
self.get_keys() self.get_keys()
self.rot = (self.rot + self.rot_speed * self.game.dt) % 360 # must be fix for manual movement
self.image = pg.transform.rotate(self.game.player_img, self.rot) # self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
image_rotation = self.rot
if(abs(image_rotation) == 90):
image_rotation *= -1
self.image = pg.transform.rotate(self.game.player_img, image_rotation)
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.center = self.pos self.rect.center = self.pos
self.pos += self.vel * self.game.dt self.pos += self.vel * self.game.dt

View File

@ -123,7 +123,8 @@ class Game():
self.debug_mode = not self.debug_mode self.debug_mode = not self.debug_mode
if event.type == pg.MOUSEBUTTONUP: if event.type == pg.MOUSEBUTTONUP:
pos = pg.mouse.get_pos() pos = pg.mouse.get_pos()
clicked_coords = [math.floor(pos[0] / TILESIZE), math.floor(pos[1] / TILESIZE)] offset_x, offset_y = self.camera.offset()
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), clicked_coords[0], clicked_coords[1], self.mapArray) actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), clicked_coords[0], clicked_coords[1], self.mapArray)
print(actions) print(actions)
t = aiPlayer.aiPlayer(self.player, game=self) t = aiPlayer.aiPlayer(self.player, game=self)

View File

@ -48,6 +48,10 @@ class Camera:
def apply_rect(self, rect): def apply_rect(self, rect):
return rect.move(self.camera.topleft) return rect.move(self.camera.topleft)
def offset(self):
x, y = self.camera.topleft
return x//TILE_SIZE_PX, y//TILE_SIZE_PX
def update(self,target): def update(self,target):
x = -target.rect.x + int(WIDTH/2) x = -target.rect.x + int(WIDTH/2)
y = -target.rect.y + int(HEIGHT / 2) y = -target.rect.y + int(HEIGHT / 2)