Posted by Epitaph64 on Sun 14 Jun 00:15
report abuse | download | new post
- package bufferedterrain;
- import org.newdawn.slick.opengl.renderer.Renderer;
- import org.newdawn.slick.AppGameContainer;
- import org.newdawn.slick.BasicGame;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Input;
- import org.newdawn.slick.SlickException;
- public class Main extends BasicGame {
- Main()
- {
- super("Terrain Test");
- }
- private Input input;
- private byte[][] terrain;
- private boolean[][] update;
- private boolean changer;
- //Game Options Start
- private boolean deleteOnBottom = false;
- private boolean normalGravity = true;
- private boolean faucetsOn = false;
- //Game Options End
- private short columnToUpdate = 0;
- private short brushX, brushY;
- private boolean brushOn;
- private byte brushType = -1;
- private byte leftBrushType = -1;
- private boolean resetTime = true;
- private byte mouseButton = 0;
- private final static short width = 400;
- private final static short height = 300;
- {
- try
- {
- AppGameContainer app = new AppGameContainer(new Main());
- app.setDisplayMode(800, 600, false);
- app.setFullscreen(false);
- app.setClearEachFrame(false);
- app.setShowFPS(false);
- app.setTargetFrameRate(75);
- app.start();
- }
- catch (SlickException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public void init(GameContainer container) throws SlickException
- {
- input = container.getInput();
- start();
- }
- @Override
- public void update(GameContainer container, int delta) throws SlickException
- {
- updateInput(container);
- updateTerrain();
- }
- {
- byte tt = 0;
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- if (update[x][y])
- {
- tt = terrain[x][y];
- switch(tt)
- {
- case -1:
- {
- break;
- }
- case 0:
- {
- break;
- }
- case 1:
- {
- break;
- }
- case 2:
- {
- break;
- }
- case 3:
- {
- break;
- }
- case 4:
- {
- break;
- }
- }
- g.fillRect(x * 2, y * 2, 2, 2);
- update[x][y] = false;
- }
- }
- }
- }
- private void drawBrush(int brushX, int brushY)
- {
- if (brushX >= 1 && brushX < width - 1 && brushY >= 1 && brushY < height - 1)
- {
- //Scripted brush
- terrain[brushX][brushY] = brushType;
- terrain[brushX + 1][brushY] = brushType;
- terrain[brushX + 1][brushY + 1] = brushType;
- terrain[brushX][brushY + 1] = brushType;
- terrain[brushX - 1][brushY] = brushType;
- terrain[brushX - 1][brushY - 1] = brushType;
- terrain[brushX][brushY - 1] = brushType;
- terrain[brushX + 1][brushY - 1] = brushType;
- terrain[brushX - 1][brushY + 1] = brushType;
- update[brushX][brushY] = true;
- update[brushX + 1][brushY] = true;
- update[brushX + 1][brushY + 1] = true;
- update[brushX][brushY + 1] = true;
- update[brushX - 1][brushY] = true;
- update[brushX - 1][brushY - 1] = true;
- update[brushX + 1][brushY - 1] = true;
- update[brushX - 1][brushY + 1] = true;
- }
- }
- private void start()
- {
- terrain = new byte[width][height];
- update = new boolean[width][height];
- }
- private void updateInput(GameContainer container)
- {
- mouseButton = 0;
- short mX = (short) (input.getMouseX() / 2);
- short mY = (short) (input.getMouseY() / 2);
- if (input.isKeyPressed(Input.KEY_ESCAPE)) container.exit();
- if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
- {
- if (resetTime)
- {
- brushX = mX;
- brushY = mY;
- brushOn = true;
- brushType = leftBrushType;
- brushOn = true;
- resetTime = false;
- drawBrush(brushX, brushY);
- }
- }
- else
- {
- mouseButton ++;
- }
- if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
- {
- if (resetTime)
- {
- brushX = mX;
- brushY = mY;
- brushOn = true;
- brushType = 0;
- brushOn = true;
- resetTime = false;
- drawBrush(brushX, brushY);
- }
- }
- else
- {
- mouseButton ++;
- }
- if (mouseButton == 2)
- {
- resetTime = true;
- brushOn = false;
- }
- if (brushOn)
- {
- while(brushX != mX && brushY != mY)
- {
- drawBrush(brushX, brushY);
- {
- if (brushX < mX)
- {
- brushX ++;
- }
- if (brushX > mX)
- {
- brushX --;
- }
- }
- else
- {
- if (brushY < mY)
- {
- brushY ++;
- }
- if (brushY > mY)
- {
- brushY --;
- }
- }
- }
- }
- //Control the program
- if (input.isKeyPressed(Input.KEY_F1))
- {
- normalGravity = !normalGravity;
- }
- if (input.isKeyPressed(Input.KEY_F2))
- {
- deleteOnBottom = !deleteOnBottom;
- }
- if (input.isKeyPressed(Input.KEY_F3))
- {
- faucetsOn = !faucetsOn;
- }
- if (input.isKeyPressed(Input.KEY_1))
- {
- leftBrushType = 1;
- }
- if (input.isKeyPressed(Input.KEY_2))
- {
- leftBrushType = 2;
- }
- if (input.isKeyPressed(Input.KEY_3))
- {
- leftBrushType = 3;
- }
- if (input.isKeyPressed(Input.KEY_4))
- {
- leftBrushType = 4;
- }
- if (input.isKeyPressed(Input.KEY_0))
- {
- leftBrushType = -1;
- }
- }
- private void updateTerrain()
- {
- if (normalGravity)
- {
- for (int i = 0; i < width / 8; i++)
- {
- for (int x = columnToUpdate; x < columnToUpdate + 16; x++)
- {
- for (int y = height - 2; y >= 0; y--)
- {
- if (terrain[x][y] > 0)
- {
- if (terrain[x][y+1] == 0 && terrain[x][y] != 0)
- {
- terrain[x][y+1] = terrain[x][y];
- terrain[x][y] = 0;
- update[x][y] = true;
- update[x][y+1] = true;
- }
- if (deleteOnBottom)
- {
- if (y > height - 3)
- {
- terrain[x][y] = 0;
- terrain[x][y+1] = 0;
- update[x][y] = true;
- update[x][y+1] = true;
- }
- }
- if (y + 1 < height)
- {
- if (changer)
- {
- if (x + 1 < width)
- {
- if (y + 1 < height - 1)
- {
- if (terrain[x+1][y+1] == 0 || terrain[x][y] > 2)
- {
- if (terrain[x+1][y] == 0)
- {
- terrain[x+1][y] = terrain[x][y];
- terrain[x][y] = 0;
- update[x][y] = true;
- update[x+1][y] = true;
- }
- }
- }
- }
- }
- else
- {
- if (x - 1 >= 0)
- {
- if (y + 1 < height - 1)
- {
- if (terrain[x-1][y+1] == 0 || terrain[x][y] > 2)
- {
- if (terrain[x-1][y] == 0)
- {
- terrain[x-1][y] = terrain[x][y];
- terrain[x][y] = 0;
- update[x][y] = true;
- update[x-1][y] = true;
- }
- }
- }
- }
- }
- changer = !changer;
- }
- }
- }
- }
- columnToUpdate += 16;
- if (columnToUpdate == width)
- {
- columnToUpdate = 0;
- if (faucetsOn)
- {
- }
- }
- }
- }
- else
- {
- for (int i = 0; i < width / 16; i++)
- {
- for (int x = columnToUpdate; x < columnToUpdate + 16; x++)
- {
- for (int y = 1; y < height - 2; y++)
- {
- if (terrain[x][y] > 0)
- {
- if (terrain[x][y-1] == 0 && terrain[x][y] != 0)
- {
- terrain[x][y-1] = terrain[x][y];
- terrain[x][y] = 0;
- update[x][y] = true;
- update[x][y-1] = true;
- }
- if (deleteOnBottom)
- {
- if (y < 2)
- {
- terrain[x][y] = 0;
- terrain[x][y-1] = 0;
- update[x][y] = true;
- update[x][y-1] = true;
- }
- }
- if (y + 1 < height - 1)
- {
- if (changer)
- {
- if (x + 1 < width)
- {
- if (y - 1 >= 0)
- {
- if (terrain[x+1][y-1] == 0 || terrain[x][y] > 2)
- {
- if (terrain[x+1][y] == 0)
- {
- terrain[x+1][y] = terrain[x][y];
- terrain[x][y] = 0;
- update[x][y] = true;
- update[x+1][y] = true;
- }
- }
- }
- }
- }
- else
- {
- if (x - 1 >= 0)
- {
- if (y - 1 >= 0)
- {
- if (terrain[x-1][y-1] == 0 || terrain[x][y] > 2)
- {
- if (terrain[x-1][y] == 0)
- {
- terrain[x-1][y] = terrain[x][y];
- terrain[x][y] = 0;
- update[x][y] = true;
- update[x-1][y] = true;
- }
- }
- }
- }
- }
- changer = !changer;
- }
- }
- }
- }
- columnToUpdate += 16;
- if (columnToUpdate == width)
- {
- columnToUpdate = 0;
- if (faucetsOn)
- {
- }
- }
- }
- }
- }
- }
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.