| @@ -0,0 +1,108 @@ | ||
| package ahmed; | ||
| import java.awt.Canvas; | ||
| import java.awt.Color; | ||
| import java.awt.Dimension; | ||
| import java.awt.Graphics; | ||
| import java.awt.Graphics2D; | ||
| import java.awt.RenderingHints; | ||
| import java.awt.image.BufferStrategy; | ||
|
|
||
| public class Launcher extends Canvas implements Runnable { | ||
|
|
||
| GameFrame gameFrame; | ||
|
|
||
| private Input input = new Input(this); | ||
|
|
||
| public static final int WIDTH = 1280; | ||
| public static final int HEIGHT = 600; | ||
|
|
||
| private Dimension size = new Dimension(WIDTH+6, HEIGHT+28); | ||
|
|
||
| private BufferStrategy bs; | ||
| private Graphics g; | ||
|
|
||
| private Handler handler; | ||
|
|
||
| private boolean running; | ||
|
|
||
| private int ticks; | ||
| public Launcher() { | ||
| init(); | ||
| } | ||
|
|
||
| public synchronized void start() { | ||
| Thread thread = new Thread(this); | ||
| running = true; | ||
| thread.start(); | ||
| } | ||
|
|
||
| public void run() { | ||
| int fps = 60; | ||
| double nsPerTick = 1000000000 / fps; | ||
| double delta = 0; | ||
| long now; | ||
| long lastTime = System.nanoTime(); | ||
| long timer = 0; | ||
| int frames = 0; | ||
|
|
||
| while (running) { | ||
| now = System.nanoTime(); | ||
| delta += (now - lastTime) / nsPerTick; | ||
| timer += now - lastTime; | ||
| lastTime = now; | ||
| if (delta >= 1) { | ||
| update(); | ||
| //ticks++; | ||
| frames++; | ||
| render(); | ||
| delta--; | ||
| } | ||
|
|
||
| if (timer >= 1000000000) { | ||
| ticks = frames; | ||
| frames = 0; | ||
| timer = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void init() { | ||
| handler = new Handler(this); | ||
| gameFrame = new GameFrame(this, size); | ||
| start(); | ||
| } | ||
|
|
||
| public void update() { | ||
| handler.update(); | ||
| } | ||
|
|
||
| public void render() { | ||
| bs = getBufferStrategy(); | ||
| if (bs == null) { | ||
| createBufferStrategy(3); | ||
| return; | ||
| } | ||
| g = bs.getDrawGraphics(); | ||
| Graphics2D g2 = (Graphics2D) g; | ||
| g2.setColor(Color.white); | ||
| g2.fillRect(0, 0, Launcher.WIDTH, Launcher.HEIGHT); | ||
| handler.render(g2); | ||
| g2.setColor(Color.cyan); | ||
| g2.drawString(""+ticks, 10, 20); | ||
| bs.show(); | ||
| g.dispose(); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| new Launcher(); | ||
| } | ||
|
|
||
| public int width() { | ||
| return WIDTH; | ||
| } | ||
|
|
||
| public int height() { | ||
| return HEIGHT; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,106 @@ | ||
| package ahmed; | ||
| import java.awt.Graphics2D; | ||
| import java.awt.image.BufferedImage; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
|
|
||
| public class Level { | ||
|
|
||
| static int tileSize = 16; | ||
|
|
||
| Handler handler; | ||
|
|
||
| int[][] objectTiles; | ||
| int[][] floorTiles; | ||
|
|
||
| int gridWidth = 0; | ||
| int gridHeight = 0; | ||
|
|
||
| public Level(Handler handler) { | ||
| this.handler = handler; | ||
| } | ||
|
|
||
| public void init() { | ||
| loadTiles(); | ||
| loadFloor(); | ||
| } | ||
|
|
||
| private void loadTiles() { | ||
|
|
||
| int[][] mapGridRGB = null; | ||
|
|
||
| try { | ||
| BufferedImage gridImage = ImageLoader.objects; | ||
| gridWidth = gridImage.getWidth(); | ||
| gridHeight = gridImage.getHeight(); | ||
| System.out.println(gridWidth + "," + gridHeight); | ||
| int[] mapArrayRGB = gridImage.getRGB(0, 0, gridWidth, gridHeight, null, 0, gridWidth); | ||
|
|
||
| for (int i = 0; i < mapArrayRGB.length; i++) { | ||
| System.out.println(mapArrayRGB[i]); | ||
| } | ||
|
|
||
| mapGridRGB = new int[gridHeight][gridWidth]; | ||
| for (int j = 0; j < gridHeight; j++) { | ||
| for (int i = 0; i < gridWidth; i++) { | ||
| mapGridRGB[j][i] = mapArrayRGB[i + j * gridWidth]; | ||
| System.out.println(mapGridRGB[j][i]); | ||
| } | ||
| } | ||
| } catch (Throwable e) { | ||
| e.printStackTrace(); | ||
| System.exit(1); | ||
| } | ||
| System.out.println("Grid loaded"); | ||
|
|
||
| objectTiles = new int[gridHeight][gridWidth]; | ||
|
|
||
| for (int j = 0; j < gridHeight; j++) { | ||
| for (int i = 0; i < gridWidth; i++) { | ||
| for (Tile tile : Tile.objectTiles) { | ||
| if (tile.tileColor == mapGridRGB[j][i]) { | ||
| // System.out.println(tile.id); | ||
| objectTiles[j][i] = tile.id; | ||
| break; | ||
| } else { | ||
| System.out.println(tile.tileColor + "," + mapGridRGB[j][i]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| System.out.println("Grid loaded2"); | ||
|
|
||
| } | ||
|
|
||
| private void loadFloor() { | ||
| // handler.launcher.gameFrame.frame.createImage(width, height); | ||
| } | ||
|
|
||
| public void update() { | ||
|
|
||
| } | ||
|
|
||
| public void render(Graphics2D g2) { | ||
| renderFloor(g2); | ||
| for (int j = 0; j < objectTiles.length; j++) { | ||
| for (int i = 0; i < objectTiles[0].length; i++) { | ||
| Tile.objectTiles.get(objectTiles[j][i]).render(g2, tileSize * i, tileSize * j); | ||
| // System.out.print( | ||
| // Tile.objectTiles.get(objectTiles[j][i]).id); | ||
| } | ||
| // System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| private void renderFloor(Graphics2D g2){ | ||
| for (int j = 0; j < objectTiles.length; j++) { | ||
| for (int i = 0; i < objectTiles[0].length; i++) { | ||
| // floorTile. | ||
| // System.out.print( | ||
| // Tile.objectTiles.get(objectTiles[j][i]).id); | ||
| } | ||
| // System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,67 @@ | ||
| package ahmed; | ||
|
|
||
| public class MouseButton { | ||
|
|
||
| private String keyName; | ||
|
|
||
| private boolean pressed = false; | ||
|
|
||
| private long lastPress = -1; | ||
| private long lastRelease = -1; | ||
|
|
||
| private int clickX = -1; | ||
| private int clickY = -1; | ||
| private int releaseX = -1; | ||
| private int releaseY = -1; | ||
|
|
||
| private static int HOLD_LENGTH = 200; | ||
|
|
||
| public MouseButton(String keyName) { | ||
| this.keyName = keyName; | ||
| } | ||
|
|
||
| public boolean isPressed() { | ||
| boolean pressed1 = pressed; | ||
| // pressed = false; | ||
| return pressed1; | ||
| } | ||
|
|
||
| public boolean isHeld() { | ||
| System.out.println(System.currentTimeMillis() - lastRelease); | ||
| return pressed && System.currentTimeMillis() - lastRelease > HOLD_LENGTH; | ||
| } | ||
|
|
||
| public int clickX() { | ||
| return clickX; | ||
| } | ||
|
|
||
| public int clickY() { | ||
| return clickY; | ||
| } | ||
|
|
||
| public int releaseX() { | ||
| return releaseX; | ||
| } | ||
|
|
||
| public int releaseY() { | ||
| return releaseY; | ||
| } | ||
|
|
||
| public void toggle(boolean isPressed) { | ||
| pressed = isPressed; | ||
| if (pressed) { | ||
| lastPress = System.currentTimeMillis(); | ||
| clickX = Input.MOUSE_X; | ||
| clickY = Input.MOUSE_Y; | ||
| } else { | ||
| lastRelease = System.currentTimeMillis(); | ||
| releaseX = Input.MOUSE_X; | ||
| releaseY = Input.MOUSE_Y; | ||
| } | ||
| } | ||
|
|
||
| public String toString() { | ||
| return keyName; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,42 @@ | ||
| package ahmed; | ||
| import java.awt.Graphics2D; | ||
| import java.awt.image.BufferedImage; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Tile { | ||
|
|
||
| int id=2; | ||
| BufferedImage image; | ||
| int tileColor; | ||
| boolean solid; | ||
|
|
||
| static ArrayList<Tile> objectTiles = new ArrayList<Tile>(); | ||
|
|
||
| static int tileSize = 16; | ||
|
|
||
| public static Tile sofa = new Tile(0, ImageLoader.sofa, 0xFFffaec9, true); | ||
| public static Tile computer = new Tile(1, ImageLoader.computer, 0xFFed1c24, true); | ||
| public static Tile coffee = new Tile(2, ImageLoader.coffee, 0xFFb97a57, true); | ||
| public static Tile floor = new Tile(2, ImageLoader.floor, 0xFFff7f27, false); | ||
|
|
||
|
|
||
| public Tile(int id, BufferedImage image, int tileColor, boolean solid){ | ||
| this.id = id; | ||
| this.image=image; | ||
| this.solid=solid; | ||
| this.tileColor=tileColor; | ||
| objectTiles.add(this); | ||
| System.out.println(0); | ||
| } | ||
|
|
||
| public void interact(Object o){ | ||
|
|
||
| } | ||
|
|
||
| public void render(Graphics2D g2, int x, int y){ | ||
| // System.out.println("Drawing: "+id+" at x: "+x+" :y "+y); | ||
| g2.drawImage(image,x, y, tileSize, tileSize,null); | ||
| // g2.drawImage(ImageLoader.objects, 0, 0, null); | ||
| } | ||
|
|
||
| } |