Download the .jar and it to your project
import com.pengu.pengulu.*;
public class TestRun extends Game {
public static void main(String[] args) {
new TestRun().go();
}
void go() {
start(); // 1
}
}- This
starts the game.
Nodes are at the heart of Pengulu. Each node has connections to other nodes and choices to do things within the node.
The superconstructor takes three arguments: connections, ids of other nodes, choices, and the id of the node itself.
Make four new files: Plains.java, Forest.java, Mountains.java, and Cave.java.
import com.pengu.pengulu.Node;
public class Plains extends Node {
public Plains() {
super(new String[] {"forest", "mountains"}, new String[] {"forest", "mountains"}, "plains");
}
}import com.pengu.pengulu.Node;
public class Forest extends Node {
public Forest() {
super(new String[] {"plains", "cave"}, new String[] {"plains", "cave"}, "forest");
}
}import com.pengu.pengulu.Node;
public class Mountains extends Node {
public Mountains() {
super(new String[] {"plains"}, new String[] {"plains"}, "mountains");
}
}import com.pengu.pengulu.*;
public class Cave extends Node {
public Cave() {
super(new String[] {"forest"}, new String[] {"forest"}, "cave");
}
}First, make sure all the nodes know the game they are a part of.
Add this to the go method in TestRun.java:
Node.setGame(this);Then we add all our nodes:
addNode(new Forest());
addNode(new Plains());
addNode(new Cave());
addNode(new Mountains());Finally we set the starting node:
setCurrentNode(Game.getNodeById("forest"));TestRun.java should now look like this:
import com.pengu.pengulu.*;
public class TestRun extends Game {
public static void main(String[] args) {
new TestRun().go();
}
void go() {
Node.setGame(this);
addNode(new Forest());
addNode(new Plains());
addNode(new Cave());
addNode(new Mountains());
setCurrentNode(Game.getNodeById("forest"));
start();
}
}The ItemTemplate superconstructor takes two arguments: count, how much of the item there is, and id.
Create three new files: Log.java, Coal.java, and Cobblestone.java.
import com.pengu.pengulu.ItemTemplate;
public class Log extends ItemTemplate {
public Log(int count) {
super(count, "log");
}
}import com.pengu.pengulu.*;
public class Coal extends ItemTemplate {
public Coal(int count) {
super(count, "coal");
}
}import com.pengu.pengulu.*;
public class Cobblestone extends ItemTemplate {
public Cobblestone(int count) {
super(count, "cobblestone");
}
}Add this to the go methoc in TestRun.java:
InventoryManager.addItem(new Log(0));
InventoryManager.addItem(new Cobblestone(0));
InventoryManager.addItem(new Coal(0));TestRun.java should now look like this:
import com.pengu.pengulu.*;
public class TestRun extends Game {
public static void main(String[] args) {
new TestRun().go();
}
void go() {
Node.setGame(this);
addNode(new Forest());
addNode(new Plains());
addNode(new Cave());
addNode(new Mountains());
InventoryManager.addItem(new Log(0));
InventoryManager.addItem(new Cobblestone(0));
InventoryManager.addItem(new Coal(0));
setCurrentNode(Game.getNodeById("forest"));
start();
}
}Add an additional choice in the constructor of Forest.java:
public Forest() {
super(new String[] {"plains", "cave", "mine trees"}, new String[] {"plains", "cave"}, "forest");
}Modify Forest.java so it implements InputListener:
public class Forest extends Node implements InputListener {Add a respond method so the node responds to the "mine trees" choice:
@Override
public void respond(String choice) {
int choiceIndex = getChoiceIndex(choice); // 1
if (choiceIndex == 2) {
Game.displayln("how many?"); // 2
requestInput(); // 3
} else {
runNode(choiceIndex); // 4
}
}getChoiceIndex(choice)returns the index of the choice made.Game.displayln(message)displays amessageon the screen.requestInput()requests user input.runNode(choiceIndex)runs a node from theconnections
Add anonInputmethod so the node responds to the requested input:
@Override
public void onInput(String inputText) {
int treeCount = Integer.parseInt(inputText);
if (treeCount > 5) {
Game.displayln("number of trees can't be greater than 5");
} else {
Game.displayln("mining " + treeCount + " trees...");
InventoryManager.incrementItem(new Log(treeCount)); // 1
}
runAgain(); // 2
}InventoryManager.incrementItem(item)orInventoryManager.incrementItem(item, amount)increments the quantity of theitemin the inventory byamount.runAgain()runs the node again.
The finalForest.javashould look like this:
import com.pengu.pengulu.*;
public class Forest extends Node implements InputListener {
public Forest() {
super(new String[] {"plains", "cave", "mine trees"}, new String[] {"plains", "cave"}, "forest");
}
@Override
public void respond(String choice) {
int choiceIndex = getChoiceIndex(choice);
if (choiceIndex == 2) {
Game.displayln("how many?");
requestInput();
} else {
runNode(choiceIndex);
}
}
@Override
public void onInput(String inputText) {
int treeCount = Integer.parseInt(inputText);
if (treeCount > 5) {
Game.displayln("number of trees can't be greater than 5");
} else {
Game.displayln("mining " + treeCount + " trees...");
InventoryManager.incrementItem(new Log(treeCount));
}
runAgain();
}
}Add two more choices in Cave.java's constructor:
public Cave() {
super(new String[] {"forest", "mine stone", "mine coal ore"}, new String[] {"forest"}, "cave");
}Add an instance of variable that determines which item to mine:
private String itemToMine;Modify Cave.java so it implements InputListener:
public class Cave extends Node implements InputListener {Add two methods, respond and onInput:
@Override
public void respond(String choice) {
int choiceIndex = getChoiceIndex(choice);
if (choiceIndex == 0) {
runNode(choiceIndex);
} else {
if (choiceIndex == 1) {
itemToMine = "cobblestone";
}
if (choiceIndex == 2) {
itemToMine = "coal";
}
Game.displayln("how many?");
requestInput();
}
}
@Override
public void onInput(String inputText) {
int itemCount = Integer.parseInt(inputText);
if (itemCount > 5) {
Game.displayln("number of items can't be greater than 5");
} else {
Game.displayln("mining " + itemCount + " " + itemToMine + "...");
InventoryManager.incrementItem(InventoryManager.getItemById(itemToMine), itemCount);
}
runAgain();
}The final Cave.java should like this:
import com.pengu.pengulu.*;
public class Cave extends Node implements InputListener {
private String itemToMine;
public Cave() {
super(new String[] {"forest", "mine stone", "mine coal ore"}, new String[] {"forest"}, "cave");
}
@Override
public void respond(String choice) {
int choiceIndex = getChoiceIndex(choice);
if (choiceIndex == 0) {
runNode(choiceIndex);
} else {
if (choiceIndex == 1) {
itemToMine = "cobblestone";
}
if (choiceIndex == 2) {
itemToMine = "coal";
}
Game.displayln("how many?");
requestInput();
}
}
@Override
public void onInput(String inputText) {
int itemCount = Integer.parseInt(inputText);
if (itemCount > 5) {
Game.displayln("number of items can't be greater than 5");
} else {
Game.displayln("mining " + itemCount + " " + itemToMine + "...");
InventoryManager.incrementItem(InventoryManager.getItemById(itemToMine), itemCount);
}
runAgain();
}
}Displaying the inventory should be a choice no matter which node you are in.
Set "display inventory" as a universal choice in the go method of TestRun.java:
Node.setUniversalChoices(new String[] {"display inventory"});Modify TestRun.java so it implements UniversalChoiceListener:
public class TestRun extends Game implements UniversalChoiceListener {Add a respond method:
@Override
public void respond(String choice) {
InventoryManager.display();
}The final TestRun.java should look like this:
import com.pengu.pengulu.*;
public class TestRun extends Game implements UniversalChoiceListener {
public static void main(String[] args) {
new TestRun().go();
}
void go() {
Node.setGame(this);
Node.setUniversalChoices(new String[] {"display inventory"});
addNode(new Forest());
addNode(new Plains());
addNode(new Cave());
addNode(new Mountains());
InventoryManager.addItem(new Log(0));
InventoryManager.addItem(new Cobblestone(0));
InventoryManager.addItem(new Coal(0));
setCurrentNode(Game.getNodeById("forest"));
start();
}
@Override
public void respond(String choice) {
InventoryManager.display();
}
}