RandomGuy32

PlaceholderName.java

Sep 3rd, 2015
170
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.imageio.ImageIO;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.io.IOException;
  7. import java.lang.InterruptedException;
  8. import java.util.List;
  9. import java.util.ArrayList;
  10. import java.awt.Dimension;
  11. import java.awt.Point;
  12. import java.io.DataOutputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.FileInputStream;
  15. import java.io.File;
  16.  
  17. public class PlaceholderName {
  18.     public static final String version = "0.0.4.1";
  19.    
  20.     public static void main(String[] args) throws IOException, InterruptedException {
  21.         // I hate all of this
  22.         JFrame frame = new JFrame("PlaceholderName v" + version);
  23.         Canvas canvas = new Canvas();
  24.        
  25.         // canvas.setPreferredSize(new Dimension(Game.outerWidth, Game.outerHeight));
  26.        
  27.         frame.getContentPane().add(canvas);
  28.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         // frame.setSize(Game.outerWidth + 30, Game.outerHeight + 30);
  30.        
  31.         frame.setResizable(true);
  32.         canvas.setPreferredSize(new Dimension(Game.outerWidth, Game.outerHeight));
  33.         frame.pack();
  34.         frame.setResizable(false);
  35.         frame.setLocation(10, 10);
  36.        
  37.         frame.setVisible(true);
  38.        
  39.         Level.loadTileset();
  40.        
  41.         Level.addChunk(0, 0).addTile(0, 0, Level.tileset.get(1));
  42.         Level.addChunk(1, 0).addTile(0, 0, Level.tileset.get(2));
  43.         Level.addChunk(2, 0).addTile(0, 0, Level.tileset.get(3));
  44.         Level.addChunk(3, 0).addTile(0, 0, Level.tileset.get(4));
  45.         Level.addChunk(4, 0).addTile(0, 0, Level.tileset.get(5));
  46.         Level.addChunk(5, 0).addTile(0, 0, Level.tileset.get(6));
  47.         Level.addChunk(6, 0).addTile(0, 0, Level.tileset.get(7));
  48.         Level.addChunk(7, 0).addTile(0, 0, Level.tileset.get(8));
  49.        
  50.         // System.out.println(FileManager.ROOT);
  51.        
  52.         FileManager.save("__test3");
  53.        
  54.         Game.tick(canvas);
  55.     }
  56. }
  57. class Game {
  58.     public static final int tps = 20;
  59.     public static boolean running = true;
  60.     public static boolean paused = false;   // yeah, as if
  61.    
  62.     public static final int outerWidth = 896;
  63.     public static final int outerHeight = 512;
  64.    
  65.     public static void tick(Canvas canvas) throws InterruptedException {
  66.         while (Game.running) {
  67.             canvas.repaint();
  68.             Thread.sleep(1000 / Game.tps);
  69.         }
  70.     }
  71. }
  72. class FileManager {
  73.     public static final String ROOT = System.getProperty("user.dir") + "\\saves\\";
  74.     public static final byte FORMATVER = 2;
  75.    
  76.     public static void save(String fileName) throws IOException {
  77.         File file = new File(ROOT + fileName + ".lvl");
  78.         if (!file.exists()) {
  79.             file.mkdirs();
  80.             file.delete();
  81.             file.createNewFile();
  82.         }
  83.        
  84.         // exactly
  85.         DataOutputStream dataStream = new DataOutputStream(new FileOutputStream(file, false));
  86.        
  87.         // first byte of data is save file format version
  88.         // may be useful in the future
  89.         dataStream.writeByte(FORMATVER);
  90.        
  91.         /* == TILE DATA == */
  92.         // chunks are written one by one into file
  93.         for (int i = 0; i < Level.chunks.length; i++) {
  94.             if (Level.chunks[i] == null || Level.chunks[i].tilesN == 0) {
  95.                 // Unicode non-character 0xfdd0 is used as token for empty chunks
  96.                 dataStream.writeShort(0xfdd0);
  97.             }
  98.             else {
  99.                 // filled chunks are checked tile by tile, which are added to the file
  100.                 for (int j = 0; j < 64; j++) {
  101.                     if (j <= 56 && Level.chunks[i].tiles[j] == null && Level.chunks[i].tiles[j + 1] == null && Level.chunks[i].tiles[j + 2] == null && Level.chunks[i].tiles[j + 3] == null && Level.chunks[i].tiles[j + 4] == null && Level.chunks[i].tiles[j + 5] == null && Level.chunks[i].tiles[j + 6] == null && Level.chunks[i].tiles[j + 7] == null) {
  102.                         // a row of 8 empty tiles is represented by Unicode non-character 0xfdd1
  103.                         dataStream.writeShort(0xfdd1);
  104.                         // parser skips ahead 7 tiles
  105.                         j += 7;
  106.                     }
  107.                     // empty tiles are equal to null
  108.                     else if (Level.chunks[i].tiles[j] == null) {
  109.                         dataStream.writeShort(0);
  110.                     }
  111.                     else {
  112.                         dataStream.writeShort(Level.chunks[i].tiles[j].id);
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.        
  118.        
  119.        
  120.         dataStream.flush(); // Java is just one giant joke
  121.         dataStream.close();
  122.     }
  123.     public static void load(String fileName) throws IOException {
  124.        
  125.     }
  126. }
  127. class Level {
  128.     // Java taught me that your program becomes better when you have as many unnecessary classes as possible
  129.     public static List<Entity> entities = new ArrayList<Entity>();
  130.     public static List<Tile> tileset = new ArrayList<Tile>();
  131.    
  132.     public static Chunk getChunk(int x, int y) {
  133.         return chunks[y * (Game.outerHeight / 128) + x];
  134.     }
  135.     public static Chunk addChunk(int x, int y) {
  136.         if (Level.chunks[y * (Game.outerHeight / 128) + x] == null) {
  137.             Level.chunks[y * (Game.outerHeight / 128) + x] = new Chunk();
  138.         }
  139.         else {
  140.             System.out.println("NOTICE: Chunk at (" + x + "," + y + ") already exists.");
  141.         }
  142.         return Level.chunks[y * (Game.outerHeight / 128) + x];
  143.     }
  144.     public static void loadTileset() {
  145.         Level.tileset.add(null);
  146.         Level.tileset.add(new BasicFloortile());
  147.         Level.tileset.add(new BasicCeilingtile());
  148.         Level.tileset.add(new BasicWalltileLeft());
  149.         Level.tileset.add(new BasicWalltileRight());
  150.         Level.tileset.add(new BasicCornertileTopLeft());
  151.         Level.tileset.add(new BasicCornertileTopRight());
  152.         Level.tileset.add(new BasicCornertileBottomRight());
  153.         Level.tileset.add(new BasicCornertileBottomLeft());
  154.     }
  155.     public static Chunk[] chunks = new Chunk[(Game.outerWidth / 128) * (Game.outerHeight / 128)];
  156. }
  157. class Chunk {
  158.     public Chunk() {};
  159.     public Chunk(Tile[] tiles_) {
  160.         tiles = tiles_;
  161.     }
  162.    
  163.     public Tile[] tiles = new Tile[64];
  164.     public int tilesN = 0;
  165.    
  166.     public Tile getTile(int x, int y) {
  167.         return this.tiles[y * 8 + x];
  168.     }
  169.     public void addTile(int x, int y, Tile tile) {
  170.         if (this.tiles[y * 8 + x] == null) {
  171.             this.tiles[y * 8 + x] = tile;
  172.             this.tilesN++;
  173.         }
  174.         else {
  175.             System.out.println("NOTICE: Tile at (" + x + "," + y + ") already exists.");
  176.         }
  177.     }
  178. }
  179. class Canvas extends JPanel {
  180.     public Canvas() throws IOException {
  181.         setDoubleBuffered(true);
  182.         cursor = this.getMousePosition();
  183.     }
  184.    
  185.     public Point cursor;   
  186.     public BufferedImage loadImage(String name) {
  187.         // ????
  188.         try {
  189.             return ImageIO.read(getClass().getResource("/assets/image/" + name));
  190.         }
  191.         catch (Throwable t) {
  192.             // fuck everything
  193.             t.printStackTrace();
  194.             return null;
  195.         }
  196.     }
  197.     public void paint(Graphics g) {
  198.         /*
  199.         for (int i = 0; i < Level.tileset.size(); i++) {
  200.             g.drawImage(loadImage(Level.tileset.get(i).sprite), Level.tileset.get(i).x, Level.tileset.get(i).y, null);
  201.         }
  202.         */
  203.         for (int i = 0; i < Level.chunks.length; i++) {
  204.             if (Level.chunks[i] != null && Level.chunks[i].tilesN != 0) {
  205.                 for (int j = 0; j < Level.chunks[i].tiles.length; j++) {
  206.                     if (Level.chunks[i].tiles[j] != null) {
  207.                         g.drawImage(loadImage(Level.chunks[i].tiles[j].sprite.src), ((i % (Game.outerWidth / 128)) * 128) + ((j % 8) * 16), ((i / (Game.outerWidth / 128)) * 128) + ((j / 8) * 16), null);
  208.                     }
  209.                 }
  210.             }
  211.         }
  212.         for (int i = 0; i < Level.entities.size(); i++) {
  213.             // fghjkl
  214.             g.drawImage(loadImage(Level.entities.get(i).sprite.src), Level.entities.get(i).x, Level.entities.get(i).y, null);
  215.         }
  216.     }
  217. }
  218. class Sprite {
  219.     public Sprite(String src_) {
  220.         x = 0;
  221.         y = 0;
  222.         offsetX = 0;
  223.         offsetY = 0;
  224.         src = src_;
  225.     }
  226.     public Sprite(int x_, int y_, String src_) {
  227.         x = x_;
  228.         y = y_;
  229.         offsetX = 0;
  230.         offsetY = 0;
  231.         src = src_;
  232.     }
  233.     public Sprite(int x_, int y_, int offsetX_, int offsetY_, String src_) {
  234.         x = x_;
  235.         y = y_;
  236.         offsetX = offsetX_;
  237.         offsetY = offsetY_;
  238.         src = src_;
  239.     }
  240.    
  241.     public int x;
  242.     public int y;
  243.     public int offsetX;
  244.     public int offsetY;
  245.     public String src;
  246. }
  247. abstract class DynamicObject {
  248.     public DynamicObject(boolean[] solid_, String sprite_) {
  249.         sprite = new Sprite(sprite_);
  250.         solid = solid_;
  251.     }
  252.    
  253.     public final Sprite sprite;
  254.     public boolean[] solid;
  255.    
  256.     public boolean isSolid(String dir) {
  257.         switch (dir) {
  258.             case "top":
  259.                 return this.solid[0];
  260.             case "right":
  261.                 return this.solid[1];
  262.             case "bottom":
  263.                 return this.solid[2];
  264.             case "left":
  265.                 return this.solid[3];
  266.             default:
  267.                 System.err.println("ERROR: Invalid argument in DynamicObject.isSolid(String).");
  268.                 return false;
  269.         }
  270.     }
  271. }
  272. abstract class AbstractEntity extends DynamicObject {
  273.     public AbstractEntity(int x_, int y_, boolean[] solid_, String sprite_) {
  274.         super(solid_, sprite_);
  275.         sprite.x = x_;
  276.         sprite.y = y_;
  277.         x = x_;
  278.         y = y_;
  279.     }
  280.     public int x;
  281.     public int y;
  282. }
  283. class Entity extends AbstractEntity {
  284.     public Entity(int x_, int y_, boolean[] solid_, String sprite_, byte dir_) {
  285.         super(x_, y_, solid_, sprite_);
  286.         dir = dir_;
  287.         velX = 0f;
  288.         velY = 0f;
  289.     }
  290.    
  291.     public byte dir;
  292.     public float velX;
  293.     public float velY;
  294. }
  295. class Tile extends DynamicObject {
  296.     public Tile(boolean[] solid_, String sprite_, String name_) {
  297.         super(solid_, sprite_);
  298.         name = name_;
  299.         id = Tile.idIndex;
  300.         Tile.idIndex++;
  301.     }
  302.    
  303.     public String name;
  304.     public short id;
  305.    
  306.     public static short idIndex = 1;
  307. }
  308. class TileEntity extends AbstractEntity {
  309.     public TileEntity (int x_, int y_, short tileID) {
  310.         super(x_, y_, new boolean[] {false, false, false, false}, Level.tileset.get(tileID).sprite.src);
  311.     }
  312. }
  313.  
  314. /* ============================================================= */
  315. /* ========================= TILES ============================= */
  316. /* ============================================================= */
  317.  
  318. class BasicFloortile extends Tile {
  319.     public BasicFloortile() {
  320.         super(new boolean[] {true, true, true, true}, "tile/basic-floortile.png", "basic_floortile");
  321.     }
  322. }
  323. class BasicCeilingtile extends Tile {
  324.     public BasicCeilingtile() {
  325.         super(new boolean[] {true, true, true, true}, "tile/basic-ceilingtile.png", "basic_ceilingtile");
  326.     }
  327. }
  328. class BasicWalltileLeft extends Tile {
  329.     public BasicWalltileLeft() {
  330.         super(new boolean[] {true, true, true, true}, "tile/basic-walltile-left.png", "basic_walltile_left");
  331.     }
  332. }
  333. class BasicWalltileRight extends Tile {
  334.     public BasicWalltileRight() {
  335.         super(new boolean[] {true, true, true, true}, "tile/basic-walltile-right.png", "basic_walltile_right");
  336.     }
  337. }
  338. class BasicCornertileTopLeft extends Tile {
  339.     public BasicCornertileTopLeft() {
  340.         super(new boolean[] {true, true, true, true}, "tile/basic-cornertile-top-left.png", "basic_cornertile_top_left");
  341.     }
  342. }
  343. class BasicCornertileTopRight extends Tile {
  344.     public BasicCornertileTopRight() {
  345.         super(new boolean[] {true, true, true, true}, "tile/basic-cornertile-top-right.png", "basic_cornertile_top_right");
  346.     }
  347. }
  348. class BasicCornertileBottomRight extends Tile {
  349.     public BasicCornertileBottomRight() {
  350.         super(new boolean[] {true, true, true, true}, "tile/basic-cornertile-bottom-right.png", "basic_cornertile_bottom_right");
  351.     }
  352. }
  353. class BasicCornertileBottomLeft extends Tile {
  354.     public BasicCornertileBottomLeft() {
  355.         super(new boolean[] {true, true, true, true}, "tile/basic-cornertile-bottom-left.png", "basic_cornertile_bottom_left");
  356.     }
  357. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×