Added comments and additional keybind functionality (clearing all entities and drawing UI [debug] data)
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
/// Screen width
|
||||
pub const WIDTH = 1920;
|
||||
/// Screen height
|
||||
pub const HEIGHT = 1080;
|
||||
|
||||
/// Default allocation size for entities before resizing is in order
|
||||
pub const ALLOC_SIZE = 50;
|
||||
/// Node radius used for display and calculation
|
||||
pub const NODE_RADIUS = 20;
|
||||
/// Node snapping radius designating the radius upon which new node will not be created
|
||||
/// but rather merged with an existing one
|
||||
pub const NODE_SNAP_RADIUS = 3;
|
||||
/// Road size used for display and calculation
|
||||
pub const ROAD_SIZE = 20;
|
||||
/// Default text size
|
||||
pub const TEXT_SIZE = 50;
|
||||
@@ -1,16 +1,16 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const s = @import("../structures.zig");
|
||||
const c = @import("../constants.zig");
|
||||
|
||||
const Road = @import("road.zig").Road;
|
||||
|
||||
pub const Node = struct {
|
||||
id: usize,
|
||||
pos: s.Vector2,
|
||||
pos: rl.Vector2,
|
||||
roads: std.ArrayList(*Road),
|
||||
|
||||
pub fn init(new_id: usize, new_pos: s.Vector2) Node {
|
||||
pub fn init(new_id: usize, new_pos: rl.Vector2) Node {
|
||||
return .{
|
||||
.id = new_id,
|
||||
.pos = new_pos,
|
||||
@@ -22,12 +22,14 @@ pub const Node = struct {
|
||||
self.roads.deinit(allocator);
|
||||
}
|
||||
|
||||
/// References the passed road with the node
|
||||
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road: *Road) !void {
|
||||
if (self.roadInList(road)) return;
|
||||
|
||||
try self.roads.append(allocator, road);
|
||||
}
|
||||
|
||||
/// Returns bool whether the road passed is part of the roads list
|
||||
fn roadInList(self: *const Node, road_to_add: *const Road) bool {
|
||||
for (self.roads.items) |road| {
|
||||
if (road.id == road_to_add.id) return true;
|
||||
|
||||
@@ -16,9 +16,7 @@ pub const NodeManager = struct {
|
||||
}
|
||||
|
||||
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
|
||||
for (self.nodes.items) |*node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
self.clear(allocator);
|
||||
self.nodes.deinit(allocator);
|
||||
}
|
||||
|
||||
@@ -39,10 +37,12 @@ pub const NodeManager = struct {
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a new node to the list, and if the temp_node already has a value it returns the pointer to the 2nd one
|
||||
pub fn add(self: *NodeManager, pos: rl.Vector2) ?*Node {
|
||||
const pos_node = self.getSelectedNode(pos);
|
||||
|
||||
if (self.temp_node) |_| {
|
||||
// TODO add road length check before creating the second node / returning
|
||||
return pos_node;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ pub const NodeManager = struct {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns the pointer to the node that is in the snapping node radius of the pos
|
||||
/// or creates a new node and returns pointer to it
|
||||
fn getSelectedNode(self: *NodeManager, pos: rl.Vector2) *Node {
|
||||
for (self.nodes.items) |*node| {
|
||||
if (!rl.checkCollisionPointCircle(pos, node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS))
|
||||
@@ -62,20 +64,28 @@ pub const NodeManager = struct {
|
||||
const node: Node = .init(self.getNewID(), pos);
|
||||
|
||||
self.nodes.appendBounded(node) catch |err| {
|
||||
std.debug.panic("This is because preallocated size got exceeded, TODO fix:\n{}\n", .{err});
|
||||
std.debug.panic("This is because preallocated size got exceeded, TODO fix: {}\n", .{err});
|
||||
};
|
||||
|
||||
return self.getLastRef().?;
|
||||
}
|
||||
|
||||
/// Returns the reference to the last element in the list; returns null if there are no elements in the list
|
||||
fn getLastRef(self: *const NodeManager) ?*Node {
|
||||
const nlen = self.nodes.items.len;
|
||||
return if (nlen == 0) null else &self.nodes.items[nlen - 1];
|
||||
}
|
||||
|
||||
/// generates finds the last element in the list and returns id + 1 or 0 if there are no elements in the list
|
||||
fn getNewID(self: *const NodeManager) usize {
|
||||
const last_ref = self.getLastRef();
|
||||
|
||||
return if (last_ref) |ref| ref.*.id + 1 else 0;
|
||||
}
|
||||
|
||||
fn getLastRef(self: *const NodeManager) ?*Node {
|
||||
const nlen = self.nodes.items.len;
|
||||
return if (nlen == 0) null else &self.nodes.items[nlen - 1];
|
||||
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
|
||||
for (self.nodes.items) |*node| {
|
||||
node.deinit(allocator);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -24,6 +24,7 @@ pub const RoadManager = struct {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new road with nodes; Then it references the said road to the nodes themselves
|
||||
pub fn add(self: *RoadManager, allocator: std.mem.Allocator, start_node: *Node, end_node: *Node) !void {
|
||||
const road: Road = .init(self.getNewID(), start_node, end_node);
|
||||
|
||||
@@ -38,12 +39,14 @@ pub const RoadManager = struct {
|
||||
try end_node.referenceRoad(allocator, road_ref);
|
||||
}
|
||||
|
||||
/// Returns the reference to the last element in the list; returns null if there are no elements in the list
|
||||
fn getLastRef(self: *const RoadManager) ?*Road {
|
||||
const rlen = self.roads.items.len;
|
||||
|
||||
return if (rlen == 0) null else &self.roads.items[rlen - 1];
|
||||
}
|
||||
|
||||
/// generates finds the last element in the list and returns id + 1 or 0 if there are no elements in the list
|
||||
fn getNewID(self: *const RoadManager) usize {
|
||||
const last_ref = self.getLastRef();
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ const c = @import("constants.zig");
|
||||
const Simulator = @import("simulator.zig").Simulator;
|
||||
|
||||
pub fn main() !void {
|
||||
// Allocator
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer {
|
||||
const result = gpa.deinit();
|
||||
@@ -15,6 +16,8 @@ pub fn main() !void {
|
||||
rl.initWindow(c.WIDTH, c.HEIGHT, "Base Road Network");
|
||||
defer rl.closeWindow();
|
||||
|
||||
|
||||
// Used to set to (my) default monitor
|
||||
const monitor = 0;
|
||||
rl.setWindowMonitor(monitor);
|
||||
rl.setTargetFPS(rl.getMonitorRefreshRate(0));
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("constants.zig");
|
||||
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
|
||||
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
|
||||
|
||||
pub const Simulator = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
/// Contains data and functions about nodes
|
||||
node_man: NodeManager,
|
||||
/// Contains data and functions about roads
|
||||
road_man: RoadManager,
|
||||
/// Tracks whether to display node snapping radius
|
||||
display_details: bool,
|
||||
/// Tracks whether to automatically start build a new road after the creation of previous one is finished
|
||||
auto_continue: bool,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) !Simulator {
|
||||
@@ -26,33 +31,63 @@ pub const Simulator = struct {
|
||||
self.road_man.deinit(self.allocator);
|
||||
}
|
||||
|
||||
/// Input handling function that is exposed to the public
|
||||
pub fn handleInput(self: *Simulator) void {
|
||||
self.handleKeyboardInput();
|
||||
self.handleMouseInput();
|
||||
}
|
||||
|
||||
/// Every keyboard event is checked here
|
||||
fn handleKeyboardInput(self: *Simulator) void {
|
||||
self.display_details = rl.isKeyDown(.left_alt);
|
||||
self.auto_continue = rl.isKeyDown(.left_control);
|
||||
|
||||
if (rl.isKeyReleased(.c)) {
|
||||
self.node_man.temp_node = null;
|
||||
self.road_man.roads.clearRetainingCapacity();
|
||||
self.node_man.clear(self.allocator);
|
||||
self.node_man.nodes.clearRetainingCapacity();
|
||||
}
|
||||
}
|
||||
|
||||
/// every mouse event is checked here
|
||||
fn handleMouseInput(self: *Simulator) void {
|
||||
const pos = rl.getMousePosition();
|
||||
if (!rl.isMouseButtonReleased(.left)) return;
|
||||
|
||||
if (self.node_man.add(pos)) |node| {
|
||||
self.road_man.add(self.allocator, self.node_man.temp_node.?, node) catch |err| {
|
||||
std.debug.panic("Error while attempting to add a road:\n{}\n", .{err});
|
||||
std.debug.panic("Error while attempting to add a road: {}\n", .{err});
|
||||
};
|
||||
|
||||
self.node_man.temp_node = if (self.auto_continue) node else null;
|
||||
}
|
||||
}
|
||||
|
||||
/// The main drawing function that is exposed upwards
|
||||
pub fn draw(self: *const Simulator) void {
|
||||
self.road_man.draw();
|
||||
self.node_man.draw(self.display_details);
|
||||
self.drawUI();
|
||||
|
||||
rl.clearBackground(.light_gray);
|
||||
}
|
||||
|
||||
fn drawUI(self: *const Simulator) void {
|
||||
var buf: [1024]u8 = undefined;
|
||||
|
||||
const entities_list =
|
||||
std.fmt.bufPrintZ(
|
||||
&buf,
|
||||
"Nodes: {d}\nRoads: {d}",
|
||||
.{self.node_man.nodes.items.len, self.road_man.roads.items.len}) catch |err| {
|
||||
std.debug.panic("Insufficient space for displaying entire entity list: {}\n", .{err});
|
||||
};
|
||||
|
||||
rl.drawText(
|
||||
entities_list,
|
||||
c.WIDTH - 13 * @as(i32, @intCast(entities_list.len)),
|
||||
c.HEIGHT - 2 * c.TEXT_SIZE,
|
||||
c.TEXT_SIZE, .black);
|
||||
}
|
||||
};
|
||||
@@ -1 +0,0 @@
|
||||
pub const Vector2 = @import("raylib").Vector2;
|
||||
Reference in New Issue
Block a user