> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.tjfJhpg5hiM/rev.344
 * 
 * authors: 
 *   Kryštof Pešek

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



/*
USE ARROW KEYS TO MOVE THE CUBE ;)
*/



/*@pjs preload="/static/uploaded_resources/p.683/cubeLT.png,/static/uploaded_resources/p.683/cubeRT.png,/static/uploaded_resources/p.683/cubeLD.png,/static/uploaded_resources/p.683/cubeRD.png"; globalKeyEvents ="true";*/

interface Javascript {}
Javascript javascript=null;
void bindJavascript(Javascript js) { javascript=js; }


int speed = 2;
int counter = 0;
boolean debug = false;

Postava p;

void setup(){
    size(320,240);
    
    frameRate(60);

    p = new Postava();


}

void draw(){
    background(255);

    p.show();
    counter++;

}


void keyPressed(){
    if(javascript==null && keyCode==LEFT){
        p.changeDir(0);
    }else if(javascript==null && keyCode==UP){
        p.changeDir(1);
    }else if(javascript==null && keyCode==RIGHT){
        p.changeDir(2);
    }else if(javascript==null && keyCode==DOWN){
        p.changeDir(3);
    }

    p.moving = true;

}

void keyReleased(){
    p.moving = false;

}




class Postava{
    ArrayList sprites = new ArrayList();
    Anim current;
    
    PGraphics shade;
    int curFaze = 0;
    float x,y;
    float moveSpeed = 1;
    int dir=0;
    boolean moving = false;
    float iso = 0.653;
    int w = 64;
    int h = 64;


    Postava(){
        if(debug)
        println("loading files ...");
        sprites.add(new Anim("/static/uploaded_resources/p.683/cubeLD.png",w,h,10));
        sprites.add(new Anim("/static/uploaded_resources/p.683/cubeLT.png",w,h,10));
        sprites.add(new Anim("/static/uploaded_resources/p.683/cubeRT.png",w,h,10));
        sprites.add(new Anim("/static/uploaded_resources/p.683/cubeRD.png",w,h,10));
        if(debug)
        println("lodaded "+sprites.size()+" files");
            
        x = width/2;
        y = height/2;
        
        shade = createGraphics(w,h,JAVA2D);
        shade.beginDraw();
        shade.fill(0,3);
        shade.noStroke();
        for(int i = 0 ;i<w/2;i+=1){
        shade.ellipse(w/2,h/2,i*2,i*1.5);
        }
        shade.endDraw();


        current = (Anim)sprites.get(dir);
    
    }

    void show(){

        image(shade,x,y+12);


        current.draw(curFaze,x,y);

if(moving){
        if(counter%speed==0){
            curFaze++;
        }

        if(curFaze>=current.pocet){
            curFaze = 0;
        }

        
        if(dir==0){
            x-=moveSpeed;
            y+=iso*moveSpeed;
        }else if(dir == 1){
            x-=moveSpeed;
            y-=iso*moveSpeed;
        }else if(dir==2){
            x+=moveSpeed;
            y-=iso*moveSpeed;
        }else if(dir == 3){
            x+=moveSpeed;
            y+=iso*moveSpeed;

        }

        if(x>width)x=-w;
        if(x<-w)x=width;
        if(y>height)y=-h;
        if(y<-h)y=height;
}else{
    curFaze = 0;
}
    }

    void changeDir(int _kam){
        dir = _kam;
        current = (Anim)sprites.get(dir);
    }


}


class Anim{
    PImage src;
    ArrayList faze = new ArrayList();
    String filename;
    int pocet;
    int w,h;

    Anim(String _filename,int _w,int _h,int _pocet){
        filename = _filename;
        src = loadImage(filename);
        if(debug)
        println(src.width);

        w=_w;
        h=_h;
        pocet = _pocet;

            for(int i = 0;i<pocet;i++){
        PGraphics tmp = createGraphics(w,h,JAVA2D);
        tmp.beginDraw();
        tmp.image(src,-i*w,0);
        tmp.endDraw();
        faze.add(tmp);
        }


    }

    void draw(int f,float _x,float _y){
        PGraphics current = (PGraphics)faze.get(f);
        image(current,_x,_y);
    }


}