Repository navigation
ECS Party | The Bag #2989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ECS Party | The Bag #2989
Changes from all commits
997a26a
4b1c92d
501be65
5f95ee2
3923e0f
c54fa7d
53941db
8410e3c
981cf90
259b5d5
1e7fe77
fb1999c
3225c9b
046e2e6
e8bae37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pub const @"cubyz:_template" = @import("_template.zig"); | ||
| pub const @"cubyz:bag" = @import("bag.zig"); | ||
| pub const @"cubyz:model" = @import("model.zig"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| const std = @import("std"); | ||
|
|
||
| const main = @import("main"); | ||
| const chunk = main.chunk; | ||
| const game = main.game; | ||
| const graphics = main.graphics; | ||
| const c = graphics.c; | ||
| const ZonElement = main.ZonElement; | ||
| const renderer = main.renderer; | ||
| const settings = main.settings; | ||
| const utils = main.utils; | ||
| const vec = main.vec; | ||
| const Mat4f = vec.Mat4f; | ||
| const Vec3d = vec.Vec3d; | ||
| const Vec3f = vec.Vec3f; | ||
| const Vec4f = vec.Vec4f; | ||
| const Vec3i = vec.Vec3i; | ||
| const NeverFailingAllocator = main.heap.NeverFailingAllocator; | ||
|
|
||
| const BinaryReader = main.utils.BinaryReader; | ||
| const BinaryWriter = main.utils.BinaryWriter; | ||
|
|
||
| const blocks = main.blocks; | ||
| const chunk_zig = main.chunk; | ||
| const ServerChunk = chunk_zig.ServerChunk; | ||
| const World = game.World; | ||
| const ServerWorld = main.server.ServerWorld; | ||
| const items = main.items; | ||
| const ItemStack = items.ItemStack; | ||
| const random = main.random; | ||
|
|
||
| pub var entityComponentID: main.entity.EntityComponentId = undefined; | ||
| pub const entityComponentVersion = 0; | ||
|
|
||
| const playerBagSizeLimit = 120; | ||
|
|
||
| // ############################# Client only stuff ################################ | ||
| pub const client = struct { | ||
| const Component = struct { | ||
| bag: items.Inventory.BagInventory, | ||
| }; | ||
| pub var components: main.utils.SparseSet(Component, main.entity.Entity) = .{}; | ||
|
|
||
| pub fn init() void {} | ||
| pub fn deinit() void { | ||
| for (components.dense.items) |bag| bag.bag.deinit(); | ||
| components.deinit(main.globalAllocator); | ||
| } | ||
| pub fn clear() void { | ||
| for (components.dense.items) |bag| bag.bag.deinit(); | ||
| components.clear(); | ||
| } | ||
|
|
||
| pub fn getBag(entityId: u32) ?*items.Inventory.BagInventory { | ||
| return &(components.get(@enumFromInt(entityId)) orelse return null).bag; | ||
| } | ||
|
|
||
| pub fn load(entityId: u32, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void { | ||
| if (version != entityComponentVersion) return error.InvalidComponentVersion; | ||
| const bag = &components.add(main.globalAllocator, @enumFromInt(entityId)).bag; | ||
| bag.* = .init(main.globalAllocator, playerBagSizeLimit); | ||
| bag.fromBytes(reader) catch return error.UnreadableComponentData; | ||
| } | ||
| pub fn unload(entityId: u32) void { | ||
| const bag = components.fetchRemove(@enumFromInt(entityId)) catch return; | ||
| bag.bag.deinit(); | ||
| } | ||
| }; | ||
|
|
||
| // ############################# Server only stuff ################################ | ||
| pub const server = struct { | ||
| pub const Component = struct { | ||
| bag: items.Inventory.BagInventory, | ||
| pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour { | ||
| if (audience != .disk and audience != .playerHimself) return .discard; | ||
| self.bag.toBytes(writer); | ||
| return .save; | ||
| } | ||
| }; | ||
| pub var components: main.utils.SparseSet(Component, main.entity.Entity) = .{}; | ||
|
|
||
| pub fn init() void { | ||
| components = .{}; | ||
| } | ||
| pub fn deinit() void { | ||
| for (components.dense.items) |bag| bag.bag.deinit(); | ||
| components.deinit(main.globalAllocator); | ||
| } | ||
|
|
||
| pub fn get(entityId: u32) ?Component { | ||
| return (components.get(@enumFromInt(entityId)) orelse return null).*; | ||
| } | ||
| pub fn getBag(entityId: u32) ?*items.Inventory.BagInventory { | ||
| return &(components.get(@enumFromInt(entityId)) orelse return null).bag; | ||
| } | ||
| pub fn loadFromData(entityId: u32, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void { | ||
| if (version != entityComponentVersion) return error.InvalidComponentVersion; | ||
| const bag = &components.add(main.globalAllocator, @enumFromInt(entityId)).bag; | ||
| bag.* = .init(main.globalAllocator, playerBagSizeLimit); | ||
| bag.fromBytes(reader) catch return error.UnreadableComponentData; | ||
| } | ||
| pub fn loadEmpty(entityId: u32) void { | ||
| const bag = &components.add(main.globalAllocator, @enumFromInt(entityId)).bag; | ||
| bag.* = .init(main.globalAllocator, playerBagSizeLimit); | ||
| } | ||
| pub fn unload(entityId: u32) void { | ||
| const bag = components.fetchRemove(@enumFromInt(entityId)) catch return; | ||
| bag.bag.deinit(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| const std = @import("std"); | ||
|
|
||
| const main = @import("main"); | ||
| const BagInventory = main.items.Inventory.BagInventory; | ||
| const graphics = main.graphics; | ||
| const draw = graphics.draw; | ||
| const Texture = graphics.Texture; | ||
| const TextBuffer = graphics.TextBuffer; | ||
| const vec = main.vec; | ||
| const Vec2f = vec.Vec2f; | ||
|
|
||
| const gui = @import("../gui.zig"); | ||
| const GuiComponent = gui.GuiComponent; | ||
|
|
||
| const BagSlot = @This(); | ||
|
|
||
| const border: f32 = 2; | ||
|
|
||
| pub const sizeWithBorder = 32 + 2*border; | ||
|
|
||
| var texture: Texture = undefined; | ||
|
|
||
| pos: Vec2f, | ||
| size: Vec2f = .{sizeWithBorder, sizeWithBorder + 8}, | ||
| inventory: *BagInventory, | ||
| hovered: bool = false, | ||
| pressed: bool = false, | ||
|
|
||
| pub fn globalInit() void { | ||
| texture = Texture.initFromFile("assets/cubyz/ui/inventory/bag_slot.png"); | ||
| } | ||
|
|
||
| pub fn __deinit() void { | ||
| texture.deinit(); | ||
| } | ||
|
|
||
| pub fn init(pos: Vec2f, inventory: *BagInventory) *BagSlot { | ||
| const self = main.globalAllocator.create(BagSlot); | ||
| self.* = .{ | ||
| .inventory = inventory, | ||
| .pos = pos, | ||
| }; | ||
| return self; | ||
| } | ||
|
|
||
| pub fn deinit(self: *const BagSlot) void { | ||
| main.globalAllocator.destroy(self); | ||
| } | ||
|
|
||
| pub fn toComponent(self: *BagSlot) GuiComponent { | ||
| return .{.bagSlot = self}; | ||
| } | ||
|
|
||
| pub fn updateHovered(self: *BagSlot, _: Vec2f) main.callbacks.Result { | ||
| self.hovered = true; | ||
| gui.hoveredItemSlot = null; | ||
| return .handled; | ||
| } | ||
|
|
||
| pub fn mainButtonPressed(self: *BagSlot, _: Vec2f) main.callbacks.Result { | ||
| self.pressed = true; | ||
| return .handled; | ||
| } | ||
|
|
||
| pub fn mainButtonReleased(self: *BagSlot, mousePosition: Vec2f) void { | ||
| if (self.pressed) { | ||
| self.pressed = false; | ||
| if (GuiComponent.contains(self.pos, self.size, mousePosition)) { | ||
| const carried = gui.inventory.carried; | ||
| if (carried.getAmount(0) != 0) { | ||
| main.sync.ClientSide.executeCommand(.{.moveToPlayerBag = .{.amount = carried.getAmount(0), .source = .{.inv = carried.super, .slot = 0}}}); | ||
| } else { | ||
| main.sync.ClientSide.executeCommand(.{.takeFromPlayerBag = .init(&.{carried}, std.math.maxInt(u16))}); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn render(self: *BagSlot, _: Vec2f) void { | ||
| draw.setColor(0xffffffff); | ||
| texture.bindTo(0); | ||
| draw.boundImage(self.pos, @splat(sizeWithBorder)); | ||
|
|
||
| for (0..5) |_i| { | ||
| const i = 4 - _i; | ||
| const item = self.inventory.peek(i).item; | ||
| if (item == .null) continue; | ||
| const opacity: f32 = std.math.pow(f32, 0.5, @as(f32, @floatFromInt(i))); | ||
| draw.setColor(0xffffff | @as(u32, @intFromFloat(opacity*255)) << 24); | ||
| item.render(self.pos, @splat(sizeWithBorder), border); | ||
| } | ||
|
|
||
| const topItem = self.inventory.peek(0); | ||
| const shouldRenderStackSizeText = topItem.item.stackSize() > 1; | ||
| if (shouldRenderStackSizeText) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from here a lot is just copy from itemslot. I know they have key differences but maybe we want to generlize something here? Like a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to move part of the rendering code to items.zig |
||
| var amount: usize = topItem.amount; | ||
| for (1..self.inventory.slots.items.len) |i| { | ||
| const otherItem = self.inventory.peek(i); | ||
| if (!std.meta.eql(topItem.item, otherItem.item)) break; | ||
| amount += otherItem.amount; | ||
| } | ||
| var buf: [16]u8 = undefined; | ||
| var text = TextBuffer.init( | ||
| main.stackAllocator, | ||
| std.fmt.bufPrint(&buf, "{}", .{amount}) catch "∞", | ||
| .{.color = if (amount == 0) 0xff0000 else 0xffffff}, | ||
| false, | ||
| .right, | ||
| ); | ||
| defer text.deinit(); | ||
| const textSize = text.calculateLineBreaks(8, self.size[0] - 2*border); | ||
| text.render(self.pos[0] + sizeWithBorder - textSize[0] - border, self.pos[1] + sizeWithBorder - textSize[1] - border, 8); | ||
| } | ||
|
|
||
| draw.setColor(0xffffffff); | ||
| draw.print("{}/{}", .{self.inventory.slots.items.len, self.inventory.sizeLimit}, self.pos[0], self.pos[1] + sizeWithBorder, 8, .left); | ||
|
|
||
| if (self.hovered) { | ||
| self.hovered = false; | ||
| draw.setColor(0x300000ff); | ||
| draw.rect(self.pos, @splat(sizeWithBorder)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.