'use strict';

const Vector = require('./vector.js');


function Player(pos) {
  this.pos    = pos;
  this.size   = new Vector(0.8, 0.8);
  this.speed  = new Vector(0, 0);
  this.status = 'alive';
  this.score  = 0;
  this.lives  = 3;
}

Player.prototype.type = "player";

Player.prototype.act = function(step, level, keys) {

  this.moveX(step, level, keys);
  this.moveY(step, level, keys);

  let otherActor = level.actorAt(this);

  if (otherActor) {
    level.playerTouched(otherActor.type, otherActor);
  }

  if (level.status === "lost") {
    //pacman dying
    this.size.x;
    this.size.y;
  }

};

let playerXSpeed = 7;

Player.prototype.moveX = function(step, level, keys) {


  if (keys.left)  {
    this.speed.x = -playerXSpeed;
    this.speed.y = 0;
  }

  if (keys.right) {
    this.speed.x = playerXSpeed;
    this.speed.y = 0;
  }

  let motion   = new Vector(this.speed.x * step, 0);
  let newPos   = this.pos.plus(motion);

  let obstacle = level.obstacleAt(newPos, this.size);
  let _this    = this;

  return obstacleName(_this, obstacle, newPos, level);

};

let playerYSpeed = 7;

Player.prototype.moveY = function(step, level, keys) {


  if (keys.up)   {
    this.speed.y = -playerYSpeed;
    this.speed.x = 0;
  }

  if (keys.down) {
    this.speed.y = playerYSpeed;
    this.speed.x = 0;
  }

  let motion   = new Vector(0, this.speed.y * step);
  let newPos   = this.pos.plus(motion);
  let obstacle = level.obstacleAt(newPos, this.size);
  let _this    = this;

  return obstacleName(_this, obstacle, newPos, level);

};

function obstacleName(_this, obstacle, newPos, level) {
  if (obstacle) {
    level.playerTouched(obstacle);
  } else {
    _this.pos = newPos;
  }

}


module.exports = Player;


@@ -550,54 +550,39 @@
   //
-pacman dying%0A    this.size.x;%0A    this.size.y;
+ TODO: add animation for pacman
 %0A  %7D
@@ -591,27 +591,28 @@
 %7D;%0A%0A
+%0A
 let 
-playerX
+default
 Speed = 
 7;%0A%0A
@@ -599,33 +599,33 @@
  defaultSpeed = 
-7
+6
 ;%0A%0APlayer.protot
@@ -659,33 +659,32 @@
 level, keys) %7B%0A%0A
-%0A
   if (keys.left)
@@ -686,20 +686,16 @@
 left)  %7B
-%0A   
  this.sp
@@ -707,49 +707,24 @@
  = -
-playerXSpeed;%0A    this.speed.y = 0;%0A 
+defaultSpeed;
  %7D%0A
-%0A
   if
@@ -738,20 +738,16 @@
 right) %7B
-%0A   
  this.sp
@@ -758,45 +758,22 @@
 x = 
-playerXSpeed;%0A    this.speed.y = 0;%0A 
+ defaultSpeed;
  %7D%0A%0A
@@ -865,17 +865,16 @@
 otion);%0A
-%0A
   let ob
@@ -923,112 +923,167 @@
 e);%0A
+%0A
   
-let _this    = this;%0A%0A  return obstacleName(_this, obstacle, newPos, level);%0A%0A%7D;%0A%0Alet playerYSpeed = 7
+if (obstacle) %7B%0A    this.speed.x = 0;%0A    this.pos.x   = Math.round(this.pos.x);%0A    level.playerTouched(obstacle);%0A  %7D else %7B%0A    this.pos = newPos;%0A  %7D%0A%0A%7D
 ;%0A%0AP
@@ -1137,17 +1137,16 @@
 eys) %7B%0A%0A
-%0A
   if (ke
@@ -1155,20 +1155,16 @@
 .up)   %7B
-%0A   
  this.sp
@@ -1176,49 +1176,24 @@
  = -
-playerYSpeed;%0A    this.speed.x = 0;%0A 
+defaultSpeed;
  %7D%0A
-%0A
   if
@@ -1206,20 +1206,16 @@
 .down) %7B
-%0A   
  this.sp
@@ -1226,45 +1226,22 @@
 y = 
-playerYSpeed;%0A    this.speed.x = 0;%0A 
+ defaultSpeed;
  %7D%0A%0A
@@ -1391,165 +1391,91 @@
 e);%0A
-  let _this    = this;%0A%0A  return obstacleName(_this, obstacle, newPos, level);%0A%0A%7D;%0A%0Afunction obstacleName(_this, obstacle, newPos, level) %7B%0A  if (obstacle) %7B
+%0A  if (obstacle) %7B%0A    this.speed.y = 0;%0A    this.pos.y   = Math.round(this.pos.y);
 %0A   
@@ -1525,9 +1525,8 @@
     
-_
 this
@@ -1546,16 +1546,17 @@
 ;%0A  %7D%0A%0A%7D
+;
 %0A%0A%0Amodul

fix player speed and movement