summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/elements/ChanceCaseElement.java
blob: 1edccd3e8e969f8e30edda259a008df32d302202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package bjc.rgens.parser.elements;

import bjc.rgens.parser.GenerationState;

/**
 * A case element that has a '1 in n' chance to generate something.
 * @author Ben Culkin
 *
 */
public class ChanceCaseElement extends CaseElement {
	/**
	 * The case element to generate.
	 */
	public final CaseElement elm;

	/**
	 * The 'rarity' of generating output.
	 */
	public int chance;

	/**
	 * Create a new chance case element.
	 * @param elm The element to generate.
	 * @param chance The 'n' in the '1 in n' chance to generate the element.
	 */
	public ChanceCaseElement(CaseElement elm, int chance) {
		super(elm.spacing);

		this.elm    = elm;
		this.chance = chance;
	}

	@Override
	public void generate(GenerationState state) {
		if(state.rnd.nextInt(chance) == 0) elm.generate(state);
	}
}