Initial commit
This commit is contained in:
12
src/constants.zig
Normal file
12
src/constants.zig
Normal file
@@ -0,0 +1,12 @@
|
||||
const clr = @import("raylib").Color;
|
||||
|
||||
pub const WIDTH = 2560;
|
||||
pub const HEIGHT = 1440;
|
||||
|
||||
pub const NODE_RADIUS = 20;
|
||||
pub const NODE_COLOUR = clr.brown;
|
||||
pub const NODE_TEMP_COLOUR = clr.orange;
|
||||
pub const NODE_CURSOR_COLOUR = clr.blue;
|
||||
|
||||
pub const ROAD_SIZE = 20;
|
||||
pub const ROAD_COLOUR = clr.black;
|
||||
21
src/infrastructure/node.zig
Normal file
21
src/infrastructure/node.zig
Normal file
@@ -0,0 +1,21 @@
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("../constants.zig");
|
||||
|
||||
pub const Node = struct {
|
||||
id: usize,
|
||||
pos: rl.Vector2,
|
||||
|
||||
pub fn init(new_id: usize, new_pos: rl.Vector2) Node {
|
||||
return .{
|
||||
.id = new_id,
|
||||
.pos = new_pos,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Node, direct_colour: ?rl.Color) void {
|
||||
const colour = if (direct_colour) |clr| clr else c.NODE_COLOUR;
|
||||
|
||||
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
|
||||
}
|
||||
};
|
||||
39
src/infrastructure/node_manager.zig
Normal file
39
src/infrastructure/node_manager.zig
Normal file
@@ -0,0 +1,39 @@
|
||||
const std = @import("std");
|
||||
const Vector2 = @import("raylib").Vector2;
|
||||
|
||||
const c = @import("../constants.zig");
|
||||
const Node = @import("node.zig").Node;
|
||||
const Road = @import("road.zig").Road;
|
||||
|
||||
pub const NodeManager = struct {
|
||||
nodes: std.ArrayList(Node),
|
||||
temp_node: ?*Node,
|
||||
|
||||
pub fn init() NodeManager {
|
||||
return .{
|
||||
.nodes = .empty,
|
||||
.temp_node = null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
|
||||
self.nodes.deinit(allocator);
|
||||
}
|
||||
|
||||
pub fn draw(self: *const NodeManager, pos: Vector2) void {
|
||||
for (self.nodes.items) |node| {
|
||||
node.draw();
|
||||
}
|
||||
|
||||
if (self.temp_node) |node| {
|
||||
// Temporary node that points at the cursor
|
||||
const cur_node = Node.init(0, pos);
|
||||
// Temporary road that is to be drawn as one in the making
|
||||
const road: Road = .init(0, node, &cur_node);
|
||||
road.draw();
|
||||
|
||||
node.*.draw(c.NODE_TEMP_COLOUR);
|
||||
cur_node.draw(c.NODE_CURSOR_COLOUR);
|
||||
}
|
||||
}
|
||||
};
|
||||
40
src/infrastructure/road.zig
Normal file
40
src/infrastructure/road.zig
Normal file
@@ -0,0 +1,40 @@
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("../constants.zig");
|
||||
const Node = @import("node.zig").Node;
|
||||
|
||||
pub const Road = struct {
|
||||
/// Road ID, used for identification, particularly as we'll access all entities via pointers
|
||||
id: usize,
|
||||
/// Pointers to the nodes that encapsulated our road
|
||||
nodes: [2]*Node,
|
||||
/// Calculated road length
|
||||
length: f32,
|
||||
|
||||
pub fn init(new_id: usize, start: *Node, end: *Node) Road {
|
||||
var road: Road = .{
|
||||
.id = new_id,
|
||||
.nodes = .{start, end},
|
||||
.length = 0,
|
||||
};
|
||||
|
||||
road.length = road.calculate_length();
|
||||
return road;
|
||||
}
|
||||
|
||||
/// Calculates length of the road by taking its two nodes
|
||||
fn calculate_length(self: *const Road) f32 {
|
||||
const start = self.nodes[0];
|
||||
const end = self.nodes[1];
|
||||
|
||||
const x_diff = end.*.pos.x - start.*.pos.x;
|
||||
const y_diff = end.*.pos.y - start.*.pos.y;
|
||||
const square_diff = x_diff * x_diff + y_diff * y_diff;
|
||||
|
||||
return @sqrt(square_diff);
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Road) void {
|
||||
rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, c.ROAD_COLOUR);
|
||||
}
|
||||
};
|
||||
22
src/infrastructure/road_manager.zig
Normal file
22
src/infrastructure/road_manager.zig
Normal file
@@ -0,0 +1,22 @@
|
||||
const std = @import("std");
|
||||
const Road = @import("road.zig").Road;
|
||||
|
||||
pub const RoadManager = struct {
|
||||
roads: std.ArrayList(Road),
|
||||
|
||||
pub fn init() RoadManager {
|
||||
return .{
|
||||
.roads = .empty,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *RoadManager, allocator: std.mem.Allocator) void {
|
||||
self.roads.deinit(allocator);
|
||||
}
|
||||
|
||||
pub fn draw(self: *const RoadManager) void {
|
||||
for (self.roads.items) |road| {
|
||||
road.draw();
|
||||
}
|
||||
}
|
||||
};
|
||||
23
src/main.zig
Normal file
23
src/main.zig
Normal file
@@ -0,0 +1,23 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("constants.zig");
|
||||
const Simulator = @import("simulator.zig").Simulator;
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const allocator = init.gpa;
|
||||
rl.initWindow(c.WIDTH, c.HEIGHT, "Base Road Network");
|
||||
defer rl.closeWindow();
|
||||
|
||||
var sim: Simulator = .init(allocator);
|
||||
|
||||
while (!rl.windowShouldClose()) {
|
||||
rl.beginDrawing();
|
||||
defer rl.endDrawing();
|
||||
|
||||
const pos = rl.getMousePosition();
|
||||
sim.handleInput(pos);
|
||||
|
||||
sim.draw(pos);
|
||||
}
|
||||
}
|
||||
43
src/simulator.zig
Normal file
43
src/simulator.zig
Normal file
@@ -0,0 +1,43 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
|
||||
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
|
||||
|
||||
pub const Simulator = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
node_man: NodeManager,
|
||||
road_man: RoadManager,
|
||||
|
||||
pub fn init(new_allocator: std.mem.Allocator) Simulator {
|
||||
return .{
|
||||
.allocator = new_allocator,
|
||||
.node_man = .init(),
|
||||
.road_man = .init(),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Simulator) void {
|
||||
self.road_man.deinit(self.allocator);
|
||||
self.node_man.deinit(self.allocator);
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Simulator, pos: rl.Vector2) void {
|
||||
rl.clearBackground(.light_gray);
|
||||
|
||||
self.road_man.draw();
|
||||
self.node_man.draw(pos);
|
||||
}
|
||||
|
||||
pub fn handleInput(self: *Simulator, pos: rl.Vector2) void {
|
||||
self.handleMouseInput(pos);
|
||||
}
|
||||
|
||||
fn handleMouseInput(self: *Simulator, pos: rl.Vector2) void {
|
||||
if (rl.isMouseButtonReleased(.left)) self.leftClickEvent(pos);
|
||||
}
|
||||
|
||||
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
|
||||
// TODO
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user