import pygame
from settings import PLAYER_SIZE

class Player:
    def __init__(self, x, y, image):
        self.x = x
        self.y = y
        self.alive = True
        self.rect = pygame.Rect(x, y, PLAYER_SIZE, PLAYER_SIZE)
        self.dir = "up"
        self.image = image

    def move(self, keys, map_w, map_h):
        if not self.alive:
            return
        dx = dy = 0
        if keys[pygame.K_w]:
            dy -= 5
            self.dir = "up"
        if keys[pygame.K_s]:
            dy += 5
            self.dir = "down"
        if keys[pygame.K_a]:
            dx -= 5
            self.dir = "left"
        if keys[pygame.K_d]:
            dx += 5
            self.dir = "right"
        self.x = max(0, min(map_w - PLAYER_SIZE, self.x + dx))
        self.y = max(0, min(map_h - PLAYER_SIZE, self.y + dy))
        self.rect.topleft = (self.x, self.y)

    def draw(self, win, ox, oy):
        if not self.alive:
            return
        img = self.image
        angle = {"up": 90, "right": 0, "down": -90, "left": 180}[self.dir]
        if img:
            rotated_img = pygame.transform.rotate(img, angle)
            win.blit(rotated_img, (self.x - ox, self.y - oy))
        else:
            pygame.draw.rect(win, (200, 200, 0), (self.x - ox, self.y - oy, PLAYER_SIZE, PLAYER_SIZE))
