attempt at tracker style music

Dependencies:   PokittoLib

main.cpp

Committer:
spinal
Date:
2018-10-20
Revision:
12:37d999e445ad
Parent:
11:a573cacdc078
Child:
13:8ce494870ba6

File content as of revision 12:37d999e445ad:

#include "Pokitto.h"

Pokitto::Core mygame;
Pokitto::Display d;

mbed::DigitalOut jh_rumble(EXT0);
mbed::DigitalIn jh_b1(EXT15);
mbed::DigitalIn jh_b2(EXT14);
mbed::AnalogIn jh_x(EXT1);
mbed::AnalogIn jh_y(EXT2);

#define ANALOG 256
#define DECAY .85
#define SEGSIZE 7
#define number_of_segments 10
#define MOVEANGLE .25

//Sprite structure
typedef struct
{
    int x,y,oldx,oldy;  //location 
    int xSpeed, ySpeed; //speed
    char frame; // body gfx
}Sprite;
Sprite marbles[number_of_segments];


bool move_tail(void){
    int pos_x,pos_y;
    int bodySize = SEGSIZE*512;
    bool collision = false;
    for(int t=1; t<number_of_segments; t++){
//    float rotation = atan2((float)marbles[t].y-marbles[t-1].y,(float)marbles[t].x-marbles[t-1].x);
/*
    // rotate the head graphic
    if(t==1)
    {
        int rot = rotation*(180 / 3.14159);
        rot+=90;
        if(rot<0)rot+=360;
        if(rot>360)rot-=360;
        rot/=10;
        rot+=3;
//        PA_SetSpriteAnimEx(1,0,16,16,1,rot);
    }
*/

    float x1 = marbles[t].x;
    float y1 = marbles[t].y;
    float x2 = marbles[t-1].x;
    float y2 = marbles[t-1].y;
    float rotation = atan2(y1-y2,x1-x2);

    for(int s=0; s<number_of_segments; s++){
        if(s<t-1 || s>t+1){
            double square_difference_x = (x1 - marbles[s].x) * (x1 - marbles[s].x);
            double square_difference_y = (y1 - marbles[s].y) * (y1 - marbles[s].y);
            double value = sqrt(square_difference_x + square_difference_y);
            
            if(value < bodySize+1){
                pos_x = x2 + bodySize * cos(rotation+MOVEANGLE);
                pos_y = y2 + bodySize * sin(rotation+MOVEANGLE);

                double square_difference_x = (pos_x - marbles[s].x) * (pos_x - marbles[s].x);
                double square_difference_y = (pos_y - marbles[s].y) * (pos_y - marbles[s].y);
                double value2 = sqrt(square_difference_x + square_difference_y);
                
                if(value < value2)rotation+=MOVEANGLE;
                if(value > value2)rotation-=MOVEANGLE;
                collision=true;
            }
        }    
    }

    pos_x = marbles[t-1].x + bodySize * cos(rotation);
    pos_y = marbles[t-1].y + bodySize * sin(rotation);
    marbles[t].x = pos_x;
    marbles[t].y = pos_y;


    } // for t...
    return collision;

}// end function



int main ()
{
    mygame.begin();

    int JCX = ((jh_x.read()*ANALOG));
    int JCY = ((jh_y.read()*ANALOG));


    while (mygame.isRunning())
    {
        if (mygame.update())
        {
                        
            int xd = ((jh_x.read()*ANALOG)-JCX)*4;
            int yd = ((jh_y.read()*ANALOG)-JCY)*4;
            
            // motion gravity etc.
            marbles[0].xSpeed += -xd;
            marbles[0].ySpeed += yd;
            
            // add some dampening to speed
            marbles[0].xSpeed *= DECAY;
            marbles[0].ySpeed *= DECAY;
            
            marbles[0].x += marbles[0].xSpeed;
            marbles[0].y += marbles[0].ySpeed;
            
            
            jh_rumble.write(mygame.buttons.aBtn());
            d.color = 1;
            d.printf("Joy X: %d\n",xd);
            d.printf("Joy Y: %d\n",yd);
            d.printf("B1: %d\n",jh_b1.read());
            d.printf("B2: %d\n",jh_b2.read());
            d.printf("A to Rumble!\n");


            // loop until the snake no longer collides with itself.
            while(move_tail()){}

            for(int t=0; t<number_of_segments; t++){
                d.color = 0;
                if(t==0) d.color = 1;
                d.fillCircle(marbles[t].x>>8,marbles[t].y>>8,SEGSIZE);
                d.color = 2;
                d.drawCircle(marbles[t].x>>8,marbles[t].y>>8,SEGSIZE);
            }




        }
    }
}