Added mass/volume limit enforcement and displays

- Also added Test:RedGraniteCobblestone
This commit is contained in:
OLEGSHA 2021-08-30 23:36:13 +03:00
parent e7d0e8fe40
commit a4b731e8a5
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
12 changed files with 243 additions and 14 deletions

View File

@ -56,6 +56,25 @@ public class Colors {
return color.mul(multiplier, multiplier, multiplier, 1, output); return color.mul(multiplier, multiplier, multiplier, 1, output);
} }
public static Vec4 mix(Vec4 zero, Vec4 one, float t, Vec4 output) {
if (output == null) {
output = new Vec4();
}
if (t <= 0) {
return output.set(zero);
} else if (t >= 1) {
return output.set(one);
}
return output.set(
zero.x * (1 - t) + one.x * t,
zero.y * (1 - t) + one.y * t,
zero.z * (1 - t) + one.z * t,
zero.w * (1 - t) + one.w * t
);
}
public static Vec4 toVector(int argb, Vec4 output) { public static Vec4 toVector(int argb, Vec4 output) {
output.w = ((argb & 0xFF000000) >>> 24) / (float) 0xFF; // Alpha output.w = ((argb & 0xFF000000) >>> 24) / (float) 0xFF; // Alpha
output.x = ((argb & 0x00FF0000) >>> 16) / (float) 0xFF; // Red output.x = ((argb & 0x00FF0000) >>> 16) / (float) 0xFF; // Red

View File

@ -72,4 +72,32 @@ public abstract class ItemContainer extends Namespaced implements Encodable {
*/ */
public abstract float getVolumeLimit(); public abstract float getVolumeLimit();
public synchronized float getMass() {
float sum = 0;
for (int i = 0; i < getSlotCount(); ++i) {
ItemSlot slot = getSlot(i);
if (slot.isEmpty()) {
continue;
}
sum += slot.getContents().getMass() * slot.getAmount();
}
return sum;
}
public synchronized float getVolume() {
float sum = 0;
for (int i = 0; i < getSlotCount(); ++i) {
ItemSlot slot = getSlot(i);
if (slot.isEmpty()) {
continue;
}
sum += slot.getContents().getVolume() * slot.getAmount();
}
return sum;
}
} }

View File

@ -36,6 +36,8 @@ public class ItemContainerHand extends ItemContainer {
super(id); super(id);
this.massLimit = massLimit; this.massLimit = massLimit;
this.volumeLimit = volumeLimit; this.volumeLimit = volumeLimit;
slot.setContainer(this);
} }
@Override @Override

View File

