import arcade
from random import randint

SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1024
GRAVITY = 1

class Player(arcade.SpriteList):
  def __init__(self,sp):
    super().__init__()
    self.sprite = sp[0]
    self.append(self.sprite)
  def draw(self):
    super().draw()
  def update(self):
    super().update()

coin_texture = arcade.SpriteSheet('./tiles/gold.png').get_texture_grid((64,64),4,4)
coin_texture_reverse = coin_texture[1:-1]
coin_texture_reverse.reverse()
coin_texture.extend(coin_texture_reverse)
coin_texture_reverse = None

class Coin(arcade.Sprite):
  sprite_list = arcade.SpriteList()
  texture_list = coin_texture
  def __init__(self,sprite):
    super().__init__(sprite.textures[0],scale=sprite.scale)
    self.center_x = sprite.center_x
    self.center_y = sprite.center_y
    self.cur_texture = randint(0,len(Coin.texture_list))
    self.texture_timer = 0
    for tex in Coin.texture_list:
      self.append_texture(tex)
    Coin.sprite_list.append(self)
  def update(self, delta_time):
    self.texture_timer += delta_time
    if self.texture_timer >= 0.2:
      self.texture_timer = 0
      self.cur_texture += 1
      if self.cur_texture > len(Coin.texture_list):
        self.cur_texture = 0
      self.set_texture(self.cur_texture)
  def collision(self):
    Coin.sprite_list.remove(self)
  @classmethod
  def draw(cls):
    cls.sprite_list.draw()

  @classmethod
  def update_list(cls):
    cls.sprite_list.update()

class MainView(arcade.View):
  def __init__(self):
    super().__init__()
    self.background_color = (100,100,200)
    self.key_up = False
    self.key_down = False
    self.key_left = False
    self.key_right = False
    self.tile_map = arcade.load_tilemap('./level1.tmj',scaling=0.5)
    self.walls = self.tile_map.sprite_lists["map"]
    self.background = self.tile_map.sprite_lists["back"]
    for coin in self.tile_map.sprite_lists["coins"]:
      Coin(coin)
    self.player = Player(self.tile_map.sprite_lists["player"])
    
    self.engine = arcade.PhysicsEnginePlatformer(
      player_sprite=self.player.sprite,
      gravity_constant=GRAVITY,
      walls=self.walls
      )
    self.engine.allow_multi_jump = False
    self.camera = arcade.Camera2D()

  def on_update(self, delta_time):
    if self.key_up and self.engine.can_jump():
      self.engine.jump(20)
    if self.key_left:
      self.player.sprite.change_x = -10
    elif self.key_right:
      self.player.sprite.change_x = 10
    else:
      self.player.sprite.change_x = 0
    self.engine.update()
    screen_center_x, screen_center_y = self.player.sprite.position
    if screen_center_x < self.camera.viewport_width/2:
      screen_center_x = self.camera.viewport_width/2
    if screen_center_y < self.camera.viewport_height/2:
      screen_center_y = self.camera.viewport_height/2
    if screen_center_x > (self.tile_map.width*self.tile_map.tile_width*self.tile_map.scaling) - self.camera.viewport_width/2:
      screen_center_x = (self.tile_map.width*self.tile_map.tile_width*self.tile_map.scaling) - self.camera.viewport_width/2
    self.camera.position = screen_center_x,screen_center_y
    Coin.update_list()
    colls = arcade.check_for_collision_with_list(self.player.sprite,Coin.sprite_list)
    for coin in colls:
      coin.collision()

  def on_draw(self):
    self.clear()
    self.camera.use()
    self.background.draw()
    Coin.draw()
    self.player.draw()
    self.walls.draw()

  def on_key_press(self,key,mod):
    if key == arcade.key.W:
      self.key_up = True
    if key == arcade.key.S:
      self.key_down = True
    if key == arcade.key.A:
      self.key_left = True
    if key == arcade.key.D:
      self.key_right = True
  def on_key_release(self,key,mod):
    if key == arcade.key.W:
      self.key_up = False
    if key == arcade.key.S:
      self.key_down = False
    if key == arcade.key.A:
      self.key_left = False
    if key == arcade.key.D:
      self.key_right = False
    
if __name__ == '__main__':
  window = arcade.Window(SCREEN_WIDTH,SCREEN_HEIGHT,'Geometry dash!',fullscreen=True)
  view = MainView()
  window.show_view(view)
  arcade.run()
