Pārlūkot izejas kodu

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

master
Dejvino pirms 7 gadiem
vecāks
revīzija
7893ff412b
2 mainītis faili ar 89 papildinājumiem un 0 dzēšanām
  1. +50
    -0
      src/roadtrip/model/AbstractProceduralBlock.java
  2. +39
    -0
      src/roadtrip/model/ProceduralBlock.java

+ 50
- 0
src/roadtrip/model/AbstractProceduralBlock.java Parādīt failu

@@ -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
- 0
src/roadtrip/model/ProceduralBlock.java Parādīt failu

@@ -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);
}

Notiek ielāde…
Atcelt
Saglabāt