pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

epitaph64 private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Epitaph64 on Sun 14 Jun 00:15
report abuse | download | new post

  1. package bufferedterrain;
  2.  
  3. import org.newdawn.slick.opengl.renderer.Renderer;
  4. import org.newdawn.slick.AppGameContainer;
  5. import org.newdawn.slick.BasicGame;
  6. import org.newdawn.slick.Color;
  7. import org.newdawn.slick.GameContainer;
  8. import org.newdawn.slick.Graphics;
  9. import org.newdawn.slick.Input;
  10. import org.newdawn.slick.SlickException;
  11.  
  12. public class Main extends BasicGame {
  13.  
  14.     Main()
  15.     {
  16.         super("Terrain Test");
  17.     }
  18.  
  19.     private Input input;
  20.     private byte[][] terrain;
  21.     private boolean[][] update;
  22.     private boolean changer;
  23.     //Game Options Start
  24.     private boolean deleteOnBottom = false;
  25.     private boolean normalGravity = true;
  26.     private boolean faucetsOn = false;
  27.     //Game Options End
  28.     private short columnToUpdate = 0;
  29.     private short brushX, brushY;
  30.     private boolean brushOn;
  31.     private byte brushType = -1;
  32.     private byte leftBrushType = -1;
  33.     private boolean resetTime = true;
  34.     private byte mouseButton = 0;
  35.     private final static short width = 400;
  36.     private final static short height = 300;
  37.  
  38.     public static void main(String[] args)
  39.     {
  40.         try
  41.         {
  42.             AppGameContainer app = new AppGameContainer(new Main());
  43.             app.setDisplayMode(800, 600, false);
  44.             app.setFullscreen(false);
  45.             app.setClearEachFrame(false);
  46.             app.setShowFPS(false);
  47.             app.setTargetFrameRate(75);
  48.             app.start();
  49.         }
  50.         catch (SlickException e)
  51.         {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public void init(GameContainer container) throws SlickException
  58.     {
  59.         input = container.getInput();
  60.         Renderer.setRenderer(Renderer.VERTEX_ARRAY_RENDERER);
  61.         start();
  62.     }
  63.  
  64.     @Override
  65.     public void update(GameContainer container, int delta) throws SlickException
  66.     {
  67.         updateInput(container);
  68.         updateTerrain();
  69.     }
  70.  
  71.     public void render(GameContainer container, Graphics g) throws SlickException
  72.     {
  73.         byte tt = 0;
  74.         for (int y = 0; y < height; y++)
  75.         {
  76.             for (int x = 0; x < width; x++)
  77.             {
  78.                 if (update[x][y])
  79.                 {
  80.                     tt = terrain[x][y];
  81.                     switch(tt)
  82.                     {
  83.                         case -1:
  84.                         {
  85.                             g.setColor(new Color(255, 100, 100));
  86.                             break;
  87.                         }
  88.                         case 0:
  89.                         {
  90.                             g.setColor(Color.black);
  91.                             break;
  92.                         }
  93.                         case 1:
  94.                         {
  95.                             g.setColor(Color.white);
  96.                             break;
  97.                         }
  98.                         case 2:
  99.                         {
  100.                             g.setColor(Color.orange);
  101.                             break;
  102.                         }
  103.                         case 3:
  104.                         {
  105.                             g.setColor(Color.blue);
  106.                             break;
  107.                         }
  108.                         case 4:
  109.                         {
  110.                             g.setColor(Color.red);
  111.                             break;
  112.                         }
  113.                     }
  114.                     g.fillRect(x * 2, y * 2, 2, 2);
  115.                     update[x][y] = false;
  116.                 }
  117.             }
  118.         }
  119.     }
  120.  
  121.     private void drawBrush(int brushX, int brushY)
  122.     {
  123.         if (brushX >= 1 && brushX < width - 1 && brushY >= 1 && brushY < height - 1)
  124.         {
  125.             //Scripted brush
  126.             terrain[brushX][brushY] = brushType;
  127.             terrain[brushX + 1][brushY] = brushType;
  128.             terrain[brushX + 1][brushY + 1] = brushType;
  129.             terrain[brushX][brushY + 1] = brushType;
  130.             terrain[brushX - 1][brushY] = brushType;
  131.             terrain[brushX - 1][brushY - 1] = brushType;
  132.             terrain[brushX][brushY - 1] = brushType;
  133.             terrain[brushX + 1][brushY - 1] = brushType;
  134.             terrain[brushX - 1][brushY + 1] = brushType;
  135.             update[brushX][brushY] = true;
  136.             update[brushX + 1][brushY] = true;
  137.             update[brushX + 1][brushY + 1] = true;
  138.             update[brushX][brushY + 1] = true;
  139.             update[brushX - 1][brushY] = true;
  140.             update[brushX - 1][brushY - 1] = true;
  141.             update[brushX + 1][brushY - 1] = true;
  142.             update[brushX - 1][brushY + 1] = true;
  143.         }
  144.     }
  145.  
  146.     private void start()
  147.     {
  148.         terrain = new byte[width][height];
  149.         update = new boolean[width][height];
  150.     }
  151.  
  152.     private void updateInput(GameContainer container)
  153.     {
  154.         mouseButton = 0;
  155.         short mX = (short) (input.getMouseX() / 2);
  156.         short mY = (short) (input.getMouseY() / 2);
  157.         if (input.isKeyPressed(Input.KEY_ESCAPE)) container.exit();
  158.         if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
  159.         {
  160.             if (resetTime)
  161.             {
  162.                 brushX = mX;
  163.                 brushY = mY;
  164.                 brushOn = true;
  165.                 brushType = leftBrushType;
  166.                 brushOn = true;
  167.                 resetTime = false;
  168.                 drawBrush(brushX, brushY);
  169.             }
  170.         }
  171.         else
  172.         {
  173.             mouseButton ++;
  174.         }
  175.         if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
  176.         {
  177.             if (resetTime)
  178.             {
  179.                 brushX = mX;
  180.                 brushY = mY;
  181.                 brushOn = true;
  182.                 brushType = 0;
  183.                 brushOn = true;
  184.                 resetTime = false;
  185.                 drawBrush(brushX, brushY);
  186.             }
  187.         }
  188.         else
  189.         {
  190.             mouseButton ++;
  191.         }
  192.         if (mouseButton == 2)
  193.         {
  194.             resetTime = true;
  195.             brushOn = false;
  196.         }
  197.         if (brushOn)
  198.         {
  199.             while(brushX != mX && brushY != mY)
  200.             {
  201.                 drawBrush(brushX, brushY);
  202.                 if (Math.abs(brushX - mX) >= (Math.abs(brushY - mY)))
  203.                 {
  204.                     if (brushX < mX)
  205.                     {
  206.                         brushX ++;
  207.                     }
  208.                     if (brushX > mX)
  209.                     {
  210.                         brushX --;
  211.                     }
  212.                 }
  213.                 else
  214.                 {
  215.                     if (brushY < mY)
  216.                     {
  217.                         brushY ++;
  218.                     }
  219.                     if (brushY > mY)
  220.                     {
  221.                         brushY --;
  222.                     }
  223.                 }
  224.             }
  225.         }
  226.         //Control the program
  227.         if (input.isKeyPressed(Input.KEY_F1))
  228.         {
  229.             normalGravity = !normalGravity;
  230.         }
  231.         if (input.isKeyPressed(Input.KEY_F2))
  232.         {
  233.             deleteOnBottom = !deleteOnBottom;
  234.         }
  235.         if (input.isKeyPressed(Input.KEY_F3))
  236.         {
  237.             faucetsOn = !faucetsOn;
  238.         }
  239.         if (input.isKeyPressed(Input.KEY_1))
  240.         {
  241.             leftBrushType = 1;
  242.         }
  243.         if (input.isKeyPressed(Input.KEY_2))
  244.         {
  245.             leftBrushType = 2;
  246.         }
  247.         if (input.isKeyPressed(Input.KEY_3))
  248.         {
  249.             leftBrushType = 3;
  250.         }
  251.         if (input.isKeyPressed(Input.KEY_4))
  252.         {
  253.             leftBrushType = 4;
  254.         }
  255.         if (input.isKeyPressed(Input.KEY_0))
  256.         {
  257.             leftBrushType = -1;
  258.         }
  259.     }
  260.  
  261.     private void updateTerrain()
  262.     {
  263.         if (normalGravity)
  264.         {
  265.             for (int i = 0; i < width / 8; i++)
  266.             {
  267.                 for (int x = columnToUpdate; x < columnToUpdate + 16; x++)
  268.                 {
  269.                     for (int y = height - 2; y >= 0; y--)
  270.                     {
  271.                         if (terrain[x][y] > 0)
  272.                         {
  273.                             if (terrain[x][y+1] == 0 && terrain[x][y] != 0)
  274.                             {
  275.                                 terrain[x][y+1] = terrain[x][y];
  276.                                 terrain[x][y] = 0;
  277.                                 update[x][y] = true;
  278.                                 update[x][y+1] = true;
  279.                             }
  280.                             if (deleteOnBottom)
  281.                             {
  282.                                 if (y > height - 3)
  283.                                 {
  284.                                     terrain[x][y] = 0;
  285.                                     terrain[x][y+1] = 0;
  286.                                     update[x][y] = true;
  287.                                     update[x][y+1] = true;
  288.                                 }
  289.                             }
  290.                             if (y + 1 < height)
  291.                             {
  292.                                 if (changer)
  293.                                 {
  294.                                     if (x + 1 < width)
  295.                                     {
  296.                                         if (y + 1 < height - 1)
  297.                                         {
  298.                                             if (terrain[x+1][y+1] == 0 || terrain[x][y] > 2)
  299.                                             {
  300.                                                 if (terrain[x+1][y] == 0)
  301.                                                 {
  302.                                                     terrain[x+1][y] = terrain[x][y];
  303.                                                     terrain[x][y] = 0;
  304.                                                     update[x][y] = true;
  305.                                                     update[x+1][y] = true;
  306.                                                 }
  307.                                             }
  308.                                         }
  309.                                     }
  310.                                 }
  311.                                 else
  312.                                 {
  313.                                     if (x - 1 >= 0)
  314.                                     {
  315.                                         if (y + 1 < height - 1)
  316.                                         {
  317.                                             if (terrain[x-1][y+1] == 0 || terrain[x][y] > 2)
  318.                                             {
  319.                                                 if (terrain[x-1][y] == 0)
  320.                                                 {
  321.                                                     terrain[x-1][y] = terrain[x][y];
  322.                                                     terrain[x][y] = 0;
  323.                                                     update[x][y] = true;
  324.                                                     update[x-1][y] = true;
  325.                                                 }
  326.                                             }
  327.                                         }
  328.                                     }
  329.                                 }
  330.                                 changer = !changer;
  331.                             }
  332.                         }
  333.                     }
  334.                 }
  335.                 columnToUpdate += 16;
  336.                 if (columnToUpdate == width)
  337.                 {
  338.                     columnToUpdate = 0;
  339.                     if (faucetsOn)
  340.                     {
  341.                         terrain[75 + (int)(Math.random() * 32)-16][0] = 1;
  342.                         terrain[150 + (int)(Math.random() * 32)-16][0] = 2;
  343.                         terrain[225 + (int)(Math.random() * 32)-16][0] = 3;
  344.                         terrain[300 + (int)(Math.random() * 32)-16][0] = 4;
  345.                     }
  346.                 }
  347.             }
  348.         }
  349.         else
  350.         {
  351.             for (int i = 0; i < width / 16; i++)
  352.             {
  353.                 for (int x = columnToUpdate; x < columnToUpdate + 16; x++)
  354.                 {
  355.                     for (int y = 1; y < height - 2; y++)
  356.                     {
  357.                         if (terrain[x][y] > 0)
  358.                         {
  359.                             if (terrain[x][y-1] == 0 && terrain[x][y] != 0)
  360.                             {
  361.                                 terrain[x][y-1] = terrain[x][y];
  362.                                 terrain[x][y] = 0;
  363.                                 update[x][y] = true;
  364.                                 update[x][y-1] = true;
  365.                             }
  366.                             if (deleteOnBottom)
  367.                             {
  368.                                 if (y < 2)
  369.                                 {
  370.                                     terrain[x][y] = 0;
  371.                                     terrain[x][y-1] = 0;
  372.                                     update[x][y] = true;
  373.                                     update[x][y-1] = true;
  374.                                 }
  375.                             }
  376.                             if (y + 1 < height - 1)
  377.                             {
  378.                                 if (changer)
  379.                                 {
  380.                                     if (x + 1 < width)
  381.                                     {
  382.                                         if (y - 1 >= 0)
  383.                                         {
  384.                                             if (terrain[x+1][y-1] == 0 || terrain[x][y] > 2)
  385.                                             {
  386.                                                 if (terrain[x+1][y] == 0)
  387.                                                 {
  388.                                                     terrain[x+1][y] = terrain[x][y];
  389.                                                     terrain[x][y] = 0;
  390.                                                     update[x][y] = true;
  391.                                                     update[x+1][y] = true;
  392.                                                 }
  393.                                             }
  394.                                         }
  395.                                     }
  396.                                 }
  397.                                 else
  398.                                 {
  399.                                     if (x - 1 >= 0)
  400.                                     {
  401.                                         if (y - 1 >= 0)
  402.                                         {
  403.                                             if (terrain[x-1][y-1] == 0 || terrain[x][y] > 2)
  404.                                             {
  405.                                                 if (terrain[x-1][y] == 0)
  406.                                                 {
  407.                                                     terrain[x-1][y] = terrain[x][y];
  408.                                                     terrain[x][y] = 0;
  409.                                                     update[x][y] = true;
  410.                                                     update[x-1][y] = true;
  411.                                                 }
  412.                                             }
  413.                                         }
  414.                                     }
  415.                                 }
  416.                                 changer = !changer;
  417.                             }
  418.                         }
  419.                     }
  420.                 }
  421.                 columnToUpdate += 16;
  422.                 if (columnToUpdate == width)
  423.                 {
  424.                     columnToUpdate = 0;
  425.                     if (faucetsOn)
  426.                     {
  427.                         terrain[75 + (int)(Math.random() * 32)-16][296] = 1;
  428.                         terrain[150 + (int)(Math.random() * 32)-16][296] = 2;
  429.                         terrain[225 + (int)(Math.random() * 32)-16][296] = 3;
  430.                         terrain[300 + (int)(Math.random() * 32)-16][296] = 4;
  431.                     }
  432.                 }
  433.             }
  434.         }
  435.     }
  436. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post