Skip to content

Commit

Permalink
Grab changes from the original zoom-minimap branch
Browse files Browse the repository at this point in the history
RIP
  • Loading branch information
z-silver committed Dec 3, 2024
1 parent 80268b1 commit cc2ba32
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
57 changes: 34 additions & 23 deletions client/src/components/hud/map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const std = @import("std");
const GameState = @import("../../game/state.zig");

pub fn init_map_texture() rl.Texture {
const side = config.map.border_thickness * 2;
const side = outerRadius * 2;
const img = rl.genImageColor(side, side, rl.Color.black);
defer img.unload();
return img.toTexture();
Expand Down Expand Up @@ -58,17 +58,13 @@ pub fn getCenter(width: f32, height: f32) rl.Vector2 {
}

pub const coordinates = struct {
fn worldToMap(position: rl.Vector2, world: *const GameState.World.Map) struct { f32, f32 } {
const character_x: f32 = rm.clamp(position.x, 0, @floatFromInt(world.instance.width * config.map.mini_map_size));
const character_y: f32 = rm.clamp(position.y, 0, @floatFromInt(world.instance.height * config.map.mini_map_size));
return .{ character_x, character_y };
}
pub fn normalize(position: rl.Vector2, map_image: *const rl.Image, world: *const GameState.World.Map) struct { f32, f32 } {
const character_x, const character_y = worldToMap(position, world);
const character_x = position.x;
const character_y = position.y;
const fWidth: f32 = @floatFromInt(world.instance.width);
const fHeight: f32 = @floatFromInt(world.instance.height);
const normalized_x = character_x * @as(f32, @as(f32, @floatFromInt(map_image.width - 2 * config.map.border_thickness)) / (config.assets.tile.size * fWidth));
const normalized_y = character_y * @as(f32, @as(f32, @floatFromInt(map_image.height - 2 * config.map.border_thickness)) / (config.assets.tile.size * fHeight));
const normalized_x = character_x * @as(f32, @as(f32, @floatFromInt(map_image.width)) / (config.assets.tile.size * fWidth));
const normalized_y = character_y * @as(f32, @as(f32, @floatFromInt(map_image.height)) / (config.assets.tile.size * fHeight));
return .{ normalized_x, normalized_y };
}
};
Expand Down Expand Up @@ -108,24 +104,36 @@ pub fn at(character: *const GameState.World.Character, world: *const GameState.W
.x = character.stats.x_position,
.y = character.stats.y_position,
};
const normalized_x, const normalized_y = coordinates.normalize(position, &character.inventory.hud.minimap.map.?, world);
const map_image = character.inventory.hud.minimap.map.?;

const normalized_x, const normalized_y = coordinates.normalize(position, &map_image, world);
const displacement_x: f32 = normalized_x - @as(f32, @floatFromInt(@divFloor(map_image.width, 2)));
const displacement_y: f32 = normalized_y - @as(f32, @floatFromInt(@divFloor(map_image.height, 2)));

const center: rl.Vector2 = getCenter(width, height);
const map_image = character.inventory.hud.minimap.map.?;
const map_x = center.x - outerRadius;
const map_y = center.y - outerRadius;

var canvas: rl.Image = rl.genImageColor(@intFromFloat(2 * outerRadius), @intFromFloat(2 * outerRadius), rl.Color.black);
defer canvas.unload();

const map_mask = rl.Rectangle{
.x = normalized_x,
.y = normalized_y,
.x = @max(0, displacement_x),
.y = @max(0, displacement_y),
.width = @floatFromInt(@min(outerRadius * 2, map_image.width)),
.height = @floatFromInt(@min(outerRadius * 2, map_image.height)),
};
var map = rl.imageFromImage(map_image, map_mask);
defer map.unload();

const target: rl.Rectangle = .{
.x = -displacement_x,
.y = -displacement_y,
.width = map_mask.width,
.height = map_mask.height,
};

canvas.drawImage(map_image, map_mask, target, rl.Color.white);

var alpha_mask = rl.genImageColor(
@intFromFloat(map_mask.width),
@intFromFloat(map_mask.height),
@intFromFloat(outerRadius * 2),
@intFromFloat(outerRadius * 2),
rl.Color.black,
);
defer alpha_mask.unload();
Expand All @@ -136,17 +144,20 @@ pub fn at(character: *const GameState.World.Character, world: *const GameState.W
innerRadius,
config.ColorPalette.secondary,
);
map.alphaMask(alpha_mask);

const pixels = try rl.loadImageColors(map);
canvas.alphaMask(alpha_mask);

const pixels = try rl.loadImageColors(canvas);

const texture = character.inventory.hud.minimap.texture.?;
rl.updateTexture(texture, pixels.ptr);

texture.draw(@intFromFloat(map_x), @intFromFloat(map_y), config.ColorPalette.secondary);
const map_x = center.x - outerRadius;
const map_y = center.y - outerRadius;
texture.draw(@intFromFloat(map_x), @intFromFloat(map_y), rl.Color.white);
rl.drawRing(center, innerRadius, outerRadius, 0, 360, 0, config.ColorPalette.primary);
rl.drawCircleLinesV(center, innerRadius, config.ColorPalette.secondary);
rl.drawCircleLinesV(center, innerRadius - 1, config.ColorPalette.secondary);

player(character.stats.face_direction, center);

drawMapName(center, character.stats.map_name, font);
Expand Down
2 changes: 0 additions & 2 deletions client/src/game/camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub fn update(gameState: *GameState) void {

const cameraAngle = rm.vector3Normalize(config.angleCameraVector);

gameState.world.cameraDistance -= rl.getMouseWheelMove() * 2;

gameState.world.camera.position = rm.vector3Add(rm.vector3Scale(cameraAngle, gameState.world.cameraDistance), gameState.world.character.position);
rl.updateCamera(&gameState.world.camera, rl.CameraMode.camera_custom);
}
11 changes: 11 additions & 0 deletions client/src/game/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ fn controlInput(gameState: *GameState) !i16 {
try server.character.exitMap(gameState);
rl.enableCursor();
}
const zoom = rl.getMouseWheelMove() * 2;
if (zoom != 0) {
const world = &gameState.world;
world.cameraDistance -= zoom;
world.map.size += zoom;
var img = rl.imageCopy(entity.inventory.hud.minimap.initial_map.?);
const width: f32 = @floatFromInt(world.map.instance.width);
const height: f32 = @floatFromInt(world.map.instance.height);
rl.imageResizeNN(&img, @intFromFloat(width * world.map.size), @intFromFloat(height * world.map.size));
entity.inventory.hud.minimap.map = img;
}

return tempAngle;
}
Expand Down
2 changes: 2 additions & 0 deletions client/src/game/state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub const World = struct {
instance: messages.Map = .{},
tiles: Tile_Table,
objects: Object_Table,
size: f32 = config.map.mini_map_size,
};
pub const Character = struct {
pub const Animation = struct {
Expand Down Expand Up @@ -73,6 +74,7 @@ pub const World = struct {
spells: []const [:0]const u8 = &.{},
consumables: []const [:0]const u8 = &.{},
minimap: struct {
initial_map: ?rl.Image = null,
map: ?rl.Image = null,
texture: ?rl.Texture = null,
} = .{},
Expand Down

0 comments on commit cc2ba32

Please sign in to comment.