-
Notifications
You must be signed in to change notification settings - Fork 187
Eating is actually doable (no raymarching) #2381
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
base: master
Are you sure you want to change the base?
Changes from all commits
d83ae73
34b5175
09bd2d9
a9e25be
a383c6a
b3d86d0
09e6044
86f786e
3a6eda4
32c7e94
2bacc52
ab703a9
31a19dd
6ac782b
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,4 +1,7 @@ | ||
| .{ | ||
| .texture = "apple.png", | ||
| .food = 4, | ||
| .onUse = .{ | ||
| .type = .eat, | ||
| .energy = 0.5, | ||
| }, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| .{ | ||
| .texture = "raw_meat.png", | ||
| .food = 3, | ||
| .onUse = .{ | ||
| .type = .eat, | ||
| .energy = 3, | ||
| }, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub const eat = @import("eat.zig"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| const std = @import("std"); | ||
|
|
||
| const main = @import("main"); | ||
|
|
||
| energy: f32, | ||
tillpp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| pub fn init(zon: main.ZonElement) ?*@This() { | ||
| const result = main.worldArena.create(@This()); | ||
| result.* = .{ | ||
| .energy = zon.get(f32, "energy", 1.0), | ||
| }; | ||
| return result; | ||
| } | ||
|
|
||
| pub fn run(self: *@This(), params: main.callbacks.UseItemCallback.Params) main.callbacks.Result { | ||
| const cmd = params.cmd; | ||
| const allocator = params.allocator; | ||
| const user = params.user; | ||
| const side = params.side; | ||
| const stack = params.source.ref(); | ||
|
|
||
| // enough items there? | ||
| if(stack.amount < 1) | ||
| return .ignored; | ||
|
|
||
| const previous = if(side == .server) user.?.player.energy else main.game.Player.super.energy; | ||
|
Member
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. Please check that user is not null, because in fact in this PR you added code that passes null for this (see comment above) |
||
| const maxEnergy = if(side == .server) user.?.player.maxEnergy else main.game.Player.super.maxEnergy; | ||
| if(self.energy > 0 and previous >= maxEnergy) | ||
| return .ignored; | ||
|
|
||
| cmd.executeBaseOperation(allocator, .{.addEnergy = .{ | ||
| .target = user, | ||
| .energy = self.energy, | ||
| .previous = previous, | ||
| }}, side); | ||
|
|
||
| // Apply inventory changes: | ||
| if(params.gamemode == .creative) return .handled; | ||
|
|
||
| cmd.executeBaseOperation(allocator, .{.delete = .{ | ||
| .source = params.source, | ||
| .amount = 1, | ||
| }}, side); | ||
| return .handled; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -528,6 +528,18 @@ pub const Player = struct { // MARK: Player | |
| } | ||
| } | ||
|
|
||
| pub fn useItem(_: main.Window.Key.Modifiers) bool { | ||
|
Member
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. Please avoid boolean return values or parameters (unless it's immediately obvious from the name what they mean which is not the case here). I'd suggest to just use the callback result. |
||
| const item = inventory.getItem(selectedSlot); | ||
| main.items.Inventory.Sync.useItem(.{.inv = inventory, .slot = selectedSlot}, .client); | ||
| switch(item) { | ||
| .baseItem => { | ||
| return !item.baseItem.onUse().isNoop(); | ||
| }, | ||
| .null => {}, | ||
| .tool => {}, | ||
| } | ||
| return false; | ||
| } | ||
| pub fn placeBlock(mods: main.Window.Key.Modifiers) void { | ||
| if(main.renderer.MeshSelection.selectedBlockPos) |blockPos| { | ||
| if(!mods.shift) { | ||
|
|
@@ -548,7 +560,7 @@ pub const Player = struct { // MARK: Player | |
| Player.super.vel = .{0, 0, 0}; | ||
|
|
||
| Player.super.health = Player.super.maxHealth; | ||
| Player.super.energy = Player.super.maxEnergy; | ||
| Player.super.energy = 0; | ||
|
|
||
| Player.eye = .{}; | ||
| Player.jumpCoyote = 0; | ||
|
|
@@ -771,7 +783,9 @@ var nextBlockBreakTime: ?std.Io.Timestamp = null; | |
| pub fn pressPlace(mods: main.Window.Key.Modifiers) void { | ||
| const time = main.timestamp(); | ||
| nextBlockPlaceTime = time.addDuration(main.settings.updateRepeatDelay); | ||
| Player.placeBlock(mods); | ||
| if(!Player.useItem(mods)) { | ||
| Player.placeBlock(mods); | ||
| } | ||
| } | ||
|
|
||
| pub fn releasePlace(_: main.Window.Key.Modifiers) void { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -242,22 +242,28 @@ pub const BaseItemIndex = enum(u16) { // MARK: BaseItemIndex | |
| pub fn getTooltip(self: BaseItemIndex) []const u8 { | ||
| return itemList[@intFromEnum(self)].getTooltip(); | ||
| } | ||
| pub fn onUse(self: BaseItemIndex) main.callbacks.UseItemCallback { | ||
| return itemList[@intFromEnum(self)].onUse; | ||
| } | ||
| }; | ||
|
|
||
| pub const BaseItem = struct { // MARK: BaseItem | ||
| image: graphics.Image, | ||
| texture: ?graphics.Texture, // TODO: Properly deinit | ||
| id: []const u8, | ||
| zon: ZonElement, | ||
| name: []const u8, | ||
| tags: []const Tag, | ||
| tooltip: []const u8, | ||
| onUse: main.callbacks.UseItemCallback, | ||
|
|
||
| stackSize: u16, | ||
| material: ?Material, | ||
| block: ?u16, | ||
| foodValue: f32, // TODO: Effects. | ||
|
|
||
| fn init(self: *BaseItem, allocator: NeverFailingAllocator, texturePath: []const u8, replacementTexturePath: []const u8, id: []const u8, zon: ZonElement) void { | ||
| self.zon = zon.clone(allocator); | ||
| self.id = allocator.dupe(u8, id); | ||
| if(texturePath.len == 0) { | ||
| self.image = graphics.Image.defaultImage; | ||
|
|
@@ -281,7 +287,7 @@ pub const BaseItem = struct { // MARK: BaseItem | |
| break :blk blocks.getTypeById(zon.get(?[]const u8, "block", null) orelse break :blk null); | ||
| }; | ||
| self.texture = null; | ||
| self.foodValue = zon.get(f32, "food", 0); | ||
| self.foodValue = zon.get(f32, "energy", 0); | ||
|
|
||
| var tooltip: main.List(u8) = .init(allocator); | ||
| tooltip.appendSlice(self.name); | ||
|
|
@@ -301,6 +307,16 @@ pub const BaseItem = struct { // MARK: BaseItem | |
| _ = tooltip.swapRemove(tooltip.items.len - 1); | ||
| } | ||
| self.tooltip = tooltip.toOwnedSlice(); | ||
| self.onUse = .noop; | ||
| } | ||
|
|
||
| pub fn finalize(self: *BaseItem) void { | ||
| self.onUse = blk: { | ||
|
Member
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. Callbacks should be registered after everything else, to allow it to reference other items or blocks.
Contributor
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. turns out to be more complex then for blocks, since blocks have an array of zon elements, but Item don't.
Contributor
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. did it the ugly way with a finalize() function and storing .zon object
Member
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 don't like this hack. We shouldn't mix runtime and loadtime data in one struct. |
||
| break :blk main.callbacks.UseItemCallback.init(self.zon.getChildOrNull("onUse") orelse break :blk .noop) orelse { | ||
| std.log.err("Failed to load onUse event for item {s}", .{self.id}); | ||
| break :blk .noop; | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| fn hashCode(self: BaseItem) u32 { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this function is only used on the client, it should only exist in
ClientSide.