Created a working crafting GUI

This commit is contained in:
josua 2020-08-05 12:45:00 +10:00
parent 505a09a4ff
commit 6e69fe2fcd
10 changed files with 171 additions and 59 deletions

View File

@ -116,6 +116,7 @@ public class Models
public static final ModelGui UI_TEMPERATURE = new ModelGui(Resources.ATLAS.get("/gui/temperature.png"), new Vec2d(0.75, 0.75));
public static final ModelGui UI_WATER = new ModelGui(Resources.ATLAS.get("/gui/water.png"), new Vec2d(0.75, 0.75));
public static final ModelGui UI_ITEM_HOVER = new ModelGui(Resources.ATLAS.get("/gui/pixel_black.png")).setOpacity(0.25);
public static final ModelGui UI_TEXT_BG = new ModelGui(Resources.ATLAS.get("/gui/pixel_white.png")).setColor(0.5f, 0.5f, 0.5f);
public static final ModelItem UI_SLOT_ARMOR_HELMET = new ModelItem(Resources.ATLAS.get("/gui/slot_armor_helmet.png"));
public static final ModelItem UI_SLOT_ARMOR_CHEST = new ModelItem(Resources.ATLAS.get("/gui/slot_armor_chest.png"));

View File

@ -4,10 +4,11 @@ import projectzombie.inventory.Inventory;
import projectzombie.inventory.Crafting;
import projectzombie.util.math.ItemStack;
public abstract class Recipe
public interface Recipe
{
public abstract ItemStack getResult();
public abstract boolean canCraft(Crafting tool);
public abstract boolean hasResourcesToCraft(Inventory inventory);
public abstract ItemStack craftResult(Inventory inventory);
public ItemStack getResult();
public boolean canCraft(Crafting tool);
public boolean hasResourcesToCraft(Inventory inventory);
public ItemStack craftResult(Inventory inventory);
public ItemStack[] getIngredients();
}

View File

@ -4,7 +4,7 @@ import projectzombie.inventory.Crafting;
import projectzombie.inventory.Inventory;
import projectzombie.util.math.ItemStack;
public class RecipeBasic extends Recipe
public class RecipeBasic implements Recipe
{
protected ItemStack[] stacks_required;
protected Crafting[] tools_required;
@ -55,4 +55,9 @@ public class RecipeBasic extends Recipe
return result.copy();
}
@Override
public ItemStack[] getIngredients() {
return stacks_required;
}
}

View File

