Advertisement
Guest User

Rooms

a guest
Oct 26th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. package de.numpad.demo;
  2.  
  3. import de.numpad.framework.Frapp;
  4. import de.numpad.framework.gui.Display;
  5. import de.numpad.framework.gui.Fonts;
  6. import de.numpad.framework.gui.elements.Button;
  7.  
  8. public class LevelNoStates extends Frapp {
  9.     private static final long serialVersionUID = 10L;
  10.    
  11.     public Level level; // Very important: Instance of the abstract class
  12.                         // Since TestLevel1 and TestLevel2 both extend Level, they are in _general_ the same.
  13.    
  14.     public void start() {
  15.         Display.size(20 * (Level.TILESIZE+1), 10 * (Level.TILESIZE +1) + 100);
  16.        
  17.         level = new TestLevel1(); // Instantiate level as TestLevel1
  18.        
  19.         GUI.add(new Button(10, Display.height - 90, 210, 60, "Change World", 2) {
  20.             public void onCreate() {
  21.                 this.font = Fonts.thin_dark;
  22.             }
  23.             public void onClick(int button) {
  24.                 if (level.id == 1) { // If the current level is 1
  25.                     level = new TestLevel2(); // change it to 2
  26.                 } else if (level.id == 2) { // If the current level is 2
  27.                     level = new TestLevel1(); // change it to 1
  28.                 }
  29.             }
  30.         });
  31.     }
  32.    
  33.     public void render() {
  34.         Display.background(51);
  35.         Display.smooth();
  36.        
  37.         level.display(); // render the level, both levels could be rendered totally different, if you want to do so.
  38.        
  39.         Display.GUI();
  40.     }
  41.    
  42.     public static void main(String[] args) {
  43.         Frapp.build(new LevelNoStates(), "Levels");
  44.     }
  45. }
  46.  
  47. abstract class Level { // An abstract class is important, it is the base frame of the level
  48.     public int[][] block; // hold a 2 dimensional array of ints representing blocks
  49.     public int width, height; // how wide and high the level is
  50.    
  51.     public int id = -1; // the current id of the level, used to differentiate the levels
  52.    
  53.     public static final int TILESIZE = 32; // the normal size per tile, change it if you want to ;)
  54.     public static final int BLOCK_AIR = 0, BLOCK_SOLID = 1; // the block types
  55.    
  56.    
  57.     public Level(int width, int height) { // a basic constructor
  58.         this.width = width;
  59.         this.height = height;
  60.        
  61.         block = new int[width][height]; // initialize the array
  62.     }
  63.    
  64.     public void display() {
  65.         for (int x = 0; x < width; x++) { // for every row...
  66.             for (int y = 0; y < height; y++) { // ...check each part in the column...
  67.                 Display.color(45 + 200 * block[x][y]); // ...and set the color depending on the tile id.
  68.                 Display.fill();
  69.                
  70.                 Display.rect(x * (TILESIZE +1), y * (TILESIZE +1), TILESIZE, TILESIZE); // draw a rect for each block.
  71.             }
  72.         }
  73.     }
  74. }
  75. class TestLevel1 extends Level { // important! the Level needs to extend the base frame, or "use its form"
  76.  
  77.     public TestLevel1() {
  78.         super(20, 10); // call the constructor -> set width and height and initialize the blocks
  79.        
  80.         this.id = 1; // id for each level: TestLevel1 has the id 1, and I think you can guess about TestLevel2. Right, 2!
  81.        
  82.         for (int x = 0; x < width; x++) {
  83.             block[x][4] = BLOCK_SOLID; // Set every block in the 4th row solid.
  84.         }
  85.     }
  86. }
  87.  
  88. class TestLevel2 extends Level { // extend again, same form, you know
  89.  
  90.     public TestLevel2() {
  91.         super(20, 10); // constructor again
  92.        
  93.         this.id = 2; // id = 2 to differentiate them
  94.        
  95.         for (int x = 0; x < width; x++) {
  96.             block[x][1] = BLOCK_SOLID; // every block in the first(actually second, because we start counting at 0) row shall be solid!
  97.         }
  98.         for (int y = 0; y < height; y++) {
  99.             block[1][y] = BLOCK_SOLID; // every block in the first...
  100.             block[5][y] = BLOCK_SOLID; // and fifth column shall be solid.
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement