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 Fri 7 Aug 18:53
report abuse | download | new post

  1. package junkengine;
  2.  
  3. import java.util.ArrayList;
  4. import org.newdawn.slick.AngelCodeFont;
  5. import org.newdawn.slick.AppGameContainer;
  6. import org.newdawn.slick.BasicGame;
  7. import org.newdawn.slick.Color;
  8. import org.newdawn.slick.GameContainer;
  9. import org.newdawn.slick.Graphics;
  10. import org.newdawn.slick.Input;
  11. import org.newdawn.slick.SlickException;
  12. import org.newdawn.slick.SpriteSheet;
  13.  
  14. public class Main extends BasicGame {
  15.  
  16.     private int[] mapSize = {100, 100};
  17.  
  18.     private int screenX = 0;
  19.     private int screenY = 0;
  20.  
  21.     private int renderX1, renderX2, renderY1, renderY2;
  22.  
  23.     private MersenneTwisterFast mt;
  24.  
  25.     private SpriteSheet tileset;
  26.     private Input input;
  27.  
  28.     private int[][] redMask = new int[mapSize[0]][mapSize[1]];
  29.     private int[][] greenMask = new int[mapSize[0]][mapSize[1]];
  30.     private int[][] blueMask = new int[mapSize[0]][mapSize[1]];
  31.  
  32.     private mapTiles[][] map = new mapTiles[mapSize[0]][mapSize[1]];
  33.    
  34.     private AngelCodeFont font;
  35.  
  36.     private enum mapTiles {Ground1, Ground2, Ground3, Box};
  37.  
  38.     private ArrayList<Light> lights = new ArrayList<Light>();
  39.  
  40.     private Light playerLight;
  41.    
  42.     Main()
  43.     {
  44.         super("Junk Engine");
  45.     }
  46.  
  47.     public static void main(String[] args)
  48.     {
  49.         try {
  50.             AppGameContainer app = new AppGameContainer(new Main());
  51.             app.setDisplayMode(1024, 768, false);
  52.             app.setFullscreen(false);
  53.             app.setShowFPS(false);
  54.             app.setTargetFrameRate(75);
  55.             app.start();
  56.         }
  57.         catch (SlickException e) {
  58.             e.printStackTrace();
  59.         }
  60.     }
  61.  
  62.     @Override
  63.     public void init(GameContainer container) throws SlickException
  64.     {
  65.         lights = new ArrayList();
  66.         input = container.getInput();
  67.         tileset = new SpriteSheet("data/tileset.png", 16, 16);
  68.         font = new AngelCodeFont("/data/default.fnt", "data/default_00.tga");
  69.         mt = new MersenneTwisterFast();
  70.         createMap();
  71.         playerLight = new Light(0, 0, 50, new Color(255, 255, 255));
  72.         lights.add(playerLight);
  73.         lights.add(new Light(12, 12, 100, new Color(255, 127, 50)));
  74.         lights.add(new Light(20, 15, 200, new Color(25, 100, 255)));
  75.         lights.add(new Light(5, 5, 100, new Color(100, 255, 60)));
  76.     }
  77.  
  78.     @Override
  79.     public void update(GameContainer container, int delta) throws SlickException
  80.     {
  81.         if (input.isKeyPressed(Input.KEY_ESCAPE))
  82.         {
  83.             container.exit();
  84.         }
  85.         if (input.isKeyPressed(Input.KEY_F2))
  86.         {
  87.             createMap();
  88.         }
  89.         if (input.isKeyDown(Input.KEY_LEFT))
  90.         {
  91.             screenX -= 4;
  92.         }
  93.         if (input.isKeyDown(Input.KEY_RIGHT))
  94.         {
  95.             screenX += 4;
  96.         }
  97.         if (input.isKeyDown(Input.KEY_UP))
  98.         {
  99.             screenY -= 4;
  100.         }
  101.         if (input.isKeyDown(Input.KEY_DOWN))
  102.         {
  103.             screenY += 4;
  104.         }
  105.         if (screenX < 0)
  106.         {
  107.             screenX = 0;
  108.         }
  109.         if (screenY < 0)
  110.         {
  111.             screenY = 0;
  112.         }
  113.         if (screenX > (mapSize[0] * 16 - 1024))
  114.         {
  115.             screenX = (mapSize[0] * 16 - 1024);
  116.         }
  117.         if (screenY > (mapSize[1] * 16 - 768))
  118.         {
  119.             screenY = (mapSize[1] * 16 - 768);
  120.         }
  121.         resetLights(0);
  122.         playerLight.x = (input.getMouseX() + (screenX * 2)) / 32;
  123.         playerLight.y = (input.getMouseY() + (screenY * 2)) / 32;
  124.         updateLighting();
  125.         renderX1 = screenX / 16;
  126.         renderX2 = (screenX + 1024) / 16;
  127.         renderY1 = screenY / 16;
  128.         renderY2 = (screenY + 768) / 16;
  129.     }
  130.  
  131.     public void render(GameContainer container, Graphics g) throws SlickException
  132.     {
  133.         g.scale(2, 2);
  134.         for (int x = renderX1; x < renderX2; x++)
  135.         {
  136.             for (int y = renderY1; y < renderY2; y++)
  137.             {
  138.                 if (redMask[x][y] > 25 || greenMask[x][y] > 25 || blueMask[x][y] > 25)
  139.                 {
  140.                     switch(map[x][y])
  141.                     {
  142.                         case Ground1:
  143.                         {
  144.                             tileset.getSprite(0, 1).draw(
  145.                                     x * 16 - screenX,
  146.                                     y * 16 - screenY,
  147.                                     new Color(redMask[x][y], greenMask[x][y], blueMask[x][y]));
  148.                             break;
  149.                         }
  150.                         case Ground2:
  151.                         {
  152.                             tileset.getSprite(1, 1).draw(
  153.                                     x * 16 - screenX,
  154.                                     y * 16 - screenY,
  155.                                     new Color(redMask[x][y], greenMask[x][y], blueMask[x][y]));
  156.                             break;
  157.                         }
  158.                         case Ground3:
  159.                         {
  160.                             tileset.getSprite(1, 0).draw(
  161.                                     x * 16 - screenX,
  162.                                     y * 16 - screenY,
  163.                                     new Color(redMask[x][y], greenMask[x][y], blueMask[x][y]));
  164.                             break;
  165.                         }
  166.                         case Box:
  167.                         {
  168.                             tileset.getSprite(0, 0).draw(
  169.                                     x * 16 - screenX,
  170.                                     y * 16 - screenY,
  171.                                     new Color(redMask[x][y], greenMask[x][y], blueMask[x][y]));
  172.                             break;
  173.                         }
  174.                     }
  175.                 }
  176.             }
  177.         }
  178.     }
  179.  
  180.     public void updateLighting()
  181.     {
  182.         for (Light light: lights)
  183.         {
  184.             for (int x = 0; x < mapSize[0]; x++)
  185.             {
  186.                 for (int y = 0; y < mapSize[1]; y++)
  187.                 {
  188.                     float lightingChange = 0;
  189.                     lightingChange += ((Math.abs(light.x - x)) * (Math.abs(light.x - x)) * (255 / light.brightness));
  190.                     lightingChange += ((Math.abs(light.y - y)) * (Math.abs(light.y - y)) * (255 / light.brightness));
  191.                     if (lightingChange <= 127)
  192.                     {
  193.                         if ((light.hue.getRed() / 2) - (int) lightingChange > 0)
  194.                         {
  195.                             redMask[x][y] += (light.hue.getRed() / 2) - (int) lightingChange;
  196.                         }
  197.                         if ((light.hue.getGreen() / 2) - (int) lightingChange > 0)
  198.                         {
  199.                             greenMask[x][y] += (light.hue.getGreen() / 2) - (int) lightingChange;
  200.                         }
  201.                         if ((light.hue.getBlue() / 2) - (int) lightingChange > 0)
  202.                         {
  203.                             blueMask[x][y] += (light.hue.getBlue() / 2) -  (int) lightingChange;
  204.                         }
  205.                         redMask[x][y] += (128 - lightingChange);
  206.                         greenMask[x][y] += (128 - lightingChange);
  207.                         blueMask[x][y] += (128 - lightingChange);
  208.                     }
  209.                 }
  210.             }
  211.         }
  212.     }
  213.  
  214.     public void resetLights(int ambience)
  215.     {
  216.         for (int x = 0; x < mapSize[0]; x++)
  217.         {
  218.             for (int y = 0; y < mapSize[1]; y++)
  219.             {
  220.                 redMask[x][y] = ambience;
  221.                 greenMask[x][y] = ambience;
  222.                 blueMask[x][y] = ambience;
  223.             }
  224.         }
  225.     }
  226.  
  227.     public void createMap()
  228.     {
  229.         int s1 = mt.nextInt(3)+1;
  230.         int s2 = mt.nextInt(3)+1;
  231.         int s3 = mt.nextInt(4)+1;
  232.         int s4 = mt.nextInt(5)+1;
  233.         int s5 = mt.nextInt(7)+1;
  234.         map = new mapTiles[mapSize[0]][mapSize[1]];
  235.         for (int x = 0; x < mapSize[0]; x++)
  236.         {
  237.             for (int y = 0; y < mapSize[1]; y++)
  238.             {
  239.                 if (x * y % s1 == 0 ^ x * y % s2 == 0)
  240.                 {
  241.                     if (x * y % s3 == 0 ^ x * y % s5 == 0)
  242.                     {
  243.                         map[x][y] = mapTiles.Box;
  244.                     }
  245.                     else
  246.                     {
  247.                         map[x][y] = mapTiles.Ground3;
  248.                     }
  249.                 }
  250.                 else
  251.                 {
  252.                     if (x * y % s2 == 2 & x * y % s4 == 0)
  253.                     {
  254.                         map[x][y] = mapTiles.Ground2;
  255.                     }
  256.                     else
  257.                     {
  258.                         map[x][y] = mapTiles.Ground1;
  259.                     }
  260.                 }
  261.             }
  262.         }
  263.     }
  264. }
  265.  
  266. package junkengine;
  267.  
  268. import org.newdawn.slick.Color;
  269.  
  270. public class Light {
  271.  
  272.     int x;
  273.     int y;
  274.     int brightness;
  275.     Color hue;
  276.  
  277.     Light(int x, int y, int brightness, Color hue)
  278.     {
  279.         this.x = x;
  280.         this.y = y;
  281.         this.brightness = brightness;
  282.         this.hue = hue;
  283.     }
  284. }

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