Skip to content

Commit

Permalink
Introduce resource proximity + rock asset (resource)
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoLR10 committed Jan 26, 2025
1 parent ef18b24 commit 4ce2876
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ References for assets used in this project:
- [[https://free3d.com/3d-model/treasure-chest-v1--156264.html][chest_model]]
- [[https://opengameart.org/content/fern][bush_model]]
- [[https://opengameart.org/content/tree-24][tree_model]]
- [[https://free3d.com/3d-model/rock-v2-lp-63239.html][rock_model]]
- [[https://www.dafont.com/eari.font?text=Lyceum][logo_font]]
- [[https://www.dafont.com/kelmscott.font?text=Connect][text_font]]
- [[https://tholgrimar.bandcamp.com/track/linear-b][background_music]]
Expand Down
3 changes: 3 additions & 0 deletions client/assets/game/world/objects/rock/base.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions client/assets/game/world/objects/rock/height.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions client/assets/game/world/objects/rock/normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions client/assets/game/world/objects/rock/rock.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
newmtl lambert2SG
illum 4
Kd 0.00 0.00 0.00
Ka 0.00 0.00 0.00
Tf 1.00 1.00 1.00
map_Kd base.png
map_Ka height.png
bump normal.png
Ni 1.00
3 changes: 3 additions & 0 deletions client/assets/game/world/objects/rock/rock.obj
Git LFS file not shown
8 changes: 4 additions & 4 deletions client/src/assets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ fn loadObject(kind: messages.Object) !Object {
},
.rock => .{
// TODO: distinguish rocks from bushes
.model = try model(config.assets.paths.game.world.objects.bush.model),
.scale = config.assets.object.bush.scale,
.axis = config.assets.object.bush.axis,
.angle = config.assets.object.bush.angle,
.model = try model(config.assets.paths.game.world.objects.rock.model),
.scale = config.assets.object.rock.scale,
.axis = config.assets.object.rock.axis,
.angle = config.assets.object.rock.angle,
},
.empty => unreachable,
};
Expand Down
41 changes: 41 additions & 0 deletions client/src/components/hud/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const map = @import("map.zig");
const consumables = @import("consumables.zig");
const info = @import("info.zig");
const GameState = @import("../../game/state.zig");
const resources = @import("resources.zig");
const rl = @import("raylib");
const rm = rl.math;
const std = @import("std");
Expand Down Expand Up @@ -40,6 +41,45 @@ fn drawPlayers(gameState: *GameState) !void {
}
}

fn detectResource(gameState: *GameState, tx: i32, ty: i32, angle: i16) void {
const width = gameState.width;
const height = gameState.height;
const world = &gameState.world.map;
const font = &gameState.menu.assets.font;

const iwidth: i32 = @intCast(world.instance.width);
const iheight: i32 = @intCast(world.instance.height);

const x = std.math.clamp(tx, 0, iwidth);
const y = std.math.clamp(ty, 0, iheight);

const entity = &gameState.world.character;
if (world.resources.get(.{ @floatFromInt(x), @floatFromInt(y) })) |resource| {
if (resource.quantity > 0 and entity.stats.face_direction == angle) {
// TODO: If the server sends a non-client-supported asset, this will explode
const value = resources.info.get(@tagName(resource.kind)).?;
value.drawer(width, height, font);
if (rl.isKeyPressed(value.key)) {
std.debug.print("Detected Resource! It is: .{}\n", .{resource.kind});
}
}
}
}

fn detectResources(gameState: *GameState) void {
const player = &gameState.world.character;

if (player.inventory.hud.chat.mode == .writing) return;

const x_tile: i32 = @intFromFloat(player.position.x / config.assets.tile.size);
const y_tile: i32 = @intFromFloat(player.position.z / config.assets.tile.size);

detectResource(gameState, x_tile, y_tile + 1, 0);
detectResource(gameState, x_tile + 1, y_tile, 90);
detectResource(gameState, x_tile, y_tile - 1, 180);
detectResource(gameState, x_tile - 1, y_tile, 270);
}

pub fn at(gameState: *GameState) !void {
const width = gameState.width;
const height = gameState.height;
Expand Down Expand Up @@ -69,4 +109,5 @@ pub fn at(gameState: *GameState) !void {
// TODO: This should be at the beginning, but mini-map screw us over.
// Putting this on the top makes the other players pointers disappear.
try drawPlayers(gameState);
detectResources(gameState);
}
85 changes: 85 additions & 0 deletions client/src/components/hud/resources.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const config = @import("../../config.zig");
const rl = @import("raylib");
const std = @import("std");

const ResourceInfo = struct {
key: rl.KeyboardKey,
key_label: [:0]const u8,
action: [:0]const u8,
drawer: *const fn (width: f32, height: f32, font: *rl.Font) void,
};

pub const info = std.StaticStringMap(ResourceInfo).initComptime(.{
.{ @tagName(.rock), .{
.key = .key_r,
.key_label = "R",
.action = "Mine",
.drawer = rock,
} },
.{ @tagName(.tree), .{
.key = .key_r,
.key_label = "R",
.action = "Chop",
.drawer = tree,
} },
.{ @tagName(.bush), .{
.key = .key_r,
.key_label = "R",
.action = "Harvest",
.drawer = bush,
} },
});

fn generic(key: [:0]const u8, width: f32, height: f32, font: *rl.Font) void {
const value = info.get(key).?;
const actionLength: rl.Vector2 = rl.measureTextEx(font.*, value.action, config.resourceActionFontSize, config.textSpacing);

const boundarySize: rl.Vector2 = .{
.x = 20 + actionLength.x,
.y = actionLength.y + 14,
};

const keyLabelLength: f32 = rl.measureTextEx(font.*, value.key_label, config.resourceActionFontSize + 6, config.textSpacing).x;
const labelBoxSize: rl.Vector2 = .{
.x = keyLabelLength + 10,
.y = actionLength.y + 14,
};

const labelBoxPosition: rl.Vector2 = .{
.x = width / 2 - boundarySize.x / 2 - labelBoxSize.x / 2,
.y = height - 162,
};

const labelPosition: rl.Vector2 = .{
.x = labelBoxPosition.x + 5,
.y = labelBoxPosition.y + 7,
};

const boundaryPosition: rl.Vector2 = .{
.x = labelBoxPosition.x + labelBoxSize.x,
.y = height - 162,
};

rl.drawRectangleV(boundaryPosition, boundarySize, config.ColorPalette.primary);
rl.drawRectangleLinesEx(.{ .x = boundaryPosition.x, .y = boundaryPosition.y, .width = boundarySize.x, .height = boundarySize.y }, 2, config.ColorPalette.secondary);
rl.drawRectangleV(labelBoxPosition, labelBoxSize, config.ColorPalette.secondary);
rl.drawTextEx(font.*, value.key_label, labelPosition, config.resourceActionFontSize + 6, config.textSpacing, config.ColorPalette.primary);

const actionPosition: rl.Vector2 = .{
.x = boundaryPosition.x + 10,
.y = boundaryPosition.y + 7,
};
rl.drawTextEx(font.*, value.action, actionPosition, config.resourceActionFontSize, config.textSpacing, config.ColorPalette.secondary);
}

fn rock(width: f32, height: f32, font: *rl.Font) void {
generic("rock", width, height, font);
}

fn tree(width: f32, height: f32, font: *rl.Font) void {
generic("tree", width, height, font);
}

fn bush(width: f32, height: f32, font: *rl.Font) void {
generic("bush", width, height, font);
}
18 changes: 18 additions & 0 deletions client/src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const angleCameraVector: rl.Vector3 = .{
pub const map = struct {
pub const border_thickness = 140;
pub const mini_map_size = 50;
pub const resource_proximity = 3;
};

pub const assets = struct {
Expand Down Expand Up @@ -66,6 +67,19 @@ pub const assets = struct {
.z = 0,
};
};
pub const rock = struct {
pub const scale: rl.Vector3 = .{
.x = 0.3,
.y = 0.3,
.z = 0.3,
};
pub const angle = 90;
pub const axis: rl.Vector3 = .{
.x = 0,
.y = 1,
.z = 0,
};
};
};
pub const paths = struct {
pub const menu = struct {
Expand Down Expand Up @@ -127,6 +141,9 @@ pub const assets = struct {
pub const bush = struct {
pub const model = "game/world/objects/bush/bush.obj";
};
pub const rock = struct {
pub const model = "game/world/objects/rock/rock.obj";
};
};
};
};
Expand All @@ -138,6 +155,7 @@ pub const defaultCameraDistance = 35;
pub const buttonFontSize = 27;
pub const titleFontSize = 25;
pub const textFontSize = 20;
pub const resourceActionFontSize = 30;
pub const textSpacing = 2.5;
pub const hubFontSize = 13;
pub const menuButtonsPadding = 16;
Expand Down
14 changes: 7 additions & 7 deletions client/src/game/character.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const std = @import("std");
const zerl = @import("zerl");
const Button = @import("../components/button.zig");

pub const Chat = struct {
pub const bufferSize = 50;
content: [bufferSize:0]u8 = .{0} ** bufferSize,
messages: std.ArrayList(chat.Message) = std.ArrayList(chat.Message).init(std.heap.c_allocator),
position: usize = 0,
mode: chat.Mode = .idle,
};
pub const Chat = struct {
pub const bufferSize = 50;
content: [bufferSize:0]u8 = .{0} ** bufferSize,
messages: std.ArrayList(chat.Message) = std.ArrayList(chat.Message).init(std.heap.c_allocator),
position: usize = 0,
mode: chat.Mode = .idle,
};

pub const Animation = struct {
pub const State = enum {
Expand Down
2 changes: 0 additions & 2 deletions client/src/game/items/equipment.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


pub const Kind = enum {
head,
top,
Expand Down
1 change: 0 additions & 1 deletion client/src/game/items/potion.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pub const Kind = enum {
health,
mana,
Expand Down

0 comments on commit 4ce2876

Please sign in to comment.