Compare commits

..

6 Commits

8 changed files with 132 additions and 35 deletions

View File

@@ -16,6 +16,7 @@ pub const NODE_COLOUR = clr.brown;
pub const NODE_TEMP_COLOUR = clr.orange; pub const NODE_TEMP_COLOUR = clr.orange;
/// The colour of the node being at the cursor /// The colour of the node being at the cursor
pub const NODE_CURSOR_COLOUR = clr.blue; pub const NODE_CURSOR_COLOUR = clr.blue;
pub const NODE_RELATED_COLOUR = clr.purple;
/// Road (line) size /// Road (line) size
pub const ROAD_SIZE = 20; pub const ROAD_SIZE = 20;

19
src/common/structures.zig Normal file
View File

@@ -0,0 +1,19 @@
const Vector2 = @import("raylib").Vector2;
const Road = @import("../infrastructure/road.zig").Road;
const Node = @import("../infrastructure/node.zig").Node;
/// Can point to either entity type (node, road, etc.)
pub const Entity = union(enum) {
node: *Node,
road: *Road,
};
/// Represents intersection data, mainly used in cases where we draw over existing roads
/// and wish to see data about such intersections or collisions
pub const IntersectionData = struct {
/// Tracks the location of the intersection
pos: Vector2,
/// Points to the road where the intersection occured
road: *Road,
};

View File

@@ -1,8 +1,9 @@
const std = @import("std"); const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const c = @import("../constants.zig"); const c = @import("../common/constants.zig");
const e = @import("../errors.zig"); const e = @import("../errors.zig");
const st = @import("../common/structures.zig");
const Road = @import("road.zig").Road; const Road = @import("road.zig").Road;
pub const Node = struct { pub const Node = struct {
@@ -39,11 +40,16 @@ pub const Node = struct {
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour); rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
} }
/// Identifies whether the pos (location) is within the snapping radius of the node /// Determines whether the pos (location) is within the snapping radius of the node
pub fn posWithinRadius(self: *const Node, pos: rl.Vector2) bool { pub fn withinSnapRadius(self: *const Node, pos: rl.Vector2) bool {
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_SNAP_RADIUS); return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_SNAP_RADIUS);
} }
/// Determines whether the pos (location) is within the strict (visual representation) radius of the node
pub fn withinRadius(self: *const Node, pos: rl.Vector2) bool {
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_RADIUS);
}
/// Tries to reference the passed road to self /// Tries to reference the passed road to self
/// ///
/// Returns an error if the passed road cannot be appended to the list or if said road is already in the list /// Returns an error if the passed road cannot be appended to the list or if said road is already in the list

View File

