All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mmb.engine.json.Save Maven / Gradle / Ivy

Go to download

Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world. THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<

There is a newer version: 0.6
Show newest version
/**
 * 
 */
package mmb.engine.json;

import java.awt.Color;
import java.util.function.Function;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

import mmb.NN;
import monniasza.collects.grid.FixedGrid;
import monniasza.collects.grid.Grid;

/**
 * @author oskar
 *
 */
public class Save {
	private Save() {}
	@NN public static  FixedGrid loadGrid(Function itemLoader, ArrayNode array){
		int width = array.get(0).asInt();
		int height = array.get(1).asInt();
		FixedGrid grid = new FixedGrid<>(width, height);
		int i = 2;
		for(int y = 0; y < height; y++) {
			for(int x = 0; x < width; x++) {
				JsonNode idata = array.get(i);
				grid.set(x, y, itemLoader.apply(idata));
				i++;
			}
		}
		return grid;
	}
	@NN public static  ArrayNode saveGrid(Function itemSaver, Grid grid) {
		ArrayNode array = JsonTool.newArrayNode();
		array.add(grid.width());
		array.add(grid.height());
		for(T item: grid) {
			JsonNode save = itemSaver.apply(item);
			array.add(save);
		}
		return array;
	}
	
	@NN public static ArrayNode saveColor(Color c) {
		ArrayNode result = JsonTool.newArrayNode();
		result.add(c.getRed());
		result.add(c.getGreen());
		result.add(c.getBlue());
		result.add(c.getAlpha());
		return result;
	}
	@NN public static Color loadColor(JsonNode node) {
		int r = node.get(0).asInt(0);
		int g = node.get(1).asInt(0);
		int b = node.get(2).asInt(0);
		int a = node.get(3).asInt(255);
		return new Color(r, g, b, a);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy