mirror of
https://github.com/Dejvino/roadtrip
synced 2024-12-22 11:48:49 +00:00
Map: added base classes for consistent procedural generation. To be used for maps.
This commit is contained in:
parent
d2b6f01798
commit
7893ff412b
50
src/roadtrip/model/AbstractProceduralBlock.java
Normal file
50
src/roadtrip/model/AbstractProceduralBlock.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
39
src/roadtrip/model/ProceduralBlock.java
Normal file
39
src/roadtrip/model/ProceduralBlock.java
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user