use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project ultimate-java by pantinor.
the class Utils method setTMXMap.
public static void setTMXMap(BaseMap map, Maps id, String tmxFile, TileSet ts) {
List<Person> people = new ArrayList<>();
Tile[] tiles = new Tile[map.getWidth() * map.getHeight()];
TmxMapLoader loader = new TmxMapLoader();
TiledMap tm = loader.load("assets/tilemaps/" + tmxFile);
TiledMapTileLayer ml = (TiledMapTileLayer) tm.getLayers().get(map.getId() + "-map");
if (ml != null) {
FileHandle f = new FileHandle("assets/tilemaps/latest-atlas.txt");
TextureAtlas.TextureAtlasData atlas = new TextureAtlas.TextureAtlasData(f, f.parent(), false);
int png_grid_width = 24;
Tile[] mapTileIds = new Tile[png_grid_width * Constants.tilePixelWidth + 1];
for (Region r : atlas.getRegions()) {
int x = r.left / r.width;
int y = r.top / r.height;
int i = x + (y * png_grid_width) + 1;
mapTileIds[i] = ts.getTileByName(r.name);
}
for (int y = 0; y < map.getHeight(); y++) {
for (int x = 0; x < map.getWidth(); x++) {
StaticTiledMapTile tr = (StaticTiledMapTile) ml.getCell(x, map.getWidth() - 1 - y).getTile();
Tile tile = mapTileIds[tr.getId()];
if (tile == null) {
tile = ts.getTileByIndex(127);
}
tiles[x + (y * map.getWidth())] = tile;
}
}
}
MapLayer objectsLayer = tm.getLayers().get(map.getId() + "-people");
if (objectsLayer != null) {
Iterator<MapObject> iter = objectsLayer.getObjects().iterator();
while (iter.hasNext()) {
MapObject obj = iter.next();
Person p = new Person();
Conversation conv = new Conversation();
conv.setName(obj.getName());
conv.setMap(id);
p.setConversation(conv);
people.add(p);
Iterator<String> keys = obj.getProperties().getKeys();
while (keys.hasNext()) {
String key = keys.next();
String value = obj.getProperties().get(key).toString();
if (key.equals("tileType")) {
Tile tile = ts.getTileByName(value);
p.setTileIndex(tile.getIndex());
p.setTile(tile);
} else if (key.equals("startX")) {
p.setStart_x(new Float(value).intValue());
} else if (key.equals("startY")) {
p.setStart_y(new Float(value).intValue());
} else if (key.equals("role")) {
PersonRole role = new PersonRole();
role.setInventoryType(InventoryType.valueOf(value));
p.setRole(role);
} else if (key.equals("movement")) {
p.setMovement(ObjectMovementBehavior.valueOf(value));
} else if (key.startsWith("description")) {
conv.setDescription(value);
conv.getTopics().add(conv.new Topic(standardQuery[2], value, null, null, null));
} else if (key.startsWith("topic")) {
String[] splits = value.split(",");
if (splits.length == 5) {
conv.getTopics().add(conv.new Topic(splits[0], splits[1], splits[2], splits[3], splits[4]));
} else {
conv.getTopics().add(conv.new Topic(splits[0], splits[1], null, null, null));
}
}
}
}
}
map.getCity().setPeople(people);
map.setTiles(tiles);
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project ultimate-java by pantinor.
the class TextureUnpacker method splitAtlas.
/**
* Splits an atlas into seperate image and ninepatch files.
*/
public void splitAtlas(TextureAtlasData atlas, String outputDir) {
// create the output directory if it did not exist yet
File outputDirFile = new File(outputDir);
if (!outputDirFile.exists()) {
outputDirFile.mkdirs();
System.out.println(String.format("Creating directory: %s", outputDirFile.getPath()));
}
for (Page page : atlas.getPages()) {
// load the image file belonging to this page as a Buffered Image
BufferedImage img = null;
try {
img = ImageIO.read(page.textureFile.file());
} catch (IOException e) {
printExceptionAndExit(e);
}
for (Region region : atlas.getRegions()) {
System.out.println(String.format("Processing image for %s: x[%s] y[%s] w[%s] h[%s], rotate[%s]", region.name, region.left, region.top, region.width, region.height, region.rotate));
// check if the page this region is in is currently loaded in a Buffered Image
if (region.page == page) {
BufferedImage splitImage = null;
String extension = null;
// check if the region is a ninepatch or a normal image and delegate accordingly
if (region.splits == null) {
splitImage = extractImage(img, region, outputDirFile, 0);
extension = OUTPUT_TYPE;
} else {
splitImage = extractNinePatch(img, region, outputDirFile);
extension = String.format("9.%s", OUTPUT_TYPE);
}
// check if the parent directories of this image file exist and create them if not
File imgOutput = new File(outputDirFile, String.format("%s.%s", region.index == -1 ? region.name : region.name + "_" + region.index, extension));
File imgDir = imgOutput.getParentFile();
if (!imgDir.exists()) {
System.out.println(String.format("Creating directory: %s", imgDir.getPath()));
imgDir.mkdirs();
}
// save the image
try {
ImageIO.write(splitImage, OUTPUT_TYPE, imgOutput);
} catch (IOException e) {
printExceptionAndExit(e);
}
}
}
}
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project ultimate-java by pantinor.
the class AndiusMapTmxConvert method create.
@Override
public void create() {
try {
File file2 = new File("assets/xml/tileset-base.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(TileSet.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
TileSet ts = (TileSet) jaxbUnmarshaller.unmarshal(file2);
ts.setMaps();
File file3 = new File("assets/xml/maps.xml");
jaxbContext = JAXBContext.newInstance(MapSet.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MapSet ms = (MapSet) jaxbUnmarshaller.unmarshal(file3);
ms.init(ts);
// load the atlas and determine the tile indexes per tilemap position
FileHandle f = new FileHandle("assets/tilemaps/latest-atlas.txt");
TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
int png_grid_width = 24;
Tile[] mapTileIds = new Tile[png_grid_width * Constants.tilePixelWidth + 1];
for (Region r : atlas.getRegions()) {
int x = r.left / r.width;
int y = r.top / r.height;
int i = x + (y * png_grid_width) + 1;
mapTileIds[i] = ts.getTileByName(r.name);
}
BaseMap world = Constants.Maps.WORLD.getMap();
Utils.setMapTiles(world, ts);
Tile[] tiles = world.getTiles();
String tmxFName = "tmx/andius/world.tmx";
WorldFormatter c = new WorldFormatter(world.getWidth(), world.getHeight(), tiles, world.getPortals(), world.getMoongates());
FileUtils.writeStringToFile(new File(tmxFName), c.toString());
System.out.printf("Wrote: %s\n", tmxFName);
for (BaseMap map : ms.getMaps()) {
if (!map.getFname().endsWith("ult")) {
continue;
}
FileInputStream is = new FileInputStream("assets/data/" + map.getFname());
byte[] bytes = IOUtils.toByteArray(is);
tiles = new Tile[map.getWidth() * map.getHeight()];
int pos = 0;
for (int y = 0; y < map.getHeight(); y++) {
for (int x = 0; x < map.getWidth(); x++) {
// convert a byte to an unsigned int value
int index = bytes[pos] & 0xff;
pos++;
Tile tile = ts.getTileByIndex(index);
if (tile == null) {
System.out.println("Tile index cannot be found: " + index + " using index 129 for black space.");
tile = ts.getTileByIndex(129);
}
tiles[x + y * map.getWidth()] = tile;
}
}
tmxFName = String.format("tmx/andius/%s.tmx", map.getCity().getName().replace(" ", "").toLowerCase());
if (map.getFname().equals("lcb_2.ult")) {
tmxFName = "tmx/andius/britannia2.tmx";
}
Formatter fmtter = new Formatter(map.getWidth(), map.getHeight(), tiles);
FileUtils.writeStringToFile(new File(tmxFName), fmtter.toString());
System.out.printf("Wrote: %s\n", tmxFName);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DONE");
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project ultimate-java by pantinor.
the class WorldMapTmxConvert method create.
@Override
public void create() {
try {
File file2 = new File("assets/xml/tileset-base.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(TileSet.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
TileSet ts = (TileSet) jaxbUnmarshaller.unmarshal(file2);
ts.setMaps();
File file3 = new File("assets/xml/maps.xml");
jaxbContext = JAXBContext.newInstance(MapSet.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MapSet ms = (MapSet) jaxbUnmarshaller.unmarshal(file3);
ms.init(ts);
BaseMap map = Maps.WORLD.getMap();
Utils.setMapTiles(map, ts);
Tile[] tiles = map.getTiles();
// load the atlas and determine the tile indexes per tilemap position
FileHandle f = new FileHandle("assets/tilemaps/latest-atlas.txt");
TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
int png_grid_width = 24;
Tile[] mapTileIds = new Tile[png_grid_width * Constants.tilePixelWidth + 1];
for (Region r : atlas.getRegions()) {
int x = r.left / r.width;
int y = r.top / r.height;
int i = x + (y * png_grid_width) + 1;
mapTileIds[i] = ts.getTileByName(r.name);
}
// map layer
StringBuilder data = new StringBuilder();
int count = 1;
int total = 1;
for (int i = 0; i < tiles.length; i++) {
Tile t = tiles[i];
data.append(findTileId(mapTileIds, t.getName())).append(",");
count++;
total++;
if (count > 256) {
data.append("\n");
count = 1;
}
}
String dl = data.toString();
dl = dl.substring(0, dl.length() - 2);
// portal layer
List<Portal> portals = map.getPortals();
StringBuilder portalBuffer = new StringBuilder();
// set map tile id per dest map type
for (Portal p : portals) {
BaseMap destMap = Maps.get(p.getDestmapid()).getMap();
p.setName(Constants.Maps.get(p.getDestmapid()).toString());
String ttype = destMap.getCity() == null ? destMap.getType().toString() : destMap.getCity().getType();
p.setMapTileId(findTileId(mapTileIds, ttype));
}
if (portals != null) {
for (int y = 0; y < map.getHeight(); y++) {
for (int x = 0; x < map.getWidth(); x++) {
Portal p = findPortalAtCoords(portals, x, y);
if (p == null) {
portalBuffer.append("0,");
} else {
portalBuffer.append(p.getMapTileId() + ",");
}
}
portalBuffer.append("\n");
}
}
String pl = portalBuffer.toString();
pl = pl.substring(0, pl.length() - 2);
// moongate layer
List<Moongate> moongates = map.getMoongates();
StringBuilder moongateBuffer = new StringBuilder();
// set map tile id per dest map type
for (Moongate m : moongates) {
m.setMapTileId(findTileId(mapTileIds, "moongate"));
}
if (moongates != null) {
for (int y = 0; y < map.getHeight(); y++) {
for (int x = 0; x < map.getWidth(); x++) {
Moongate p = findMoongateAtCoords(moongates, x, y);
if (p == null) {
moongateBuffer.append("0,");
} else {
moongateBuffer.append(p.getMapTileId()).append(",");
}
}
moongateBuffer.append("\n");
}
}
String ml = moongateBuffer.toString();
ml = ml.substring(0, ml.length() - 2);
Formatter c = new Formatter(map.getFname(), "latest.png", map.getWidth(), map.getHeight(), Constants.tilePixelWidth, Constants.tilePixelWidth, dl, pl, ml, portals, moongates, map.getLabels());
FileUtils.writeStringToFile(new File("tmx/map_" + map.getId() + "_World.tmx"), c.toString());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DONE");
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project ultimate-java by pantinor.
the class StaticGeneratedDungeonScreen method init.
public void init() {
assets = new AssetManager();
assets.load("assets/graphics/dirt.png", Texture.class);
assets.load("assets/graphics/map.png", Texture.class);
assets.load("assets/graphics/Stone_Masonry.jpg", Texture.class);
assets.load("assets/graphics/door.png", Texture.class);
assets.load("assets/graphics/mortar.png", Texture.class);
assets.load("assets/graphics/rock.png", Texture.class);
assets.update(2000);
assets.get("assets/graphics/rock.png", Texture.class).setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
assets.get("assets/graphics/door.png", Texture.class).setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
assets.get("assets/graphics/mortar.png", Texture.class).setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
assets.get("assets/graphics/dirt.png", Texture.class).setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
ModelLoader<?> gloader = new G3dModelLoader(new UBJsonReader());
fountainModel = gloader.loadModel(Gdx.files.internal("assets/graphics/fountain2.g3db"));
ladderModel = gloader.loadModel(Gdx.files.internal("assets/graphics/ladder.g3db"));
chestModel = gloader.loadModel(Gdx.files.internal("assets/graphics/chest.g3db"));
orbModel = gloader.loadModel(Gdx.files.internal("assets/graphics/orb.g3db"));
altarModel = gloader.loadModel(Gdx.files.internal("assets/graphics/altar.g3db"));
rocksModel = gloader.loadModel(Gdx.files.internal("assets/graphics/rocks.g3db"));
campfireModel = gloader.loadModel(Gdx.files.internal("assets/graphics/campfire.g3db"));
font = new BitmapFont();
font.setColor(Color.WHITE);
fixedLight = new PointLight().set(1f, 0.8f, 0.6f, 4f, 4f, 4f, 5f);
modelBatch = new ModelBatch();
batch = new SpriteBatch();
camera = new PerspectiveCamera(67, Ultima4.MAP_WIDTH, Ultima4.MAP_HEIGHT);
camera.near = 0.1f;
camera.far = 1000f;
decalBatch = new DecalBatch(new CameraGroupStrategy(camera));
// inputController = new CameraInputController(camera);
// inputController.rotateLeftKey = inputController.rotateRightKey = inputController.forwardKey = inputController.backwardKey = 0;
// inputController.translateUnits = 30f;
ModelBuilder builder = new ModelBuilder();
Model fm = builder.createBox(1, 1, 1, new Material(TextureAttribute.createDiffuse(assets.get("assets/graphics/rock.png", Texture.class))), Usage.Position | Usage.TextureCoordinates | Usage.Normal);
Model cm = builder.createBox(1, 1, 1, new Material(TextureAttribute.createDiffuse(assets.get("assets/graphics/dirt.png", Texture.class))), Usage.Position | Usage.TextureCoordinates | Usage.Normal);
try {
int TILE_SIZE = 16;
FileHandle f = new FileHandle("assets/tilemaps/tiles-vga-atlas.txt");
TextureAtlasData a = new TextureAtlasData(f, f.parent(), false);
mapTileIds = new String[a.getRegions().size + 1];
for (Region r : a.getRegions()) {
int x = r.left / r.width;
int y = r.top / r.height;
int i = y * TILE_SIZE + x + 1;
mapTileIds[i] = r.name;
}
TiledMap map = new TmxMapLoader().load("assets/tilemaps/delveOfSorrows.tmx");
Iterator<MapLayer> iter = map.getLayers().iterator();
int level = 0;
while (iter.hasNext()) {
environment[level] = new Environment();
environment[level].set(new ColorAttribute(ColorAttribute.Ambient, 0.5f, 0.5f, 0.5f, 1f));
environment[level].add(fixedLight);
layers[level] = (TiledMapTileLayer) iter.next();
for (int y = 0; y < DUNGEON_MAP; y++) {
for (int x = 0; x < DUNGEON_MAP; x++) {
String val = mapTileIds[layers[level].getCell(x, DUNGEON_MAP - y - 1).getTile().getId()];
DungeonTile tile = DungeonTile.getTileByName(val);
if (tile == null) {
CreatureType ct = CreatureType.get(val);
if (ct != null) {
Creature creature = Ultima4.creatures.getInstance(ct, Ultima4.standardAtlas);
creature.currentX = x;
creature.currentY = y;
creature.currentLevel = level;
creature.getDecal().setPosition(creature.currentX + .5f, .3f, creature.currentY + .5f);
dngMap.getMap().addCreature(creature);
} else {
System.err.println(val);
}
dungeonTiles[level][x][y] = DungeonTile.NOTHING;
} else if (tile == DungeonTile.WATER) {
Model w = builder.createBox(1, 1, 1, getMaterial(Color.BLUE, .9f), Usage.Position | Usage.Normal);
ModelInstance wi = new ModelInstance(w, x + .5f, -.5f, y + .5f);
DungeonTileModelInstance fin = new DungeonTileModelInstance(wi, DungeonTile.WATER, level);
modelInstances.add(fin);
dungeonTiles[level][x][y] = DungeonTile.NOTHING;
} else {
dungeonTiles[level][x][y] = tile;
addBlock(level, tile, x + .5f, .5f, y + .5f);
}
if (tile == null || tile != DungeonTile.WATER) {
DungeonTileModelInstance fin = new DungeonTileModelInstance(new ModelInstance(fm, new Vector3(x + .5f, -.5f, y + .5f)), DungeonTile.FLOOR, level);
modelInstances.add(fin);
}
DungeonTileModelInstance cin = new DungeonTileModelInstance(new ModelInstance(cm, new Vector3(x + .5f, 1.5f, y + .5f)), DungeonTile.FLOOR, level);
modelInstances.add(cin);
}
}
level++;
}
setStartPosition();
camera.position.set(currentPos);
camera.lookAt(currentPos.x + 1, currentPos.y, currentPos.z);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations