attempt at tracker style music

Dependencies:   PokittoLib

Revision:
12:37d999e445ad
Parent:
11:a573cacdc078
Child:
13:8ce494870ba6
--- a/main.cpp	Wed Apr 18 10:41:10 2018 +0000
+++ b/main.cpp	Sat Oct 20 15:16:16 2018 +0000
@@ -1,79 +1,143 @@
 #include "Pokitto.h"
 
 Pokitto::Core mygame;
+Pokitto::Display d;
 
-//------------------------[ Button handling, very accurate ]------------------------
-#define HELD 0
-#define NEW 1
-#define RELEASE 2
-byte CompletePad, ExPad, TempPad, myPad;
-bool _A[3], _B[3], _C[3], _Up[3], _Down[3], _Left[3], _Right[3];
+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];
+
 
-DigitalIn _aPin(P1_9);
-DigitalIn _bPin(P1_4);
-DigitalIn _cPin(P1_10);
-DigitalIn _upPin(P1_13);
-DigitalIn _downPin(P1_3);
-DigitalIn _leftPin(P1_25);
-DigitalIn _rightPin(P1_7);
+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);
 
-void UPDATEPAD(int pad, int var) {
-  _C[pad] = (var >> 1)&1;
-  _B[pad] = (var >> 2)&1;
-  _A[pad] = (var >> 3)&1;
-  _Down[pad] = (var >> 4)&1;
-  _Left[pad] = (var >> 5)&1;
-  _Right[pad] = (var >> 6)&1;
-  _Up[pad] = (var >> 7)&1;
-}
+                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));
+
 
-byte updateButtons(byte var){
-   var = 0;
-   if (_cPin) var |= (1<<1);
-   if (_bPin) var |= (1<<2);
-   if (_aPin) var |= (1<<3); // P1_9 = A
-   if (_downPin) var |= (1<<4);
-   if (_leftPin) var |= (1<<5);
-   if (_rightPin) var |= (1<<6);
-   if (_upPin) var |= (1<<7);
-   return var;
+    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);
+            }
+
+
+
+
+        }
+    }
 }
 
 
-void UpdatePad(int joy_code){
-  ExPad = CompletePad;
-  CompletePad = joy_code;
-  UPDATEPAD(HELD, CompletePad); // held
-  UPDATEPAD(RELEASE, (ExPad & (~CompletePad))); // released
-  UPDATEPAD(NEW, (CompletePad & (~ExPad))); // newpress
-}
-
-//----------------------------------------------------------------------------------
-
-int main () {
-    mygame.begin();
-    while (mygame.isRunning()) {
-        if (mygame.update()) {            
-            // update buttons
-            myPad = updateButtons(myPad);
-            UpdatePad(myPad);
-
-            if(_Left[HELD]){
-                mygame.display.setColor(1);
-                mygame.display.fillRectangle(0, 32, 32, 32);
-            }
-            if(_Right[HELD]){
-                mygame.display.setColor(2);
-                mygame.display.fillRectangle(32, 32, 32, 32);
-            }
-            if(_Up[HELD]){
-                mygame.display.setColor(3);
-                mygame.display.fillRectangle(0, 0, 32, 32);
-            }
-            if(_Down[HELD]){
-                mygame.display.setColor(4);
-                mygame.display.fillRectangle(0, 64, 32, 32);
-            }
-        } 
-    }    
-}
\ No newline at end of file