1
0
mirror of https://github.com/Dejvino/roadtrip synced 2024-11-13 04:27:30 +00:00

Map: added base classes for consistent procedural generation. To be used for maps.

This commit is contained in:
Dejvino 2017-01-21 16:10:04 +01:00
parent d2b6f01798
commit 7893ff412b
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package roadtrip.model;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;
/**
* Created by dejvino on 21.01.2017.
*/
public abstract class AbstractProceduralBlock implements ProceduralBlock
{
private long seed;
public AbstractProceduralBlock(long seed)
{
this.seed = seed;
}
@Override
public long getBlockSeed()
{
return seed;
}
@Override
public Random getBlockRandom()
{
return new Random(seed);
}
@Override
public long getSubBlockSeed(String subBlockKey)
{
return (String.valueOf(seed) + "::" + subBlockKey).hashCode();
}
@Override
public <T extends ProceduralBlock> T getSubBlock(String subBlockKey, Class<T> subBlockClass)
{
if (subBlockClass == null) throw new NullPointerException("subBlockClass");
try {
Constructor<T> constructor = subBlockClass.getConstructor(Long.class);
return constructor.newInstance(getSubBlockSeed(subBlockKey));
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Class " + subBlockClass + " does not have the default constructor with a single 'long' parameter.", e);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new IllegalArgumentException("Unable to instantiate sub-block.", e);
}
}
}

View File

@ -0,0 +1,39 @@
package roadtrip.model;
import java.util.Random;
/**
* Created by dejvino on 21.01.2017.
*/
public interface ProceduralBlock
{
/**
* The main PRNG seed defining this block's content.
* @return Seed value.
*/
long getBlockSeed();
/**
* Random generator initialized with the block's seed.
*
* @return fresh PRNG.
*/
Random getBlockRandom();
/**
* Provides a seed to be used with a sub-block with a given key (identifier).
*
* @param subBlockKey
* @return Sub-block seed.
*/
long getSubBlockSeed(String subBlockKey);
/**
* Provides a sub-block of the given class. The sub-block's seed is based on this block's seed.
*
* @param subBlockClass
* @param <T>
* @return Newly added sub-block.
*/
<T extends ProceduralBlock> T getSubBlock(String subBlockKey, Class<T> subBlockClass);
}