Posted by Epitaph64 on Mon 8 Jun 01:05
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 int[][] terrain;
- private int columnToUpdate = 0;
- private int brushX, brushY;
- private boolean brushOn;
- private int brushType = -1;
- private boolean resetTime = true;
- private int mouseButton = 0;
- private int width = 400;
- private int height = 300;
- try
- {
- AppGameContainer app = new AppGameContainer(new Main());
- app.setDisplayMode(800, 600, false);
- app.setFullscreen(false);
- app.setShowFPS(false);
- app.setClearEachFrame(false);
- app.setTargetFrameRate(60);
- 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
- {
- mouseButton = 0;
- if (input.isKeyPressed(Input.KEY_ESCAPE)) container.exit();
- if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
- {
- if (resetTime)
- {
- brushX = mX;
- brushY = mY;
- brushOn = true;
- brushType = -1;
- 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 --;
- }
- }
- }
- }
- for (int i = 0; i < width / 32; 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+1] = terrain[x][y];
- terrain[x][y] = 0;
- }
- if (y + 1 < height - 2)
- {
- {
- if (x + 1 < width)
- {
- if (terrain[x+1][y] == 0)
- {
- terrain[x+1][y] = terrain[x][y];
- terrain[x][y] = 0;
- }
- }
- }
- else
- {
- if (x - 1 >= 0)
- {
- if (terrain[x-1][y] == 0)
- {
- terrain[x-1][y] = terrain[x][y];
- terrain[x][y] = 0;
- }
- }
- }
- }
- }
- }
- }
- columnToUpdate += 16;
- if (columnToUpdate == width)
- {
- columnToUpdate = 0;
- }
- }
- }
- {
- g.fillRect(0, 0, 800, 600);
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- if (terrain[x][y] != 0)
- {
- switch(terrain[x][y])
- {
- case -1:
- {
- break;
- }
- case 1:
- {
- break;
- }
- case 2:
- {
- break;
- }
- case 3:
- {
- break;
- }
- case 4:
- {
- break;
- }
- }
- g.fillRect(x * 2, y * 2, 2, 2);
- }
- }
- }
- }
- 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;
- }
- }
- private void start()
- {
- terrain = new int[width][height];
- }
- }
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.