diff --git a/src/RustToDust.Ymir/ArenaSession.cs b/src/RustToDust.Ymir/ArenaSession.cs index bedaa5f..931609a 100644 --- a/src/RustToDust.Ymir/ArenaSession.cs +++ b/src/RustToDust.Ymir/ArenaSession.cs @@ -63,41 +63,31 @@ namespace RustToDust.Ymir return; } var c = State.Contenders[contenderId]; - float y = State.Terrain.SampleHeight(x, z) + c.Grid.Spec.HalfHeight + 0.15f; c.X = x; - c.Y = y; c.Z = z; if (yaw.HasValue) c.Yaw = yaw.Value; - if (_contenderBodies.TryGetValue(contenderId, out var handle)) + if (_contenderBodies.TryGetValue(contenderId, out var oldHandle)) { - var view = _world.View; - int lane = handle.Lane; - view.PositionX[lane] = x; - view.PositionY[lane] = y; - view.PositionZ[lane] = z; - view.VelocityX[lane] = 0f; - view.VelocityY[lane] = 0f; - view.VelocityZ[lane] = 0f; - if (yaw.HasValue) - { - var q = Quaternion.CreateFromAxisAngle(Vector3.UnitY, yaw.Value); - view.RotationX[lane] = q.X; - view.RotationY[lane] = q.Y; - view.RotationZ[lane] = q.Z; - view.RotationW[lane] = q.W; - } + _world.RemoveBody(oldHandle); + _contenderBodies.Remove(contenderId); + } + if (c.Alive) + { + SpawnContenderBody(c); } } public void SetSpectatorPose(int spectatorId, float x, float z, float? yaw = null) { SpectatorState? spec = null; + int index = -1; for (int i = 0; i < State.Spectators.Spectators.Count; i++) { if (State.Spectators.Spectators[i].Id == spectatorId) { spec = State.Spectators.Spectators[i]; + index = i; break; } } @@ -106,30 +96,18 @@ namespace RustToDust.Ymir return; } - float y = State.Terrain.SampleHeight(x, z) + spec.Grid.Spec.HalfHeight + 0.15f; spec.X = x; - spec.Y = y; spec.Z = z; if (yaw.HasValue) spec.Yaw = yaw.Value; - if (_spectatorBodies.TryGetValue(spectatorId, out var handle)) + if (_spectatorBodies.TryGetValue(spectatorId, out var oldHandle)) { - var view = _world.View; - int lane = handle.Lane; - view.PositionX[lane] = x; - view.PositionY[lane] = y; - view.PositionZ[lane] = z; - view.VelocityX[lane] = 0f; - view.VelocityY[lane] = 0f; - view.VelocityZ[lane] = 0f; - if (yaw.HasValue) - { - var q = Quaternion.CreateFromAxisAngle(Vector3.UnitY, yaw.Value); - view.RotationX[lane] = q.X; - view.RotationY[lane] = q.Y; - view.RotationZ[lane] = q.Z; - view.RotationW[lane] = q.W; - } + _world.RemoveBody(oldHandle); + _spectatorBodies.Remove(spectatorId); + } + if (spec.Alive) + { + SpawnSpectatorBody(index, spec); } } @@ -318,29 +296,33 @@ namespace RustToDust.Ymir { for (int i = 0; i < State.Spectators.Spectators.Count; i++) { - var s = State.Spectators.Spectators[i]; - var spec = s.Grid.Spec; - float volume = 8f * spec.HalfWidth * spec.HalfHeight * spec.HalfLength; - float density = spec.MassKg / MathF.Max(0.1f, volume); - var q = Quaternion.CreateFromAxisAngle(Vector3.UnitY, s.Yaw); - ulong stableId = SpectatorStableIdBase + (ulong)s.Id; - float spawnY = State.Terrain.SampleHeight(s.X, s.Z) + spec.HalfHeight + 0.15f; - s.Y = spawnY; - - var handle = _world.AddBox( - stableId, - (s.X, spawnY, s.Z), - (spec.HalfWidth, spec.HalfHeight, spec.HalfLength), - density, - (q.X, q.Y, q.Z, q.W), - friction: 0.2f, - restitution: 0.1f); - - _spectatorBodies[s.Id] = handle; - _entityByStableId[stableId] = (false, i); + SpawnSpectatorBody(i, State.Spectators.Spectators[i]); } } + private void SpawnSpectatorBody(int index, SpectatorState s) + { + var spec = s.Grid.Spec; + float volume = 8f * spec.HalfWidth * spec.HalfHeight * spec.HalfLength; + float density = spec.MassKg / MathF.Max(0.1f, volume); + var q = Quaternion.CreateFromAxisAngle(Vector3.UnitY, s.Yaw); + ulong stableId = SpectatorStableIdBase + (ulong)s.Id; + float spawnY = State.Terrain.SampleHeight(s.X, s.Z) + spec.HalfHeight + 0.15f; + s.Y = spawnY; + + var handle = _world.AddBox( + stableId, + (s.X, spawnY, s.Z), + (spec.HalfWidth, spec.HalfHeight, spec.HalfLength), + density, + (q.X, q.Y, q.Z, q.W), + friction: 0.2f, + restitution: 0.1f); + + _spectatorBodies[s.Id] = handle; + _entityByStableId[stableId] = (false, index); + } + private void UpdateRingClearance() { Span contacts = stackalloc RingContact[State.Contenders.Count];