mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交
This commit is contained in:
@@ -9,7 +9,7 @@ namespace Script
|
||||
|
||||
public static readonly JNGSocket Socket = new JNGSocket();
|
||||
public static readonly JNGSyncFrame Sync = new JNGSyncFrame();
|
||||
public static readonly JAPI Api = new(new JAPIConfig(){BaseURL = "http://localhost:8080"});
|
||||
public static readonly JAPI Api = new(new JAPIConfig(){BaseURL = "http://192.168.0.118:8080"});
|
||||
public static readonly EventDispatcher Event = EventDispatcher.Event;
|
||||
|
||||
public static SystemBase[] System()
|
||||
|
@@ -9,7 +9,8 @@ namespace Script.AppImpl
|
||||
{
|
||||
protected override async UniTask<string> GetUrl()
|
||||
{
|
||||
return "ws://localhost:8080/websocket";
|
||||
await UniTask.NextFrame();
|
||||
return "ws://192.168.0.118:8080/websocket";
|
||||
}
|
||||
}
|
||||
}
|
@@ -13,10 +13,13 @@ namespace Script.AppImpl
|
||||
|
||||
protected override async UniTask<JNFrameInfos> OnServerData(int start, int end)
|
||||
{
|
||||
Debug.Log($"OnServerData - {start}");
|
||||
var data = (await App.Api.GetByte($"/sync/frame?start={start}"));
|
||||
if (data is { Length: > 0 })
|
||||
{
|
||||
return JNFrameInfos.Parser.ParseFrom(data);
|
||||
JNFrameInfos info = JNFrameInfos.Parser.ParseFrom(data);
|
||||
Debug.Log($"OnServerData - {start} 结束");
|
||||
return info;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@@ -12,6 +12,10 @@ namespace Script
|
||||
{
|
||||
public class Main : MonoBehaviour
|
||||
{
|
||||
private void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(this.gameObject);
|
||||
}
|
||||
|
||||
async void Start()
|
||||
{
|
||||
|
@@ -1,17 +1,44 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Script.AppImpl;
|
||||
using Script.battle;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Script
|
||||
{
|
||||
public class UIMain : MonoBehaviour
|
||||
{
|
||||
public Text frameLabel;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(this.gameObject);
|
||||
}
|
||||
|
||||
//重置
|
||||
public void OnClickReset()
|
||||
{
|
||||
GBattleModeManager.Instance.Open(GBattleMode.Default).Forget();
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
float f = 1.23456789f;
|
||||
for (int i = 10 - 1; i >= 0; i--)
|
||||
{
|
||||
// 进行一些基本的浮点数运算
|
||||
f = f + f;
|
||||
f = f - (f / 2);
|
||||
f = f * (f / 2);
|
||||
f = f / (f / 2);
|
||||
f = (float)Math.Sqrt(f);
|
||||
f = Math.Abs(f);
|
||||
f = (float)Math.Log(f);
|
||||
f = (float)Math.Log10(f);
|
||||
f = (float)Math.Exp(f);
|
||||
}
|
||||
this.frameLabel.text = $"浮点数计算: {f} 服务器帧数: {App.Sync.NServerFrame} 本地运行帧 {App.Sync.NLocalFrame} 本地已执行帧 {App.Sync.NLocalRunFrame} 是否正在请求{App.Sync.IsRequestServerData} FPS:{1f/Time.deltaTime} this.DyTime():{App.Sync.DyTime()}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -55,6 +55,7 @@ namespace Script.battle
|
||||
public async UniTask OnReset()
|
||||
{
|
||||
await this.UnloadScene();
|
||||
await UniTask.NextFrame();
|
||||
App.Sync.OnReset();
|
||||
await this.LoadScene();
|
||||
_current = GBattleMode.Not;
|
||||
@@ -72,7 +73,7 @@ namespace Script.battle
|
||||
GBattleMode mode = this._current;
|
||||
if (mode == GBattleMode.Not) return;
|
||||
Debug.Log($"[GBattleModeManager] 打开场景{GetWorldName(mode)}");
|
||||
await SceneManager.LoadSceneAsync(GetWorldName(mode), LoadSceneMode.Additive);
|
||||
await SceneManager.LoadSceneAsync(GetWorldName(mode));
|
||||
}
|
||||
|
||||
//销毁所有场景
|
||||
@@ -80,17 +81,19 @@ namespace Script.battle
|
||||
{
|
||||
|
||||
Debug.Log($"[GBattleModeManager] 关闭场景");
|
||||
foreach (var world in Worlds)
|
||||
{
|
||||
try
|
||||
{
|
||||
await SceneManager.UnloadSceneAsync(world);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
await UniTask.NextFrame();
|
||||
//
|
||||
// for (int i = SceneManager.sceneCount - 1; i >= 0; i--)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// await SceneManager.UnloadSceneAsync(SceneManager.GetSceneAt(i));
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// // ignored
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
130
JNFrame/Assets/Script/battle/mode/DemoCallbacks.cs
Normal file
130
JNFrame/Assets/Script/battle/mode/DemoCallbacks.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using BepuUtilities;
|
||||
using BepuUtilities.Memory;
|
||||
using BepuPhysics;
|
||||
using BepuPhysics.Collidables;
|
||||
using BepuPhysics.CollisionDetection;
|
||||
using BepuPhysics.Constraints;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Demos
|
||||
{
|
||||
public struct DemoPoseIntegratorCallbacks : IPoseIntegratorCallbacks
|
||||
{
|
||||
/// <summary>
|
||||
/// Gravity to apply to dynamic bodies in the simulation.
|
||||
/// </summary>
|
||||
public Vector3 Gravity;
|
||||
/// <summary>
|
||||
/// Fraction of dynamic body linear velocity to remove per unit of time. Values range from 0 to 1. 0 is fully undamped, while values very close to 1 will remove most velocity.
|
||||
/// </summary>
|
||||
public float LinearDamping;
|
||||
/// <summary>
|
||||
/// Fraction of dynamic body angular velocity to remove per unit of time. Values range from 0 to 1. 0 is fully undamped, while values very close to 1 will remove most velocity.
|
||||
/// </summary>
|
||||
public float AngularDamping;
|
||||
|
||||
Vector3 gravityDt;
|
||||
float linearDampingDt;
|
||||
float angularDampingDt;
|
||||
|
||||
public AngularIntegrationMode AngularIntegrationMode => AngularIntegrationMode.Nonconserving;
|
||||
|
||||
public void Initialize(Simulation simulation)
|
||||
{
|
||||
//In this demo, we don't need to initialize anything.
|
||||
//If you had a simulation with per body gravity stored in a CollidableProperty<T> or something similar, having the simulation provided in a callback can be helpful.
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new set of simple callbacks for the demos.
|
||||
/// </summary>
|
||||
/// <param name="gravity">Gravity to apply to dynamic bodies in the simulation.</param>
|
||||
/// <param name="linearDamping">Fraction of dynamic body linear velocity to remove per unit of time. Values range from 0 to 1. 0 is fully undamped, while values very close to 1 will remove most velocity.</param>
|
||||
/// <param name="angularDamping">Fraction of dynamic body angular velocity to remove per unit of time. Values range from 0 to 1. 0 is fully undamped, while values very close to 1 will remove most velocity.</param>
|
||||
public DemoPoseIntegratorCallbacks(Vector3 gravity, float linearDamping = .03f, float angularDamping = .03f) : this()
|
||||
{
|
||||
Gravity = gravity;
|
||||
LinearDamping = linearDamping;
|
||||
AngularDamping = angularDamping;
|
||||
}
|
||||
|
||||
public void PrepareForIntegration(float dt)
|
||||
{
|
||||
//No reason to recalculate gravity * dt for every body; just cache it ahead of time.
|
||||
gravityDt = Gravity * dt;
|
||||
//Since these callbacks don't use per-body damping values, we can precalculate everything.
|
||||
linearDampingDt = MathF.Pow(MathHelper.Clamp(1 - LinearDamping, 0, 1), dt);
|
||||
angularDampingDt = MathF.Pow(MathHelper.Clamp(1 - AngularDamping, 0, 1), dt);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void IntegrateVelocity(int bodyIndex, in RigidPose pose, in BodyInertia localInertia, int workerIndex, ref BodyVelocity velocity)
|
||||
{
|
||||
//Note that we avoid accelerating kinematics. Kinematics are any body with an inverse mass of zero (so a mass of ~infinity). No force can move them.
|
||||
if (localInertia.InverseMass > 0)
|
||||
{
|
||||
velocity.Linear = (velocity.Linear + gravityDt) * linearDampingDt;
|
||||
velocity.Angular = velocity.Angular * angularDampingDt;
|
||||
}
|
||||
//Implementation sidenote: Why aren't kinematics all bundled together separately from dynamics to avoid this per-body condition?
|
||||
//Because kinematics can have a velocity- that is what distinguishes them from a static object. The solver must read velocities of all bodies involved in a constraint.
|
||||
//Under ideal conditions, those bodies will be near in memory to increase the chances of a cache hit. If kinematics are separately bundled, the the number of cache
|
||||
//misses necessarily increases. Slowing down the solver in order to speed up the pose integrator is a really, really bad trade, especially when the benefit is a few ALU ops.
|
||||
|
||||
//Note that you CAN technically modify the pose in IntegrateVelocity by directly accessing it through the Simulation.Bodies.ActiveSet.Poses, it just requires a little care and isn't directly exposed.
|
||||
//If the PositionFirstTimestepper is being used, then the pose integrator has already integrated the pose.
|
||||
//If the PositionLastTimestepper or SubsteppingTimestepper are in use, the pose has not yet been integrated.
|
||||
//If your pose modification depends on the order of integration, you'll want to take this into account.
|
||||
|
||||
//This is also a handy spot to implement things like position dependent gravity or per-body damping.
|
||||
}
|
||||
|
||||
}
|
||||
public struct DemoNarrowPhaseCallbacks : INarrowPhaseCallbacks
|
||||
{
|
||||
public SpringSettings ContactSpringiness;
|
||||
|
||||
public void Initialize(Simulation simulation)
|
||||
{
|
||||
//Use a default if the springiness value wasn't initialized.
|
||||
if (ContactSpringiness.AngularFrequency == 0 && ContactSpringiness.TwiceDampingRatio == 0)
|
||||
ContactSpringiness = new SpringSettings(30, 1);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool AllowContactGeneration(int workerIndex, CollidableReference a, CollidableReference b)
|
||||
{
|
||||
//While the engine won't even try creating pairs between statics at all, it will ask about kinematic-kinematic pairs.
|
||||
//Those pairs cannot emit constraints since both involved bodies have infinite inertia. Since most of the demos don't need
|
||||
//to collect information about kinematic-kinematic pairs, we'll require that at least one of the bodies needs to be dynamic.
|
||||
return a.Mobility == CollidableMobility.Dynamic || b.Mobility == CollidableMobility.Dynamic;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool AllowContactGeneration(int workerIndex, CollidablePair pair, int childIndexA, int childIndexB)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool ConfigureContactManifold<TManifold>(int workerIndex, CollidablePair pair, ref TManifold manifold, out PairMaterialProperties pairMaterial) where TManifold : struct, IContactManifold<TManifold>
|
||||
{
|
||||
pairMaterial.FrictionCoefficient = 1f;
|
||||
pairMaterial.MaximumRecoveryVelocity = 2f;
|
||||
pairMaterial.SpringSettings = ContactSpringiness;
|
||||
return true;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool ConfigureContactManifold(int workerIndex, CollidablePair pair, int childIndexA, int childIndexB, ref ConvexContactManifold manifold)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
3
JNFrame/Assets/Script/battle/mode/DemoCallbacks.cs.meta
Normal file
3
JNFrame/Assets/Script/battle/mode/DemoCallbacks.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 797b78425bbe4587bf1af94093daf985
|
||||
timeCreated: 1706613055
|
@@ -1,4 +1,8 @@
|
||||
using UnityEngine;
|
||||
using BepuPhysics;
|
||||
using BepuPhysics.Constraints;
|
||||
using BepuUtilities.Memory;
|
||||
using Demos;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Script.battle.mode
|
||||
{
|
||||
@@ -9,23 +13,27 @@ namespace Script.battle.mode
|
||||
public class GWorldSceneMode:GBaseMode<GWorldSceneModeInput>
|
||||
{
|
||||
|
||||
public GameObject ball;
|
||||
public GameObject[] balls = null;
|
||||
|
||||
// public override void OnSyncLoad()
|
||||
// {
|
||||
// base.OnSyncLoad();
|
||||
// Instantiate(ball,this.transform);
|
||||
// }
|
||||
private Simulation _simulation;
|
||||
|
||||
public override void OnSyncLoad()
|
||||
{
|
||||
// BufferPool pool = new BufferPool();
|
||||
// Simulation.Create(pool, new DemoNarrowPhaseCallbacks(), new DemoPoseIntegratorCallbacks(new System.Numerics.Vector3(0, -10, 0)), new PositionFirstTimestepper());
|
||||
}
|
||||
|
||||
public override void OnSyncUpdate(int dt, JNFrameInfo frame, GWorldSceneModeInput input)
|
||||
{
|
||||
if (frame.Index > 0)
|
||||
{
|
||||
var ballNode = Instantiate(ball,this.transform);
|
||||
var index = GetSync().nRandomInt(0, balls.Length - 1);
|
||||
Debug.Log(index);
|
||||
var ballNode = Instantiate(balls[index],this.transform);
|
||||
var transformPosition = ballNode.transform.position;
|
||||
ballNode.transform.position = new Vector3(transformPosition.x,transformPosition.y,GetSync().nRandomInt(-3,3));
|
||||
ballNode.transform.position = new Vector3(GetSync().nRandomFloat(-6,6),transformPosition.y,GetSync().nRandomFloat(-6,6));
|
||||
}
|
||||
Debug.Log("OnSyncUpdate");
|
||||
// _simulation.Timestep(dt);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user