bakagamedev

halp

Jun 11th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.28 KB | None | 0 0
  1. #include "Arduboy.h"
  2. Arduboy ab;
  3.  
  4. //3d view or map?
  5. bool viewMap=false;
  6. //list of visible walls
  7. bool wallShow[11] = { };
  8. //this maze should probably be in PROGMEM if you want it to be static, but the intention here is to integrate it into a generator
  9. bool maze[8][8] =
  10. {
  11.   1,1,1,1,1,1,1,1,
  12.   1,0,0,0,1,0,0,1,
  13.   1,1,0,1,0,0,1,1,
  14.   1,0,0,0,0,1,0,1,
  15.   1,1,0,1,1,1,0,1,
  16.   1,0,0,0,0,0,0,1,
  17.   1,0,1,1,1,1,0,1,
  18.   1,1,1,1,1,1,1,1
  19. };
  20.  
  21. uint8_t cameraX = 1;
  22. uint8_t cameraY = 1;
  23. uint8_t cameraDir = 0;
  24.  
  25. void calculateView()
  26. {
  27.   wallShow[10] = false; //[10] is the players current position, don't bother checking there.
  28.   //Searches the grid and fills a visibility array with whatever it finds.
  29.   //This should probably be automated with a fancy for loop.
  30.   if (cameraDir == 0)
  31.   {
  32.     wallShow[0] = wallCheck(cameraX+3,cameraY-1);
  33.     wallShow[1] = wallCheck(cameraX+3,cameraY);
  34.     wallShow[2] = wallCheck(cameraX+3,cameraY+1);
  35.     wallShow[3] = wallCheck(cameraX+2,cameraY-1);
  36.     wallShow[4] = wallCheck(cameraX+2,cameraY);
  37.     wallShow[5] = wallCheck(cameraX+2,cameraY+1);
  38.     wallShow[6] = wallCheck(cameraX+1,cameraY-1);
  39.     wallShow[7] = wallCheck(cameraX+1,cameraY);
  40.     wallShow[8] = wallCheck(cameraX+1,cameraY+1);
  41.     wallShow[9] = wallCheck(cameraX,cameraY-1);
  42.     wallShow[11] = wallCheck(cameraX,cameraY+1);
  43.   }
  44.   if (cameraDir == 1)
  45.   {
  46.     wallShow[0] = wallCheck(cameraX+1,cameraY+3);
  47.     wallShow[1] = wallCheck(cameraX,cameraY+3);
  48.     wallShow[2] = wallCheck(cameraX-1,cameraY+3);
  49.     wallShow[3] = wallCheck(cameraX+1,cameraY+2);
  50.     wallShow[4] = wallCheck(cameraX,cameraY+2);
  51.     wallShow[5] = wallCheck(cameraX-1,cameraY+2);
  52.     wallShow[6] = wallCheck(cameraX+1,cameraY+1);
  53.     wallShow[7] = wallCheck(cameraX,cameraY+1);
  54.     wallShow[8] = wallCheck(cameraX-1,cameraY+1);
  55.     wallShow[9] = wallCheck(cameraX+1,cameraY);
  56.     wallShow[11] = wallCheck(cameraX-1,cameraY);
  57.   }
  58.   if (cameraDir == 2)
  59.   {
  60.     wallShow[0] = wallCheck(cameraX-3,cameraY+1);
  61.     wallShow[1] = wallCheck(cameraX-3,cameraY);
  62.     wallShow[2] = wallCheck(cameraX-3,cameraY-1);
  63.     wallShow[3] = wallCheck(cameraX-2,cameraY+1);
  64.     wallShow[4] = wallCheck(cameraX-2,cameraY);
  65.     wallShow[5] = wallCheck(cameraX-2,cameraY-1);
  66.     wallShow[6] = wallCheck(cameraX-1,cameraY+1);
  67.     wallShow[7] = wallCheck(cameraX-1,cameraY);
  68.     wallShow[8] = wallCheck(cameraX-1,cameraY-1);
  69.     wallShow[9] = wallCheck(cameraX,cameraY+1);
  70.     wallShow[11] = wallCheck(cameraX,cameraY-1);
  71.   }
  72.   if (cameraDir == 3)
  73.   {
  74.     wallShow[0] = wallCheck(cameraX-1,cameraY-3);
  75.     wallShow[1] = wallCheck(cameraX,cameraY-3);
  76.     wallShow[2] = wallCheck(cameraX+1,cameraY-3);
  77.     wallShow[3] = wallCheck(cameraX-1,cameraY-2);
  78.     wallShow[4] = wallCheck(cameraX,cameraY-2);
  79.     wallShow[5] = wallCheck(cameraX+1,cameraY-2);
  80.     wallShow[6] = wallCheck(cameraX-1,cameraY-1);
  81.     wallShow[7] = wallCheck(cameraX,cameraY-1);
  82.     wallShow[8] = wallCheck(cameraX+1,cameraY-1);
  83.     wallShow[9] = wallCheck(cameraX-1,cameraY);
  84.     wallShow[11] = wallCheck(cameraX+1,cameraY);
  85.   }
  86.   if (wallShow[7])  //speed up by disabling hidden walls
  87.   {
  88.     wallShow[4] = false;
  89.     wallShow[0] = false;
  90.     wallShow[2] = false;
  91.   }
  92.   if (wallShow[4])
  93.   {
  94.     wallShow[1] = false;
  95.   }
  96. }
  97.  
  98. bool wallCheck(int x,int y)
  99. {
  100.   if((x<0) or (y<0) or (x>7) or (y>7))
  101.     return (true);  //Out of bounds is full of walls, means you don't have to waste space by surrounding a map
  102.   else
  103.     return (maze[x][y]);
  104. }
  105.  
  106. void drawView() //byte x,byte width)
  107. {
  108.   /*
  109.   draws the viewpoint as calculated in calculateView(), drawing every wall in turn, from far->near.
  110.   */
  111.   int wallSize[] = { 6,10,18,32,64};  //size in pixels of each step
  112.   char wall = 0;  //current wall
  113.  
  114.   int drawSize,halfSize,backSize,halfBackSize,left,leftBack,top,topBack;
  115.   for(char i=0; i<4; i++) //distance
  116.   {
  117.     drawSize = wallSize[i+1]; halfSize = drawSize/2;      //size of walls on screen
  118.     backSize = wallSize[i];   halfBackSize = backSize/2;  //size of the backside of the walls, for depth
  119.     leftBack = 32-(halfBackSize*3);      //x position of the walls on screen
  120.     left     = 32-(halfSize*3);
  121.     topBack  = 32-halfBackSize;         //y position of the walls on screen
  122.     top      = 32-halfSize;
  123.  
  124.     for(char n=0; n<3; n++) //left->right
  125.     {
  126.       if (wallShow[wall]) //if wall exists, draw it
  127.       {
  128.         if((n==0)&&(!wallShow[wall+1]))  //left wall, only draw if the middle wall is missing
  129.         {
  130.           ab.fillRect(left+drawSize,top,(leftBack+backSize)-(left+drawSize),drawSize,0);  //blank out area behind wall
  131.           ab.drawLine(leftBack+backSize,topBack+backSize,left+drawSize,top+drawSize,1);   //lower line
  132.           ab.drawLine(left+drawSize,top,leftBack+backSize,topBack,1);                     //upper line
  133.           ab.drawLine(leftBack+backSize,topBack,leftBack+backSize,topBack+backSize,1);    //far line
  134.         }
  135.         if((n==2)&&(!wallShow[wall-1])) //right wall, ditto
  136.         {
  137.           ab.fillRect(leftBack,top,left-leftBack,drawSize,0);
  138.           ab.drawLine(leftBack,topBack,left,top,1);     //upper
  139.           ab.drawLine(leftBack,topBack+backSize,left,top+drawSize,1); //lower
  140.           ab.drawLine(leftBack,topBack,leftBack,topBack+backSize,1);  //side
  141.         }
  142.         if((i<3)&&(!wallShow[wall+3]))  //draw flat wall if not immediately next to the camera, and if there is no wall infront
  143.         {
  144.           int wid = drawSize; //width of wall
  145.           if ((n==2) && (left+wid > 64))  //if the wall goes off the render area, chop the width down
  146.           {
  147.             wid = 15; //(64-halfSize)-1;  //magic numbering this because the only time it ever happens is on a close right side wall
  148.           }
  149.           ab.fillRect(left,top,wid,drawSize,0);     //blank out wall area and draw then draw the outline
  150.           ab.drawRect(left,top,wid+1,drawSize+1,1);
  151.         }
  152.       }
  153.       wall++;
  154.       left += drawSize;     //advance left positions
  155.       leftBack += backSize;
  156.     }
  157.   }
  158.   ab.drawRect(0,0,64,64,1);
  159. }
  160.  
  161. void drawMap()
  162. {
  163.   //draw map grid
  164.   const uint8_t dx = 0;  //x offset, puts on right side of the screen
  165.   for(int iy=0; iy<8; iy++) //loops x&y, draws a rectangle for every wall
  166.   {
  167.     for(int ix=0; ix<8; ix++)
  168.     {
  169.       if (wallCheck(ix,iy))
  170.       {
  171.         ab.drawRect(dx+(ix*8),(iy*8),9,9,1);
  172.       }
  173.     }
  174.   }
  175.   //draws the player as a cross
  176.   char cx = cameraX+1;
  177.   char cy = cameraY+1;
  178.   ab.drawLine(dx+(((cx)*8)-6),(cy*8)-6,dx+((cx*8)-2),(cy*8)-2,1);
  179.   ab.drawLine(dx+(((cx)*8)-6),(cy*8)-2,dx+((cx*8)-2),(cy*8)-6,1);
  180.  
  181.   //outlines the map
  182.   ab.drawLine(dx+64,0,dx+64,64,1);
  183.   ab.drawLine(dx,64,dx+64,63,1);
  184. }
  185.  
  186. void setup() {
  187.   ab.begin();
  188.   ab.setFrameRate(1); //Kludge around not bothering to implement a keydown function
  189.   calculateView();
  190. }
  191.  
  192. void loop() {
  193.   if (!(ab.nextFrame()))
  194.     return;
  195.  
  196.   //change view angle on press
  197.   char cd = cameraDir;
  198.   if (ab.pressed(LEFT_BUTTON))
  199.     cameraDir -= 1;
  200.   if (ab.pressed(RIGHT_BUTTON))
  201.     cameraDir += 1;
  202.  
  203.   if (cameraDir < 0)
  204.     cameraDir = 3;
  205.   if (cameraDir > 3)
  206.     cameraDir = 0;
  207.   if (cameraDir != cd)
  208.     calculateView();
  209.  
  210.   //if the player presses up or down and the space is free, go to it
  211.   if (ab.pressed(UP_BUTTON) or (ab.pressed(DOWN_BUTTON)))
  212.   {
  213.     //calculate next space
  214.     int8_t nx=0,ny=0;   //using int8 rather than uint8 so negative numbers are possible
  215.     if (cameraDir==0)
  216.       { nx = 1; }
  217.     if (cameraDir==1)
  218.       { ny = 1; }
  219.     if (cameraDir==2)
  220.       { nx = -1; }
  221.     if (cameraDir==3)
  222.       { ny = -1; }
  223.  
  224.     if (ab.pressed(DOWN_BUTTON))  //if they pressed down, flip the direction
  225.     {
  226.       nx = 0-nx;
  227.       ny = 0-ny;
  228.     }
  229.  
  230.     nx += cameraX;  //calculate new coordinate
  231.     ny += cameraY;
  232.     if (!wallCheck(nx,ny))  //if space is empty and in bounds, move to it
  233.     {
  234.       cameraX = nx;
  235.       cameraY = ny;
  236.       calculateView();
  237.     }
  238.   }
  239.  
  240.   if(ab.pressed(B_BUTTON))  //swap screen mode when B button is pressed
  241.   {
  242.     viewMap = !viewMap;
  243.   }
  244.  
  245.   ab.clear(); //clear screen
  246.  
  247.   if (viewMap == 1)
  248.   {
  249.     drawMap();  //draw map
  250.   }
  251.   else
  252.   {
  253.     drawView(); //draw perspective
  254.     //draw camera angle indicator, for debugging.
  255.     for(char i=1; i<cameraDir+1; i++)
  256.     {
  257.       ab.drawLine(30+(i*2),0,30+(i*2),4,1);
  258.     }
  259.   }
  260.  
  261.  
  262.   ab.display();
  263. }
Add Comment
Please, Sign In to add comment