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 Mon 8 Jun 01:05
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.    
  21.     private int[][] terrain;
  22.  
  23.     private int columnToUpdate = 0;
  24.  
  25.     private int brushX, brushY;
  26.     private boolean brushOn;
  27.     private int brushType = -1;
  28.     private boolean resetTime = true;
  29.     private int mouseButton = 0;
  30.  
  31.     private int width = 400;
  32.     private int height = 300;
  33.  
  34.     public static void main(String[] args) {
  35.         try
  36.         {
  37.             AppGameContainer app = new AppGameContainer(new Main());
  38.             app.setDisplayMode(800, 600, false);
  39.             app.setFullscreen(false);
  40.             app.setShowFPS(false);
  41.             app.setClearEachFrame(false);
  42.             app.setTargetFrameRate(60);
  43.             app.start();
  44.         }
  45.         catch (SlickException e)
  46.         {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.  
  51.     @Override
  52.     public void init(GameContainer container) throws SlickException
  53.     {
  54.         input = container.getInput();
  55.         Renderer.setRenderer(Renderer.VERTEX_ARRAY_RENDERER);
  56.         start();
  57.     }
  58.  
  59.     @Override
  60.     public void update(GameContainer container, int delta) throws SlickException
  61.     {
  62.         mouseButton = 0;
  63.         int mX = (int) Math.floor(input.getMouseX() / 2);
  64.         int mY = (int) Math.floor(input.getMouseY() / 2);
  65.         if (input.isKeyPressed(Input.KEY_ESCAPE)) container.exit();
  66.         if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
  67.         {
  68.             if (resetTime)
  69.             {
  70.                 brushX = mX;
  71.                 brushY = mY;
  72.                 brushOn = true;
  73.                 brushType = -1;
  74.                 brushOn = true;
  75.                 resetTime = false;
  76.                 drawBrush(brushX, brushY);
  77.             }
  78.         }
  79.         else
  80.         {
  81.             mouseButton ++;
  82.         }
  83.         if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
  84.         {
  85.             if (resetTime)
  86.             {
  87.                 brushX = mX;
  88.                 brushY = mY;
  89.                 brushOn = true;
  90.                 brushType = 0;
  91.                 brushOn = true;
  92.                 resetTime = false;
  93.                 drawBrush(brushX, brushY);
  94.             }
  95.         }
  96.         else
  97.         {
  98.             mouseButton ++;
  99.         }
  100.         if (mouseButton == 2)
  101.         {
  102.             resetTime = true;
  103.             brushOn = false;
  104.         }
  105.         if (brushOn)
  106.         {
  107.             while(brushX != mX && brushY != mY)
  108.             {
  109.                 drawBrush(brushX, brushY);
  110.                 if (Math.abs(brushX - mX) >= (Math.abs(brushY - mY)))
  111.                 {
  112.                     if (brushX < mX)
  113.                     {
  114.                         brushX ++;
  115.                     }
  116.                     if (brushX > mX)
  117.                     {
  118.                         brushX --;
  119.                     }
  120.                 }
  121.                 else
  122.                 {
  123.                     if (brushY < mY)
  124.                     {
  125.                         brushY ++;
  126.                     }
  127.                     if (brushY > mY)
  128.                     {
  129.                         brushY --;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.         for (int i = 0; i < width / 32; i++)
  135.         {
  136.             for (int x = columnToUpdate; x < columnToUpdate + 16; x++)
  137.             {
  138.                 for (int y = height - 2; y >= 0; y--)
  139.                 {
  140.                     if (terrain[x][y] > 0)
  141.                     {
  142.                         if (terrain[x][y+1] == 0)
  143.                         {
  144.                             terrain[x][y+1] = terrain[x][y];
  145.                             terrain[x][y] = 0;
  146.                         }
  147.                         if (y + 1 < height - 2)
  148.                         {
  149.                             if (Math.random() * 2 > 1)
  150.                             {
  151.                                 if (x + 1 < width)
  152.                                 {
  153.                                     if (terrain[x+1][y] == 0)
  154.                                     {
  155.                                         terrain[x+1][y] = terrain[x][y];
  156.                                         terrain[x][y] = 0;
  157.                                     }
  158.                                 }
  159.                             }
  160.                             else
  161.                             {
  162.                                 if (x - 1 >= 0)
  163.                                 {
  164.                                     if (terrain[x-1][y] == 0)
  165.                                     {
  166.                                         terrain[x-1][y] = terrain[x][y];
  167.                                         terrain[x][y] = 0;
  168.                                     }
  169.                                 }
  170.                             }
  171.                         }
  172.                     }
  173.                 }
  174.             }
  175.             columnToUpdate += 16;
  176.             if (columnToUpdate == width)
  177.             {
  178.                 columnToUpdate = 0;
  179.                 terrain[75 + (int)(Math.random() * 32)-16][0] = 1;
  180.                 terrain[150 + (int)(Math.random() * 32)-16][0] = 2;
  181.                 terrain[225 + (int)(Math.random() * 32)-16][0] = 3;
  182.                 terrain[300 + (int)(Math.random() * 32)-16][0] = 4;
  183.             }
  184.         }
  185.     }
  186.  
  187.     public void render(GameContainer container, Graphics g) throws SlickException
  188.     {
  189.         g.setColor(Color.black);
  190.         g.fillRect(0, 0, 800, 600);
  191.         for (int y = 0; y < height; y++)
  192.         {
  193.             for (int x = 0; x < width; x++)
  194.             {
  195.                 if (terrain[x][y] != 0)
  196.                 {
  197.                     switch(terrain[x][y])
  198.                     {
  199.                         case -1:
  200.                         {
  201.                             g.setColor(new Color(255, 100, 100));
  202.                             break;
  203.                         }
  204.                         case 1:
  205.                         {
  206.                             g.setColor(Color.white);
  207.                             break;
  208.                         }
  209.                         case 2:
  210.                         {
  211.                             g.setColor(Color.orange);
  212.                             break;
  213.                         }
  214.                         case 3:
  215.                         {
  216.                             g.setColor(Color.blue);
  217.                             break;
  218.                         }
  219.                         case 4:
  220.                         {
  221.                             g.setColor(Color.red);
  222.                             break;
  223.                         }
  224.                     }
  225.                     g.fillRect(x * 2, y * 2, 2, 2);
  226.                 }
  227.             }
  228.         }
  229.     }
  230.  
  231.     private void drawBrush(int brushX, int brushY)
  232.     {
  233.         if (brushX >= 1 && brushX < width - 1 && brushY >= 1 && brushY < height - 1)
  234.         {
  235.             //Scripted brush
  236.             terrain[brushX][brushY] = brushType;
  237.             terrain[brushX + 1][brushY] = brushType;
  238.             terrain[brushX + 1][brushY + 1] = brushType;
  239.             terrain[brushX][brushY + 1] = brushType;
  240.             terrain[brushX - 1][brushY] = brushType;
  241.             terrain[brushX - 1][brushY - 1] = brushType;
  242.             terrain[brushX][brushY - 1] = brushType;
  243.             terrain[brushX + 1][brushY - 1] = brushType;
  244.             terrain[brushX - 1][brushY + 1] = brushType;
  245.         }
  246.     }
  247.  
  248.     private void start()
  249.     {
  250.         terrain = new int[width][height];
  251.     }
  252. }

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