import pygame as pg 
from settings import *
from tilemap import collide_hit_rect
vec = pg.math.Vector2
#Player


class Player(pg.sprite.Sprite):
  def __init__(self, game, x, y):
    self.groups = game.all_sprites
    pg.sprite.Sprite.__init__(self, self.groups)
    self.game = game
    self.image = game.player_spr
    self.rect = self.image.get_rect()
    self.hit_rect = PLAYER_HIT_RECT
    self.hit_rect.center = self.rect.center
    self.pos = vec(x,y) * TILESIZE
    self.rot = 0

  def move(self):
    self.rot_speed = 0 
    self.vel = vec(0,0)
    keys = pg.key.get_pressed()
    if keys[pg.K_LEFT] or keys[pg.K_a]:
      self.rot_speed = PLAYER_ROT_SPEED
    if keys[pg.K_RIGHT] or keys[pg.K_d]:
      self.rot_speed = -PLAYER_ROT_SPEED
    if keys[pg.K_UP] or keys[pg.K_w]:
      self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot)
    if keys[pg.K_DOWN] or keys[pg.K_s]:
      self.vel = vec(-PLAYER_SPEED/2, 0).rotate(-self.rot)
    
 

    
  def collide_with_walls(self, dir):
    #collistion with X axis
    if dir == "x":
      hits = pg.sprite.spritecollide(self, self.game.walls, False, collide_hit_rect)
      if hits:
        if self.vel.x > 0:
          self.pos.x = hits[0].rect.left - self.hit_rect.width / 2.0
        if self.vel.x < 0:
          self.pos.x = hits[0].rect.right + self.hit_rect.width / 2.0
        self.vx = 0
        self.hit_rect.centerx = self.pos.x
    #collistion with y axis
    if dir == "y":
      hits = pg.sprite.spritecollide(self, self.game.walls, False, collide_hit_rect)
      if hits:
        if self.vel.y > 0:
          self.pos.y = hits[0].rect.top - self.hit_rect.height / 2.0
        if self.vel.y < 0:
          self.pos.y = hits[0].rect.bottom + self.hit_rect.height / 2.0
        self.vel.y = 0
        self.hit_rect.centery = self.pos.y 

  def update(self):#update playeras pos on screen 
    self.move()
    self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
    self.image = pg.transform.rotate(self.game.player_spr, self.rot)
    self.rect = self.image.get_rect()
    self.rect.center = self.pos
    self.pos += self.vel * self.game.dt
    self.hit_rect.centerx = self.pos.x
    self.collide_with_walls('x')
    self.hit_rect.centery = self.pos.y
    self.collide_with_walls('y')
    self.rect.center = self.hit_rect.center
    
    

class Wall(pg.sprite.Sprite):
  def __init__(self,game,x,y, floor_spr):
    self.groups = game.all_sprites, game.walls 
    pg.sprite.Sprite.__init__(self, self.groups)
    self.game = game
    self.image = game.wall_spr
    self.rect = self.image.get_rect()
    self.x = x
    self.y = y
    self.rect.x = x * TILESIZE
    self.rect.y = y * TILESIZE 
    slef 
        

