var game = new Phaser.Game (
	1024, 768,
	Phaser.AUTO,
	'game',
	{
		preload: preload,
		create: create,
		update: update,
    render: render
	}
);

    /*-----ПРОТОТИП ИГРОКА-----*/

function Player(name) {
  this.name = name;
  this.hp = 100;
};

Player.prototype.spawn = function() {

  /*-----СОЗДАНИЕ АНИМАЦИИ-----*/

  this.name = game.add.sprite(480, 350, 'humantile');
  this.name.animations.add('left', [14, 15, 16], 10, true);
  this.name.animations.add('right', [17, 18, 19], 10, true);
  this.name.animations.add('up', [20, 21, 22], 10, true);
  this.name.animations.add('down', [23, 24, 25], 10, true);
  this.name.animations.add('attack', [1, 2, 3], 10, true);

  game.physics.enable(this.name);
};

Player.prototype.animation = function() {
  game.physics.arcade.collide(this.name, platforms);

  this.name.body.velocity.x = 0;
  this.name.body.velocity.y = 0;
  this.name.body.angularVelocity = 0;

  /*-----ДВИЖЕНИЕ ИГРОКА-----*/

     if (cursors.left.isDown)
      {
          //  Движение влево
          this.name.body.velocity.x = -250;
          if (!attackbut.isDown){this.name.animations.play('left');}
          // Движение влево вверх
          if (cursors.up.isDown){
            this.name.body.velocity.x = -175;
            this.name.body.velocity.y = -175;
            if (!attackbut.isDown){this.name.animations.play('left');}
          }
          // Движение влево вниз
          if (cursors.down.isDown){
            this.name.body.velocity.x = -175;
            this.name.body.velocity.y = 175;
            if (!attackbut.isDown){this.name.animations.play('left');}
          }
      }
      else if (cursors.right.isDown)
      {
          //  Движение вправо
          this.name.body.velocity.x = 250;
          if (!attackbut.isDown){this.name.animations.play('right');}
          // Движение вправо вниз
          if (cursors.up.isDown){
            this.name.body.velocity.x = 175;
            this.name.body.velocity.y = -175;
            if (!attackbut.isDown){this.name.animations.play('right');}
          }
          // Движение вправо вверх
          if (cursors.down.isDown){
            this.name.body.velocity.x = 175;
            this.name.body.velocity.y = 175;
            if (!attackbut.isDown){this.name.animations.play('right');}
          }
      }
      else if (cursors.up.isDown)
      {
          //  Движение вверх
          this.name.body.velocity.y = -250;
          if (!attackbut.isDown){this.name.animations.play('up');}
      }
      else if (cursors.down.isDown)
      {
          //  Движение вниз
          this.name.body.velocity.y = 250;
          if (!attackbut.isDown){this.name.animations.play('down');}
      }
      else
      {
        // Неподвижно
        if (!attackbut.isDown){this.name.animations.stop();}
        this.name.frame = 0;
      }

      if (attackbut.isDown){
    	this.name.animations.play('attack');
      }
};

    /*-----ПРОТОТИП МОНСТРОВ-----*/

function Monster(name, line, timespawn) {
  this.name = name;
  this.monsterhp = 50;  //количество жизней
  this.damage = 2;  //наносимый им урон
  this.range = 50;  //радиус атаки игрока
  this.arange = -50;
  this.monster_count = 0;  //счетчик кадров (60 в секунду)
  this.line = line;  //сторона появления (35 или 940)
  this.timespawn = timespawn;  //время появления, указывается в секундах
  this.ymelee = 200;  //расстояние от игрока до врага
  this.xmelee = 200;
};

    /*-----СПАВН МОНСТРОВ-----*/

Monster.prototype.spawn = function() {
  this.monster_count++;
  if(this.monster_count == (this.timespawn * 60)) {
    this.name = game.add.sprite(this.line, game.world.randomY, 'monsterstile');
    this.name.animations.add('left', [4, 5, 6], 10, true);
    this.name.animations.add('right', [1, 2, 3], 10, true);
    this.name.animations.add('up', [7, 8, 9], 10, true);
    this.name.animations.add('down', [10, 11, 12], 10, true);
    this.name.animations.add('dmg', [13, 14, 15], 10, true);    
  }
};

Monster.prototype.move = function() {

    /*-----ДВИЖЕНИЕ МОНСТРОВ-----*/

  game.physics.enable(this.name);
  
if (this.name.x > poz.name.x)
      {
          //  Движение влево
          this.name.body.velocity.x = -80;
          this.name.animations.play('left');
          // Движение влево вверх
          if (this.name.y > poz.name.y){
            this.name.body.velocity.x = -50;
            this.name.body.velocity.y = -50;
            this.name.animations.play('left');
          }
          // Движение влево вниз
          if (this.name.y < poz.name.y){
            this.name.body.velocity.x = -50;
            this.name.body.velocity.y = 50;
            this.name.animations.play('left');
          }
      }
      else if (this.name.x < poz.name.x)
      {
          //  Движение вправо
          this.name.body.velocity.x = 80;
          this.name.animations.play('right');
          // Движение вправо вниз
          if (this.name.y > poz.name.y){
            this.name.body.velocity.x = 50;
            this.name.body.velocity.y = -50;
            this.name.animations.play('right');
          }
          // Движение вправо вверх
          if (this.name.y < poz.name.y){
            this.name.body.velocity.x = 50;
            this.name.body.velocity.y = 50;
            this.name.animations.play('right');
          }
      }
      else if (this.name.y < poz.name.y)
      {
          //  Движение вверх
          this.name.body.velocity.y = -80;
          this.name.animations.play('up');
      }
      else if (this.name.y > poz.name.y)
      {
          //  Движение вниз
          this.name.body.velocity.y = 80;
          this.name.animations.play('down');
      }
  

};

  /*-----БОЙ МОНСТРОВ-----*/

