Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.numpad.demo;
- import de.numpad.framework.Frapp;
- import de.numpad.framework.gui.Display;
- import de.numpad.framework.gui.Fonts;
- import de.numpad.framework.gui.elements.Button;
- public class LevelNoStates extends Frapp {
- private static final long serialVersionUID = 10L;
- public Level level; // Very important: Instance of the abstract class
- // Since TestLevel1 and TestLevel2 both extend Level, they are in _general_ the same.
- public void start() {
- Display.size(20 * (Level.TILESIZE+1), 10 * (Level.TILESIZE +1) + 100);
- level = new TestLevel1(); // Instantiate level as TestLevel1
- GUI.add(new Button(10, Display.height - 90, 210, 60, "Change World", 2) {
- public void onCreate() {
- this.font = Fonts.thin_dark;
- }
- public void onClick(int button) {
- if (level.id == 1) { // If the current level is 1
- level = new TestLevel2(); // change it to 2
- } else if (level.id == 2) { // If the current level is 2
- level = new TestLevel1(); // change it to 1
- }
- }
- });
- }
- public void render() {
- Display.background(51);
- Display.smooth();
- level.display(); // render the level, both levels could be rendered totally different, if you want to do so.
- Display.GUI();
- }
- public static void main(String[] args) {
- Frapp.build(new LevelNoStates(), "Levels");
- }
- }
- abstract class Level { // An abstract class is important, it is the base frame of the level
- public int[][] block; // hold a 2 dimensional array of ints representing blocks
- public int width, height; // how wide and high the level is
- public int id = -1; // the current id of the level, used to differentiate the levels
- public static final int TILESIZE = 32; // the normal size per tile, change it if you want to ;)
- public static final int BLOCK_AIR = 0, BLOCK_SOLID = 1; // the block types
- public Level(int width, int height) { // a basic constructor
- this.width = width;
- this.height = height;
- block = new int[width][height]; // initialize the array
- }
- public void display() {
- for (int x = 0; x < width; x++) { // for every row...
- for (int y = 0; y < height; y++) { // ...check each part in the column...
- Display.color(45 + 200 * block[x][y]); // ...and set the color depending on the tile id.
- Display.fill();
- Display.rect(x * (TILESIZE +1), y * (TILESIZE +1), TILESIZE, TILESIZE); // draw a rect for each block.
- }
- }
- }
- }
- class TestLevel1 extends Level { // important! the Level needs to extend the base frame, or "use its form"
- public TestLevel1() {
- super(20, 10); // call the constructor -> set width and height and initialize the blocks
- this.id = 1; // id for each level: TestLevel1 has the id 1, and I think you can guess about TestLevel2. Right, 2!
- for (int x = 0; x < width; x++) {
- block[x][4] = BLOCK_SOLID; // Set every block in the 4th row solid.
- }
- }
- }
- class TestLevel2 extends Level { // extend again, same form, you know
- public TestLevel2() {
- super(20, 10); // constructor again
- this.id = 2; // id = 2 to differentiate them
- for (int x = 0; x < width; x++) {
- block[x][1] = BLOCK_SOLID; // every block in the first(actually second, because we start counting at 0) row shall be solid!
- }
- for (int y = 0; y < height; y++) {
- block[1][y] = BLOCK_SOLID; // every block in the first...
- block[5][y] = BLOCK_SOLID; // and fifth column shall be solid.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement