this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: cleanup

+77 -84
+61
src/hex.zig
··· 1 + //! By convention, root.zig is the root source file when making a library. 2 + const std = @import("std"); 3 + 4 + const PI: comptime_float = std.math.pi; 5 + const sqrt_3: comptime_float = std.math.sqrt(3.0); 6 + 7 + pub const Point = struct { x: f32, y: f32 }; 8 + pub const HexCell = struct { inner_radius: f32, outer_radius: f32, height: f32, width: f32, center: Point }; 9 + pub const HexDirections = enum { top_left, top, top_right, bottom_right, bottom, bottom_left }; 10 + 11 + fn calculate_outer_radius(inner_radius: f32) f32 { 12 + return 2.0 * inner_radius / sqrt_3; 13 + } 14 + fn calculate_inner_radius(outer_radius: f32) f32 { 15 + return outer_radius / 2.0 * sqrt_3; 16 + } 17 + 18 + pub fn calculate_hex_corners(center: Point, size: f32, _i: usize) Point { 19 + const i: f32 = @floatFromInt(_i); 20 + const angle_deg = 60.0 * i; 21 + const angle_rad = std.math.degreesToRadians(angle_deg); 22 + 23 + const x_offset: f32 = size * std.math.cos(angle_rad); 24 + const y_offset: f32 = size * std.math.sin(angle_rad); 25 + 26 + return Point{ .x = center.x + x_offset, .y = center.y + y_offset }; 27 + } 28 + 29 + pub fn new_hex(outer_radius: f32, center: Point) HexCell { 30 + const size = outer_radius; 31 + const inner = calculate_inner_radius(size); 32 + return HexCell{ .inner_radius = inner, .outer_radius = size, .height = sqrt_3 * size, .width = 2 * size, .center = center }; 33 + } 34 + 35 + pub fn add_hex(base: HexCell, direction: HexDirections) HexCell { 36 + const x_sign: f32 = switch (direction) { 37 + .bottom_right => 1.0, 38 + .top_right => 1.0, 39 + .top => 0, 40 + .bottom => 0, 41 + .bottom_left => -1, 42 + .top_left => -1, 43 + }; 44 + 45 + const y_sign: f32 = switch (direction) { 46 + .top => -1, 47 + .top_left => -0.5, 48 + .top_right => -0.5, 49 + .bottom_left => 0.5, 50 + .bottom_right => 0.5, 51 + .bottom => 1, 52 + }; 53 + 54 + const x_offset: f32 = x_sign * 1.5 * base.outer_radius; 55 + const y_offset: f32 = y_sign * base.height; 56 + 57 + return new_hex(base.outer_radius, .{ 58 + .x = base.center.x + x_offset, 59 + .y = base.center.y + y_offset, 60 + }); 61 + }
+15 -25
src/main.zig
··· 1 1 const std = @import("std"); 2 2 const lib = @import("zig_civ"); 3 - 4 3 const rl = @import("raylib"); 5 4 6 - fn draw_hex(hex: lib.HexCell) void { 7 - //const center_x: i32 = @intFromFloat(@round(hex.center.x)); 8 - //const center_y: i32 = @intFromFloat(@round(hex.center.y)); 9 - //rl.drawCircle(center_x, center_y, hex.outer_radius, .green); 10 - //rl.drawCircle(center_x, center_y, hex.inner_radius, .red); 11 - 12 - var corners: [6]lib.Point = undefined; 5 + fn draw_hex(hex: lib.hex.HexCell) void { 6 + var corners: [6]lib.hex.Point = undefined; 13 7 for (0..6) |i| { 14 - const corner = lib.calculate_hex_corners(hex.center, hex.outer_radius, i); 8 + const corner = lib.hex.calculate_hex_corners(hex.center, hex.outer_radius, i); 15 9 corners[i] = corner; 16 - 17 - //const corner_y: i32 = @intFromFloat(@round(corner.y)); 18 - //const corner_x: i32 = @intFromFloat(@round(corner.x)); 19 - 20 - //rl.drawCircle(corner_x, corner_y, 5.0, .black); 21 10 } 22 11 23 12 for (0..6) |i| { 24 13 const start = corners[i]; 25 - const end = corners[@mod(i + 1, corners.len)]; 14 + const end = corners[@mod(i + 1, 6)]; 26 15 27 16 std.debug.print("sx: {d} sy: {d} ex:{d} ey:{d}\n", .{ start.x, start.y, end.x, end.y }); 28 17 ··· 40 29 //-------------------------------------------------------------------------------------- 41 30 const screenWidth = 800; 42 31 const screenHeight = 450; 32 + const fps = 60; 43 33 44 - rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); 34 + rl.initWindow(screenWidth, screenHeight, "raylib.hex-zig [core] example - basic window"); 45 35 defer rl.closeWindow(); // Close window and OpenGL context 46 36 47 - rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second 37 + rl.setTargetFPS(fps); // Set our game to run at 60 frames-per-second 48 38 //-------------------------------------------------------------------------------------- 49 39 50 40 // Main game loop ··· 53 43 //---------------------------------------------------------------------------------- 54 44 // TODO: Update your variables here 55 45 //---------------------------------------------------------------------------------- 56 - const cell = lib.new_hex(50.0, .{ 46 + const cell = lib.hex.new_hex(50.0, .{ 57 47 .x = screenWidth / 2, 58 48 .y = screenHeight / 2, 59 49 }); 60 50 std.debug.print("cell : {}", .{cell}); 61 51 62 - const tl = lib.add_hex(cell, .top_left); 63 - const tr = lib.add_hex(cell, .top_right); 64 - const t = lib.add_hex(cell, .top); 65 - const br = lib.add_hex(cell, .bottom_right); 66 - const bl = lib.add_hex(cell, .bottom_left); 67 - const b = lib.add_hex(cell, .bottom); 52 + const tl = lib.hex.add_hex(cell, .top_left); 53 + const tr = lib.hex.add_hex(cell, .top_right); 54 + const t = lib.hex.add_hex(cell, .top); 55 + const br = lib.hex.add_hex(cell, .bottom_right); 56 + const bl = lib.hex.add_hex(cell, .bottom_left); 57 + const b = lib.hex.add_hex(cell, .bottom); 68 58 69 - const grid = [_]lib.HexCell{ cell, tl, t, tr, br, b, bl }; 59 + const grid = [_]lib.hex.HexCell{ cell, tl, t, tr, br, b, bl }; 70 60 71 61 // Draw 72 62 //----------------------------------------------------------------------------------
+1 -59
src/root.zig
··· 1 1 //! By convention, root.zig is the root source file when making a library. 2 - const std = @import("std"); 3 - 4 - const Point = struct { x: f32, y: f32 }; 5 - const HexCell = struct { inner_radius: f32, outer_radius: f32, height: f32, width: f32, center: Point }; 6 - const HexDirections = enum { top_left, top, top_right, bottom_right, bottom, bottom_left }; 7 - 8 - const PI = std.math.pi; 9 - 10 - fn calculate_hex_corners(center: Point, size: f32, _i: usize) Point { 11 - const i: f32 = @floatFromInt(_i); 12 - const angle_deg = 60.0 * i; 13 - const angle_rad = std.math.degreesToRadians(angle_deg); 14 - 15 - const x_offset: f32 = size * std.math.cos(angle_rad); 16 - const y_offset: f32 = size * std.math.sin(angle_rad); 17 - 18 - return Point{ .x = center.x + x_offset, .y = center.y + y_offset }; 19 - } 20 - 21 - fn add_hex(base: HexCell, direction: HexDirections) HexCell { 22 - const x_sign: f32 = switch (direction) { 23 - .bottom_right => 1.0, 24 - .top_right => 1.0, 25 - .top => 0, 26 - .bottom => 0, 27 - .bottom_left => -1, 28 - .top_left => -1, 29 - }; 30 - 31 - const y_sign: f32 = switch (direction) { 32 - .top => -1, 33 - .top_left => -0.5, 34 - .top_right => -0.5, 35 - .bottom_left => 0.5, 36 - .bottom_right => 0.5, 37 - .bottom => 1, 38 - }; 39 - 40 - const x_offset: f32 = x_sign * 1.5 * base.outer_radius; 41 - const y_offset: f32 = y_sign * base.height; 42 - 43 - return new_hex(base.outer_radius, .{ 44 - .x = base.center.x + x_offset, 45 - .y = base.center.y + y_offset, 46 - }); 47 - } 48 - 49 - fn calculate_outer_radius(inner_radius: f32) f32 { 50 - return 2.0 * inner_radius / std.math.sqrt(3.0); 51 - } 52 - fn calculate_inner_radius(outer_radius: f32) f32 { 53 - return outer_radius / 2.0 * std.math.sqrt(3.0); 54 - } 55 - 56 - fn new_hex(outer_radius: f32, center: Point) HexCell { 57 - const size = outer_radius; 58 - const inner = calculate_inner_radius(size); 59 - return HexCell{ .inner_radius = inner, .outer_radius = size, .height = std.math.sqrt(3.0) * size, .width = 2 * size, .center = center }; 60 - } 2 + pub const hex = @import("hex.zig");