Monster.prototype.death = function() {
	this.ymelee = this.name.y - poz.name.y;
	this.xmelee = this.name.x - poz.name.x;
	if (this.ymelee < this.range && this.ymelee > this.arange && this.xmelee < this.range && this.xmelee > this.arange && attackbut.isDown) {
      	this.monsterhp -= this.damage;
  	  }
	if (this.monsterhp <= 0) {
    if (poz.hp > 0) {
      xp += 5;
    }
    this.monsterhp = 50;
    if (this.line == 35) {
      this.name.x = this.line - 300;
      this.name.y = game.world.randomY;
    }
    if (this.line == 940) {
      this.name.x = this.line + 300;
      this.name.y = game.world.randomY;
    }
	}

	if (this.ymelee < this.range - 20 && this.ymelee > this.arange + 20 && this.xmelee < this.range - 20 && this.xmelee > this.arange + 20) {
    this.name.animations.play('dmg');
		poz.hp -= 1;
	}
	return poz.hp;
};

function gameover() {
  if (poz.hp <= 0) {
    endString = 'Game Over. Your Score - ';
    endText = game.add.text(320, 200, endString + xp, { font: '34px Arial', fill: '#fff' });
    poz.name.kill();
  }
}

function preload() {
	  game.load.image('map', 'assets/mapp2.png');
    game.load.spritesheet('humantile', 'assets/humantile.png', 64, 64);
    game.load.spritesheet('monsterstile', 'assets/monsterstile.png', 64, 64);
    game.load.image('board_left', 'assets/board_left.png');
    game.load.image('board_right', 'assets/board_right.png');
    game.load.image('board_up', 'assets/board_up.png');
    game.load.image('board_down', 'assets/board_down.png');
}

var cursors;
var attackbut;
var layer;
var map;
var boardl, boardr, boardd, boardu;
var platforms;
var xp = 0;

var skeleton11 = new Monster('sk11', 35, 1);
var skeleton12 = new Monster('sk12', 35, 1);
var skeleton13 = new Monster('sk13', 35, 5);
var skeleton14 = new Monster('sk14', 35, 5);
var skeleton15 = new Monster('sk15', 35, 5);
var skeleton16 = new Monster('sk16', 35, 12);
var skeleton17 = new Monster('sk17', 35, 12);
var skeleton18 = new Monster('sk18', 35, 12);
var skeleton19 = new Monster('sk19', 35, 12);
var skeleton10 = new Monster('sk10', 35, 25);
var skeleton111 = new Monster('sk111', 35, 25);
var skeleton112 = new Monster('sk112', 35, 25);
var skeleton113 = new Monster('sk113', 35, 25);
var skeleton114 = new Monster('sk114', 35, 25);
var skeleton21 = new Monster('sk21', 940, 1);
var skeleton22 = new Monster('sk22', 940, 1);
var skeleton23 = new Monster('sk23', 940, 5);
var skeleton24 = new Monster('sk24', 940, 5);
var skeleton25 = new Monster('sk25', 940, 5);
var skeleton26 = new Monster('sk26', 940, 12);
var skeleton27 = new Monster('sk27', 940, 12);
var skeleton28 = new Monster('sk28', 940, 12);
var skeleton29 = new Monster('sk29', 940, 12);
var skeleton20 = new Monster('sk20', 940, 25);
var skeleton211 = new Monster('sk211', 940, 25);
var skeleton212 = new Monster('sk212', 940, 25);
var skeleton213 = new Monster('sk213', 940, 25);
var skeleton214 = new Monster('sk214', 940, 25);

var poz = new Player('pozh');

function create() {
	game.physics.startSystem(Phaser.Physics.ARCADE);
  
  /*-----ГЕНЕРАЦИЯ УРОВНЯ-----*/

  platforms = game.add.group();
  platforms.enableBody = true;
  boardl = platforms.create(0, 0, 'board_left');
  boardr = platforms.create(1000, 0, 'board_right');
  boardd = platforms.create(0, 750, 'board_down');
  boardu = platforms.create(0, 0, 'board_up');
  boardl.body.immovable = true;
  boardr.body.immovable = true;
  boardd.body.immovable = true;
  boardu.body.immovable = true;

  game.add.sprite(0, 0, 'map');  
  
  poz.spawn();

  hpString = 'Health: ';  //Здоровье
  hpText = game.add.text(10, 10, hpString + poz.hp, { font: '24px Arial', fill: '#fff' });
  xpString = 'Score: ';  //Очки
  xpText = game.add.text(850, 10, xpString + xp, { font: '24px Arial', fill: '#fff' });

  cursors = game.input.keyboard.createCursorKeys();
  attackbut = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}

/*-----ИГРОВОЙ ПРОЦЕСС-----*/

function update() {
  poz.animation();
  gameover();

  if (poz.hp >= 0) {
    hpText.text = hpString + poz.hp;
  }
  xpText.text = xpString + xp;

  monsterspawn();
}

function render () {
  
}