> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://playgramming.sketchpad.cc/sp/pad/view/ro.kXI$33liQ9d/rev.184
 * 
 * authors: 
 *   Devon Scott-Tunkin

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// This sketch builds on a prior work, "Knight Example", created by Devon Scott-Tunkin
// http://playgramming.sketchpad.cc/sp/pad/view/ro.8sKeZNuPCNSiF3/rev.777



/* @pjs preload="/static/uploaded_resources/p.9705/sprites_map_claudius.png"; */

class Knight
{
  PImage image;
  PVector position;
  int frameRow;
  int frameColumn;
  float frameTimer;
  PVector velocity;
  float speed;
  float jumpSpeed;
}

int left = 0;
int right = 0;
int up = 0;
int down = 0;

// half a pixel per frame gravity.
float gravity = .5;
 
// Y coordinate of ground for collision
float ground = 100;


Knight claudius = new Knight();

void setup() {  // this is run once.   
    size(300, 300); 

    claudius.image =  loadImage("/static/uploaded_resources/p.9705/sprites_map_claudius.png");
    claudius.position = new PVector(100, 200);
    claudius.frameTimer = 1.0;
    claudius.frameRow = 0;
    claudius.speed = 3;
    claudius.jumpSpeed = 10;
    claudius.velocity = new PVector(0, 0);

} 

void draw() {  // this is run repeatedly. 
    background(255);
    claudius.frameTimer += 0.2; // 0.1 is the framerate or speed of animation. 
    claudius.velocity.x = claudius.speed * (right - left);
    claudius.position.x += claudius.velocity.x;
    
    
  // Only apply gravity if above ground (since y positive is down we use < ground)
  if (claudius.position.y < ground)
  {
    claudius.velocity.y += gravity;
  }
  else
  {
    claudius.position.y = ground;
  }
  
  // If on the ground and "jump" keyy is pressed set my upward velocity to the jump speed!
  if (claudius.position.y >= ground && up != 0)
  {
     claudius.velocity.y = -claudius.jumpSpeed;
  }
  
  claudius.position.y += claudius.velocity.y;
    
    if (claudius.frameTimer >= 6)
    {
        claudius.frameTimer = 1;
    }
    if (claudius.velocity.x == 0) {
        claudius.frameTimer = 0;
    }
    
    PImage frameImage = claudius.image.get((int)claudius.frameTimer * 32, claudius.frameRow * 64, 32, 64);
    
    image(frameImage, claudius.position.x, claudius.position.y);
}

void keyPressed() {
    if (keyCode == UP) {
        up = 1;
    }
    if (keyCode == DOWN) {
        down = 1;
    }
    if (keyCode == RIGHT) {
        claudius.frameRow = 3;
        right = 1;
    }
    if (keyCode == LEFT) {
        claudius.frameRow = 1;
        left = 1;
    }
}

void keyReleased() {
    if (keyCode == RIGHT) {
        right = 0;
    }
    if (keyCode == LEFT) {
        left = 0;
    }
    if (keyCode == UP) {
        up = 0;
    }
    if (keyCode == DOWN) {
        down = 0;
    }
}