|
| 1 | +const std = @import("std"); |
| 2 | +const godot = @import("godot-zig"); |
| 3 | + |
| 4 | +const SpinningCube = struct { |
| 5 | + const Self = @This(); |
| 6 | + pub const BaseClass = godot.MeshInstance; |
| 7 | + |
| 8 | + base: *BaseClass, |
| 9 | + // start: godot.Vector3, |
| 10 | + time: f32, |
| 11 | + rotate_speed: f32 = 1.0, // TODO: this should be a property... |
| 12 | + |
| 13 | + pub fn ready(self: *Self) void { |
| 14 | + _ = self; |
| 15 | + } |
| 16 | + |
| 17 | + pub fn update(self: *Self, delta: f32) void { |
| 18 | + _ = delta; |
| 19 | + // TODO: still crashes for reasons |
| 20 | + // following change was necessary to get this to compile |
| 21 | + // spatial.zig:750: @ptrCast(*const anyopaque, &arg_angle), |
| 22 | + godot.callUp(BaseClass, self.base, "rotateY", .{self.rotate_speed * 0.0001}) catch |err| { |
| 23 | + std.log.err("godot-zig: SpinningCube update: {s}\n", .{err}); |
| 24 | + }; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +fn init() !void { |
| 29 | + try godot.registerClass(SpinningCube); |
| 30 | + try godot.registerMethod(SpinningCube, SpinningCube.ready, "_ready"); |
| 31 | + try godot.registerMethod(SpinningCube, SpinningCube.update, "_physics_process"); |
| 32 | +} |
| 33 | + |
| 34 | +export fn godot_nativescript_init(handle: *anyopaque) void { |
| 35 | + std.log.info("godot-zig: godot_nativescript_init()\n", .{}); |
| 36 | + godot.initNativeScript(handle); |
| 37 | + |
| 38 | + init() catch |err| { |
| 39 | + std.log.err("godot-zig: godot_nativescript_init: spinning-cube: {s}\n", .{err}); |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export fn godot_gdnative_init(options: *godot.c_api.godot_gdnative_init_options) void { |
| 44 | + std.log.info("godot-zig: godot_gdnative_init()\n", .{}); |
| 45 | + godot.init(std.heap.c_allocator, options); |
| 46 | +} |
| 47 | + |
| 48 | +export fn godot_gdnative_terminate(options: *godot.c_api.godot_gdnative_terminate_options) void { |
| 49 | + _ = options; |
| 50 | + std.log.info("godot-zig: godot_gdnative_terminate()\n", .{}); |
| 51 | + godot.terminate(); |
| 52 | +} |
0 commit comments