@ -50,7 +50,23 @@ public abstract class ItemContainerMixed extends ItemContainer {
* *
* @param amount the amount of slots to add * @param amount the amount of slots to add
*/ */
public abstract void addSlots(int amount); public synchronized void addSlots(int amount) {
List<ItemSlot> slots = getSlots();
for (int i = 0; i < amount; ++i) {
ItemSlot slot = createSlot(slots.size());
slots.add(slot);
slot.setContainer(this);
}
}
/**
* Instantiates a new slot object that will be appended to the container.
*
* @param index the index that the new slot will receive
* @return the new slot
*/
protected abstract ItemSlot createSlot(int index);
@Override @Override
public ItemSlot getSlot(int index) { public ItemSlot getSlot(int index) {

View File

@ -40,11 +40,8 @@ public class ItemContainerMixedSimple extends ItemContainerMixed {
} }
@Override @Override
public synchronized void addSlots(int amount) { public ItemSlot createSlot(int index) {
((ArrayList<ItemSlot>) list).ensureCapacity(list.size() + amount); return new ItemSlot();
for (int i = 0; i < amount; ++i) {
list.add(new ItemSlot());
}
} }
@Override @Override

View File

@ -33,6 +33,22 @@ public class ItemSlot implements Encodable {
private ItemData contents; private ItemData contents;
private int amount; private int amount;
private ItemContainer container;
/**
* @return the container
*/
public ItemContainer getContainer() {
return container;
}
/**
* @param container the container to set
*/
void setContainer(ItemContainer container) {
this.container = container;
}
/** /**
* Retrieves the contents of this slot. * Retrieves the contents of this slot.
* *
@ -95,14 +111,19 @@ public class ItemSlot implements Encodable {
} }
public synchronized boolean canInsert(ItemData contents, int amount) { public synchronized boolean canInsert(ItemData contents, int amount) {
if (contents == null) {
// Ignore amount return false;
if (this.contents == null) {
return true;
} }
return this.contents.equals(contents); if (container.getMass() + contents.getMass() * amount > container.getMassLimit()) {
return false;
}
if (container.getVolume() + contents.getVolume() * amount > container.getVolumeLimit()) {
return false;
}
return this.contents == null || this.contents.equals(contents);
} }
public synchronized boolean canRemove(int amount) { public synchronized boolean canRemove(int amount) {

View File

@ -65,8 +65,9 @@ public class PlayerManager {
player.getInventory().addSlots(10); player.getInventory().addSlots(10);
player.getInventory().getSlot(3).setContents(ItemDataRegistry.getInstance().create("Test:Stick"), 5); player.getInventory().getSlot(3).setContents(ItemDataRegistry.getInstance().create("Test:Stick"), 7);
player.getInventory().getSlot(5).setContents(ItemDataRegistry.getInstance().create("Test:Stick"), 4); player.getInventory().getSlot(5).setContents(ItemDataRegistry.getInstance().create("Test:Stick"), 4);
player.getInventory().getSlot(9).setContents(ItemDataRegistry.getInstance().create("Test:RedGraniteCobblestone"), 1);
player.getInventory().getSlot(6).setContents(ItemDataRegistry.getInstance().create("Test:MoonTypeIceCream"), 1); player.getInventory().getSlot(6).setContents(ItemDataRegistry.getInstance().create("Test:MoonTypeIceCream"), 1);
player.setPosition(getServer().getWorld().getGenerator().suggestSpawnLocation()); player.setPosition(getServer().getWorld().getGenerator().suggestSpawnLocation());

View File

@ -245,6 +245,7 @@ public class TestContent {
private static void registerItems() { private static void registerItems() {
registerSimplestItem("MoonTypeIceCream", Units.get("200 g"), Units.get("1 L")); registerSimplestItem("MoonTypeIceCream", Units.get("200 g"), Units.get("1 L"));
registerSimplestItem("Stick", Units.get("260 g"), Units.get("0.5 L")); registerSimplestItem("Stick", Units.get("260 g"), Units.get("0.5 L"));
registerSimplestItem("RedGraniteCobblestone", Units.get("4 kg"), Units.get("1500 cm^3"));
} }
private static void registerSimplestBlock(String name) { private static void registerSimplestBlock(String name) {

View File

@ -0,0 +1,113 @@
/*
* Progressia
* Copyright (C) 2020-2021 Wind Corporation and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ru.windcorp.progressia.test.inv;
import glm.vec._3.Vec3;
import glm.vec._4.Vec4;
import ru.windcorp.jputil.functions.FloatSupplier;
import ru.windcorp.progressia.client.graphics.Colors;
import ru.windcorp.progressia.client.graphics.backend.Usage;
import ru.windcorp.progressia.client.graphics.flat.FlatRenderProgram;
import ru.windcorp.progressia.client.graphics.flat.RenderTarget;
import ru.windcorp.progressia.client.graphics.gui.Component;
import ru.windcorp.progressia.client.graphics.model.Renderable;
import ru.windcorp.progressia.client.graphics.model.Shape;
import ru.windcorp.progressia.client.graphics.model.ShapeParts;
import ru.windcorp.progressia.client.graphics.model.ShapeRenderHelper;
public class Bar extends Component {
private static final int THICKNESS = 5;
private final boolean isVertical;
private final FloatSupplier value;
private final FloatSupplier maxValue;
private final Vec4 color;
private final Vec4 backgroundColor;
private static Renderable unitSquare = null;
public Bar(String name, boolean isVertical, Vec4 color, FloatSupplier value, FloatSupplier maxValue) {
super(name);
this.isVertical = isVertical;
this.value = value;
this.maxValue = maxValue;
this.color = color;
this.backgroundColor = Colors.mix(color, Colors.WHITE, 0.75f, null);
if (unitSquare == null) {
unitSquare = new Shape(
Usage.STATIC,
FlatRenderProgram.getDefault(),
ShapeParts.createRectangle(
FlatRenderProgram.getDefault(),
null,
Colors.WHITE,
new Vec3(0, 0, 0),
new Vec3(1, 0, 0),
new Vec3(0, 1, 0),
false
)
);
}
setPreferredSize(THICKNESS, THICKNESS);
}
@Override
protected void assembleSelf(RenderTarget target) {
target.addCustomRenderer(this::renderSelf);
}
private void renderSelf(ShapeRenderHelper renderer) {
renderer.pushTransform()
.translate(getX(), getY(), 0)
.scale(getWidth(), getHeight(), 1);
float length = value.getAsFloat() / maxValue.getAsFloat();
if (length < 0) {
length = 0;
} else if (length > 1) {
length = 1;
}
// TODO why is the order reverse????
renderRectangle(renderer, color, length);
renderRectangle(renderer, backgroundColor, 1);
renderer.popTransform();
}
private void renderRectangle(ShapeRenderHelper renderer, Vec4 color, float length) {
renderer.pushColorMultiplier().mul(color);
if (length != 1) {
renderer.pushTransform().scale(isVertical ? 1 : length, isVertical ? length : 1, 1);
}
unitSquare.render(renderer);
if (length != 1) {
renderer.popTransform();
}
renderer.popColorMultiplier();
}
}

View File

@ -132,6 +132,10 @@ public class InventoryScreen extends Component {
success = Items.swap(handSlot, invSlot); success = Items.swap(handSlot, invSlot);
} }
if (!success && handSlot.isEmpty()) {
success = Items.pour(invSlot, handSlot) != 0;
}
if (success) { if (success) {
requestReassembly(); requestReassembly();
} }

View File

@ -21,8 +21,11 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import glm.vec._2.i.Vec2i; import glm.vec._2.i.Vec2i;
import ru.windcorp.progressia.client.graphics.Colors;
import ru.windcorp.progressia.client.graphics.gui.Component;
import ru.windcorp.progressia.client.graphics.gui.Group; import ru.windcorp.progressia.client.graphics.gui.Group;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutBorderHorizontal; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutBorderHorizontal;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutBorderVertical;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutGrid; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutGrid;
import ru.windcorp.progressia.common.world.item.ItemContainer; import ru.windcorp.progressia.common.world.item.ItemContainer;
@ -35,7 +38,31 @@ public class SimpleInventoryComponent extends InventoryComponent {
super("Inventory"); super("Inventory");
setLayout(new LayoutBorderHorizontal(15)); setLayout(new LayoutBorderHorizontal(15));
addChild(slots.setLayoutHint(LayoutBorderHorizontal.CENTER));
Bar massBar = new Bar(
"MassBar",
true,
Colors.toVector(0xFF44AAAA),
container::getMass,
container::getMassLimit
);
Bar volumeBar = new Bar(
"VolumeBar",
false,
Colors.toVector(0xFFAA4444),
container::getVolume,
container::getVolumeLimit
);
Component slotsAndVolumeBar = new Group(
"SlotsAndVolumeBar",
new LayoutBorderVertical(15),
slots.setLayoutHint(LayoutBorderVertical.CENTER),
volumeBar.setLayoutHint(LayoutBorderVertical.UP)
);
addChild(slotsAndVolumeBar.setLayoutHint(LayoutBorderHorizontal.CENTER));
addChild(massBar.setLayoutHint(LayoutBorderHorizontal.LEFT));
for (int i = 0; i < container.getSlotCount(); ++i) { for (int i = 0; i < container.getSlotCount(); ++i) {
addSlot(container, i); addSlot(container, i);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB