_some progerss?

This commit is contained in:
2025-09-12 13:33:49 +03:00
parent 95de571eb5
commit 5af8610c0c
4 changed files with 72 additions and 33 deletions

View File

@@ -22,4 +22,16 @@ public static class Utilities
Profiler.EndEvent();
}
}
}
public static class VectorExtensions
{
/*extension(Float3 value)
{
Float2 XY => new Float2(value.X, value.Y);
}*/
public static Float2 XY(this Float3 vec) => new Float2(vec.X, vec.Y);
public static Vector2 XY(this Vector3 vec) => new Vector2(vec.X, vec.Y);
}

View File

@@ -117,12 +117,18 @@ public class World
protected void CreateScene(string sceneNamePrefix, string sceneGuid)
{
string physicsSceneName = $"{sceneNamePrefix}PhysicsScene";
PhysicsScene localPhysicsScene = Physics.FindOrCreateScene(physicsSceneName);
if (false && sceneNamePrefix == "Client")
{
_scene = Level.Scenes.FirstOrDefault(x => !x.Name.StartsWith("Server"));
}
else
{
string physicsSceneName = $"{sceneNamePrefix}PhysicsScene";
PhysicsScene localPhysicsScene = Physics.FindOrCreateScene(physicsSceneName);
Guid guid = JsonSerializer.ParseID(sceneGuid);
string guidStr = JsonSerializer.GetStringID(guid);
string sceneData = $@"
Guid guid = JsonSerializer.ParseID(sceneGuid);
string guidStr = JsonSerializer.GetStringID(guid);
string sceneData = $@"
{{
""ID"": ""{guidStr}"",
""TypeName"": ""FlaxEngine.SceneAsset"",
@@ -145,25 +151,26 @@ public class World
]
}}";
{
var onSceneLoaded = (Scene loadedScene, Guid id) =>
{
if (guid == id)
var onSceneLoaded = (Scene loadedScene, Guid id) =>
{
loadedScene.PhysicsScene = localPhysicsScene;
//scene = loadedScene;
}
};
if (guid == id)
{
loadedScene.PhysicsScene = localPhysicsScene;
//scene = loadedScene;
}
};
try
{
Level.SceneLoaded += onSceneLoaded;
//Level.LoadScene(new SceneReference(guid));
_scene = Level.LoadSceneFromBytes(Encoding.ASCII.GetBytes(sceneData));
}
finally
{
Level.SceneLoaded -= onSceneLoaded;
try
{
Level.SceneLoaded += onSceneLoaded;
//Level.LoadScene(new SceneReference(guid));
_scene = Level.LoadSceneFromBytes(Encoding.ASCII.GetBytes(sceneData));
}
finally
{
Level.SceneLoaded -= onSceneLoaded;
}
}
}
Assert.IsTrue(_scene);
@@ -382,8 +389,8 @@ file class ServerWorld : World
actorState.jumped = networkEvent.Message.ReadBoolean();
//inputState.frame = receivedFrame;
inputState.ViewDelta.X = networkEvent.Message.ReadSingle();
inputState.ViewDelta.Y = networkEvent.Message.ReadSingle();
//inputState.ViewDelta.X = networkEvent.Message.ReadSingle();
//inputState.ViewDelta.Y = networkEvent.Message.ReadSingle();
inputState.MoveForward = networkEvent.Message.ReadSingle();
inputState.MoveRight = networkEvent.Message.ReadSingle();
inputState.Attack = networkEvent.Message.ReadBoolean();
@@ -400,8 +407,9 @@ file class ServerWorld : World
uint playerId = networkEvent.Message.ReadUInt32();
float time = networkEvent.Message.ReadSingle();
float delay = Time.TimeSinceStartup - time;
inputState.ViewDelta.X = networkEvent.Message.ReadSingle();
inputState.ViewDelta.Y = networkEvent.Message.ReadSingle();
inputState.ViewDelta = Float2.Zero;
inputState.ViewAngles.X = networkEvent.Message.ReadSingle();
inputState.ViewAngles.Y = networkEvent.Message.ReadSingle();
inputState.MoveForward = networkEvent.Message.ReadSingle();
inputState.MoveRight = networkEvent.Message.ReadSingle();
inputState.Attack = networkEvent.Message.ReadBoolean();
@@ -595,7 +603,9 @@ file class ClientWorld : World
message.WriteUInt64(Frame);
message.WriteUInt32(player.PlayerId);
message.WriteSingle(Time.TimeSinceStartup);
message.WriteVector2(inputState.ViewDelta);
message.WriteSingle(movementState.viewAngles.X);
message.WriteSingle(movementState.viewAngles.Y);
//message.WriteSingle(movementState.viewAngles.Z);
message.WriteSingle(inputState.MoveForward);
message.WriteSingle(inputState.MoveRight);
message.WriteBoolean(inputState.Attack);

View File

@@ -14,12 +14,14 @@ namespace Game;
public struct PlayerInputState2 : IEquatable<PlayerInputState2>
{
public ulong Frame;
public Float2 ViewDelta;
public Float2 ViewAngles;
public float MoveForward;
public float MoveRight;
public bool Attack;
public bool Jump;
public Float2 ViewDelta; // Only used when gathering input for local player
/*public override bool Equals([NotNullWhen(true)] object obj)
{
if (obj is not PlayerInputState2 other)
@@ -29,12 +31,19 @@ public struct PlayerInputState2 : IEquatable<PlayerInputState2>
public bool Equals(PlayerInputState2 other)
{
return Frame == other.Frame && ViewDelta == other.ViewDelta && MoveForward == other.MoveForward && MoveRight == other.MoveRight && Attack == other.Attack && Jump == other.Jump;
return Frame == other.Frame && ViewAngles == other.ViewAngles && MoveForward == other.MoveForward && MoveRight == other.MoveRight && Attack == other.Attack && Jump == other.Jump && ViewDelta == other.ViewDelta;
}
public static bool operator ==(PlayerInputState2 left, PlayerInputState2 right) => left.Equals(right);
public static bool operator !=(PlayerInputState2 left, PlayerInputState2 right) => !left.Equals(right);
// Apply delta to calculate the final view angles for this state
public void UpdateViewAngles(Float2 lastInputViewAngles)
{
ViewAngles = lastInputViewAngles + ViewDelta;
ViewDelta = Float2.Zero;
}
}
public interface IPlayerInput
@@ -91,7 +100,7 @@ public class PlayerInput2 : IPlayerInput
float turnSpeed = 100.0f;
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * _sensitivity;
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("LookRight"), Input.GetAxisRaw("LookUp")) * Time.DeltaTime * turnSpeed;
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("LookRight"), Input.GetAxisRaw("LookUp")) * Time.DeltaTime * turnSpeed;
_recordState.MoveForward = MathF.MaxMagnitude(_recordState.MoveForward, Input.GetAxis("Vertical"));
_recordState.MoveRight = MathF.MaxMagnitude(_recordState.MoveRight, Input.GetAxis("Horizontal"));
@@ -101,6 +110,10 @@ public class PlayerInput2 : IPlayerInput
if (Input.GetKeyDown(KeyboardKeys.F))
Thread.Sleep(20);
var asdf = Input.GetAxis("Vertical");
if (asdf != 0.0f)
asdf = asdf;
}
public PlayerInputState2 GetState() => _recordState;

View File

@@ -137,7 +137,11 @@ public class PlayerMovement : Script
private PlayerActor playerActor;
public Actor rootActor;
public Float3 viewAngles;
public Float3 viewAngles
{
get => movementState.viewAngles;
set => movementState.viewAngles = value;
}
private Float3 viewAnglesLastFrame;
public uint PlayerId => playerActor ? playerActor.PlayerId : 0;
@@ -271,8 +275,6 @@ public class PlayerMovement : Script
//if (Input is PlayerInputDemo /*&& currentInputFrame2 >= currentInputFrame*/)
// return;
if (World.IsServer)
viewAngles = viewAngles;
Input.SetFrame(World.Frame);
Input.UpdateState();
@@ -473,6 +475,7 @@ public class PlayerMovement : Script
inputState =
{
Frame = currentFrame,
ViewAngles = movementState.viewAngles.XY(),
},
movementState = movementState,
};
@@ -488,7 +491,8 @@ public class PlayerMovement : Script
Input.SetFrame(currentFrame);
Input.UpdateState();
ApplyInputToCamera(frameInfo.inputState, true);
SetCameraEulerAngles(new Float3(frameInfo.inputState.ViewAngles.X, frameInfo.inputState.ViewAngles.Y, viewAngles.Z), true);
//ApplyInputToCamera(frameInfo.inputState, true);
lastFrameInfo = frameInfo;
}
SimulatePlayerMovement(frameInfo.inputState, currentFrame);