-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
3,604 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package minicraft.screen; | ||
|
||
import java.io.IOException; | ||
|
||
import minicraft.saveload.Load; | ||
|
||
public class BookData { | ||
|
||
public static final String about = "Modded by David.b and +Dillyg10+ until 1.8, then taken over by Chris J until 2.0.4, and then by afyber. Our goal is to expand Minicraft to be more fun and continuous.\nMinicraft was originally made by Markus Perrson for ludum dare 22 competition."; | ||
|
||
public static final String instructions = "With the default controls...\n\nMove your character with arrow keys or WSAD. Press C to attack and X to open the inventory, and to use items. Pickup furniture and torches with V. Select an item in the inventory to equip it.\n\nThe Goal: Defeat Cthulhu!"; | ||
|
||
public static final String antVenomBook = loadBook("antidous"); | ||
public static final String storylineGuide = loadBook("story_guide"); | ||
|
||
private static final String loadBook(String bookTitle) { | ||
String book; | ||
try { | ||
book = String.join("\n", Load.loadFile("/resources/"+bookTitle+".txt")); | ||
book = book.replaceAll("\\\\0", "\0"); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
book = ""; | ||
} | ||
|
||
return book; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package minicraft.screen; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
import minicraft.core.Game; | ||
import minicraft.core.io.InputHandler; | ||
import minicraft.core.io.Localization; | ||
import minicraft.gfx.Color; | ||
import minicraft.gfx.Font; | ||
import minicraft.gfx.Point; | ||
import minicraft.gfx.Screen; | ||
import minicraft.gfx.SpriteSheet; | ||
import minicraft.screen.entry.StringEntry; | ||
|
||
public class BookDisplay extends Display { | ||
|
||
// null characters "\0" denote page breaks. | ||
private static final String defaultBook = " \n \0"+"There is nothing of use here."+"\0 \0"+"Still nothing... :P"; | ||
|
||
private static final int spacing = 3; | ||
private static final int minX = 15, maxX = 15+8*32, minY = 8*5, maxY = 8*5+8*16; | ||
|
||
private String[][] lines; | ||
private int page; | ||
|
||
private final boolean hasTitle; | ||
private final boolean showPageCount; | ||
private final int pageOffset; | ||
|
||
public BookDisplay(String book) { this(book, false); } | ||
public BookDisplay(String book, boolean hasTitle) {// this(book, hasTitle, !hasTitle); } | ||
//public BookDisplay(String book, boolean hasTitle, boolean hideCountIfOnePage) { | ||
page = 0; | ||
if(book == null) { | ||
book = defaultBook; | ||
hasTitle = false; | ||
} | ||
book = Localization.getLocalized(book); | ||
this.hasTitle = hasTitle; | ||
|
||
ArrayList<String[]> pages = new ArrayList<>(); | ||
String[] splitContents = book.split("\0"); | ||
for(String content: splitContents) { | ||
String[] remainder = {content}; | ||
while(remainder[remainder.length-1].length() > 0) { | ||
remainder = Font.getLines(remainder[remainder.length-1], maxX-minX, maxY-minY, spacing, true); | ||
pages.add(Arrays.copyOf(remainder, remainder.length-1)); // removes the last element of remainder, which is the leftover. | ||
} | ||
} | ||
|
||
lines = pages.toArray(new String[pages.size()][]); | ||
|
||
showPageCount = hasTitle || lines.length != 1; | ||
pageOffset = showPageCount ? 1 : 0; | ||
|
||
Menu.Builder builder = new Menu.Builder(true, spacing, RelPos.CENTER); | ||
|
||
Menu pageCount = builder // the small rect for the title | ||
.setPositioning(new Point(Screen.w/2, 0), RelPos.BOTTOM) | ||
.setEntries(StringEntry.useLines(Color.BLACK, "Page", hasTitle?"Title":"1/"+lines.length)) | ||
.setSelection(1) | ||
.createMenu(); | ||
|
||
builder | ||
.setPositioning(new Point(Screen.w/2, pageCount.getBounds().getBottom() + spacing), RelPos.BOTTOM) | ||
.setSize(maxX-minX + SpriteSheet.boxWidth*2, maxY-minY + SpriteSheet.boxWidth*2) | ||
.setShouldRender(false); | ||
|
||
menus = new Menu[lines.length+pageOffset]; | ||
if(showPageCount) menus[0] = pageCount; | ||
for(int i = 0; i < lines.length; i++) { | ||
menus[i+pageOffset] = builder.setEntries(StringEntry.useLines(Color.WHITE, lines[i])).createMenu(); | ||
} | ||
|
||
menus[page+pageOffset].shouldRender = true; | ||
} | ||
|
||
private void turnPage(int dir) { | ||
if(page+dir >= 0 && page+dir < lines.length) { | ||
menus[page+pageOffset].shouldRender = false; | ||
page += dir; | ||
if(showPageCount) menus[0].updateSelectedEntry(new StringEntry(page==0 && hasTitle?"Title":(page+1)+"/"+lines.length, Color.BLACK)); | ||
menus[page+pageOffset].shouldRender = true; | ||
} | ||
} | ||
|
||
@Override | ||
public void tick(InputHandler input) { | ||
if (input.getKey("menu").clicked || input.getKey("exit").clicked) | ||
Game.exitMenu(); // this is what closes the book; TODO if books were editable, I would probably remake the book here with the edited pages. | ||
if (input.getKey("cursor-left").clicked) turnPage(-1); // this is what turns the page back | ||
if (input.getKey("cursor-right").clicked) turnPage(1); // this is what turns the page forward | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package minicraft.screen; | ||
|
||
import minicraft.core.Game; | ||
import minicraft.core.io.InputHandler; | ||
import minicraft.entity.ItemHolder; | ||
import minicraft.entity.furniture.Chest; | ||
import minicraft.entity.mob.Player; | ||
import minicraft.gfx.Screen; | ||
import minicraft.item.Inventory; | ||
import minicraft.item.Item; | ||
import minicraft.item.StackableItem; | ||
|
||
public class ContainerDisplay extends Display { | ||
|
||
private static final int padding = 10; | ||
|
||
private Player player; | ||
private Chest chest; | ||
|
||
public ContainerDisplay(Player player, Chest chest) { | ||
super(new InventoryMenu(chest, chest.getInventory(), chest.name), new InventoryMenu(player, player.getInventory(), "Inventory")); | ||
//pInv = player.getInventory(); | ||
//cInv = chest.getInventory(); | ||
this.player = player; | ||
this.chest = chest; | ||
|
||
menus[1].translate(menus[0].getBounds().getWidth() + padding, 0); | ||
|
||
if(menus[0].getNumOptions() == 0) onSelectionChange(0, 1); | ||
} | ||
|
||
@Override | ||
protected void onSelectionChange(int oldSel, int newSel) { | ||
super.onSelectionChange(oldSel, newSel); | ||
if(oldSel == newSel) return; // this also serves as a protection against access to menus[0] when such may not exist. | ||
int shift = 0; | ||
if(newSel == 0) shift = padding - menus[0].getBounds().getLeft(); | ||
if(newSel == 1) shift = (Screen.w - padding) - menus[1].getBounds().getRight(); | ||
for(Menu m: menus) | ||
m.translate(shift, 0); | ||
} | ||
|
||
private int getOtherIdx() { return (selection+1) % 2; } | ||
|
||
@Override | ||
public void tick(InputHandler input) { | ||
super.tick(input); | ||
|
||
if(input.getKey("menu").clicked || chest.isRemoved()) { | ||
Game.setMenu(null); | ||
return; | ||
} | ||
|
||
Menu curMenu = menus[selection]; | ||
int otherIdx = getOtherIdx(); | ||
|
||
if(curMenu.getNumOptions() > 0 && (input.getKey("attack").clicked || input.getKey("drop-one").clicked)) { | ||
// switch inventories | ||
Inventory from, to; | ||
if(selection == 0) { | ||
from = chest.getInventory(); | ||
to = player.getInventory(); | ||
} else { | ||
from = player.getInventory(); | ||
to = chest.getInventory(); | ||
} | ||
|
||
int toSel = menus[otherIdx].getSelection(); | ||
int fromSel = curMenu.getSelection(); | ||
|
||
if(Game.isValidClient() && from == chest.getInventory()) { | ||
// just send, take no actual action | ||
Game.client.removeFromChest(chest, fromSel, toSel, input.getKey("attack").clicked); | ||
return; | ||
} | ||
|
||
Item fromItem = from.get(fromSel); | ||
|
||
boolean transferAll = input.getKey("attack").clicked || !(fromItem instanceof StackableItem) || ((StackableItem)fromItem).count == 1; | ||
|
||
Item toItem = fromItem.clone(); | ||
|
||
if(!transferAll) { | ||
((StackableItem)fromItem).count--; // this is known to be valid. | ||
((StackableItem)toItem).count = 1; | ||
// items are setup for sending. | ||
} | ||
else { // transfer whole item/stack. | ||
if(! (Game.isMode("creative") && from == player.getInventory()) ) | ||
from.remove(fromSel); // remove it | ||
} | ||
|
||
if(!Game.isValidClient()) { | ||
to.add(toSel, toItem); | ||
update(); | ||
} else if(to == chest.getInventory()) { | ||
// is online client, and from == player | ||
Game.client.addToChest(chest, toSel, toItem); | ||
} | ||
} | ||
} | ||
|
||
public void onInvUpdate(ItemHolder holder) { | ||
if(holder == player || holder == chest) | ||
update(); | ||
} | ||
|
||
private void update() { | ||
menus[0] = new InventoryMenu((InventoryMenu) menus[0]); | ||
menus[1] = new InventoryMenu((InventoryMenu) menus[1]); | ||
menus[1].translate(menus[0].getBounds().getWidth() + padding, 0); | ||
onSelectionChange(0, selection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package minicraft.screen; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import minicraft.core.Game; | ||
import minicraft.core.io.InputHandler; | ||
import minicraft.entity.mob.Player; | ||
import minicraft.gfx.Point; | ||
import minicraft.gfx.SpriteSheet; | ||
import minicraft.item.Item; | ||
import minicraft.item.Items; | ||
import minicraft.item.Recipe; | ||
import minicraft.screen.entry.ItemListing; | ||
|
||
public class CraftingDisplay extends Display { | ||
|
||
private Player player; | ||
private Recipe[] recipes; | ||
|
||
private RecipeMenu recipeMenu; | ||
private Menu.Builder itemCountMenu, costsMenu; | ||
|
||
private boolean isPersonalCrafter; | ||
|
||
public CraftingDisplay(List<Recipe> recipes, String title, Player player) { this(recipes, title, player, false); } | ||
public CraftingDisplay(List<Recipe> recipes, String title, Player player, boolean isPersonal) { | ||
for(Recipe recipe: recipes) | ||
recipe.checkCanCraft(player); | ||
|
||
this.isPersonalCrafter = isPersonal; | ||
|
||
if(!isPersonal) | ||
recipeMenu = new RecipeMenu(recipes, title, player); | ||
else | ||
recipeMenu = new RecipeMenu(recipes, title, player); | ||
|
||
this.player = player; | ||
this.recipes = recipes.toArray(new Recipe[recipes.size()]); | ||
|
||
itemCountMenu = new Menu.Builder(true, 0, RelPos.LEFT) | ||
.setTitle("Have:") | ||
.setTitlePos(RelPos.TOP_LEFT) | ||
.setPositioning(new Point(recipeMenu.getBounds().getRight()+SpriteSheet.boxWidth, recipeMenu.getBounds().getTop()), RelPos.BOTTOM_RIGHT); | ||
|
||
costsMenu = new Menu.Builder(true, 0, RelPos.LEFT) | ||
.setTitle("Cost:") | ||
.setTitlePos(RelPos.TOP_LEFT) | ||
.setPositioning(new Point(itemCountMenu.createMenu().getBounds().getLeft(), recipeMenu.getBounds().getBottom()), RelPos.TOP_RIGHT); | ||
|
||
menus = new Menu[] {recipeMenu, itemCountMenu.createMenu(), costsMenu.createMenu()}; | ||
|
||
refreshData(); | ||
} | ||
|
||
private void refreshData() { | ||
Menu prev = menus[2]; | ||
menus[2] = costsMenu | ||
.setEntries(getCurItemCosts()) | ||
.createMenu(); | ||
menus[2].setColors(prev); | ||
|
||
menus[1] = itemCountMenu | ||
.setEntries(new ItemListing(recipes[recipeMenu.getSelection()].getProduct(), String.valueOf(getCurItemCount()))) | ||
.createMenu(); | ||
menus[1].setColors(prev); | ||
} | ||
|
||
private int getCurItemCount() { | ||
return player.getInventory().count(recipes[recipeMenu.getSelection()].getProduct()); | ||
} | ||
|
||
private ItemListing[] getCurItemCosts() { | ||
ArrayList<ItemListing> costList = new ArrayList<>(); | ||
HashMap<String, Integer> costMap = recipes[recipeMenu.getSelection()].getCosts(); | ||
for(String itemName: costMap.keySet()) { | ||
Item cost = Items.get(itemName); | ||
costList.add(new ItemListing(cost, costMap.get(itemName)+"/"+player.getInventory().count(cost))); | ||
} | ||
|
||
return costList.toArray(new ItemListing[costList.size()]); | ||
} | ||
|
||
@Override | ||
public void tick(InputHandler input) { | ||
if(input.getKey("menu").clicked || (isPersonalCrafter && input.getKey("craft").clicked)) { | ||
Game.exitMenu(); | ||
return; | ||
} | ||
|
||
int prevSel = recipeMenu.getSelection(); | ||
super.tick(input); | ||
if(prevSel != recipeMenu.getSelection()) | ||
refreshData(); | ||
|
||
if((input.getKey("select").clicked || input.getKey("attack").clicked) && recipeMenu.getSelection() >= 0) { | ||
// check the selected recipe | ||
Recipe r = recipes[recipeMenu.getSelection()]; | ||
if(r.getCanCraft()) { | ||
r.craft(player); | ||
|
||
refreshData(); | ||
for(Recipe recipe: recipes) | ||
recipe.checkCanCraft(player); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.