@Override public void processTask(GameEntity obj) { // Collect data GameTile oldTile = obj.tile[0][0]; int newX = oldTile.x + dir.getX(); int newY = oldTile.y + dir.getY(); GameTile newTile = oldTile.level.getGameTile(newX, newY); Item wep = obj.getInventory().getEquip(EquipmentSlot.WEAPON); Array<GameTile> hitTiles = buildHitTileArray(obj, dir); // Check if should attack something boolean hitSomething = false; for (GameTile tile : hitTiles) { if (tile.entity != null && !tile.entity.isAllies(obj)) { hitSomething = true; break; } if (tile.environmentEntity != null && tile.environmentEntity.canTakeDamage && !tile.environmentEntity.passableBy.intersect(obj.getTravelType())) { hitSomething = true; } } // Do attack if (hitSomething) { doAttack(hitTiles, obj, wep); // do graphics stuff obj.sprite.spriteAnimation = new BumpAnimation(0.1f, dir); } }
public static Array<Point> buildAllDirectionHitTiles(GameEntity entity) { Array<Point> points = new Array<Point>(); Item weapon = entity.getInventory().getEquip(EquipmentSlot.WEAPON); for (Direction dir : Direction.values()) { if (Global.CanMoveDiagonal || dir.isCardinal()) { int xstep = 0; int ystep = 0; int sx = 0; int sy = 0; if (dir == Direction.NORTH) { sx = 0; sy = entity.size - 1; xstep = 1; ystep = 0; } else if (dir == Direction.SOUTH) { sx = 0; sy = 0; xstep = 1; ystep = 0; } else if (dir == Direction.EAST) { sx = entity.size - 1; sy = 0; xstep = 0; ystep = 1; } else if (dir == Direction.WEST) { sx = 0; sy = 0; xstep = 0; ystep = 1; } for (int i = 0; i < entity.size; i++) { GameTile attackerTile = entity.tile[sx + xstep * i][sy + ystep * i]; if (weapon != null && weapon.wepDef != null) { Matrix3 mat = new Matrix3(); mat.setToRotation(dir.getAngle()); Vector3 vec = new Vector3(); for (Point point : weapon.wepDef.hitPoints) { vec.set(point.x, point.y, 0); vec.mul(mat); int dx = Math.round(vec.x); int dy = Math.round(vec.y); Point pos = Global.PointPool.obtain().set(attackerTile.x + dx, attackerTile.y + dy); points.add(pos); } } else { Point pos = Global.PointPool.obtain() .set(attackerTile.x + dir.getX(), attackerTile.y + dir.getY()); points.add(pos); } } } } // restrict by visibility and remove duplicates Array<Point> visibleTiles = entity.visibilityCache.getCurrentShadowCast(); Iterator<Point> itr = points.iterator(); while (itr.hasNext()) { Point pos = itr.next(); boolean matchFound = false; // Remove not visible for (Point point : visibleTiles) { if (point.x == pos.x && point.y == pos.y) { matchFound = true; break; } } // Remove duplicates for (int i = 0; i < points.size; i++) { Point opos = points.get(i); if (opos != pos && opos.x == pos.x && opos.y == pos.y) { matchFound = false; break; } } if (!matchFound) { itr.remove(); Global.PointPool.free(pos); } } return points; }
public static Array<GameTile> buildHitTileArray(GameEntity attacker, Direction dir) { Array<GameTile> tiles = new Array<GameTile>(); Item weapon = attacker.getInventory().getEquip(EquipmentSlot.WEAPON); int xstep = 0; int ystep = 0; int sx = 0; int sy = 0; if (dir == Direction.NORTH) { sx = 0; sy = attacker.size - 1; xstep = 1; ystep = 0; } else if (dir == Direction.SOUTH) { sx = 0; sy = 0; xstep = 1; ystep = 0; } else if (dir == Direction.EAST) { sx = attacker.size - 1; sy = 0; xstep = 0; ystep = 1; } else if (dir == Direction.WEST) { sx = 0; sy = 0; xstep = 0; ystep = 1; } for (int i = 0; i < attacker.size; i++) { GameTile attackerTile = attacker.tile[sx + xstep * i][sy + ystep * i]; if (weapon != null && weapon.wepDef != null) { Matrix3 mat = new Matrix3(); mat.setToRotation(dir.getAngle()); Vector3 vec = new Vector3(); for (Point point : weapon.wepDef.hitPoints) { vec.set(point.x, point.y, 0); vec.mul(mat); int dx = Math.round(vec.x); int dy = Math.round(vec.y); GameTile tile = attackerTile.level.getGameTile(attackerTile.x + dx, attackerTile.y + dy); if (tile != null) { tiles.add(tile); } } } else { tiles.add( attackerTile.level.getGameTile( attackerTile.x + dir.getX(), attackerTile.y + dir.getY())); } } // restrict by visibility and remove duplicates Array<Point> visibleTiles = attacker.visibilityCache.getCurrentShadowCast(); Iterator<GameTile> itr = tiles.iterator(); while (itr.hasNext()) { GameTile tile = itr.next(); boolean matchFound = false; // Remove not visible for (Point point : visibleTiles) { if (point.x == tile.x && point.y == tile.y) { matchFound = true; break; } } // Remove duplicates for (int i = 0; i < tiles.size; i++) { GameTile otile = tiles.get(i); if (otile != tile && otile.x == tile.x && otile.y == tile.y) { matchFound = false; break; } } if (!matchFound) { itr.remove(); } } return tiles; }