Implemented Entity tagged union for highlighted entity (similar to abstract class in some ways)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
const std = @import("std");
|
||||
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 RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
|
||||
|
||||
@@ -18,6 +20,12 @@ pub const Simulator = struct {
|
||||
/// Tracks whether the system will delete the road cursor is pointed at
|
||||
/// (in such case, the road-to-be-deleted will also be highlighted)
|
||||
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)
|
||||
show_connections: bool,
|
||||
highlighted_entity: ?st.Entity,
|
||||
|
||||
/// Constructor for convenience
|
||||
pub fn init(new_allocator: std.mem.Allocator) Simulator {
|
||||
@@ -27,6 +35,8 @@ pub const Simulator = struct {
|
||||
.road_man = .init(),
|
||||
.auto_continue = false,
|
||||
.delete_mode = false,
|
||||
.show_connections = false,
|
||||
.highlighted_entity = null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -40,13 +50,21 @@ pub const Simulator = struct {
|
||||
pub fn draw(self: *const Simulator, pos: rl.Vector2) void {
|
||||
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);
|
||||
}
|
||||
|
||||
/// Update tick
|
||||
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
|
||||
@@ -59,6 +77,7 @@ pub const Simulator = struct {
|
||||
fn handleKeyboardInput(self: *Simulator) void {
|
||||
self.auto_continue = rl.isKeyDown(.left_control);
|
||||
self.delete_mode = rl.isKeyDown(.left_shift);
|
||||
self.show_connections = rl.isKeyDown(.left_alt);
|
||||
|
||||
if (rl.isKeyReleased(.c)) self.clear() catch |err| {
|
||||
std.debug.panic("Failed to clear the entities: {}\n", .{err});
|
||||
@@ -72,7 +91,7 @@ pub const Simulator = struct {
|
||||
|
||||
/// Function that handles functionality that executes upon left click
|
||||
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| {
|
||||
std.debug.panic("Failed to delete the road: {}\n", .{err});
|
||||
};
|
||||
@@ -104,8 +123,9 @@ pub const Simulator = struct {
|
||||
|
||||
/// User initiated road destroying functionality
|
||||
fn delete_road(self: *Simulator) !void {
|
||||
if (self.road_man.highlighted_road == null) return;
|
||||
const h_road = self.road_man.highlighted_road.?;
|
||||
// We can trust this because this only gets called if valid and if type is 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 end_node = h_road.nodes[1];
|
||||
@@ -113,7 +133,6 @@ pub const Simulator = struct {
|
||||
self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
|
||||
std.debug.panic("Road deletion failed: {}\n", .{err});
|
||||
};
|
||||
self.road_man.highlighted_road = null;
|
||||
|
||||
if (start_node.roads.items.len == 0)
|
||||
try self.node_man.deleteNode(self.allocator, start_node);
|
||||
@@ -127,4 +146,19 @@ pub const Simulator = struct {
|
||||
self.road_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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user