Posted by Epitaph64 on Fri 7 Aug 18:53
report abuse | download | new post
- package junkengine;
- import java.util.ArrayList;
- import org.newdawn.slick.AngelCodeFont;
- 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;
- import org.newdawn.slick.SpriteSheet;
- public class Main extends BasicGame {
- private int[] mapSize = {100, 100};
- private int screenX = 0;
- private int screenY = 0;
- private int renderX1, renderX2, renderY1, renderY2;
- private MersenneTwisterFast mt;
- private SpriteSheet tileset;
- private Input input;
- private int[][] redMask = new int[mapSize[0]][mapSize[1]];
- private int[][] greenMask = new int[mapSize[0]][mapSize[1]];
- private int[][] blueMask = new int[mapSize[0]][mapSize[1]];
- private mapTiles[][] map = new mapTiles[mapSize[0]][mapSize[1]];
- private AngelCodeFont font;
- private ArrayList<Light> lights = new ArrayList<Light>();
- private Light playerLight;
- Main()
- {
- super("Junk Engine");
- }
- {
- try {
- AppGameContainer app = new AppGameContainer(new Main());
- app.setDisplayMode(1024, 768, false);
- app.setFullscreen(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();
- tileset = new SpriteSheet("data/tileset.png", 16, 16);
- font = new AngelCodeFont("/data/default.fnt", "data/default_00.tga");
- mt = new MersenneTwisterFast();
- createMap();
- lights.add(playerLight);
- }
- @Override
- public void update(GameContainer container, int delta) throws SlickException
- {
- if (input.isKeyPressed(Input.KEY_ESCAPE))
- {
- container.exit();
- }
- if (input.isKeyPressed(Input.KEY_F2))
- {
- createMap();
- }
- if (input.isKeyDown(Input.KEY_LEFT))
- {
- screenX -= 4;
- }
- if (input.isKeyDown(Input.KEY_RIGHT))
- {
- screenX += 4;
- }
- if (input.isKeyDown(Input.KEY_UP))
- {
- screenY -= 4;
- }
- if (input.isKeyDown(Input.KEY_DOWN))
- {
- screenY += 4;
- }
- if (screenX < 0)
- {
- screenX = 0;
- }
- if (screenY < 0)
- {
- screenY = 0;
- }
- if (screenX > (mapSize[0] * 16 - 1024))
- {
- screenX = (mapSize[0] * 16 - 1024);
- }
- if (screenY > (mapSize[1] * 16 - 768))
- {
- screenY = (mapSize[1] * 16 - 768);
- }
- resetLights(0);
- playerLight.x = (input.getMouseX() + (screenX * 2)) / 32;
- playerLight.y = (input.getMouseY() + (screenY * 2)) / 32;
- updateLighting();
- renderX1 = screenX / 16;
- renderX2 = (screenX + 1024) / 16;
- renderY1 = screenY / 16;
- renderY2 = (screenY + 768) / 16;
- }
- {
- g.scale(2, 2);
- for (int x = renderX1; x < renderX2; x++)
- {
- for (int y = renderY1; y < renderY2; y++)
- {
- if (redMask[x][y] > 25 || greenMask[x][y] > 25 || blueMask[x][y] > 25)
- {
- switch(map[x][y])
- {
- case Ground1:
- {
- tileset.getSprite(0, 1).draw(
- x * 16 - screenX,
- y * 16 - screenY,
- break;
- }
- case Ground2:
- {
- tileset.getSprite(1, 1).draw(
- x * 16 - screenX,
- y * 16 - screenY,
- break;
- }
- case Ground3:
- {
- tileset.getSprite(1, 0).draw(
- x * 16 - screenX,
- y * 16 - screenY,
- break;
- }
- case Box:
- {
- tileset.getSprite(0, 0).draw(
- x * 16 - screenX,
- y * 16 - screenY,
- break;
- }
- }
- }
- }
- }
- }
- public void updateLighting()
- {
- for (Light light: lights)
- {
- for (int x = 0; x < mapSize[0]; x++)
- {
- for (int y = 0; y < mapSize[1]; y++)
- {
- float lightingChange = 0;
- if (lightingChange <= 127)
- {
- if ((light.hue.getRed() / 2) - (int) lightingChange > 0)
- {
- redMask[x][y] += (light.hue.getRed() / 2) - (int) lightingChange;
- }
- if ((light.hue.getGreen() / 2) - (int) lightingChange > 0)
- {
- greenMask[x][y] += (light.hue.getGreen() / 2) - (int) lightingChange;
- }
- if ((light.hue.getBlue() / 2) - (int) lightingChange > 0)
- {
- blueMask[x][y] += (light.hue.getBlue() / 2) - (int) lightingChange;
- }
- redMask[x][y] += (128 - lightingChange);
- greenMask[x][y] += (128 - lightingChange);
- blueMask[x][y] += (128 - lightingChange);
- }
- }
- }
- }
- }
- public void resetLights(int ambience)
- {
- for (int x = 0; x < mapSize[0]; x++)
- {
- for (int y = 0; y < mapSize[1]; y++)
- {
- redMask[x][y] = ambience;
- greenMask[x][y] = ambience;
- blueMask[x][y] = ambience;
- }
- }
- }
- public void createMap()
- {
- int s1 = mt.nextInt(3)+1;
- int s2 = mt.nextInt(3)+1;
- int s3 = mt.nextInt(4)+1;
- int s4 = mt.nextInt(5)+1;
- int s5 = mt.nextInt(7)+1;
- map = new mapTiles[mapSize[0]][mapSize[1]];
- for (int x = 0; x < mapSize[0]; x++)
- {
- for (int y = 0; y < mapSize[1]; y++)
- {
- if (x * y % s1 == 0 ^ x * y % s2 == 0)
- {
- if (x * y % s3 == 0 ^ x * y % s5 == 0)
- {
- }
- else
- {
- map[x][y] = mapTiles.Ground3;
- }
- }
- else
- {
- if (x * y % s2 == 2 & x * y % s4 == 0)
- {
- map[x][y] = mapTiles.Ground2;
- }
- else
- {
- map[x][y] = mapTiles.Ground1;
- }
- }
- }
- }
- }
- }
- package junkengine;
- import org.newdawn.slick.Color;
- public class Light {
- int x;
- int y;
- int brightness;
- Color hue;
- {
- this.x = x;
- this.y = y;
- this.brightness = brightness;
- this.hue = hue;
- }
- }
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.