Schedule interval improvement

This commit is contained in:
ookami125 2025-05-03 00:31:26 -04:00
parent 04cb434a60
commit f2ef9bbbc3
2 changed files with 27 additions and 4 deletions

View file

@ -8,6 +8,7 @@ comptime { _ = spacetime; }
const stdb_math = @import("spacetime/math.zig");
const DbVector2 = stdb_math.DbVector2;
const DbVector3 = stdb_math.DbVector3;
const ScheduleAt = spacetime.ScheduleAt;
const START_PLAYER_MASS: u32 = 15;
const START_PLAYER_SPEED: u32 = 10;
@ -270,15 +271,15 @@ pub fn init(ctx: *spacetime.ReducerContext) !void {
});
_ = try ctx.db.get("circle_decay_timer").insert(CircleDecayTimer {
.scheduled_id = 0,
.scheduled_at = .{ .Interval = .{ .__time_duration_micros__ = 5 * std.time.us_per_s }},
.scheduled_at = ScheduleAt.interval(5, .Seconds),
});
_ = try ctx.db.get("spawn_food_timer").insert(SpawnFoodTimer {
.scheduled_id = 0,
.scheduled_at = .{ .Interval = .{ .__time_duration_micros__ = 500 * std.time.us_per_ms }}
.scheduled_at = ScheduleAt.interval(500, .Milliseconds),
});
_ = try ctx.db.get("move_all_players_timer").insert(MoveAllPlayersTimer {
.scheduled_id = 0,
.scheduled_at = .{ .Interval = .{ .__time_duration_micros__ = 50 * std.time.us_per_ms }}
.scheduled_at = ScheduleAt.interval(50, .Milliseconds),
});
}

View file

@ -91,15 +91,31 @@ pub const Timestamp = struct {
};
pub const TimeUnit = enum {
Minutes,
Seconds,
Milliseconds,
Microseconds,
};
pub const TimeDuration = struct {
__time_duration_micros__: i64,
pub fn as_f32(self: @This(), unit: TimeUnit) f32 {
const micros: f32 = @floatFromInt(self.__time_duration_micros__);
return switch(unit) {
.Seconds => @as(f32, @floatFromInt(self.__time_duration_micros__)) / std.time.us_per_s,
.Minutes => micros / std.time.us_per_min,
.Seconds => micros / std.time.us_per_s,
.Milliseconds => micros / std.time.us_per_ms,
.Microseconds => micros,
};
}
pub fn create(time: f32, unit: TimeUnit) TimeDuration {
return switch(unit) {
.Minutes => .{ .__time_duration_micros__ = time * std.time.us_per_min},
.Seconds => .{ .__time_duration_micros__ = time * std.time.us_per_s},
.Milliseconds => .{ .__time_duration_micros__ = time * std.time.us_per_ms},
.Microseconds => .{ .__time_duration_micros__ = time }
};
}
};
@ -117,6 +133,12 @@ pub const ScheduleAt = union(enum){
}
};
}
pub fn interval(time: f32, unit: TimeUnit) ScheduleAt {
return .{
.Interval = TimeDuration.create(time, unit),
};
}
};
pub const ConnectionId = struct {