@ -19,13 +19,10 @@ public class MenuInventoryBasic extends MenuInventory
{
private GUIContainerSlider slider;
private Inventory inventory;
private Crafting tool;
public MenuInventoryBasic(Menu parent, Crafting tool) {
super(parent);
this.tool = tool;
Recipe[] recipies = Recipies.getCraftableRecipies(tool);
inventory = Main.player.getInventory();
@ -39,7 +36,9 @@ public class MenuInventoryBasic extends MenuInventory
{
Recipe recipe = recipies[i];
GUIRecipeCard slot = new GUIRecipeCard(inventory, slider, recipe, tool, new Vec2d(0, i*1.5));
GUIRecipeCard slot = new GUIRecipeCard(inventory, slider, recipe, tool, new Vec2d(0,
Models.UI_SELECTION_BOX_BIG.getHeight() / 2 -
(i + 1) * Models.UI_LABEL_RECIPE.getHeight() * 36 / 34.0));
slider.add(slot);

View File

@ -5,6 +5,7 @@ import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.entity.EntityItem;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.text.Text;
import projectzombie.util.math.ItemStack;
@ -23,22 +24,58 @@ public class GUIItemHolder implements GUIComponent
@Override
public void render(Matrix4 matrix, Vec2d mousePos, boolean canHover)
{
matrix = Matrix4.translate(mousePos.x, mousePos.y, 0);
if(!holding.isEmpty())
{
Model model = holding.item.getModel(holding.meta).getGuiModel();
matrix = Matrix4.multiply(Matrix4.translate(mousePos.x - 0.425, mousePos.y - 0.425, 0), matrix);
Matrix4 holding_matrix = Matrix4.multiply(matrix, Matrix4.translate(-0.425, -0.425, 0));
model.setModel(matrix);
Model model = holding.item.getModel(holding.meta).getGuiModel();
model.setModel(holding_matrix);
model.render();
if(holding.count > 1)
{
Matrix4 text_matrix = Matrix4.multiply(matrix, Matrix4.translate(-0.75 / 4, -0.75 / 4, 0));
Matrix4 text_matrix = Matrix4.multiply(holding_matrix, Matrix4.translate(-0.75 / 4, -0.75 / 4, 0));
text_matrix = Matrix4.multiply(Matrix4.scale(new Vec3d(0.4, 0.4, 0.4)), text_matrix);
Text.render("" + holding.count, text_matrix);
}
}
if(hover != null || !holding.isEmpty())
{
ItemStack stack;
if(!holding.isEmpty()) {
stack = holding;
} else {
stack = hover.getItemStack();
}
if(!stack.isEmpty())
{
String name = stack.item.getName(stack.meta);
Matrix4 container_matrix = matrix;
container_matrix = Matrix4.multiply(container_matrix, Matrix4.translate(
-0.4 * name.length() / 2, -1, 0));
Matrix4 text_matrix = container_matrix;
text_matrix = Matrix4.multiply(Matrix4.scale(new Vec3d(0.4, 0.4, 0.4)), text_matrix);
container_matrix = Matrix4.multiply(container_matrix, Matrix4.translate(-0.2, -0.2, 0));
container_matrix = Matrix4.multiply(Matrix4.scale(new Vec3d(
(0.4 * (name.length() + 1)), 0.8, 0)), container_matrix);
Models.UI_TEXT_BG.setModel(container_matrix);
Models.UI_TEXT_BG.render();
Text.render(name, text_matrix);
}
}
}
@Override
@ -73,7 +110,7 @@ public class GUIItemHolder implements GUIComponent
public void onRightClick(Vec2d pos)
{
// Drop part of the holding item if the hover item doesn't exist
if(hover == null)
if(hover == null || this.hover.getter.isReadOnly())
{
if(!holding.isEmpty())
{
@ -141,7 +178,7 @@ public class GUIItemHolder implements GUIComponent
public void onMouseClick(Vec2d pos)
{
// Drop the holding item if the hover item doesn't exist
if(hover == null)
if(hover == null || this.hover.getter.isReadOnly())
{
if(!holding.isEmpty())
{

View File

@ -27,6 +27,10 @@ public class GUIItemSlot implements GUIComponent
this.pos = pos;
}
public Vec2d getPos() {
return pos;
}
public boolean isItemAllowed(ItemStack stack) {
return getter.isAllowed(stack);
}
@ -61,7 +65,7 @@ public class GUIItemSlot implements GUIComponent
model.render();
}
if(checkMouseHover(mousePos) && canHover)
if(!getter.isReadOnly() && checkMouseHover(mousePos) && canHover)
{
double offset = (0.85 - hitboxSize) / 2;
Matrix4 hover_matrix = Matrix4.multiply(matrix, Matrix4.translate(offset, offset, 0));

View File

@ -14,4 +14,8 @@ public interface GUIItemSlotGetter {
public default boolean mustTakeAll() {
return false;
}
public default boolean isReadOnly() {
return false;
}
}

View File

@ -9,7 +9,7 @@ import projectzombie.inventory.recipe.Recipe;
import projectzombie.model.ModelGui;
import projectzombie.util.math.ItemStack;
public class GUIRecipeCard implements GUIContainer, GUIItemSlotGetter
public class GUIRecipeCard implements GUIContainer
{
private static final ModelGui LABEL = Models.UI_LABEL_RECIPE;
@ -17,6 +17,7 @@ public class GUIRecipeCard implements GUIContainer, GUIItemSlotGetter
private Recipe recipe;
private GUIContainer gui;
private GUIItemSlot result_slot;
private GUIItemSlot[] ingredients;
private Inventory inventory;
private Crafting tool;
@ -28,10 +29,77 @@ public class GUIRecipeCard implements GUIContainer, GUIItemSlotGetter
this.inventory = inventory;
this.tool = tool;
result_slot = new GUIItemSlot(1.5, true, this);
result_slot = new GUIItemSlot(1.5, true, new GUIItemSlotGetter()
{
@Override
public void setItemStack(ItemStack stack) {
}
@Override
public boolean isAllowed(ItemStack stack) {
return false;
}
@Override
public ItemStack getItemStack() {
return (recipe.canCraft(tool) &&
recipe.hasResourcesToCraft(inventory)) ?
recipe.getResult().copy() :
ItemStack.getEmpty();
}
@Override
public boolean mustTakeAll() {
return true;
}
@Override
public void onRemoveItemStack() {
if(recipe.canCraft(tool) && recipe.hasResourcesToCraft(inventory)) {
recipe.craftResult(inventory);
}
}
});
result_slot.setPos(pos.add(new Vec2d(
Models.UI_LABEL_RECIPE.getWidth() * 102 / 128.0,
Models.UI_LABEL_RECIPE.getHeight() * 8 / 32.0)));
Models.UI_LABEL_RECIPE.getWidth() * 103 / 128.0 + 0.25,
Models.UI_LABEL_RECIPE.getHeight() * 9 / 32.0 + 0.25)));
ItemStack[] ingredients = recipe.getIngredients();
this.ingredients = new GUIItemSlot[ingredients.length];
for(int i=0;i<ingredients.length;i++)
{
ItemStack ingredient = ingredients[i];
GUIItemSlot ingredientDisplay = new GUIItemSlot(1, true, new GUIItemSlotGetter()
{
@Override
public void setItemStack(ItemStack stack) {
}
@Override
public boolean isAllowed(ItemStack stack) {
return false;
}
@Override
public ItemStack getItemStack() {
return ingredient.copy();
}
@Override
public boolean isReadOnly() {
return true;
}
});
ingredientDisplay.setPos(pos.add(new Vec2d(
Models.UI_LABEL_RECIPE.getWidth() * (27 + 16 * i) / 128.0 + 0.25,
Models.UI_LABEL_RECIPE.getHeight() * 9 / 32.0 + 0.25)));
this.ingredients[i] = ingredientDisplay;
}
}
@Override
@ -40,18 +108,29 @@ public class GUIRecipeCard implements GUIContainer, GUIItemSlotGetter
LABEL.setModel(Matrix4.multiply(matrix, Matrix4.translate(pos.x, pos.y, 0)));
LABEL.render();
result_slot.render(matrix, mousePos, canHover);
for(GUIComponent c : allComponents()) {
c.render(matrix, mousePos, canHover);
}
}
private GUIComponent[] allComponents() {
return new GUIComponent[] {
result_slot
};
private GUIComponent[] allComponents()
{
GUIComponent[] components = new GUIComponent[ingredients.length + 1];
components[0] = result_slot;
for(int i=0;i<ingredients.length;i++) {
components[i+1] = ingredients[i];
}
return components;
}
@Override
public void update(Vec2d mousePos) {
for(GUIComponent c : allComponents()) {
c.update(mousePos);
}
}
@Override
@ -103,7 +182,7 @@ public class GUIRecipeCard implements GUIContainer, GUIItemSlotGetter
public GUIItemSlot getHoveringItemSlot(Vec2d mousePos)
{
for(GUIComponent c : allComponents()) {
if(c instanceof GUIItemSlot) {
if(c.checkMouseHover(mousePos) && c instanceof GUIItemSlot) {
return (GUIItemSlot)c;
}
}
@ -121,33 +200,6 @@ public class GUIRecipeCard implements GUIContainer, GUIItemSlotGetter
}
@Override
public void setItemStack(ItemStack stack) {
}
@Override
public boolean isAllowed(ItemStack stack) {
return false;
}
@Override
public ItemStack getItemStack() {
return (recipe.canCraft(tool) &&
recipe.hasResourcesToCraft(inventory)) ?
recipe.getResult().copy() :
ItemStack.getEmpty();
}
@Override
public boolean mustTakeAll() {
return true;
}
@Override
public void onRemoveItemStack() {
if(recipe.canCraft(tool) && recipe.hasResourcesToCraft(inventory)) {
recipe.craftResult(inventory);
}
}
}

View File

@ -5,6 +5,7 @@ import gl_engine.vec.Vec2d;
public class ModelGui extends Model
{
private int color, flags;
private int animationSize;
private int animationSpeed;
private double width, height;
@ -19,6 +20,8 @@ public class ModelGui extends Model
this.animationSize = animationSize;
this.animationSpeed = animationSpeed;
flags = 0b10000;
}
public ModelGui(TextureRef3D tex, int animationSize, int animationSpeed) {
@ -38,6 +41,12 @@ public class ModelGui extends Model
return this;
}
public ModelGui setColor(float r, float g, float b) {
color = (int)(r * 63) + ((int)(g * 63) << 6) + ((int)(b * 63) << 12);
flags |= 0b10;
return this;
}
@Override
public int getSize() {
return 4;
@ -57,10 +66,10 @@ public class ModelGui extends Model
height = y;
return new float[] {
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
x, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
x, y, 0, 1, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
0, y, 0, 0, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, color, flags,
x, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, color, flags,
x, y, 0, 1, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, color, flags,
0, y, 0, 0, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, color, flags,
};
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB