This commit is contained in:
2026-07-19 15:04:15 +08:00
parent c0ee537e96
commit fc3bc322a5
2 changed files with 70 additions and 0 deletions
@@ -1049,6 +1049,72 @@ public class CreateFishManager : MonoSingleton<CreateFishManager>
WebSocketSendList.SyncGameData(sgd);
}
/// <summary>
/// 进度条加载结束时房主调用,创建一批初始鱼(进度 0.2~0.8),让屏幕开局就有鱼不会显得空。
/// 只在房主端调用,通过 SyncGameData 广播给其他客户端,所有客户端收到后按 time 字段计算 moveTime,
/// 自然处于路径中段。
/// </summary>
/// <param name="count">要创建的鱼数量</param>
public void CreateInitialFishes(int count)
{
if (!fangzhu) return;
if (spawnList == null || spawnList.Count == 0) return;
if (FishDatas == null || FishDatas.Count == 0) return;
// 只选普通鱼(type==1),鱼阵留给正常刷怪逻辑
var normalFishes = spawnList.Where(f => f.type == 1).ToList();
if (normalFishes.Count == 0) return;
int actualCount = Mathf.Min(count, normalFishes.Count * 3); // 避免极端值
var created = new HashSet<string>(); // 防止 guid 重复(虽然概率极低)
for (int i = 0; i < actualCount; i++)
{
var fish = normalFishes[Random.Range(0, normalFishes.Count)];
// 从该鱼的路径列表里随机选一条
if (!FishDatas.TryGetValue(fish.fishNumber, out var ft)) continue;
if (ft.pathList == null || ft.pathList.Count == 0) continue;
int pathId = ft.pathList[Random.Range(0, ft.pathList.Count)];
// ⭐ 关键:通过 time 字段控制鱼已走过的进度
// mc.moveTime = (CurrentTimestampMs - time) / 1000f
// 想让 progress = moveTime / times ∈ [0.2, 0.8]
// → time = CurrentTimestampMs - progress * times * 1000
float progress = Random.Range(0.2f, 0.8f);
float moveTimeSec = progress * fish.times;
long timeOffsetMs = (long)(moveTimeSec * 1000f);
long sendTime = BeijingTimeManager.Instance.CurrentTimestampMs - timeOffsetMs;
string guid = Guid.NewGuid().ToString();
int safety = 0;
while (created.Contains(guid) && safety < 10)
{
guid = Guid.NewGuid().ToString();
safety++;
}
created.Add(guid);
var sgd = new SyncGameData
{
sitPos = GameManager.Instance.gameData.roomJoinNotice.position,
frameType = FrameDataType.createFish,
time = sendTime,
frameData = new FrameData_CreateFish
{
fishNumber = fish.fishNumber,
guid = guid,
moveLineId = pathId.ToString()
}
};
WebSocketSendList.SyncGameData(sgd);
}
Debug.Log($"[CreateFishManager] 初始鱼已创建: {actualCount} 条 (房主广播)");
}
#endregion
#region 创建鱼