@@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const Vector2 = @import("raylib").Vector2; const Vector2 = @import("raylib").Vector2;
const c = @import("../constants.zig"); const c = @import("../common/constants.zig");
const e = @import("../errors.zig"); const e = @import("../errors.zig");
const Node = @import("node.zig").Node; const Node = @import("node.zig").Node;
const Road = @import("road.zig").Road; const Road = @import("road.zig").Road;
@@ -26,6 +26,7 @@ pub const NodeManager = struct {
/// Deinitialises every node (pointer) within the list and then deinits the list containing the nodes itself /// Deinitialises every node (pointer) within the list and then deinits the list containing the nodes itself
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) !void { pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) !void {
for (self.nodes.items) |node| { for (self.nodes.items) |node| {
node.roads.clearRetainingCapacity();
try node.deinit(allocator); try node.deinit(allocator);
} }
self.nodes.deinit(allocator); self.nodes.deinit(allocator);
@@ -44,7 +45,7 @@ pub const NodeManager = struct {
const road: Road = .init(0, node, &cur_node); const road: Road = .init(0, node, &cur_node);
road.draw(false); road.draw(false);
node.*.draw(c.NODE_TEMP_COLOUR); node.draw(c.NODE_TEMP_COLOUR);
cur_node.draw(c.NODE_CURSOR_COLOUR); cur_node.draw(c.NODE_CURSOR_COLOUR);
} }
} }
@@ -53,7 +54,7 @@ pub const NodeManager = struct {
/// otherwise creates a new node and returns its pointer /// otherwise creates a new node and returns its pointer
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node { pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
for (self.nodes.items) |node| { for (self.nodes.items) |node| {
if (node.posWithinRadius(pos)) return node; if (node.withinSnapRadius(pos)) return node;
} }
// No node is within that position, so we must create a new one // No node is within that position, so we must create a new one
@@ -77,6 +78,7 @@ pub const NodeManager = struct {
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void { pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void {
self.temp_node = null; self.temp_node = null;
for (self.nodes.items) |node| { for (self.nodes.items) |node| {
node.roads.clearRetainingCapacity();
try node.deinit(allocator); try node.deinit(allocator);
} }
self.nodes.clearRetainingCapacity(); self.nodes.clearRetainingCapacity();
@@ -97,6 +99,15 @@ pub const NodeManager = struct {
return e.Entity.NotFound; return e.Entity.NotFound;
} }
/// Runs a scan through all the nodes and returns the reference to one that is within the radius
pub fn getHighlightedNode(self: *const NodeManager, pos: Vector2) ?*Node {
for (self.nodes.items) |node| {
if (node.withinRadius(pos)) return node;
}
return null;
}
}; };
const expect = std.testing.expect; const expect = std.testing.expect;

View File

@@ -1,6 +1,7 @@
const rl = @import("raylib"); const rl = @import("raylib");
const c = @import("../constants.zig"); const c = @import("../common/constants.zig");
const st = @import("../common/structures.zig");
const Node = @import("node.zig").Node; const Node = @import("node.zig").Node;
pub const Road = struct { pub const Road = struct {
@@ -33,8 +34,8 @@ pub const Road = struct {
const start = self.nodes[0]; const start = self.nodes[0];
const end = self.nodes[1]; const end = self.nodes[1];
const x_diff = end.*.pos.x - start.*.pos.x; const x_diff = end.pos.x - start.pos.x;
const y_diff = end.*.pos.y - start.*.pos.y; const y_diff = end.pos.y - start.pos.y;
const square_diff = x_diff * x_diff + y_diff * y_diff; const square_diff = x_diff * x_diff + y_diff * y_diff;
return @sqrt(square_diff); return @sqrt(square_diff);
@@ -46,7 +47,7 @@ pub const Road = struct {
/// it will gradually become more complex /// it will gradually become more complex
pub fn draw(self: *const Road, highlighted: bool) void { pub fn draw(self: *const Road, highlighted: bool) void {
const colour = if (highlighted) c.ROAD_HIGHLIGHTED_COLOUR else c.ROAD_COLOUR; const colour = if (highlighted) c.ROAD_HIGHLIGHTED_COLOUR else c.ROAD_COLOUR;
rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, colour); rl.drawLineEx(self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE, colour);
} }
/// Important: after this function executes, this road is no longer reachable from its bounding nodes /// Important: after this function executes, this road is no longer reachable from its bounding nodes
@@ -59,7 +60,7 @@ pub const Road = struct {
/// Checks whether pos coordinate is on the referenced road /// Checks whether pos coordinate is on the referenced road
pub fn isHighlighted(self: *const Road, pos: rl.Vector2) bool { pub fn isHighlighted(self: *const Road, pos: rl.Vector2) bool {
return rl.checkCollisionPointLine(pos, self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE); return rl.checkCollisionPointLine(pos, self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE);
} }
}; };
@@ -74,7 +75,7 @@ test "valid road nodes" {
const start: Node = .init(34, .{.x = 500, .y = 500}); const start: Node = .init(34, .{.x = 500, .y = 500});
const start_ptr = try allocator.create(Node); const start_ptr = try allocator.create(Node);
defer { defer {
start_ptr.*.deinit(allocator); start_ptr.deinit(allocator);
allocator.destroy(start_ptr); allocator.destroy(start_ptr);
} }
start_ptr.* = start; start_ptr.* = start;
@@ -82,7 +83,7 @@ test "valid road nodes" {
const end: Node = .init(227, .{.x = 600, .y = 500}); const end: Node = .init(227, .{.x = 600, .y = 500});
const end_ptr = try allocator.create(Node); const end_ptr = try allocator.create(Node);
defer { defer {
end_ptr.*.deinit(allocator); end_ptr.deinit(allocator);
allocator.destroy(end_ptr); allocator.destroy(end_ptr);
} }
end_ptr.* = end; end_ptr.* = end;

View File

@@ -7,13 +7,11 @@ const Node = @import("node.zig").Node;
pub const RoadManager = struct { pub const RoadManager = struct {
next_id: usize, next_id: usize,
roads: std.ArrayList(*Road), roads: std.ArrayList(*Road),
highlighted_road: ?*Road,
pub fn init() RoadManager { pub fn init() RoadManager {
return .{ return .{
.next_id = 0, .next_id = 0,
.roads = .empty, .roads = .empty,
.highlighted_road = null,
}; };
} }
@@ -26,9 +24,9 @@ pub const RoadManager = struct {
} }
/// Draws all the roads in the list, sends the information ahead whether the road drawn should be highlighted /// Draws all the roads in the list, sends the information ahead whether the road drawn should be highlighted
pub fn draw(self: *const RoadManager, delete_mode: bool) void { pub fn draw(self: *const RoadManager, highlighted_road: ?*Road) void {
for (self.roads.items) |road| { for (self.roads.items) |road| {
const is_highlighted = delete_mode and self.highlighted_road != null and self.highlighted_road.? == road; const is_highlighted = if (highlighted_road) |h_road| road == h_road else false;
road.draw(is_highlighted); road.draw(is_highlighted);
} }
} }
@@ -85,16 +83,13 @@ pub const RoadManager = struct {
return e.Entity.NotFound; return e.Entity.NotFound;
} }
/// Sets the pointer to the road that is intersection with pos coordinate /// Returns if pos is pointing at a road, or null if it isn't at any
pub fn update_highlighted_road(self: *RoadManager, pos: Vector2) void { pub fn getHighlightedRoad(self: *const RoadManager, pos: Vector2) ?*Road {
for (self.roads.items) |road| { for (self.roads.items) |road| {
if (!road.isHighlighted(pos)) continue; if (road.isHighlighted(pos)) return road;
self.highlighted_road = road;
return;
} }
self.highlighted_road = null; return null;
} }
}; };

View File

@@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const c = @import("constants.zig"); const c = @import("common/constants.zig");
const Simulator = @import("simulator.zig").Simulator; const Simulator = @import("simulator.zig").Simulator;
pub fn main(init: std.process.Init) !void { pub fn main(init: std.process.Init) !void {

View File

@@ -1,7 +1,9 @@
const std = @import("std"); const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const c = @import("constants.zig"); const c = @import("common/constants.zig");
const st = @import("common/structures.zig");
const Road = @import("infrastructure/road.zig").Road;
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager; const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager; const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
@@ -18,6 +20,14 @@ pub const Simulator = struct {
/// Tracks whether the system will delete the road cursor is pointed at /// Tracks whether the system will delete the road cursor is pointed at
/// (in such case, the road-to-be-deleted will also be highlighted) /// (in such case, the road-to-be-deleted will also be highlighted)
delete_mode: bool, delete_mode: bool,
/// Tracks whether highlighting all entities that are connected to hovered entity is enabled
///
/// For example, if I hover over a node it will highlight all roads that are connected to it;
/// Same goes for hovering over a road or in the future, a car (might show destination and path to it)
///
/// Note: It only works outside of the delete mode
show_connections: bool,
highlighted_entity: ?st.Entity,
/// Constructor for convenience /// Constructor for convenience
pub fn init(new_allocator: std.mem.Allocator) Simulator { pub fn init(new_allocator: std.mem.Allocator) Simulator {
@@ -27,6 +37,8 @@ pub const Simulator = struct {
.road_man = .init(), .road_man = .init(),
.auto_continue = false, .auto_continue = false,
.delete_mode = false, .delete_mode = false,
.show_connections = false,
.highlighted_entity = null,
}; };
} }
@@ -40,13 +52,47 @@ pub const Simulator = struct {
pub fn draw(self: *const Simulator, pos: rl.Vector2) void { pub fn draw(self: *const Simulator, pos: rl.Vector2) void {
rl.clearBackground(c.BACKGROUND_COLOR); rl.clearBackground(c.BACKGROUND_COLOR);
self.road_man.draw(self.delete_mode); var highlighted_road: ?*Road = null;
if (self.delete_mode) {
if (self.highlighted_entity) |entity| {
if (entity == .road) highlighted_road = entity.road;
}
}
self.road_man.draw(highlighted_road);
self.node_man.draw(pos); self.node_man.draw(pos);
self.drawRelatedSelectedEntities();
}
fn drawRelatedSelectedEntities(self: *const Simulator) void {
if (!self.show_connections or self.highlighted_entity == null) return;
const h_entity = self.highlighted_entity.?;
switch (h_entity) {
.node => {
const node = h_entity.node;
for (node.roads.items) |road| {
road.draw(true);
}
node.draw(c.NODE_RELATED_COLOUR);
},
.road => {
const road = h_entity.road;
road.draw(true);
road.nodes[0].draw(c.NODE_RELATED_COLOUR);
road.nodes[1].draw(c.NODE_RELATED_COLOUR);
},
}
} }
/// Update tick /// Update tick
pub fn update(self: *Simulator, pos: rl.Vector2) void { pub fn update(self: *Simulator, pos: rl.Vector2) void {
self.road_man.update_highlighted_road(pos); self.updateHighlightedEntity(pos);
} }
/// Exposed input handling function exposed to raylib /// Exposed input handling function exposed to raylib
@@ -59,6 +105,7 @@ pub const Simulator = struct {
fn handleKeyboardInput(self: *Simulator) void { fn handleKeyboardInput(self: *Simulator) void {
self.auto_continue = rl.isKeyDown(.left_control); self.auto_continue = rl.isKeyDown(.left_control);
self.delete_mode = rl.isKeyDown(.left_shift); self.delete_mode = rl.isKeyDown(.left_shift);
self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode;
if (rl.isKeyReleased(.c)) self.clear() catch |err| { if (rl.isKeyReleased(.c)) self.clear() catch |err| {
std.debug.panic("Failed to clear the entities: {}\n", .{err}); std.debug.panic("Failed to clear the entities: {}\n", .{err});
@@ -72,7 +119,7 @@ pub const Simulator = struct {
/// Function that handles functionality that executes upon left click /// Function that handles functionality that executes upon left click
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void { fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
if (self.delete_mode) { if (self.delete_mode and self.highlighted_entity != null and self.highlighted_entity.? == .road) {
self.delete_road() catch |err| { self.delete_road() catch |err| {
std.debug.panic("Failed to delete the road: {}\n", .{err}); std.debug.panic("Failed to delete the road: {}\n", .{err});
}; };
@@ -83,6 +130,7 @@ pub const Simulator = struct {
/// User initiated road building functionality /// User initiated road building functionality
fn new_road(self: *Simulator, pos: rl.Vector2) void { fn new_road(self: *Simulator, pos: rl.Vector2) void {
if (self.show_connections) return;
const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| { const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| {
std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{ std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{
pos.x, pos.y, err pos.x, pos.y, err
@@ -90,7 +138,7 @@ pub const Simulator = struct {
}; };
if (self.node_man.temp_node) |temp| { if (self.node_man.temp_node) |temp| {
if (temp.*.id == cur_node.*.id) return; if (temp.id == cur_node.id) return;
self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| { self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| {
std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err}); std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err});
}; };
@@ -104,16 +152,16 @@ pub const Simulator = struct {
/// User initiated road destroying functionality /// User initiated road destroying functionality
fn delete_road(self: *Simulator) !void { fn delete_road(self: *Simulator) !void {
if (self.road_man.highlighted_road == null) return; // We can trust this because this only gets called if valid and if type is road
const h_road = self.road_man.highlighted_road.?; std.debug.assert(self.highlighted_entity != null and self.highlighted_entity.? == .road);
const h_road = self.highlighted_entity.?.road;
const start_node = h_road.*.nodes[0]; const start_node = h_road.nodes[0];
const end_node = h_road.*.nodes[1]; const end_node = h_road.nodes[1];
self.road_man.deleteRoad(self.allocator, h_road) catch |err| { self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
std.debug.panic("Road deletion failed: {}\n", .{err}); std.debug.panic("Road deletion failed: {}\n", .{err});
}; };
self.road_man.highlighted_road = null;
if (start_node.roads.items.len == 0) if (start_node.roads.items.len == 0)
try self.node_man.deleteNode(self.allocator, start_node); try self.node_man.deleteNode(self.allocator, start_node);
@@ -124,7 +172,23 @@ pub const Simulator = struct {
/// Clearing node and road lists without deinitialising them (only the children) /// Clearing node and road lists without deinitialising them (only the children)
fn clear(self: *Simulator) !void { fn clear(self: *Simulator) !void {
self.highlighted_entity = null;
self.road_man.clear(self.allocator); self.road_man.clear(self.allocator);
try self.node_man.clear(self.allocator); try self.node_man.clear(self.allocator);
} }
/// Updates the variable that tracks the highlighted entity
fn updateHighlightedEntity(self: *Simulator, pos: rl.Vector2) void {
if (self.node_man.getHighlightedNode(pos)) |node| {
self.highlighted_entity = .{ .node = node };
return;
}
if (self.road_man.getHighlightedRoad(pos)) |road| {
self.highlighted_entity = .{ .road = road };
return;
}
self.highlighted_entity = null;
}
}; };