refactor(scene): decouple player level/position from notifying systems

This commit is contained in:
xeon
2026-02-04 00:45:37 +03:00
parent 7a91e1e975
commit f96fb51e3c
3 changed files with 46 additions and 15 deletions

View File

@@ -15,10 +15,32 @@ pub const PlayerId = struct {
uid: mem.LimitedString(max_length),
};
// TODO: this can be made a persistent Player component.
pub const Location = struct {
const default_level = "map02_lv001";
level: i32,
position: [3]f32,
fn createDefault(assets: *const Assets) Location {
const level_config = assets.level_config_table.getPtr(default_level).?;
return .{
.level = level_config.idNum,
.position = .{
level_config.playerInitPos.x,
level_config.playerInitPos.y,
level_config.playerInitPos.z,
},
};
}
};
player_id: PlayerId,
session: *Session, // TODO: should it be here this way? Do we need an abstraction?
res: logic.Resource,
player: logic.Player,
location: Location,
pub fn init(
session: *Session,
@@ -34,6 +56,7 @@ pub fn init(
.session = session,
.player = player,
.res = .init(assets, io),
.location = .createDefault(assets),
};
}
@@ -48,6 +71,7 @@ pub const GetComponentError = error{
pub fn getComponentByType(world: *World, comptime T: type) GetComponentError!T {
switch (T) {
PlayerId => return world.player_id,
*Location, *const Location => return &world.location,
*Session => return world.session,
*logic.Resource.PingTimer => return &world.res.ping_timer,
*const Assets => return world.res.assets,

View File

@@ -1,5 +1,7 @@
pub const Login = struct {};
pub const ChangeSceneBegin = struct {};
pub const CharBagTeamModified = struct {
team_index: usize,
modification: enum {

View File

@@ -8,32 +8,37 @@ const Player = logic.Player;
const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator;
const default_level = "map02_lv001";
pub fn enterSceneOnLogin(
rx: logic.event.Receiver(.login),
tx: logic.event.Sender(.change_scene_begin),
) !void {
_ = rx;
try tx.send(.{});
}
pub fn beginChangingScene(
rx: logic.event.Receiver(.change_scene_begin),
session: *Session,
assets: *const Assets,
base_comp: Player.Component(.base),
location: *const logic.World.Location,
) !void {
_ = rx;
const level_config = assets.level_config_table.getPtr(default_level).?;
const position: pb.VECTOR = .{
.X = level_config.playerInitPos.x,
.Y = level_config.playerInitPos.y,
.Z = level_config.playerInitPos.z,
.X = location.position[0],
.Y = location.position[1],
.Z = location.position[2],
};
try session.send(pb.SC_CHANGE_SCENE_BEGIN_NOTIFY{
.scene_num_id = level_config.idNum,
.scene_num_id = location.level,
.position = position,
.pass_through_data = .init,
});
try session.send(pb.SC_ENTER_SCENE_NOTIFY{
.role_id = base_comp.data.role_id,
.scene_num_id = level_config.idNum,
.scene_num_id = location.level,
.position = position,
.pass_through_data = .init,
});
@@ -58,6 +63,7 @@ pub fn syncSelfScene(
session: *Session,
arena: logic.Resource.Allocator(.arena),
char_bag: logic.Player.Component(.char_bag),
location: *const logic.World.Location,
assets: *const Assets,
) !void {
const reason: pb.SELF_INFO_REASON_TYPE = switch (rx.payload.reason) {
@@ -65,18 +71,17 @@ pub fn syncSelfScene(
.team_modified => .SLR_CHANGE_TEAM,
};
const level_config = assets.level_config_table.getPtr(default_level).?;
const position: pb.VECTOR = .{
.X = level_config.playerInitPos.x,
.Y = level_config.playerInitPos.y,
.Z = level_config.playerInitPos.z,
.X = location.position[0],
.Y = location.position[1],
.Z = location.position[2],
};
const team_index = char_bag.data.meta.curr_team_index;
const leader_index = char_bag.data.teams.items(.leader_index)[team_index];
var self_scene_info: pb.SC_SELF_SCENE_INFO = .{
.scene_num_id = level_config.idNum,
.scene_num_id = location.level,
.self_info_reason = @intFromEnum(reason),
.teamInfo = .{
.team_type = .CHAR_BAG_TEAM_TYPE_MAIN,
@@ -106,7 +111,7 @@ pub fn syncSelfScene(
.templateid = char_template_id,
.position = position,
.rotation = .{},
.scene_num_id = level_config.idNum,
.scene_num_id = location.level,
},
};