// JavaScript test 2016-10-27

//ThorBoard
//A simple skateboarding game

console.log("ThorBoard v 0.1");

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

var skater = new Image();
skater.src = 'resources/skater.png';

var skaterPosDefault = [50,93];
var skaterPos = skaterPosDefault;
var skaterInAir = false;
var jumpHeight = 15;
var jumpHeightTotal = jumpHeight;
var jumpHeightMax = 60;
var jumpTimer = 0;

var downKeyIsDown = false;

context.moveTo(0,240);
context.lineTo(720,240);
context.stroke();

var score = 0
var gameStarted = false;

// execute main function now
main();

//Functions:

function main(){
  var text = "hello world. press space to play or something";
  displayText(text);

  skater.onload = function(){
    context.drawImage(skater, skaterPos[0], skaterPos[1]);
  }
  //------------
  //Key Handlers
  //------------
  function keyPressHandler(event)
  {
  	var keyPressed = event.keyCode;
    if(gameStarted == true){
    	if (keyPressed == 38)
    	{
    		    console.log("up");
            if(skaterPos == skaterPosDefault){
              skaterInAir = true;
              console.log("jump!");
            }
    	}
    	else if (keyPressed == 40)
    	{
          console.log("down");
          downKeyIsDown = true;
    	}
    }
  }

function keyUpHandler(event){
  var keyPressed = event.keyCode;
  if(gameStarted == true){
    if (keyPressed == 40){
        downKeyIsDown = false;
    }
  }
}


  document.addEventListener("keydown",keyPressHandler, false);
  document.addEventListener("keyup",keyUpHandler, false);

  onkeypress = function(){
    if (gameStarted == false){
      setInterval("drawFrame()", 1000/60); //60 Frames per second
      gameStarted = true;
    }
  }// end key handling

} //end of main

function displayText(text) {
  context.font = "30px Arial";
  context.strokeText(text,10,50);
}

function drawPlayer(){
  if(skaterInAir){
    jumpTimer++;
    if(jumpTimer < jumpHeightTotal){
      skaterPos[1]--;
    }
    else if (jumpTimer == jumpHeightTotal){
      score++;
      console.log(jumpTimer + " jumpTimer,");
    }
    else if (jumpTimer > jumpHeightTotal && jumpTimer < jumpHeightTotal*2){
      skaterPos[1]++;
    }
    else {
      skaterInAir = false;
      jumpTimer = 0;
    }
  }
  else if(downKeyIsDown && (jumpHeightTotal < jumpHeightMax)){
    jumpHeightTotal++;
  }
  else if(!downKeyIsDown) {
    if(jumpHeightTotal > jumpHeight){
      jumpHeightTotal--;
    }
  }


  context.drawImage(skater, skaterPos[0], skaterPos[1]);

}

function drawFrame(){
    context.clearRect(0,0,canvas.width,canvas.height);
    //Draw anything else...
    displayText(jumpHeightTotal);
    score++;

    context.moveTo(0,240);
    context.lineTo(720,240);
    context.stroke();


    drawPlayer();


}

@@ -420,17 +420,18 @@
 htMax = 
-6
+10
 0;%0Avar j
@@ -443,16 +443,57 @@
 mer = 0;
+%0Avar kickflipTime = 10;%0Avar flipTime = 0;
 %0A%0Avar do
@@ -512,16 +512,41 @@
 = false;
+%0Avar upKeyIsDown = false;
 %0A%0Acontex
@@ -1075,24 +1075,56 @@
  38)%0A    %09%7B%0A
+            upKeyIsDown = true;%0A
     %09%09    co
@@ -1549,24 +1549,89 @@
 alse;%0A    %7D%0A
+    if (keyPressed == 38)%0A    %7B%0A      upKeyIsDown = false;%0A    %7D%0A
   %7D%0A%7D%0A%0A%0A  do
@@ -2084,16 +2084,95 @@
 InAir)%7B%0A
+    if(downKeyIsDown && ! upKeyIsDown)%7B%0A%0A      displayText(%22Kickflip!%22);%0A    %7D%0A
     jump
@@ -2525,24 +2525,79 @@
 ;%0A    %7D%0A  %7D%0A
+  else if(flipTime %3E 2)%7B%0A%0A    console.log(%22DEAD%22);%0A  %7D%0A
   else if(do
@@ -2775,14 +2775,13 @@
   %7D%0A
+
   %7D%0A%0A
-%0A
   co

add keyUpEvent for up, increase max jump height