promet-sim/src/area/spawn-area.zig

53 lines
2.0 KiB
Zig

const std = @import("std");
const rl = @import("raylib");
const structures = @import("../structures.zig");
const globals = @import("../globals.zig");
const car = @import("../car/car.zig");
pub const SpawnArea = struct {
area: structures.AreaLocation,
location: rl.Vector2,
width: i32,
height: i32,
pub fn init(area_loc: structures.AreaLocation) SpawnArea {
var new_spawn = SpawnArea{
.area = area_loc,
.location = undefined,
.width = 100,
.height = 50,
};
// need to do this outside the "constructor", as the function needs reference to self
new_spawn.setLocation();
return new_spawn;
}
pub fn setLocation(self: *SpawnArea) void {
self.location = switch (self.area) {
.top_left => rl.Vector2{ .x = 0, .y = 0 },
.top_right => rl.Vector2{ .x = globals.getScreenWidthF32() - @as(f32, @floatFromInt(self.width)) * globals.getScale(), .y = 0 },
.bottom_left => rl.Vector2{ .x = 0, .y = globals.getScreenHeightF32() - @as(f32, @floatFromInt(self.height)) * globals.getScale() },
.bottom_right => rl.Vector2{ .x = globals.getScreenWidthF32() - @as(f32, @floatFromInt(self.width)) * globals.getScale(), .y = globals.getScreenHeightF32() - @as(f32, @floatFromInt(self.height)) * globals.getScale() },
};
}
pub fn getNodeLocation(self: *const SpawnArea) rl.Vector2 {
return rl.Vector2{
.x = self.location.x + @as(f32, @floatFromInt(self.width)) * globals.getScale() / 2.0,
.y = self.location.y + @as(f32, @floatFromInt(self.height)) * globals.getScale() / 2.0,
};
}
pub fn draw(self: *const SpawnArea) void {
const rect = rl.Rectangle{
.x = self.location.x,
.y = self.location.y,
.width = @as(f32, @floatFromInt(self.width)) * globals.getScale(),
.height = @as(f32, @floatFromInt(self.height)) * globals.getScale(),
};
rl.drawRectangleRec(rect, .dark_gray);
}
};