Skip to content

Commit

Permalink
compiled and tested on 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
peterino2 committed Dec 27, 2022
1 parent 66fe47f commit 53334f9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 92 deletions.
2 changes: 1 addition & 1 deletion engine/modules/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn splitIntoLines(file_contents: []const u8) std.mem.SplitIterator(u8) {
pub fn loadFileAlloc(filename: []const u8, comptime alignment: usize, allocator: std.mem.Allocator) ![]const u8 {
var file = try std.fs.cwd().openFile(filename, .{ .mode = .read_only });
const filesize = (try file.stat()).size;
var buffer: []u8 = try allocator.allocAdvanced(u8, @intCast(u29, alignment), @intCast(usize, filesize), .exact);
var buffer: []u8 = try allocator.alignedAlloc(u8, alignment, filesize);
try file.reader().readNoEof(buffer);
return buffer;
}
Expand Down
2 changes: 1 addition & 1 deletion engine/modules/core/fileUtils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const std = @import("std");
pub fn load_file_alloc(filename: []const u8, comptime alignment: usize, allocator: std.mem.Allocator) ![]const u8 {
var file = try std.fs.cwd().openFile(filename, .{ .mode = .read_only });
const filesize = (try file.stat()).size;
var buffer: []u8 = try allocator.allocAdvanced(u8, @intCast(u29, alignment), filesize, .exact);
var buffer: []u8 = try allocator.alignedAlloc(u8, alignment, filesize);
try file.reader().readNoEof(buffer);
return buffer;
}
Expand Down
50 changes: 18 additions & 32 deletions engine/modules/core/scene.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub const SceneMobilityMode = enum {
};

pub const SceneObjectPosRot = struct {
position: core.Vectorf = .{
.x = 0, .y = 0, .z = 0
},
position: core.Vectorf = .{ .x = 0, .y = 0, .z = 0 },
rotation: core.Rotation = core.Rotation.init(),
scale: core.Vectorf = core.Vectorf.new(1.0, 1.0, 1.0),
};
Expand Down Expand Up @@ -88,16 +86,15 @@ pub const SceneObject = struct {
.transform => {
self._repr.transform = params.transform;
self.posRot.position = core.Vectorf.fromZm(core.zm.mul(params.transform, core.Vectorf.zero().toZm()));
self.posRot.rotation = .{.quat = core.zm.matToQuat(params.transform)};
self.posRot.rotation = .{ .quat = core.zm.matToQuat(params.transform) };
},
.position => {},
.rotation => {},
.positionRotAngles => {},
.positionRot => {},
}

if(shouldUpdate)
{
if (shouldUpdate) {
self.update();
}

Expand All @@ -115,7 +112,7 @@ pub const SceneObjectInitParams = union(enum) {
},
positionRot: struct {
position: core.Vectorf = .{ .x = 0.0, .y = 0.0, .z = 0.0 },
angles: core.Quat = core.zm.quatFromRollPitchYaw(0.0, 0.0, 0.0),
angles: core.Quat = core.zm.qidentity(),
},
};

Expand All @@ -131,7 +128,6 @@ pub const SceneSystem = struct {
pub const Field = SceneSet.Field;
pub const FieldType = SceneSet.FieldType;


// ----- creating and updating objects -----

// scene objects are the primary object type
Expand All @@ -149,27 +145,26 @@ pub const SceneSystem = struct {
return newHandle;
}

pub fn setPosition(self:* @This(), handle: core.ObjectHandle, position: core.Vectorf) void {
pub fn setPosition(self: *@This(), handle: core.ObjectHandle, position: core.Vectorf) void {
self.objects.get(handle, .posRot).?.*.position = position;
}

pub fn setRotation(self:* @This(), handle: core.ObjectHandle, rotation: core.Rotation) void {
pub fn setRotation(self: *@This(), handle: core.ObjectHandle, rotation: core.Rotation) void {
self.objects.get(handle, .posRot).?.*.rotation = rotation;
}

pub fn setScale(self:* @This(), handle: core.ObjectHandle, x: f32, y: f32, z: f32) void {
self.objects.get(handle, .posRot).?.*.scale = .{.x = x, .y = y, .z = z};
pub fn setScale(self: *@This(), handle: core.ObjectHandle, x: f32, y: f32, z: f32) void {
self.objects.get(handle, .posRot).?.*.scale = .{ .x = x, .y = y, .z = z };
}

pub fn setScaleV(self:* @This(), handle: core.ObjectHandle, scale: core.Vectorf) void {
pub fn setScaleV(self: *@This(), handle: core.ObjectHandle, scale: core.Vectorf) void {
self.objects.get(handle, .posRot).?.*.scale = scale;
}

pub fn getPosition(self: *@This(), handle: core.ObjectHandle) core.Vectorf {
return self.objects.get(handle, .posRot).?.position;
}


pub fn getRotation(self: *@This(), handle: core.ObjectHandle) core.Rotation {
return self.objects.get(handle, .posRot).?.rotation;
}
Expand All @@ -185,8 +180,7 @@ pub const SceneSystem = struct {
// ----- subsystem update procedures

// internal update transform function
fn updateTransform(self: *@This(), repr: *SceneObjectRepr, posRot: SceneObjectPosRot) void
{
fn updateTransform(self: *@This(), repr: *SceneObjectRepr, posRot: SceneObjectPosRot) void {
_ = self;

repr.*.transform = core.zm.mul(
Expand All @@ -198,13 +192,10 @@ pub const SceneSystem = struct {
);
}

pub fn updateTransforms(self: *@This()) void
{
for(self.objects.denseItems(._repr)) |*repr, i|
{
pub fn updateTransforms(self: *@This()) void {
for (self.objects.denseItems(._repr)) |*repr, i| {
var settings = self.objects.readDense(i, .settings);
if(settings.sceneMode == .moveable)
{
if (settings.sceneMode == .moveable) {
var posRot = self.objects.readDense(i, .posRot);
self.updateTransform(repr, posRot.*);
}
Expand All @@ -219,21 +210,16 @@ pub const SceneSystem = struct {
};
}

pub fn setMobility(self: *@This(), objectHandle: core.ObjectHandle, mobility: SceneMobilityMode) !void
{
pub fn setMobility(self: *@This(), objectHandle: core.ObjectHandle, mobility: SceneMobilityMode) !void {
var settings = self.objects.get(objectHandle, .settings).?;
if(mobility == .moveable)
{
if (settings.sceneMode == .static)
{
if (mobility == .moveable) {
if (settings.sceneMode == .static) {
try self.dynamicObjects.append(self.allocator, objectHandle);
}
}

if(mobility == .static)
{
if(settings.sceneMode == .moveable)
{
if (mobility == .static) {
if (settings.sceneMode == .moveable) {
core.engine_errs("TODO: Changing sceneMode from moveable back to static is not supported yet.");
unreachable;
}
Expand Down
2 changes: 1 addition & 1 deletion engine/projects/cognesia/animations.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub const SpriteSheet = struct {
allocator: std.mem.Allocator,
animationName: core.Name,
triggerFrame: usize,
func: fn (*anyopaque) void,
func: *const fn (*anyopaque) void,
context: *anyopaque,
) !void {
if (!self.animationCallbacks.contains(animationName.hash)) {
Expand Down
80 changes: 26 additions & 54 deletions engine/projects/cognesia/interactable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ pub const InteractableInterface = struct {
typeSize: usize,
typeAlign: usize,

onInteract: fn (*anyopaque) void,
serialize: fn (*anyopaque, std.mem.Allocator) std.ArrayList(u8),
onInteract: *const fn (*anyopaque) void,
serialize: *const fn (*anyopaque, std.mem.Allocator) std.ArrayList(u8),

pub fn from(comptime TargetType: type) @This() {
pub fn from(comptime TargetType: type) @This() {
const wrappedFuncs = struct {

pub fn onInteract(pointer: *anyopaque) void {
var ptr = @ptrCast(*TargetType, @alignCast(@alignOf(TargetType), pointer));
ptr.onInteract();
Expand All @@ -41,7 +40,6 @@ pub const InteractableInterface = struct {

return self;
}

};

pub const InteractableObject = struct {
Expand All @@ -50,13 +48,11 @@ pub const InteractableObject = struct {
radius: f32,
disabled: bool = false,

pub fn interact(self: *@This()) void
{
pub fn interact(self: *@This()) void {
self.interface.onInteract();
}

pub fn serialize(self: *@This(), allocator: std.mem.Allocator) std.ArrayList(u8)
{
pub fn serialize(self: *@This(), allocator: std.mem.Allocator) std.ArrayList(u8) {
return self.interface.vtable.serialize(self.interface.ptr, allocator);
}
};
Expand All @@ -71,13 +67,11 @@ pub const HalcyonInteractable = struct {
dialogueTag: []const u8,
handle: core.ObjectHandle = undefined,

pub fn onInteract(self: *@This()) void
{
pub fn onInteract(self: *@This()) void {
halcyonSys.startDialogue(self.dialogueTag);
}

pub fn serialize(self: @This(), allocator: std.mem.Allocator) std.ArrayList(u8)
{
pub fn serialize(self: @This(), allocator: std.mem.Allocator) std.ArrayList(u8) {
var ostr = std.ArrayList(u8).init(allocator);
ostr.appendSlice(self.dialogueTag) catch unreachable;

Expand All @@ -89,30 +83,27 @@ pub const InteractableRef = struct {
ptr: *anyopaque,
vtable: *const InteractableInterface,

pub fn makeRef(target: anytype) @This()
{
pub fn makeRef(target: anytype) @This() {
return .{
.ptr = target,
.vtable = &@TypeOf(target.*).InteractableVTable,
};
}

pub fn onInteract(self: *@This()) void
{
pub fn onInteract(self: *@This()) void {
self.vtable.onInteract(self.ptr);
}
};

pub const InteractableObjectSet = core.SparseMultiSet(struct{ object: InteractableObject });
pub const InteractableObjectSet = core.SparseMultiSet(struct { object: InteractableObject });

pub const InteractionSystem = struct {
interactables: InteractableObjectSet,
allocator: std.mem.Allocator,
highLightObject: ?core.ObjectHandle,
talkables: std.AutoHashMapUnmanaged(u32, *HalcyonInteractable),

pub fn init(allocator: std.mem.Allocator) @This()
{
pub fn init(allocator: std.mem.Allocator) @This() {
return .{
.allocator = allocator,
.interactables = InteractableObjectSet.init(allocator),
Expand All @@ -121,73 +112,55 @@ pub const InteractionSystem = struct {
};
}

pub fn addInteractable(self: *@This(), interactable: InteractableObject) !core.ObjectHandle
{
pub fn addInteractable(self: *@This(), interactable: InteractableObject) !core.ObjectHandle {
return try self.interactables.createObject(.{
.object = interactable,
});
}

pub fn addTalkable(self: *@This(), interactableName: core.Name, text: []const u8, position: core.Vectorf, radius: f32) !core.ObjectHandle
{

if(self.talkables.get(interactableName.hash)) |original|
{
pub fn addTalkable(self: *@This(), interactableName: core.Name, text: []const u8, position: core.Vectorf, radius: f32) !core.ObjectHandle {
if (self.talkables.get(interactableName.hash)) |original| {
var handle = original.*.handle;
self.interactables.get(handle, .object).?.*.disabled = false;
self.interactables.get(handle, .object).?.*.position = position;
self.interactables.get(handle, .object).?.*.radius = radius;
return handle;
}
else {
} else {
var testText = try self.allocator.create(HalcyonInteractable);
testText.*.dialogueTag = text;

try self.talkables.put(self.allocator, interactableName.hash, testText);

var interactable: InteractableObject = .{
.interface = InteractableRef.makeRef(testText),
.position = position,
.radius = radius
};
var interactable: InteractableObject = .{ .interface = InteractableRef.makeRef(testText), .position = position, .radius = radius };

var handle = try self.addInteractable(interactable);
testText.*.handle = handle;
return handle;
}


}

pub fn disableAll(self: *@This()) void
{
for(self.interactables.denseItems(.object)) |*interactable|
{
pub fn disableAll(self: *@This()) void {
for (self.interactables.denseItems(.object)) |*interactable| {
interactable.*.disabled = true;
}
}

pub fn getNearestObjectInRange(self: *@This(), position: core.Vectorf, radius: f32, showDebug: bool) ?*InteractableObject
{
pub fn getNearestObjectInRange(self: *@This(), position: core.Vectorf, radius: f32, showDebug: bool) ?*InteractableObject {
// only considers x and z position
var nearest: ?*InteractableObject = null;
var nearestDist: f32 = 100000000;

for(self.interactables.denseItems(.object)) |*interactable|
{
if(interactable.*.disabled)

for (self.interactables.denseItems(.object)) |*interactable| {
if (interactable.*.disabled)
continue;
var distance = interactable.position.sub(position).lengthXZ();
if(showDebug)
{
if (showDebug) {
graphics.debug_draw.debugSphere(interactable.position, interactable.radius, .{
.color = core.Vectorf.new(1.0, 1.0, 0.0),
});
}
if( distance < (interactable.radius + radius) )
{
if(distance < nearestDist)
{
if (distance < (interactable.radius + radius)) {
if (distance < nearestDist) {
nearest = interactable;
nearestDist = distance;
}
Expand All @@ -196,5 +169,4 @@ pub const InteractionSystem = struct {

return nearest;
}

};
};
4 changes: 2 additions & 2 deletions engine/projects/cognesia/papyrus.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ pub const PapyrusSubsystem = struct {
gc.vkd,
gc.allocator,
resources.sprite_mesh_vert.len,
@ptrCast([*]const u32, resources.sprite_mesh_vert),
@ptrCast([*]const u32, &resources.sprite_mesh_vert),
resources.sprite_mesh_frag.len,
@ptrCast([*]const u32, resources.sprite_mesh_frag),
@ptrCast([*]const u32, &resources.sprite_mesh_frag),
);
defer pipelineBuilder.deinit();

Expand Down
2 changes: 1 addition & 1 deletion engine/projects/cognesia/zig-halcyon

0 comments on commit 53334f9

Please sign in to comment.