public static boolean collision(int xCord, int yCord, boolean b) { if (GameData.isValid(xCord, yCord) && GameData.instance().blockMap[xCord][yCord] != null) { return true; } if (b) { Point p = Frame.game.m.cordToPos(xCord, yCord); Rectangle r1 = new Rectangle(p.x, p.y, 60, 60); for (int i = 0; i < GameData.instance().entityList.size(); i++) { Entity e = GameData.instance().entityList.get(i); if (e.isSolid()) { Rectangle r2 = e.getBounds(); if (r1.intersects(r2)) { return true; } } } } return false; }
public void updateLight() { byte temp = lightValue; if (this.isTop()) { lightValue = GameData.instance().sunLight; } else { lightValue = 0; } for (int a = -1; a < 2; a++) { for (int b = -1; b < 2; b++) { if (getBlock(a, b, this) != null && getBlock(a, b, this).getLightValue() / 3 > lightValue) { lightValue = (byte) (getBlock(a, b, this).getLightValue() / 3); } BlockAir b1 = BlockAir.getBlock(getXCord() + a, getYCord() + b); if (b1 != null && b1.getLightValue() / 2 > lightValue) { lightValue = (byte) (b1.getLightValue() / 2); } } } if (lightValue != temp) { for (int i1 = -1; i1 < 2; i1++) { for (int i2 = -1; i2 < 2; i2++) { if (getBlock(i1, i2, this) != null) { getBlock(i1, i2, this).updateLight(); } } } } updateNeighbourValues(); }
// Returns block at given coords. public static Block getBlock(int x, int y) { if (GameData.isValid(x, y)) { return GameData.instance().blockMap[x][y]; } else { return null; } }
static { try { terrainMap = ImageIO.read(GameData.instance().getClass().getResource("/Resources/TerrainMap.png")); } catch (Exception e) { } }
public boolean isTop() { for (int i = yCord + 1; i < GameData.worldHeight; i++) { if (GameData.instance().blockMap[xCord][i] != null) { return false; } } return true; }
protected void initLight() { if (getBlock(0, 1, this) != null) { getBlock(0, 1, this).updateLight(); } else if (BlockAir.getBlock(xCord, yCord + 1) != null) { BlockAir.getBlock(xCord, yCord + 1).updateLight(); } if (getBlock(0, -1, this) != null) { getBlock(0, -1, this).updateLight(); } else if (BlockAir.getBlock(xCord, yCord - 1) != null) { BlockAir.getBlock(xCord, yCord - 1).updateLight(); if (BlockAir.getBlock(xCord, yCord - 1).isDirectLight()) { BlockAir.getBlock(xCord, yCord - 1).setDirectLight(false); } } if (getBlock(1, 0, this) != null) { getBlock(1, 0, this).updateLight(); } else if (BlockAir.getBlock(xCord + 1, yCord) != null) { BlockAir.getBlock(xCord + 1, yCord).updateLight(); } if (getBlock(-1, 0, this) != null) { getBlock(-1, 0, this).updateLight(); } else if (BlockAir.getBlock(xCord - 1, yCord) != null) { BlockAir.getBlock(xCord - 1, yCord).updateLight(); } if (isTop()) { for (int i = 0; i < GameData.worldHeight; i++) { Block b = GameData.instance().blockMap[getXCord()][i]; if (b != null && b.lightValue == GameData.instance().sunLight) { b.updateLight(); } } } }
public void remove() { GameData.instance().blockList.remove(this); GameData.instance().blockMap[xCord][yCord] = null; for (int a = -1; a < 2; a++) { for (int b = -1; b < 2; b++) { if (getBlock(a, b, this) != null) { getBlock(a, b, this).updateLight(); } } } if (isTop()) { for (int i = 0; i < GameData.worldHeight; i++) { if (GameData.instance().blockMap[xCord][i] != null && GameData.instance().blockMap[xCord][i].isTop()) { GameData.instance().blockMap[xCord][i].updateLight(); } } } BlockAir.create(xCord, yCord, true); BlockAir.getBlock(xCord, yCord).updateLight(); }
static { try { BufferedImage spriteMap = ImageIO.read(GameData.instance().getClass().getResource("/Resources/jimmyMap.png")); for (int b = 0; b < 3; b++) { bodySprite[b] = new BufferedImage(bodyWidth, bodyHeight, spriteMap.getType()); Graphics2D g = bodySprite[b].createGraphics(); g.drawImage( spriteMap, 0, 0, bodyWidth, bodyHeight, bodyWidth * b, 0, bodyWidth * b + bodyWidth, bodyHeight, null); g.dispose(); } for (int b = 0; b < 6; b++) { headSprite[b] = new BufferedImage(headWidth, headHeight, spriteMap.getType()); Graphics2D g = headSprite[b].createGraphics(); g.drawImage( spriteMap, 0, 0, headWidth, headHeight, headWidth * b, bodyHeight, headWidth * b + headWidth, bodyHeight + headHeight, null); g.dispose(); } for (int b = 0; b < 5; b++) { headSprite[b + 6] = new BufferedImage(headWidth, headHeight, spriteMap.getType()); Graphics2D g = headSprite[b + 6].createGraphics(); g.drawImage( spriteMap, 0, 0, headWidth, headHeight, headWidth * b, bodyHeight + headHeight, headWidth * b + headWidth, bodyHeight + headHeight * 2, null); g.dispose(); } armSprite = new BufferedImage(armWidth, armHeight, spriteMap.getType()); Graphics2D g = armSprite.createGraphics(); g.drawImage( spriteMap, 0, 0, armWidth, armHeight, (bodyWidth * 3), 0, armWidth + (bodyWidth * 3), armHeight, null); g.dispose(); legSprite = new BufferedImage(legWidth, legHeight, spriteMap.getType()); g = legSprite.createGraphics(); g.drawImage( spriteMap, 0, 0, legWidth, legHeight, bodyWidth * 3 + armWidth, 0, legWidth + armWidth + (bodyWidth * 3), legHeight, null); g.dispose(); footSprite = new BufferedImage(footWidth, footHeight, spriteMap.getType()); g = footSprite.createGraphics(); g.drawImage( spriteMap, 0, 0, footWidth, footHeight, armWidth + legWidth + (bodyWidth * 3), 0, armWidth + legWidth + footWidth + (bodyWidth * 3), footHeight, null); g.dispose(); } catch (Exception e) { System.err.println(e.getMessage()); } }
public Block(short xCord, short yCord) { this(); setCoordinates(xCord, yCord); GameData.instance().blockMap[xCord][yCord] = this; }
public Block() { setSize(60, 60); GameData.instance().blockList.add(this); setPaintLevel((byte) 3); }