allow spectating as normal players, fix some spectating bugs, change spectate to the keys "," for left, "." for right

This commit is contained in:
bgkillas 2024-08-25 17:10:57 -04:00
parent 81bbd5a96d
commit a4a862963f
4 changed files with 114 additions and 94 deletions

View file

@ -346,7 +346,7 @@ impl WorldManager {
WorldNetMessage::ListenRequest { chunk } => {
let Some(ChunkState::Authority { listeners }) = self.chunk_state.get_mut(&chunk)
else {
warn!("Can't listen for {chunk:?} - not an authority");
//warn!("Can't listen for {chunk:?} - not an authority");
return;
};
listeners.insert(source);
@ -359,7 +359,7 @@ impl WorldManager {
WorldNetMessage::ListenStopRequest { chunk } => {
let Some(ChunkState::Authority { listeners }) = self.chunk_state.get_mut(&chunk)
else {
warn!("Can't stop listen for {chunk:?} - not an authority");
//warn!("Can't stop listen for {chunk:?} - not an authority");
return;
};
listeners.remove(&source);
@ -377,10 +377,10 @@ impl WorldManager {
let Some(ChunkState::Listening { authority: _ }) =
self.chunk_state.get_mut(&delta.chunk_coord)
else {
warn!(
/*warn!(
"Can't handle ListenUpdate for {:?} - not a listener",
delta.chunk_coord
);
);*/
return;
};
self.inbound_model.apply_chunk_delta(&delta);
@ -424,4 +424,4 @@ impl Drop for WorldManager {
impl SaveStateEntry for FxHashMap<ChunkCoord, ChunkData> {
const FILENAME: &'static str = "world_chunks";
}
}

View file

@ -592,74 +592,11 @@ local function teleport_to_next_hm()
end
end
local camera_player = -1
local camera_target
local inventory_target
local function set_camera_pos()
if camera_player < 1 then
camera_player = 1000
end
local i = 0
local cam_target
for peer_id, potential_target in pairs(ctx.players) do
local entity = potential_target.entity
i = i + 1
if i == camera_player or (i == -1 and peer_id == ctx.my_id) then
cam_target = entity
camera_player = i
end
end
if camera_player == 1000 then
camera_player = i
set_camera_pos()
elseif i ~= 0 and i < camera_player then
camera_player = 1
set_camera_pos()
elseif cam_target ~= nil then
local t_x, t_y = EntityGetFirstHitboxCenter(cam_target)
if t_x == nil then
t_x, t_y = EntityGetTransform(cam_target)
end
GameSetCameraPos(t_x, t_y)
if camera_target == nil then
camera_target = ctx.my_player.entity
end
if camera_target ~= cam_target then
if ctx.my_player.entity ~= camera_target and inventory_target ~= nil then
EntityRemoveComponent(camera_target, inventory_target)
end
inventory_target = nil
if ctx.my_player.entity ~= cam_target then
inventory_target = EntityAddComponent2(cam_target, "InventoryGuiComponent")
end
local audio = EntityGetFirstComponent(camera_target, "AudioListenerComponent")
local keep_alive = EntityGetFirstComponent(camera_target, "StreamingKeepAliveComponent")
if audio ~= nil then
EntityRemoveComponent(camera_target, audio)
if camera_target ~= ctx.my_player.ent then
EntityRemoveComponent(camera_target, keep_alive)
end
EntityAddComponent2(cam_target, "AudioListenerComponent")
EntityAddComponent2(cam_target, "StreamingKeepAliveComponent")
end
end
camera_target = cam_target
end
end
local function better_player(length, did_hit, potential_target)
return ((last_length == nil or (not did_hit and ((last_length > length or not last_did_hit) or EntityHasTag(target, "polymorphed"))))
and (not IsInvisible(potential_target) or length < 50*50))
end
local left_held = false
local right_held = false
local function update()
-- No taking control back, even after pressing esc.
ComponentSetValue2(state.control_component, "enabled", false)
@ -893,32 +830,6 @@ local function update()
if (GameGetFrameNum() % 300) == 299 then
teleport_to_next_hm()
end
if InputIsMouseButtonJustDown(1) then
if not left_held then
if camera_player == -1 then
camera_player = 1001
end
camera_player = camera_player - 1
left_held = true
end
elseif InputIsMouseButtonJustDown(2) then
if not right_held then
if camera_player == -1 then
camera_player = 0
end
camera_player = camera_player + 1
right_held = true
end
else
left_held = false
right_held = false
end
set_camera_pos()
ComponentSetValue2(state.gui_component, "mActive", false)
if inventory_target ~= nil then
ComponentSetValue2(inventory_target, "mActive", false)
end
end
function module.on_world_update()

View file

@ -0,0 +1,108 @@
local spectate = {}
local camera_player = -1
local camera_target
local inventory_target
local function set_camera_pos()
if camera_player < 1 and camera_player ~= -1 then
camera_player = 1000
end
local i = 0
local cam_target
for peer_id, potential_target in pairs(ctx.players) do
local entity = potential_target.entity
i = i + 1
if i == camera_player or (i == -1 and peer_id == ctx.my_id) then
cam_target = entity
camera_player = i
end
end
if camera_player == 1000 then
camera_player = i
set_camera_pos()
elseif i ~= 0 and i < camera_player then
camera_player = 1
set_camera_pos()
elseif cam_target ~= nil then
if cam_target == ctx.my_player.entity and not EntityHasTag(ctx.my_player.entity, "ew_notplayer") then
GameSetCameraFree(false)
if camera_target == nil then
camera_target = ctx.my_player.entity
elseif camera_target ~= ctx.my_player.entity then
EntityAddComponent2(cam_target, "AudioListenerComponent")
local audio = EntityGetFirstComponent(camera_target, "AudioListenerComponent")
local keep_alive = EntityGetFirstComponent(camera_target, "StreamingKeepAliveComponent")
EntityRemoveComponent(camera_target, audio)
EntityRemoveComponent(camera_target, keep_alive)
EntityRemoveComponent(camera_target, inventory_target)
camera_target = cam_target
end
return
end
GameSetCameraFree(true)
local t_x, t_y = EntityGetFirstHitboxCenter(cam_target)
if t_x == nil then
t_x, t_y = EntityGetTransform(cam_target)
end
GameSetCameraPos(t_x, t_y)
if camera_target == nil then
camera_target = ctx.my_player.entity
end
if camera_target ~= cam_target then
if ctx.my_player.entity ~= camera_target and inventory_target ~= nil then
EntityRemoveComponent(camera_target, inventory_target)
end
inventory_target = nil
if ctx.my_player.entity ~= cam_target then
inventory_target = EntityAddComponent2(cam_target, "InventoryGuiComponent")
end
local audio = EntityGetFirstComponent(camera_target, "AudioListenerComponent")
local keep_alive = EntityGetFirstComponent(camera_target, "StreamingKeepAliveComponent")
if audio ~= nil then
EntityRemoveComponent(camera_target, audio)
if camera_target ~= ctx.my_player.entity then
EntityRemoveComponent(camera_target, keep_alive)
end
EntityAddComponent2(cam_target, "AudioListenerComponent")
if cam_target ~= ctx.my_player.entity then
EntityAddComponent2(cam_target, "StreamingKeepAliveComponent")
end
end
end
camera_target = cam_target
end
end
local left_held = false
local right_held = false
function spectate.on_world_update()
if InputIsKeyDown(54) then
if not left_held then
if camera_player == -1 then
camera_player = 1001
end
camera_player = camera_player - 1
left_held = true
end
elseif InputIsKeyDown(55) then
if not right_held then
if camera_player == -1 then
camera_player = 0
end
camera_player = camera_player + 1
right_held = true
end
else
left_held = false
right_held = false
end
set_camera_pos()
end
return spectate

View file

@ -90,6 +90,7 @@ local function load_modules()
ctx.load_system("orb_sync")
ctx.load_system("flag_sync")
ctx.load_system("essence_sync")
ctx.load_system("spectate")
-- ctx.load_system("effect_data_sync")
end