Initial world system with tile rendering & ionization support.

This commit is contained in:
Ondřej Hruška
2014-04-20 03:45:31 +02:00
parent 65ad97994b
commit 8872fd3f5f
45 changed files with 1209 additions and 136 deletions
@@ -725,6 +725,69 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
};
}
/**
* Round coords down
*
* @return result
*/
public Rect floor()
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().floor();
}
@Override
public Vect origin()
{
return t.origin().floor();
}
};
}
/**
* Round coords up
*
* @return result
*/
public Rect ceil()
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().ceil();
}
@Override
public Vect origin()
{
return t.origin().ceil();
}
};
}
public Num x()
@@ -985,4 +1048,5 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
// overflow || intersect
return ((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry));
}
}
@@ -36,6 +36,8 @@ public class RectConst extends Rect {
private RectConst v_edge_t;
private RectConst v_edge_b;
private RectDigest digest;
private RectConst v_floor;
private RectConst v_ceil;
/**
@@ -152,6 +154,20 @@ public class RectConst extends Rect {
}
@Override
public RectConst floor()
{
return (v_floor != null) ? v_floor : (v_floor = Rect.make(pos.floor(), size.floor()));
}
@Override
public RectConst ceil()
{
return (v_ceil != null) ? v_ceil : (v_ceil = Rect.make(pos.ceil(), size.ceil()));
}
@Override
public NumConst x()
{
@@ -0,0 +1,36 @@
package mightypork.util.error;
import java.io.IOException;
/**
* To be used when a data could not be read successfully.
*
* @author MightyPork
*/
public class CorruptedDataException extends IOException {
public CorruptedDataException()
{
super();
}
public CorruptedDataException(String message, Throwable cause)
{
super(message, cause);
}
public CorruptedDataException(String message)
{
super(message);
}
public CorruptedDataException(Throwable cause)
{
super(cause);
}
}
+36 -20
View File
@@ -6,6 +6,7 @@ import java.nio.ByteBuffer;
import java.util.*;
import java.util.Map.Entry;
import mightypork.util.error.CorruptedDataException;
import mightypork.util.logging.Log;
@@ -83,7 +84,7 @@ public class Ion {
static final short DATA_LIST = 81;
/** Ionizables<Mark, Class> */
private static Map<Short, Class<?>> customIonizables = new HashMap<>();
private static Map<Short, Class<? extends Ionizable>> customIonizables = new HashMap<>();
// buffers and helper arrays for storing to streams.
private static ByteBuffer bi = ByteBuffer.allocate(Integer.SIZE / 8);
@@ -122,7 +123,7 @@ public class Ion {
* except
* @param objClass class of the registered Ionizable
*/
public static void registerIonizable(int mark, Class<?> objClass)
public static void registerIonizable(int mark, Class<? extends Ionizable> objClass)
{
// negative marks are allowed.
if (mark > Short.MAX_VALUE) throw new IllegalArgumentException("Mark too high (max " + Short.MAX_VALUE + ").");
@@ -147,9 +148,9 @@ public class Ion {
*
* @param path file path
* @return the loaded object
* @throws IOException on failure
* @throws CorruptedDataException
*/
public static Object fromFile(String path) throws IOException
public static Object fromFile(String path) throws CorruptedDataException
{
return fromFile(new File(path));
}
@@ -160,9 +161,9 @@ public class Ion {
*
* @param file file
* @return the loaded object
* @throws IOException on failure
* @throws CorruptedDataException
*/
public static Object fromFile(File file) throws IOException
public static Object fromFile(File file) throws CorruptedDataException
{
try(InputStream in = new FileInputStream(file)) {
@@ -170,7 +171,7 @@ public class Ion {
return obj;
} catch (final IOException e) {
throw new IOException("Error loading ION file.", e);
throw new CorruptedDataException("Error loading ION file.", e);
}
}
@@ -257,7 +258,7 @@ public class Ion {
loaded = ((Ionizable) clz.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new IOException("Cound not instantiate: " + Log.str(customIonizables.get(mark)), e);
throw new RuntimeException("Cound not instantiate: " + Log.str(customIonizables.get(mark)), e);
}
loaded.load(in);
@@ -370,14 +371,14 @@ public class Ion {
return Strings;
default:
throw new IOException("Invalid Ion mark: " + mark);
throw new CorruptedDataException("Invalid Ion mark: " + mark);
}
}
public static void expect(InputStream in, short mark) throws IOException
{
if (readMark(in) != mark) throw new IOException("Unexpected mark in ION stream.");
if (readMark(in) != mark) throw new CorruptedDataException("Unexpected mark in ION stream.");
}
@@ -403,7 +404,20 @@ public class Ion {
public static void writeObject(OutputStream out, Object obj) throws IOException
{
if (obj instanceof Ionizable) {
writeMark(out, ((Ionizable) obj).getIonMark());
short mark = ((Ionizable) obj).getIonMark();
Class<? extends Ionizable> clzRegistered = customIonizables.get(mark);
if (clzRegistered == null) {
throw new IOException("Ionizable object not registered: " + Log.str(obj.getClass()));
}
if (clzRegistered != obj.getClass()) {
throw new IOException("Registered class does not match actual one for " + Log.str(obj.getClass()));
}
writeMark(out, mark);
((Ionizable) obj).save(out);
return;
}
@@ -1172,12 +1186,12 @@ public class Ion {
if (mark == ENTRY) return true;
if (mark == END) return false;
throw new IOException("Unexpected mark encountered while reading sequence.");
throw new CorruptedDataException("Unexpected mark " + mark + " encountered in sequence, expected " + ENTRY + " or " + END);
}
/**
* Read a sequence of elements
* Read a sequence of elements into an ArrayList
*
* @param in input stream
* @return the collection
@@ -1185,12 +1199,12 @@ public class Ion {
*/
public static <T> Collection<T> readSequence(InputStream in) throws IOException
{
return readSequence(in, new LinkedList<T>());
return readSequence(in, new ArrayList<T>());
}
/**
* Load entries into a collection
* Load entries into a collection. The collection is cleaned first.
*
* @param in input stream
* @param filled collection to populate
@@ -1201,12 +1215,13 @@ public class Ion {
public static <T> Collection<T> readSequence(InputStream in, Collection<T> filled) throws IOException
{
try {
filled.clear();
while (hasNextEntry(in)) {
filled.add((T) readObject(in));
}
return filled;
} catch (final ClassCastException e) {
throw new IOException("Unexpected element type.");
throw new CorruptedDataException("Unexpected element type.", e);
}
}
@@ -1229,7 +1244,7 @@ public class Ion {
/**
* Read a map of elements
* Read element pairs into a HashMap
*
* @param in input stream
* @return the map
@@ -1237,12 +1252,12 @@ public class Ion {
*/
public static <K, V> Map<K, V> readMap(InputStream in) throws IOException
{
return readMap(in, new LinkedHashMap<K, V>());
return readMap(in, new HashMap<K, V>());
}
/**
* Load data into a map
* Load data into a map. The map is cleaned first.
*
* @param in input stream
* @param filled filled map
@@ -1253,6 +1268,7 @@ public class Ion {
public static <K, V> Map<K, V> readMap(InputStream in, Map<K, V> filled) throws IOException
{
try {
filled.clear();
while (hasNextEntry(in)) {
final K key = (K) readObject(in);
final V value = (V) readObject(in);
@@ -1261,7 +1277,7 @@ public class Ion {
}
return filled;
} catch (final ClassCastException e) {
throw new IOException("Unexpected element type.");
throw new CorruptedDataException("Unexpected element type.", e);
}
}
+9 -21
View File
@@ -1,18 +1,21 @@
package mightypork.util.files.ion;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import mightypork.util.files.ion.templates.IonizableHashMap;
/**
* <p>
* Data bundle.
* </p>
* <p>
* Storing data in a bundle guarantees that future versions will be compatible
* with the older format. Reading using default values ensures that you will get
* some value even if it was not saved in the file.
* </p>
*
* @author MightyPork
*/
public class IonBundle extends LinkedHashMap<String, Object> implements Ionizable {
public class IonBundle extends IonizableHashMap<String, Object> {
/**
* Get an object. If not found, fallback is returned.
@@ -51,19 +54,4 @@ public class IonBundle extends LinkedHashMap<String, Object> implements Ionizabl
return Ion.DATA_BUNDLE;
}
@Override
public void load(InputStream in) throws IOException
{
clear();
Ion.readMap(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeMap(out, this);
}
}
@@ -9,8 +9,17 @@ import java.lang.annotation.Target;
/**
* <p>
* Implicit constructor marked like this is intended to be solely used for ION
* de-serialization. This is a description annotation and has no other function.
* de-serialization.
* </p>
* <p>
* Constructors marked like this should create a functional instance with
* default values.
* </p>
* <p>
* This is a descriptive annotation and has no other function.
* </p>
*
* @author MightyPork
*/
@@ -0,0 +1,28 @@
package mightypork.util.files.ion.templates;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public abstract class IonizableArrayList<E> extends ArrayList<E> implements Ionizable {
@Override
public void load(InputStream in) throws IOException
{
Ion.readSequence(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeSequence(out, this);
}
}
@@ -0,0 +1,28 @@
package mightypork.util.files.ion.templates;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public abstract class IonizableHashMap<K, V> extends HashMap<K, V> implements Ionizable {
@Override
public void load(InputStream in) throws IOException
{
Ion.readMap(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeMap(out, this);
}
}
@@ -0,0 +1,28 @@
package mightypork.util.files.ion.templates;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public abstract class IonizableHashSet<E> extends HashSet<E> implements Ionizable {
@Override
public void load(InputStream in) throws IOException
{
Ion.readSequence(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeSequence(out, this);
}
}
@@ -0,0 +1,28 @@
package mightypork.util.files.ion.templates;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public abstract class IonizableLinkedHashMap<K, V> extends LinkedHashMap<K, V> implements Ionizable {
@Override
public void load(InputStream in) throws IOException
{
Ion.readMap(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeMap(out, this);
}
}
@@ -0,0 +1,28 @@
package mightypork.util.files.ion.templates;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public abstract class IonizableLinkedList<E> extends LinkedList<E> implements Ionizable {
@Override
public void load(InputStream in) throws IOException
{
Ion.readSequence(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeSequence(out, this);
}
}
@@ -0,0 +1,28 @@
package mightypork.util.files.ion.templates;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Stack;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public abstract class IonizableStack<E> extends Stack<E> implements Ionizable {
@Override
public void load(InputStream in) throws IOException
{
Ion.readSequence(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeSequence(out, this);
}
}
@@ -0,0 +1,28 @@
package mightypork.util.files.ion.templates;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TreeSet;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public abstract class IonizableTreeSet<E> extends TreeSet<E> implements Ionizable {
@Override
public void load(InputStream in) throws IOException
{
Ion.readSequence(in, this);
}
@Override
public void save(OutputStream out) throws IOException
{
Ion.writeSequence(out, this);
}
}
+2
View File
@@ -23,4 +23,6 @@ public interface RGB {
Color PINK = Color.fromHex(0xFF3FFC);
Color ORANGE = Color.fromHex(0xFC4800);
Color NONE = Color.NONE;
}