protected Tile laserTarget(Tile tile, int rotation){ rotation = Mathf.mod(rotation, 4); GridPoint2 point = Geometry.getD4Points()[rotation]; int i = 0; for(i = 1; i < laserRange; i++){ Tile other = Vars.world.tile(tile.x + i * point.x, tile.y + i * point.y); if(other != null && other.block() instanceof PowerAcceptor){ Tile linked = other.getLinked(); if(linked == null || linked instanceof PowerAcceptor){ return other; } } } return null; }
private boolean isFreeTetriminoPosition(GridPoint2 position, int direction) { boolean turn = true; for(GridPoint2 p : currentTetrimino.position[direction]) { if(p.x + position.x >= 0 && p.x + position.x < PLAYFIELD_WIDTH && p.y + position.y >= 0 && p.y + position.y < PLAYFIELD_HEIGHT) { Color color = cells[p.x + position.x][p.y + position.y]; if(color != null) { turn = false; break; } } else { turn = false; break; } } return turn; }
boolean checkDpadDown(int keyCode){ GridPoint2 position = player.getPosition(); if(keyCode == Input.Keys.W || keyCode == Input.Keys.UP){ position.y++; } else if(keyCode == Input.Keys.S || keyCode == Input.Keys.DOWN){ position.y--; } else if(keyCode == Input.Keys.D || keyCode == Input.Keys.RIGHT){ position.x++; } else if(keyCode == Input.Keys.A || keyCode == Input.Keys.LEFT){ position.x--; } if (position.equals(player.getPosition())){ return false; } else { player.moveTo(position); return true; } }
private static GridPoint2 getNextPointTowardsEndPoint(GridPoint2 startPoint, GridPoint2 endPoint){ int x = startPoint.x; int y = startPoint.y; if (startPoint.x < endPoint.x){ x++; } else if(startPoint.x > endPoint.x){ x--; } if (startPoint.y < endPoint.y){ y++; } else if(startPoint.y > endPoint.y){ y--; } return new GridPoint2(x, y); }
void placeBoundingWalls(Dungeon dungeon, int x, int y, int width, int height){ for(int i = x+1; i < x + width-1; i++){ for(int j = y+1; j < y + height-1; j++) { dungeon.setTile(new FloorDungeonTile(new GridPoint2(i, j), dungeon)); } } for (int j = x; j < width+x; j++) { dungeon.setTile(new WallDungeonTile(new GridPoint2(j, y), dungeon, false)); dungeon.setTile(new WallDungeonTile(new GridPoint2(j, height + y - 1), dungeon, false)); } for (int i = y; i < height+y; i++) { dungeon.setTile(new WallDungeonTile(new GridPoint2(x, i), dungeon, false)); dungeon.setTile(new WallDungeonTile(new GridPoint2(x + width - 1, i), dungeon, false)); } }
public Dungeon(int mapWidth, int mapHeight){ this.mapWidth = mapWidth; this.mapHeight = mapHeight; renderer = new DungeonRenderer(this); map = new DungeonTile[mapWidth+2][mapHeight+2]; for(int i = 0; i < mapWidth+2; i++){ for (int j = 0; j < mapHeight+2; j++){ map[i][j] = new EmptyDungeonTile(new GridPoint2(i-1, j-1), this); } } dungeonRooms = new Array<>(); monsters = new Array<>(); level = 1; }
Array<Array<AstarNode>> getAstarGraph(){ Array<Array<AstarNode>> astarGraph = new Array<>(); for(int i = 0; i < getMapWidth(); i++){ astarGraph.add(new Array<>()); } for (int i = 0; i < getMapWidth(); i++){ for(int j = 0; j < getMapHeight(); j++){ AstarNode node = new AstarNode(); node.passingCost = getDungeonTile(new GridPoint2(i, j)).getPassingCost(); node.x = i; node.y = j; astarGraph.get(i).add(node); } } return astarGraph; }
private void placeStartAndEndStairs(Dungeon dungeon) { GridPoint2 stairsUpPos; GridPoint2 stairsDownPos; Room startRoom = dungeon.startRoom; Room endRoom = DungeonUtils.getRandomNotStartDungeonRoom(dungeon); stairsUpPos = startRoom.getRandomFloorTile(); stairsDownPos = endRoom.getRandomFloorTile(); StairsUpDungeonTile stairsUpDungeonTile = new StairsUpDungeonTile(stairsUpPos, dungeon); StairsDownDungeonTile stairsDownDungeonTile = new StairsDownDungeonTile(stairsDownPos, dungeon); dungeon.setTile(stairsUpDungeonTile); dungeon.setTile(stairsDownDungeonTile); dungeon.stairsDownDungeonTile = stairsDownDungeonTile; dungeon.stairsUpDungeonTile = stairsUpDungeonTile; }
private Array<Array<AstarNode>> getDungeonAsAstarNodeGraph(Dungeon dungeon) { Array<Array<AstarNode>> graph = new Array<>(); for(int i = 0; i < dungeon.getMapWidth(); i++){ graph.add(new Array<>()); } for (int i = 0; i < dungeon.getMapWidth(); i++){ for(int j = 0; j < dungeon.getMapHeight(); j++){ AstarNode node = new AstarNode(); node.passingCost = dungeon.getDungeonTile(new GridPoint2(i, j)).getCorridorPlacingCost(); node.x = i; node.y = j; graph.get(i).add(node); } } return graph; }
private void addCorridorTile(Dungeon dungeon, GridPoint2 pos, Direction direction){ DungeonTile tile = dungeon.getDungeonTile(pos); // Set wall tiles this corridor passes through to doors if(tile instanceof WallDungeonTile){ if (direction == Direction.VERTICAL ){ // Place a perpendicular door across the corridor dungeon.setTile(new DoorDungeonTile(pos, dungeon, true)); } else { dungeon.setTile(new DoorDungeonTile(pos, dungeon, false)); } } // Otherwise set the tile to a floor tiles with walls either side else if(tile instanceof EmptyDungeonTile || tile instanceof CorridorWallDungeonTile) { dungeon.setTile(new CorridorFloorDungeonTile(pos, dungeon)); if(direction == Direction.VERTICAL){ addCorridorWall(dungeon, new GridPoint2(pos.x+1, pos.y)); addCorridorWall(dungeon, new GridPoint2(pos.x-1, pos.y)); } else { addCorridorWall(dungeon, new GridPoint2(pos.x, pos.y+1)); addCorridorWall(dungeon, new GridPoint2(pos.x, pos.y-1)); } } }
private boolean getNewPath(){ Dungeon dungeon = Dungeon.getActiveDungeon(); int tries = 0; GridPoint2 potentialTarget = DungeonUtils.getRandomTileInAnyRoom(dungeon); Array<AstarNode> potentialPath = DungeonUtils.generateNewPathBetween(character.getPosition(), potentialTarget, character.getDungeon()); //Loop trying to find a path to a random room. Give up after 3 tries. //TODO build the fScore limit into the algo to avoid unnecessary path generation float pathCostThreshold = 50000; while (potentialPath.size == 0 || potentialPath.get(0).fScore > pathCostThreshold){ potentialTarget = DungeonUtils.getRandomTileInAnyRoom(dungeon); potentialPath = DungeonUtils.generateNewPathBetween(character.getPosition(), potentialTarget, character.getDungeon()); tries++; if (tries >= 3){ return false; } } pathTarget.set(potentialTarget); path = potentialPath; return true; }
private GridPoint2 project(float x, float y){ float ratio = 1f / ((float)editor.pixmap().getWidth() / editor.pixmap().getHeight()); float size = Math.min(width, height); float sclwidth = size * zoom; float sclheight = size * zoom * ratio; x = (x - getWidth()/2 + sclwidth/2 - offsetx*zoom) / sclwidth * editor.texture().getWidth(); y = (y - getHeight()/2 + sclheight/2 - offsety*zoom) / sclheight * editor.texture().getHeight(); return Tmp.g1.set((int)x, editor.texture().getHeight() - 1 - (int)y); }
/** * Input is in block coordinates, not world coordinates. * @return null if no collisions found, block position otherwise. */ public GridPoint2 raycast(int x0f, int y0f, int x1, int y1){ int x0 = x0f; int y0 = y0f; int dx = Math.abs(x1 - x0); int dy = Math.abs(y1 - y0); int sx = x0 < x1 ? 1 : -1; int sy = y0 < y1 ? 1 : -1; int err = dx - dy; int e2; while(true){ if(!passable(x0, y0)){ return Tmp.g1.set(x0, y0); } if(x0 == x1 && y0 == y1) break; e2 = 2 * err; if(e2 > -dy){ err = err - dy; x0 = x0 + sx; } if(e2 < dx){ err = err + dx; y0 = y0 + sy; } } return null; }
public void testRemovedPixmapField(){ GraphHeader.currentReadWriteVersion = 0; TestClass object = new TestClass(); object.someInt = randInt(); object.someFloat = randFloat(); object.point = new GridPoint2(randInt(), randInt()); object.color = new Color(Color.CYAN); object.smallTestClass = new SmallTestClass(); object.smallTestClass.someValue = randInt(); Pixmap pixmap = new Pixmap(20, 24, Pixmap.Format.RGBA8888); for (int i = 0; i < pixmap.getWidth(); i++) { for (int j = 0; j < pixmap.getHeight(); j++) { pixmap.drawPixel(i, j, randInt()); } } object.smallTestClass.somePixmap = pixmap; kryo.register(SmallTestClass.class); kryo.register(TestClass.class); GraphHeader<TestClass> graphHeader = new GraphHeader<TestClass>(); graphHeader.data = object; GraphHeader.currentReadWriteVersion = 0; byte[] written = write(graphHeader); GraphHeader.currentReadWriteVersion = 1; GraphHeader<TestClass> returned = read(written, GraphHeader.class); assertTrue(equals(graphHeader, returned)); assertTrue(returned.data.smallTestClass.somePixmap == null); }
@Override public void receiveMessage(MessageType type, Object... args) { switch (type) { case LOAD_ANIMATIONS: EntityConfig config = (EntityConfig) args[0]; Array<AnimationConfig> animationConfigs = config.getAnimationConfig(); int animationWidth = config.getAnimationWidth(); int animationHeight = config.getAnimationHeight(); String charAtlasID = config.getCharAtlasID(); for (AnimationConfig animationConfig : animationConfigs) { Array<GridPoint2> gridPoints = animationConfig.getGridPoints(); AnimationType animationType = animationConfig.getAnimationType(); float frameDuration = animationConfig.getFrameDuration(); animations.put(animationType, loadAnimation(charAtlasID, gridPoints, animationWidth, animationHeight, frameDuration)); } break; case SET_SIZE: origin.set((Float) args[0] * 0.5f, (Float) args[1] * 0.5f); break; case SET_STATE: this.state = (State) args[0]; updateCurrentAnimation(); break; case SET_DIRECTION: this.direction = (Direction) args[0]; updateCurrentAnimation(); break; default: break; } }
private Animation<TextureRegion> loadAnimation(String charAtlasID, Array<GridPoint2> points, int tileWidth, int tileHeight, float frameDuration) { AtlasRegion charTextureRegion = Utils.CHARACTERS_TEXTURE_ATLAS.findRegion(charAtlasID); TextureRegion[][] frames = charTextureRegion.split(tileWidth, tileHeight); Array<TextureRegion> animationFrames = new Array<TextureRegion>(points.size); for (GridPoint2 point : points) { animationFrames.add(frames[point.y][point.x]); } return new Animation<TextureRegion>(frameDuration, animationFrames, Animation.PlayMode.LOOP); }
private void newTetrimino() { Tetrimino[] tetriminos = Tetrimino.values(); boolean change; do { if(nextTetrimino != null) { currentTetrimino = nextTetrimino; nextTetrimino = tetriminos[rand.nextInt(tetriminos.length)]; } else { nextTetrimino = tetriminos[rand.nextInt(tetriminos.length)]; currentTetrimino = tetriminos[rand.nextInt(tetriminos.length)]; } if(nextTetrimino == currentTetrimino && countSameTetrimino < 1) { countSameTetrimino++; } if(nextTetrimino == currentTetrimino && countSameTetrimino >= 1) { change = true; } else { countSameTetrimino = 0; change = false; } }while(change); currentDirection = 0; GridPoint2 startPosition = currentTetrimino.startPosition.cpy(); for(int i = currentTetrimino.startPosition.y; i >= 0; i--) { startPosition.y = i; if(isFreeTetriminoPosition(startPosition, currentDirection)) { currentPosition = startPosition; return; } } }
@Override public void show() { int tileSize = ResourceLoader.getTileSize(); Gdx.input.setInputProcessor(inputMultiplexer); GridPoint2 playerPos = PlayerCharacterEntity.getInstance().getPosition(); camera.position.x = playerPos.x * tileSize; camera.position.y = playerPos.y * tileSize; }
@Override public boolean moveTo(GridPoint2 position) { if(Dungeon.getActiveDungeon().isTilePassable(position)){ this.position.set(position); return true; } else { return false; } }
@Override public void render(float delta, SpriteBatch batch) { if (characterEntity.isAlive()){ GridPoint2 pos = characterEntity.getPosition(); if (Dungeon.getActiveDungeon().getDungeonTile(pos).isVisible()){ float visibility = Dungeon.getActiveDungeon().getDungeonTile(characterEntity.getPosition()).getVisibilityLevel(); batch.setColor(visibility, visibility, visibility, 1); batch.draw(characterEntity.getTexture(), characterEntity.getPosition().x * tileSize, characterEntity.getPosition().y * tileSize); healthBarRenderer.render(delta, batch); } batch.setColor(Color.WHITE); } }
public static boolean checkLineOfSight(GridPoint2 startPoint, GridPoint2 endPoint, Dungeon dungeon){ // Moves the start point towards the end point to avoid points being obstructed by themselves startPoint = getNextPointTowardsEndPoint(startPoint, endPoint); Array<GridPoint2> cellsOnBresenhamLine = runLine(endPoint, startPoint); for (int i = 0; i < cellsOnBresenhamLine.size; i++) { GridPoint2 point = cellsOnBresenhamLine.get(i); if (dungeon.getDungeonTile(point).isVisionObstructing() && !point.equals(endPoint)){ return false; } } return true; }
private static Array<GridPoint2> runLine (int startX, int startY, int endX, int endY) { Array<GridPoint2> line = new Array<>(); // The amount the line has to cover in the x and y axes int deltaX = endX - startX; int deltaY = endY - startY; // the delta error would be infinite on a vertical line and therefore cannot be calculated in the same way if (deltaX == 0){ return runVertLine(startX, startY, endY); } // The amount the points have deviated from the line float error = 0; // The amount the points deviate from the line for each movement along the x axis float deltaError = Math.abs ((float)deltaY / (float)deltaX); int y = startY; // plot pixels along the x axis occasionally moving in the y axis once we have deviated too far from the real line for (int x = startX; x != endX; x+=getSignNumber(deltaX)){ line.add(new GridPoint2(x, y)); error += deltaError; // While we have deviated more than 1/2 a pixel from the real line, we need to correct for it // by moving in the y axis while (error >= 0.5f) { y += getSignNumber(endY - startY); error--; line.add(new GridPoint2(x, y)); } } // Due to not knowing which way the loop is reaching it's condition from (x-- or x++) // the last point has to be added here line.add(new GridPoint2(endX, endY)); return line; }
private static Array<GridPoint2> runVertLine(int x, int startY, int endY) { Array<GridPoint2> line = new Array<>(); // The direction the line is being drawn in (1 for up the Y axis and -1 for down the Y axis) int deltaYSign = getSignNumber(endY-startY); for (int y = startY; y != endY; y+=deltaYSign){ line.add(new GridPoint2(x, y)); } line.add(new GridPoint2(x, endY)); return line; }
public static GridPoint2 getRandomNonVisibleTilePosInAnyRoom(Dungeon dungeon){ int roomIndex = MathUtils.random(dungeon.getRoomCount()-1); GridPoint2 tilePos = dungeon.getDungeonRoom(roomIndex).getRandomFloorTile(); while(dungeon.getDungeonTile(tilePos).isVisible() || dungeon.getDungeonTile(tilePos).isVisionObstructing()){ roomIndex = MathUtils.random(dungeon.getRoomCount()-1); tilePos = dungeon.getDungeonRoom(roomIndex).getRandomFloorTile(); } return tilePos; }
public float getVisibilityLevel() { if(isVisible()){ tileIsDiscovered = true; GridPoint2 playerGridPoint = PlayerCharacterEntity.getInstance().getPosition(); float distance = (float)Point.distance(playerGridPoint.x, playerGridPoint.y, pos.x, pos.y); return 1 - distance/(VIEW_DIST*2); } else { return getHasBeenVisibleVisibility(); } }
public boolean isVisible(){ GridPoint2 playerGridPoint = PlayerCharacterEntity.getInstance().getPosition(); float distance = (float)Point.distance(playerGridPoint.x, playerGridPoint.y, pos.x, pos.y); if (distance > VIEW_DIST) { return false; } else return distance < 1.5f || isLOSBlocked(); }
public GridPoint2 getRandomFloorTile() { GridPoint2 pos = new GridPoint2(); pos.y = MathUtils.random(getY()+1, getY() + getHeight()-2); pos.x = MathUtils.random(getX()+1, getX() + getWidth()-2); if (dungeon.getDungeonTile(pos) instanceof FloorDungeonTile){ return pos; } else { return getRandomFloorTile(); } }
@Override public void generate() { super.generate(); int yOffset = (getHeight()/2) + 1; for (int i = 0; i + 4 <= getWidth(); i += 4){ placeCell(dungeon, getX() + i, getY(), true); placeCell(dungeon, getX() + i, getY()+yOffset, false); } dungeon.setTile(new WallDungeonTile(new GridPoint2(getX(), getY()+getHeight()/2), dungeon, true)); dungeon.setTile(new WallDungeonTile(new GridPoint2(getX()+getWidth()-1, getY()+getHeight()/2), dungeon, true)); }
private void placeCell(Dungeon dungeon, int x, int y, boolean southFacing){ placeBoundingWalls(dungeon, x, y, 5, 5); int doorY = southFacing ? y + 4 : y; dungeon.setTile(new DoorDungeonTile(new GridPoint2(x+2, doorY), dungeon, true)); if(MathUtils.randomBoolean(0.2f)){ dungeon.setTile(new ChestDungeonTile(new GridPoint2(x+2, y+2), dungeon)); } }
public DoorDungeonTile(GridPoint2 pos, Dungeon dungeon, boolean isHorizontal) { super(pos, dungeon); if(isHorizontal){ texture = ResourceLoader.getResTextureRegion("door-horizontal"); }else{ texture = ResourceLoader.getResTextureRegion("door-vertical"); } }
private boolean roomFits(Dungeon dungeon, Room room){ int x = room.getX(); int y = room.getY(); int width = room.getWidth(); int height = room.getHeight(); for(int i = x; i < x + width; i++){ for(int j = y; j < y + height; j++){ if(!dungeon.isTileEmpty(new GridPoint2(i, j))){ return false; } } } return true; }