Compare commits
2 Commits
edfc54b927
...
37e2f36ae9
| Author | SHA1 | Date | |
|---|---|---|---|
| 37e2f36ae9 | |||
| ccbf7344b5 |
@@ -1,12 +1,26 @@
|
|||||||
const clr = @import("raylib").Color;
|
const clr = @import("raylib").Color;
|
||||||
|
|
||||||
pub const WIDTH = 2560;
|
/// Screen Width
|
||||||
pub const HEIGHT = 1440;
|
pub const WIDTH = 1366;
|
||||||
|
/// Screen Height
|
||||||
|
pub const HEIGHT = 768;
|
||||||
|
pub const BACKGROUND_COLOR = clr.dark_gray;
|
||||||
|
pub const ALLOC_SIZE = 50;
|
||||||
|
|
||||||
|
/// Base node radius
|
||||||
pub const NODE_RADIUS = 20;
|
pub const NODE_RADIUS = 20;
|
||||||
|
/// The radius around node center where no new node gets created but rather it snaps onto existing one
|
||||||
|
pub const NODE_SNAP_RADIUS = 3 * NODE_RADIUS;
|
||||||
|
/// Regular (finished) node colour
|
||||||
pub const NODE_COLOUR = clr.brown;
|
pub const NODE_COLOUR = clr.brown;
|
||||||
|
/// Temporary node colour - currently being pulled from
|
||||||
pub const NODE_TEMP_COLOUR = clr.orange;
|
pub const NODE_TEMP_COLOUR = clr.orange;
|
||||||
|
/// The colour of the node being at the cursor
|
||||||
pub const NODE_CURSOR_COLOUR = clr.blue;
|
pub const NODE_CURSOR_COLOUR = clr.blue;
|
||||||
|
|
||||||
|
/// Road (line) size
|
||||||
pub const ROAD_SIZE = 20;
|
pub const ROAD_SIZE = 20;
|
||||||
|
/// Regular road colour
|
||||||
pub const ROAD_COLOUR = clr.black;
|
pub const ROAD_COLOUR = clr.black;
|
||||||
|
/// Colour of the road that is highlighted
|
||||||
|
pub const ROAD_HIGHLIGHTED_COLOUR = clr.green;
|
||||||
|
|||||||
@@ -1,21 +1,42 @@
|
|||||||
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
const c = @import("../constants.zig");
|
const c = @import("../constants.zig");
|
||||||
|
const Road = @import("road.zig").Road;
|
||||||
|
|
||||||
pub const Node = struct {
|
pub const Node = struct {
|
||||||
id: usize,
|
id: usize,
|
||||||
pos: rl.Vector2,
|
pos: rl.Vector2,
|
||||||
|
roads: std.ArrayList(*Road),
|
||||||
|
|
||||||
pub fn init(new_id: usize, new_pos: rl.Vector2) Node {
|
pub fn init(new_id: usize, new_pos: rl.Vector2) Node {
|
||||||
return .{
|
return .{
|
||||||
.id = new_id,
|
.id = new_id,
|
||||||
.pos = new_pos,
|
.pos = new_pos,
|
||||||
|
.roads = .empty,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Node, allocator: std.mem.Allocator) void {
|
||||||
|
self.roads.deinit(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const Node, direct_colour: ?rl.Color) void {
|
pub fn draw(self: *const Node, direct_colour: ?rl.Color) void {
|
||||||
const colour = if (direct_colour) |clr| clr else c.NODE_COLOUR;
|
const colour = if (direct_colour) |clr| clr else c.NODE_COLOUR;
|
||||||
|
|
||||||
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
|
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn posWithinRadius(self: *const Node, pos: rl.Vector2) bool {
|
||||||
|
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_SNAP_RADIUS);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road_to_add: *Road) !void {
|
||||||
|
// Note the road_to_add pointer must be one from the roads list as otherwise the pointer is dangling one
|
||||||
|
for (self.roads.items) |road| {
|
||||||
|
if (road.*.id == road_to_add.*.id) return error.RoadAlreadyReferenced;
|
||||||
|
}
|
||||||
|
|
||||||
|
try self.roads.append(allocator, road_to_add);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -6,28 +6,33 @@ const Node = @import("node.zig").Node;
|
|||||||
const Road = @import("road.zig").Road;
|
const Road = @import("road.zig").Road;
|
||||||
|
|
||||||
pub const NodeManager = struct {
|
pub const NodeManager = struct {
|
||||||
|
next_id: usize,
|
||||||
nodes: std.ArrayList(Node),
|
nodes: std.ArrayList(Node),
|
||||||
temp_node: ?*Node,
|
temp_node: ?*Node,
|
||||||
|
|
||||||
pub fn init() NodeManager {
|
pub fn init() NodeManager {
|
||||||
return .{
|
return .{
|
||||||
|
.next_id = 0,
|
||||||
.nodes = .empty,
|
.nodes = .empty,
|
||||||
.temp_node = null,
|
.temp_node = null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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| {
|
||||||
|
node.deinit(allocator);
|
||||||
|
}
|
||||||
self.nodes.deinit(allocator);
|
self.nodes.deinit(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const NodeManager, pos: Vector2) void {
|
pub fn draw(self: *const NodeManager, pos: Vector2) void {
|
||||||
for (self.nodes.items) |node| {
|
for (self.nodes.items) |node| {
|
||||||
node.draw();
|
node.draw(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.temp_node) |node| {
|
if (self.temp_node) |node| {
|
||||||
// Temporary node that points at the cursor
|
// Temporary node that points at the cursor
|
||||||
const cur_node = Node.init(0, pos);
|
var cur_node = Node.init(0, pos);
|
||||||
// Temporary road that is to be drawn as one in the making
|
// Temporary road that is to be drawn as one in the making
|
||||||
const road: Road = .init(0, node, &cur_node);
|
const road: Road = .init(0, node, &cur_node);
|
||||||
road.draw();
|
road.draw();
|
||||||
@@ -36,4 +41,32 @@ pub const NodeManager = struct {
|
|||||||
cur_node.draw(c.NODE_CURSOR_COLOUR);
|
cur_node.draw(c.NODE_CURSOR_COLOUR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
|
||||||
|
for (self.nodes.items) |*node| {
|
||||||
|
if (node.posWithinRadius(pos)) return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No node is within that position, so we must create a new one
|
||||||
|
const node: Node = .init(self.getNextID(), pos);
|
||||||
|
try self.nodes.append(allocator, node);
|
||||||
|
|
||||||
|
return &self.nodes.items[self.nodes.items.len - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getNextID(self: *NodeManager) usize {
|
||||||
|
const id = self.next_id;
|
||||||
|
self.next_id += 1;
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
|
||||||
|
self.temp_node = null;
|
||||||
|
for (self.nodes.items) |*node| {
|
||||||
|
node.deinit(allocator);
|
||||||
|
}
|
||||||
|
self.nodes.clearRetainingCapacity();
|
||||||
|
self.next_id = 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Road = @import("road.zig").Road;
|
const Road = @import("road.zig").Road;
|
||||||
|
const Node = @import("node.zig").Node;
|
||||||
|
|
||||||
pub const RoadManager = struct {
|
pub const RoadManager = struct {
|
||||||
|
next_id: usize,
|
||||||
roads: std.ArrayList(Road),
|
roads: std.ArrayList(Road),
|
||||||
|
|
||||||
pub fn init() RoadManager {
|
pub fn init() RoadManager {
|
||||||
return .{
|
return .{
|
||||||
|
.next_id = 0,
|
||||||
.roads = .empty,
|
.roads = .empty,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -19,4 +22,25 @@ pub const RoadManager = struct {
|
|||||||
road.draw();
|
road.draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn addRoad(self: *RoadManager, allocator: std.mem.Allocator, start: *Node, end: *Node) !void {
|
||||||
|
const road: Road = .init(self.getNextID(), start, end);
|
||||||
|
try self.roads.append(allocator, road);
|
||||||
|
|
||||||
|
const ref = &self.roads.items[self.roads.items.len - 1];
|
||||||
|
try start.referenceRoad(allocator, ref);
|
||||||
|
try end.referenceRoad(allocator, ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getNextID(self: *RoadManager) usize {
|
||||||
|
const id = self.next_id;
|
||||||
|
self.next_id += 1;
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(self: *RoadManager) void {
|
||||||
|
self.roads.clearRetainingCapacity();
|
||||||
|
self.next_id = 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
16
src/main.zig
16
src/main.zig
@@ -4,12 +4,28 @@ const rl = @import("raylib");
|
|||||||
const c = @import("constants.zig");
|
const c = @import("constants.zig");
|
||||||
const Simulator = @import("simulator.zig").Simulator;
|
const Simulator = @import("simulator.zig").Simulator;
|
||||||
|
|
||||||
|
// TODO PLANS
|
||||||
|
// Implement that roads, nodes in managers will be stored as pointers (allocated in heap)
|
||||||
|
// So when appending causes the list to relocate, nothing will have to change
|
||||||
|
// since the allocated nodes/roads will remain where they were
|
||||||
|
// Additionally add more robust error and test handling
|
||||||
|
|
||||||
pub fn main(init: std.process.Init) !void {
|
pub fn main(init: std.process.Init) !void {
|
||||||
const allocator = init.gpa;
|
const allocator = init.gpa;
|
||||||
|
|
||||||
|
rl.setConfigFlags(.{
|
||||||
|
.msaa_4x_hint = true,
|
||||||
|
.window_highdpi = true,
|
||||||
|
});
|
||||||
rl.initWindow(c.WIDTH, c.HEIGHT, "Base Road Network");
|
rl.initWindow(c.WIDTH, c.HEIGHT, "Base Road Network");
|
||||||
defer rl.closeWindow();
|
defer rl.closeWindow();
|
||||||
|
|
||||||
|
const monitor = 0;
|
||||||
|
rl.setWindowMonitor(monitor);
|
||||||
|
rl.setTargetFPS(rl.getMonitorRefreshRate(monitor));
|
||||||
|
|
||||||
var sim: Simulator = .init(allocator);
|
var sim: Simulator = .init(allocator);
|
||||||
|
defer sim.deinit();
|
||||||
|
|
||||||
while (!rl.windowShouldClose()) {
|
while (!rl.windowShouldClose()) {
|
||||||
rl.beginDrawing();
|
rl.beginDrawing();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
|
const c = @import("constants.zig");
|
||||||
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;
|
||||||
|
|
||||||
@@ -8,12 +9,15 @@ pub const Simulator = struct {
|
|||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
node_man: NodeManager,
|
node_man: NodeManager,
|
||||||
road_man: RoadManager,
|
road_man: RoadManager,
|
||||||
|
// vars
|
||||||
|
auto_continue: bool,
|
||||||
|
|
||||||
pub fn init(new_allocator: std.mem.Allocator) Simulator {
|
pub fn init(new_allocator: std.mem.Allocator) Simulator {
|
||||||
return .{
|
return .{
|
||||||
.allocator = new_allocator,
|
.allocator = new_allocator,
|
||||||
.node_man = .init(),
|
.node_man = .init(),
|
||||||
.road_man = .init(),
|
.road_man = .init(),
|
||||||
|
.auto_continue = false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,21 +27,48 @@ 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(.light_gray);
|
rl.clearBackground(c.BACKGROUND_COLOR);
|
||||||
|
|
||||||
self.road_man.draw();
|
self.road_man.draw();
|
||||||
self.node_man.draw(pos);
|
self.node_man.draw(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handleInput(self: *Simulator, pos: rl.Vector2) void {
|
pub fn handleInput(self: *Simulator, pos: rl.Vector2) void {
|
||||||
|
self.handleKeyboardInput();
|
||||||
self.handleMouseInput(pos);
|
self.handleMouseInput(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handleKeyboardInput(self: *Simulator) void {
|
||||||
|
self.auto_continue = rl.isKeyDown(.left_control);
|
||||||
|
|
||||||
|
if (rl.isKeyReleased(.c)) self.clear();
|
||||||
|
}
|
||||||
|
|
||||||
fn handleMouseInput(self: *Simulator, pos: rl.Vector2) void {
|
fn handleMouseInput(self: *Simulator, pos: rl.Vector2) void {
|
||||||
if (rl.isMouseButtonReleased(.left)) self.leftClickEvent(pos);
|
if (rl.isMouseButtonReleased(.left)) self.leftClickEvent(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
|
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
|
||||||
// TODO
|
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", .{
|
||||||
|
pos.x, pos.y, err
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (self.node_man.temp_node) |temp| {
|
||||||
|
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});
|
||||||
|
};
|
||||||
|
|
||||||
|
self.node_man.temp_node = if (self.auto_continue) cur_node else null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.node_man.temp_node = cur_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear(self: *Simulator) void {
|
||||||
|
self.road_man.clear();
|
||||||
|
self.node_man.clear(self.allocator);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user