clean up old files
This commit is contained in:
@@ -1,261 +0,0 @@
|
|||||||
#if false
|
|
||||||
using FlaxEditor.Content.Settings;
|
|
||||||
using FlaxEngine;
|
|
||||||
using FlaxEngine.Networking;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Game
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// The game mode logic that only exists on server.
|
|
||||||
/// </summary>
|
|
||||||
public class GameMode
|
|
||||||
{
|
|
||||||
private static GameMode currentGameMode = null;
|
|
||||||
|
|
||||||
private static bool ServerRunning => currentGameMode != null;
|
|
||||||
|
|
||||||
public const string DefaultServerAddress = "localhost";
|
|
||||||
public const ushort DefaultServerPort = 59183;
|
|
||||||
|
|
||||||
public string ServerAddress = DefaultServerAddress;
|
|
||||||
public ushort ServerPort = DefaultServerPort;
|
|
||||||
|
|
||||||
private Dictionary<uint, PlayerActor> players = new Dictionary<uint, PlayerActor>();
|
|
||||||
|
|
||||||
public static bool Connect(string ip = null, ushort port = 0)
|
|
||||||
{
|
|
||||||
Console.Clear();
|
|
||||||
if (ServerRunning)
|
|
||||||
NetworkManager_Cleanup();
|
|
||||||
|
|
||||||
var networkSettings = GameSettings.Load<NetworkSettings>();
|
|
||||||
networkSettings.Address = ip ?? DefaultServerAddress;
|
|
||||||
networkSettings.Port = port != 0 ? port : DefaultServerPort;
|
|
||||||
GameSettings.LoadAsset<NetworkSettings>().SetInstance(networkSettings);
|
|
||||||
|
|
||||||
if (NetworkManager.StartClient())
|
|
||||||
{
|
|
||||||
Console.PrintError("Server connection failed.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.Print($"Connecting to {networkSettings.Address}:{networkSettings.Port}...");
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool StartServer(bool listenServer)
|
|
||||||
{
|
|
||||||
Console.Clear();
|
|
||||||
if (ServerRunning)
|
|
||||||
NetworkManager_Cleanup();
|
|
||||||
|
|
||||||
var networkSettings = GameSettings.Load<NetworkSettings>();
|
|
||||||
networkSettings.Address = "any";
|
|
||||||
networkSettings.Port = DefaultServerPort;
|
|
||||||
GameSettings.LoadAsset<NetworkSettings>().SetInstance(networkSettings);
|
|
||||||
|
|
||||||
currentGameMode = new GameMode()
|
|
||||||
{
|
|
||||||
ServerAddress = "localhost",
|
|
||||||
ServerPort = networkSettings.Port,
|
|
||||||
};
|
|
||||||
|
|
||||||
Console.Print("Starting server... port: " + networkSettings.Port);
|
|
||||||
|
|
||||||
#if FLAX_EDITOR
|
|
||||||
FlaxEditor.Editor.Instance.PlayModeEnd += NetworkManager_Cleanup;
|
|
||||||
#else
|
|
||||||
|
|
||||||
#endif
|
|
||||||
NetworkManager.ClientConnected += NetworkManager_ClientConnected;
|
|
||||||
NetworkManager.ClientConnecting += NetworkManager_ClientConnecting;
|
|
||||||
NetworkManager.StateChanged += NetworkManager_StateChanged;
|
|
||||||
|
|
||||||
bool failure;
|
|
||||||
if (listenServer)
|
|
||||||
failure = NetworkManager.StartHost();
|
|
||||||
else
|
|
||||||
failure = NetworkManager.StartServer();
|
|
||||||
if (failure)
|
|
||||||
{
|
|
||||||
Console.PrintError($"Failed to start the server, unable to bind to address {networkSettings.Address}:{networkSettings.Port}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentGameMode.Start();
|
|
||||||
|
|
||||||
Console.Open();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void StopServer()
|
|
||||||
{
|
|
||||||
if (ServerRunning)
|
|
||||||
NetworkManager_Cleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void NetworkManager_Cleanup()
|
|
||||||
{
|
|
||||||
if (ServerRunning)
|
|
||||||
{
|
|
||||||
NetworkManager.ClientConnected -= NetworkManager_ClientConnected;
|
|
||||||
NetworkManager.ClientConnecting -= NetworkManager_ClientConnecting;
|
|
||||||
NetworkManager.StateChanged -= NetworkManager_StateChanged;
|
|
||||||
|
|
||||||
#if FLAX_EDITOR
|
|
||||||
FlaxEditor.Editor.Instance.PlayModeEnd -= NetworkManager_Cleanup;
|
|
||||||
#else
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
currentGameMode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void NetworkManager_StateChanged()
|
|
||||||
{
|
|
||||||
Console.Print("network manager state changed: " + NetworkManager.State.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void NetworkManager_ClientConnected(NetworkClient networkClient)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Console.Print("new client connected");
|
|
||||||
|
|
||||||
foreach (var (playerId, playerActor) in currentGameMode.players)
|
|
||||||
{
|
|
||||||
NetworkReplicator.SpawnObject(playerActor, new[] { networkClient.ClientId });
|
|
||||||
playerActor.Initialize(playerId, playerActor.Position, playerActor.GetRotation());
|
|
||||||
}
|
|
||||||
|
|
||||||
currentGameMode.OnPlayerSpawn(networkClient.ClientId);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Console.Print(e.ToString());
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void NetworkManager_ClientConnecting(ref NetworkClientConnectionData arg0)
|
|
||||||
{
|
|
||||||
Console.Print("new client is connecting");
|
|
||||||
|
|
||||||
bool allowConnection = true;
|
|
||||||
arg0.Result = allowConnection ? 0 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void Start()
|
|
||||||
{
|
|
||||||
// When mode is started
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Tick()
|
|
||||||
{
|
|
||||||
// Run on every frame
|
|
||||||
}
|
|
||||||
|
|
||||||
public void End()
|
|
||||||
{
|
|
||||||
// When win condition is met
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClientConnect()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClientDisconnect()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClientKill()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool OnJoin()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool OnSpectate()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool OnPlayerRequestRespawn()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPlayerSpawn(uint clientId)
|
|
||||||
{
|
|
||||||
// Get a random spawn
|
|
||||||
var spawns = Level.GetActors<Actor>().Where(x => x.Name.StartsWith("PlayerSpawn_")).ToArray();
|
|
||||||
|
|
||||||
var randomSpawn = spawns.FirstOrDefault();
|
|
||||||
if (players.Count > 0)
|
|
||||||
randomSpawn = spawns.LastOrDefault();
|
|
||||||
if (randomSpawn == null)
|
|
||||||
{
|
|
||||||
Console.Print("No spawns found for player");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Console.Print($"found {spawns.Length} spawns");
|
|
||||||
|
|
||||||
Float3 spawnPosition = randomSpawn.Position + new Float3(0f, 4.1f, 0f);
|
|
||||||
Float3 spawnAngles = randomSpawn.Orientation.EulerAngles;
|
|
||||||
|
|
||||||
// Create player actor
|
|
||||||
string prefabPath = Path.Combine(AssetManager.ContentPath, "Common");
|
|
||||||
var playerPrefab = Content.Load<Prefab>(Path.Combine(prefabPath, "PlayerPrefab.prefab"));
|
|
||||||
if (playerPrefab == null)
|
|
||||||
Console.PrintError("GameModeManager: Failed to find PlayerPrefab");
|
|
||||||
|
|
||||||
PlayerActor playerActor = PrefabManager.SpawnPrefab(playerPrefab).As<PlayerActor>();
|
|
||||||
NetworkReplicator.AddObject(playerActor);
|
|
||||||
playerActor.Initialize(clientId, spawnPosition, spawnAngles);
|
|
||||||
|
|
||||||
//playerActor.Teleport(spawnPosition, spawnAngles);
|
|
||||||
//NetworkReplicator.SetObjectOwnership(playerActor, clientId);
|
|
||||||
|
|
||||||
NetworkReplicator.SpawnObject(playerActor);
|
|
||||||
players.Add(clientId, playerActor);
|
|
||||||
//playerActor.Initialize(clientId);
|
|
||||||
//playerActor.hai = 345;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPlayerInit()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPlayerDeath()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPlayerTakeDamage(/*entity player, float damage, entity source*/)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public Float3 OnPlayerApplyForce(/*entity player, vector force, entity source*/)
|
|
||||||
{
|
|
||||||
return Float3.Zero;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
using FlaxEngine;
|
|
||||||
|
|
||||||
namespace Game
|
|
||||||
{
|
|
||||||
public class CustomCharacterController : CharacterController
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,765 +0,0 @@
|
|||||||
#if false
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using FlaxEngine;
|
|
||||||
using Cabrito;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using FlaxEngine.Assertions;
|
|
||||||
|
|
||||||
namespace Game
|
|
||||||
{
|
|
||||||
public struct TraceInfo_NK
|
|
||||||
{
|
|
||||||
public RayCastHit[] hitInfos;
|
|
||||||
public bool startSolid;
|
|
||||||
|
|
||||||
// closest hit
|
|
||||||
public float fraction;
|
|
||||||
public Float3 endPosition;
|
|
||||||
public Float3 hitNormal;
|
|
||||||
public Float3 hitPosition;
|
|
||||||
|
|
||||||
// furthest hit
|
|
||||||
//public float maxFraction;
|
|
||||||
//public Float3 maxHitNormal;
|
|
||||||
//public Float3 maxEndPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PlayerMovement_NK : Script
|
|
||||||
{
|
|
||||||
[Limit(0, 9000), Tooltip("Base Movement speed")]
|
|
||||||
public float MoveSpeed { get; set; } = 320;
|
|
||||||
|
|
||||||
private float viewPitch;
|
|
||||||
private float viewYaw;
|
|
||||||
private float viewRoll;
|
|
||||||
|
|
||||||
private InputEvent onExit = new InputEvent("Exit");
|
|
||||||
|
|
||||||
private const float collisionMargin = 0.031f * 1.666f;
|
|
||||||
|
|
||||||
Actor rootActor;
|
|
||||||
public override void OnAwake()
|
|
||||||
{
|
|
||||||
base.OnAwake();
|
|
||||||
|
|
||||||
onExit.Triggered += () =>
|
|
||||||
{
|
|
||||||
if (Console.IsSafeToQuit)
|
|
||||||
Engine.RequestExit();
|
|
||||||
};
|
|
||||||
|
|
||||||
rootActor = Actor.GetChild(0);
|
|
||||||
|
|
||||||
RigidBody rigidBody = Actor.As<RigidBody>();
|
|
||||||
rigidBody.CollisionEnter += OnCollisionEnter;
|
|
||||||
rigidBody.CollisionExit += OnCollisionLeave;
|
|
||||||
rigidBody.TriggerEnter += OnTriggerEnter;
|
|
||||||
|
|
||||||
//rigidBody.SetSolverIterationCounts(40, 10);
|
|
||||||
|
|
||||||
Console.Print("playermovement init: " + rigidBody.MaxDepenetrationVelocity.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnTriggerEnter(PhysicsColliderActor colliderActor)
|
|
||||||
{
|
|
||||||
Console.Print("trogger: ");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnCollisionEnter(Collision collision)
|
|
||||||
{
|
|
||||||
//Console.Print("collision: " + collision.Contacts[0].Normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnCollisionLeave(Collision collision)
|
|
||||||
{
|
|
||||||
//Console.Print("collision leave: " + collision.Contacts[0].Normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnDestroy()
|
|
||||||
{
|
|
||||||
base.OnDestroy();
|
|
||||||
|
|
||||||
onExit.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnStart()
|
|
||||||
{
|
|
||||||
var initialEulerAngles = Actor.Orientation.EulerAngles;
|
|
||||||
viewPitch = initialEulerAngles.X;
|
|
||||||
viewYaw = initialEulerAngles.Y;
|
|
||||||
viewRoll = initialEulerAngles.Z;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks for overlapping colliders in place using the player's rigidbody.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="position">Position</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private Collider[] TracePlayer(Float3 position, float tolerance = 0.0f)
|
|
||||||
{
|
|
||||||
Collider[] colliders = null;
|
|
||||||
|
|
||||||
bool collided = false;
|
|
||||||
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
|
|
||||||
var boxCollider = Actor.GetChild<BoxCollider>();
|
|
||||||
var meshCollider = Actor.GetChild<MeshCollider>();
|
|
||||||
PhysicsColliderActor colliderActor = null;
|
|
||||||
if (capsuleCollider && capsuleCollider.IsActive)
|
|
||||||
{
|
|
||||||
colliderActor = capsuleCollider;
|
|
||||||
collided = Physics.OverlapCapsule(position,
|
|
||||||
capsuleCollider.Radius + tolerance, capsuleCollider.Height + tolerance,
|
|
||||||
out colliders, capsuleCollider.Orientation,
|
|
||||||
uint.MaxValue,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
else if (meshCollider && meshCollider.IsActive)
|
|
||||||
{
|
|
||||||
colliderActor = meshCollider;
|
|
||||||
collided = Physics.OverlapConvex(position,
|
|
||||||
meshCollider.CollisionData, meshCollider.Scale + tolerance,
|
|
||||||
out colliders, meshCollider.Orientation,
|
|
||||||
uint.MaxValue,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
else if (boxCollider && boxCollider.IsActive)
|
|
||||||
{
|
|
||||||
colliderActor = boxCollider;
|
|
||||||
collided = Physics.OverlapBox(position,
|
|
||||||
boxCollider.OrientedBox.Extents + tolerance,
|
|
||||||
out colliders, boxCollider.Orientation,
|
|
||||||
uint.MaxValue,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Assert.Fail("No supported colliders found for rigidbody");
|
|
||||||
|
|
||||||
if (!collided)
|
|
||||||
return colliders;
|
|
||||||
|
|
||||||
List<Collider> collidersFiltered = new List<Collider>();
|
|
||||||
foreach (var collider in colliders)
|
|
||||||
{
|
|
||||||
if (collider == colliderActor)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
collidersFiltered.Add(collider);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (collidersFiltered.Count == 0)
|
|
||||||
return colliders; // self-collision?
|
|
||||||
|
|
||||||
return collidersFiltered.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sweeps the player rigidbody in world and returns geometry which was hit during the trace.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="start">Start position</param>
|
|
||||||
/// <param name="end">End position</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private TraceInfo TracePlayer(Float3 start, Float3 end, float tolerance = 0.0f)
|
|
||||||
{
|
|
||||||
TraceInfo traceInfo = new TraceInfo();
|
|
||||||
|
|
||||||
Float3 delta = end - start;
|
|
||||||
float maxDistance = delta.Length;
|
|
||||||
Float3 direction = delta.Normalized;
|
|
||||||
|
|
||||||
bool collided = false;
|
|
||||||
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
|
|
||||||
var boxCollider = Actor.GetChild<BoxCollider>();
|
|
||||||
var meshCollider = Actor.GetChild<MeshCollider>();
|
|
||||||
PhysicsColliderActor colliderActor = null;
|
|
||||||
if (capsuleCollider && capsuleCollider.IsActive)
|
|
||||||
{
|
|
||||||
colliderActor = capsuleCollider;
|
|
||||||
collided = Physics.CapsuleCastAll(start,
|
|
||||||
capsuleCollider.Radius + tolerance, capsuleCollider.Height,
|
|
||||||
direction, out traceInfo.hitInfos, capsuleCollider.Orientation, maxDistance,
|
|
||||||
uint.MaxValue,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
else if (meshCollider && meshCollider.IsActive)
|
|
||||||
{
|
|
||||||
colliderActor = meshCollider;
|
|
||||||
collided = Physics.ConvexCastAll(start,
|
|
||||||
meshCollider.CollisionData, meshCollider.Scale + tolerance,
|
|
||||||
direction, out traceInfo.hitInfos, meshCollider.Orientation, maxDistance,
|
|
||||||
uint.MaxValue,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
else if (boxCollider && boxCollider.IsActive)
|
|
||||||
{
|
|
||||||
colliderActor = boxCollider;
|
|
||||||
collided = Physics.BoxCastAll(start,
|
|
||||||
boxCollider.OrientedBox.Extents + tolerance,
|
|
||||||
direction, out traceInfo.hitInfos, boxCollider.Orientation, maxDistance, uint.MaxValue,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (collided)
|
|
||||||
{
|
|
||||||
List<RayCastHit> hitInfosFiltered = new List<RayCastHit>();
|
|
||||||
RayCastHit closest = new RayCastHit();
|
|
||||||
closest.Distance = float.MaxValue;
|
|
||||||
foreach (var hitInfo in traceInfo.hitInfos)
|
|
||||||
{
|
|
||||||
if (hitInfo.Collider == colliderActor)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
hitInfosFiltered.Add(hitInfo);
|
|
||||||
|
|
||||||
if (hitInfo.Distance < closest.Distance && hitInfo.Distance != 0.0f)
|
|
||||||
closest = hitInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hitInfosFiltered.Count == 0)
|
|
||||||
collided = false; // self-collision?
|
|
||||||
else //if (closest.Distance > 0f)
|
|
||||||
{
|
|
||||||
if (closest.Distance == float.MaxValue)
|
|
||||||
{
|
|
||||||
foreach (var hitInfo in hitInfosFiltered)
|
|
||||||
{
|
|
||||||
if (hitInfo.Distance < closest.Distance)
|
|
||||||
closest = hitInfo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
traceInfo.hitInfos = hitInfosFiltered.ToArray();
|
|
||||||
|
|
||||||
traceInfo.fraction = closest.Distance / maxDistance;
|
|
||||||
traceInfo.hitNormal = closest.Normal;
|
|
||||||
traceInfo.hitPosition = closest.Point;
|
|
||||||
traceInfo.endPosition = start + (delta * traceInfo.fraction);
|
|
||||||
|
|
||||||
if (traceInfo.fraction == 0f && maxDistance > 0f)
|
|
||||||
traceInfo.startSolid = true;
|
|
||||||
}
|
|
||||||
/*else
|
|
||||||
{
|
|
||||||
traceInfo.startSolid = true;
|
|
||||||
traceInfo.fraction = 0f;
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!collided)
|
|
||||||
{
|
|
||||||
traceInfo.hitInfos = new RayCastHit[0];
|
|
||||||
traceInfo.fraction = 1f;
|
|
||||||
traceInfo.endPosition = end;
|
|
||||||
}
|
|
||||||
|
|
||||||
return traceInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnDebugDraw()
|
|
||||||
{
|
|
||||||
base.OnDebugDraw();
|
|
||||||
|
|
||||||
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
|
|
||||||
var boxCollider = Actor.GetChild<BoxCollider>();
|
|
||||||
var meshCollider = Actor.GetChild<MeshCollider>();
|
|
||||||
if (capsuleCollider && capsuleCollider.IsActive)
|
|
||||||
{
|
|
||||||
Quaternion rotation = capsuleCollider.LocalOrientation * Quaternion.Euler(0f, 90f, 0f);
|
|
||||||
DebugDraw.DrawWireTube(capsuleCollider.Position, rotation, capsuleCollider.Radius, capsuleCollider.Height, Color.GreenYellow * 0.8f);
|
|
||||||
}
|
|
||||||
else if (meshCollider && meshCollider.IsActive)
|
|
||||||
{
|
|
||||||
//Quaternion rotation = meshCollider.LocalOrientation * Quaternion.Euler(0f, 90f, 0f);
|
|
||||||
DebugDraw.DrawWireCylinder(meshCollider.Position, meshCollider.Orientation, capsuleCollider.Radius, capsuleCollider.Height + capsuleCollider.Radius * 2, Color.GreenYellow * 0.8f);
|
|
||||||
//DebugDraw.DrawWireTube(meshCollider.Position, rotation, meshCollider.Radius, meshCollider.Height, Color.GreenYellow * 0.8f);
|
|
||||||
}
|
|
||||||
else if (boxCollider && boxCollider.IsActive)
|
|
||||||
{
|
|
||||||
DebugDraw.DrawWireBox(boxCollider.OrientedBox.GetBoundingBox(), Color.GreenYellow * 0.8f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SlideMove(ref Vector3 position, bool stepUp, ref Vector3 velocity, bool asdf = false)
|
|
||||||
{
|
|
||||||
if (velocity.IsZero)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Vector3 originalPosition = position;
|
|
||||||
Vector3 originalVelocity = velocity;
|
|
||||||
|
|
||||||
float timeleft = Time.DeltaTime;
|
|
||||||
|
|
||||||
List<Float3> hitNormals = new List<Float3>();
|
|
||||||
|
|
||||||
for (int bump = 0; bump < 4; bump++)
|
|
||||||
{
|
|
||||||
Vector3 startPos = position;
|
|
||||||
Vector3 endPos = position + (velocity * timeleft);
|
|
||||||
|
|
||||||
TraceInfo trace = TracePlayer(startPos, endPos);
|
|
||||||
// TODO: handle portals here
|
|
||||||
|
|
||||||
float fraction = trace.fraction;
|
|
||||||
Vector3 hitNormal = trace.hitNormal;
|
|
||||||
|
|
||||||
if (trace.startSolid)
|
|
||||||
{
|
|
||||||
velocity = Float3.Zero;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (physicsInteractions)
|
|
||||||
{
|
|
||||||
RigidBody rigidBody = Actor.As<RigidBody>();
|
|
||||||
foreach (var hit in trace.hitInfos)
|
|
||||||
{
|
|
||||||
if (hit.Collider.AttachedRigidBody == null || hit.Collider.AttachedRigidBody.IsKinematic)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Vector3 force = -hit.Normal * velocity.Length * rigidBody.Mass;
|
|
||||||
//Console.Print("move force: " + (force.Length / timeleft));
|
|
||||||
hit.Collider.AttachedRigidBody.AddForce(force, ForceMode.Force);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fraction > 0f)
|
|
||||||
{
|
|
||||||
position = trace.endPosition;
|
|
||||||
hitNormals.Clear(); // this is present in some forks, not in Q3
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fraction >= 1f)
|
|
||||||
break;
|
|
||||||
|
|
||||||
timeleft *= 1.0f - fraction;
|
|
||||||
|
|
||||||
// this doesn't seem to do anything, we never have any hitNormals stored here
|
|
||||||
bool hitPreviousNormal = false;
|
|
||||||
foreach (Float3 normal in hitNormals)
|
|
||||||
{
|
|
||||||
if (Float3.Dot(hitNormal, normal) > 0.99)
|
|
||||||
{
|
|
||||||
// nudge away from the same wall we hit earlier and try again
|
|
||||||
velocity += hitNormal;
|
|
||||||
hitPreviousNormal = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (hitPreviousNormal)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
hitNormals.Add(hitNormal);
|
|
||||||
if (hitNormals.Count != 1)
|
|
||||||
Console.Print("hitNormals: " + hitNormals.Count);
|
|
||||||
|
|
||||||
int plane;
|
|
||||||
Vector3 normalMargin = Float3.Zero;
|
|
||||||
for (plane = 0; plane < hitNormals.Count; plane++)
|
|
||||||
{
|
|
||||||
Vector3 normal = hitNormals[plane];
|
|
||||||
|
|
||||||
// clip velocity
|
|
||||||
velocity -= normal * Float3.Dot(velocity, normal);
|
|
||||||
//velocity = Float3.ProjectOnPlane(velocity, normal);
|
|
||||||
|
|
||||||
//traceOffset = normal * 1f;
|
|
||||||
normalMargin += normal;
|
|
||||||
//position += normal * 0.031f;
|
|
||||||
|
|
||||||
int plane2;
|
|
||||||
for (plane2 = 0; plane2 < hitNormals.Count; plane2++)
|
|
||||||
{
|
|
||||||
if (plane == plane2)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (Float3.Dot(velocity, hitNormals[plane2]) < 0f)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (plane2 == hitNormals.Count)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// push off slightly away from the walls to not get stuck
|
|
||||||
position += normalMargin.Normalized * collisionMargin;
|
|
||||||
|
|
||||||
if (plane == hitNormals.Count)
|
|
||||||
{
|
|
||||||
if (hitNormals.Count == 2)
|
|
||||||
{
|
|
||||||
Vector3 dir = Float3.Cross(hitNormals[0], hitNormals[1]);
|
|
||||||
//dir.Normalize();
|
|
||||||
float dist = Float3.Dot(dir, velocity);
|
|
||||||
velocity = dist * dir;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
velocity = Float3.Zero;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// nudge very slightly away from the wall to avoid getting stuck
|
|
||||||
//position += trace.hitNormal * 0.01f;
|
|
||||||
//velocity += trace.hitNormal * 0.01f;
|
|
||||||
}
|
|
||||||
|
|
||||||
// prevents bouncing against the wall
|
|
||||||
if (/*velocity.Length > 0f && */Float3.Dot(velocity, originalVelocity) <= 0f)
|
|
||||||
{
|
|
||||||
velocity = Float3.Zero;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Vector3 wishVelocity = Float3.Zero;
|
|
||||||
//Vector3 gravityVelocity = Float3.Zero;
|
|
||||||
//Vector3 currentVelocity = Float3.Zero;
|
|
||||||
private bool onGround = false;
|
|
||||||
|
|
||||||
private const float friction = 4f;
|
|
||||||
private const float stopspeed = 100f;
|
|
||||||
private const float accelerationGround = 10f;
|
|
||||||
private const float jumpVelocity = 270f;
|
|
||||||
|
|
||||||
private const float maxAirSpeed = 320f;
|
|
||||||
private const float maxAirStrafeSpeed = 30f; //Q2+
|
|
||||||
private const float airAcceleration = 0.4f * 0f; //Q2+
|
|
||||||
private const float airStopAcceleration = 2.5f * 0f; //Q2+
|
|
||||||
private const float airStrafeAcceleration = 70f * 0f; //CPM?
|
|
||||||
private const float strafeAcceleration = 10f; //QW
|
|
||||||
private const float airControl = 0f; //CPM
|
|
||||||
private bool physicsInteractions = false;
|
|
||||||
|
|
||||||
private bool jumped = false;
|
|
||||||
|
|
||||||
private Vector3 safePosition;
|
|
||||||
private Vector3 currentVelocity;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public override void OnFixedUpdate()
|
|
||||||
{
|
|
||||||
RigidBody rigidBody = Actor.As<RigidBody>();
|
|
||||||
Transform rootTrans = rootActor.Transform;
|
|
||||||
|
|
||||||
Vector3 inputDirection =
|
|
||||||
new Float3(InputManager.GetAxis("Horizontal"), 0.0f, InputManager.GetAxis("Vertical"));
|
|
||||||
Vector3 moveDirection = rootTrans.TransformDirection(inputDirection);
|
|
||||||
|
|
||||||
//Vector3 position = rigidBody.Position;
|
|
||||||
Vector3 velocity = rigidBody.LinearVelocity;//currentVelocity;
|
|
||||||
|
|
||||||
Vector3 wishVelocity = Float3.Zero;
|
|
||||||
if (!inputDirection.IsZero)
|
|
||||||
wishVelocity = moveDirection.Normalized * MoveSpeed;
|
|
||||||
|
|
||||||
// categorize position
|
|
||||||
onGround = true;
|
|
||||||
Vector3 groundDelta = Physics.Gravity.Normalized * collisionMargin*2;
|
|
||||||
Vector3 traceGroundStart = rigidBody.Position - groundDelta;
|
|
||||||
Vector3 traceGroundEnd = rigidBody.Position + groundDelta;
|
|
||||||
TraceInfo traceGround = TracePlayer(traceGroundStart, traceGroundEnd, -0.1f);
|
|
||||||
|
|
||||||
float groundDistance = 0.1f;
|
|
||||||
|
|
||||||
if (traceGround.fraction < 1f/* && !traceGround.startSolid*//*&& ddot < 0.7f*/)
|
|
||||||
{
|
|
||||||
|
|
||||||
/*
|
|
||||||
// slope
|
|
||||||
// clip velocity
|
|
||||||
Vector3 bounce = -groundDelta;
|
|
||||||
Vector3 bounceDir = -groundDelta.Normalized;//traceGround.hitNormal;
|
|
||||||
//Vector3 velocityProjected = Float3.ProjectOnPlane(velocity, normal);
|
|
||||||
float backoff = Float3.Dot(bounce, bounceDir) * 2f;
|
|
||||||
bounce -= bounceDir * backoff;
|
|
||||||
//velocity = velocityProjected;
|
|
||||||
|
|
||||||
Vector3 point = (rigidBody.Position + groundDelta) +
|
|
||||||
(1f - traceGround.fraction) * bounce;
|
|
||||||
//Vector3 point = rigidBody.Position - ((1f - traceGround.fraction) * groundDelta);
|
|
||||||
|
|
||||||
//rigidBody.Position = point;
|
|
||||||
// retrace
|
|
||||||
//traceGround = TracePlayer(traceGround.endPosition, point);
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/*if (!traceGround.startSolid && traceGround.fraction < 1f &&
|
|
||||||
-Float3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < 0.7f)
|
|
||||||
{
|
|
||||||
// slope
|
|
||||||
// clip velocity
|
|
||||||
Vector3 bounce = groundDelta;
|
|
||||||
//Vector3 velocityProjected = Float3.ProjectOnPlane(velocity, normal);
|
|
||||||
float backoff = Float3.Dot(bounce, traceGround.hitNormal) * 2f;
|
|
||||||
bounce -= traceGround.hitNormal * backoff;
|
|
||||||
//velocity = velocityProjected;
|
|
||||||
|
|
||||||
Vector3 point = (rigidBody.Position + groundDelta) +
|
|
||||||
(1f - traceGround.fraction) * bounce;
|
|
||||||
|
|
||||||
// retrace
|
|
||||||
traceGround = TracePlayer(rigidBody.Position, rigidBody.Position + point);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
//if (!traceGround.startSolid && (traceGround.fraction >= 1f ||
|
|
||||||
// -Float3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < 0.7f))
|
|
||||||
|
|
||||||
float rampDot = -Float3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal);
|
|
||||||
if (traceGround.fraction >= 1f)
|
|
||||||
rampDot = 1f;
|
|
||||||
else if (traceGround.hitNormal.IsZero)
|
|
||||||
rampDot = 99f;
|
|
||||||
|
|
||||||
if (traceGround.fraction >= 1f || rampDot < 0.7f)
|
|
||||||
{
|
|
||||||
//Console.Print("air: " + (-Float3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal)).ToString());
|
|
||||||
// falling or sliding down a slope
|
|
||||||
onGround = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
float closestDist = float.MaxValue;
|
|
||||||
foreach (var hit in traceGround.hitInfos)
|
|
||||||
{
|
|
||||||
if (hit.Distance < closestDist && hit.Distance != 0f)
|
|
||||||
closestDist = hit.Distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (traceGround.fraction == 0f && closestDist != 0f)
|
|
||||||
Console.Print("minteresting");
|
|
||||||
//Console.Print("grund: "+ (-Float3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal)).ToString());
|
|
||||||
onGround = true;//!traceGround.startSolid;
|
|
||||||
//velocity.Y = 0f;
|
|
||||||
|
|
||||||
// move slightly above the ground
|
|
||||||
// maintain distance off the ground
|
|
||||||
float dist = ((traceGroundEnd - rigidBody.Position) * (traceGround.fraction - 0.5f)).Length;
|
|
||||||
|
|
||||||
if (dist < groundDistance)
|
|
||||||
{
|
|
||||||
var newPos = rigidBody.Position;
|
|
||||||
newPos.Y += groundDistance - dist;
|
|
||||||
//rigidBody.Position = newPos;
|
|
||||||
//velocity.Y = 0f;
|
|
||||||
}
|
|
||||||
//velocity.Y = 0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
onGround = true;
|
|
||||||
//Console.Print("frac: " + traceGround.fraction + "norm: " + rampDot);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//onGround |= overlaps.Length > 0;
|
|
||||||
|
|
||||||
/*if (onGround && physicsInteractions && traceGround.hitInfos.Length > 0)
|
|
||||||
{
|
|
||||||
// apply resting force to rigid bodies under the player
|
|
||||||
//bool collided = false;
|
|
||||||
foreach (var hit in traceGround.hitInfos)
|
|
||||||
{
|
|
||||||
if (hit.Collider.AttachedRigidBody == null || hit.Collider.AttachedRigidBody.IsKinematic)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (hit.Distance <= 0f)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
//Console.Print(Physics.Gravity.Length.ToString());
|
|
||||||
Vector3 force = -hit.Normal * (Physics.Gravity.Length) * rigidBody.Mass * Time.DeltaTime;
|
|
||||||
hit.Collider.AttachedRigidBody.AddForceAtPosition(force, hit.Point, ForceMode.Impulse);
|
|
||||||
//collided = true;
|
|
||||||
//Console.Print("downforce: " + force.Length / Time.DeltaTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
//if (collided)
|
|
||||||
// fraction = 1.0f; // finish movement and stop
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*if (!onGround)
|
|
||||||
Console.Print("air");
|
|
||||||
else
|
|
||||||
Console.Print("ground");*/
|
|
||||||
|
|
||||||
|
|
||||||
/*if (onGround)
|
|
||||||
{
|
|
||||||
// snap to ground
|
|
||||||
if (!traceGround.startSolid)
|
|
||||||
{
|
|
||||||
Vector3 newPos = rigidBody.Position;
|
|
||||||
|
|
||||||
if (traceGround.fraction < 1f)
|
|
||||||
{
|
|
||||||
//newPos += -Physics.Gravity.Normalized * traceGround.fraction;
|
|
||||||
}
|
|
||||||
rigidBody.Position = newPos;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
/*if (traceGround.startSolid)
|
|
||||||
{
|
|
||||||
Console.Print("stuk: ");
|
|
||||||
|
|
||||||
rigidBody.Position = safePosition;
|
|
||||||
|
|
||||||
traceGround = TracePlayer(rigidBody.Position, rigidBody.Position + Physics.Gravity.Normalized,
|
|
||||||
false);
|
|
||||||
|
|
||||||
//onGround = true;
|
|
||||||
|
|
||||||
//currentVelocity.Y = 0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!traceGround.startSolid)
|
|
||||||
{
|
|
||||||
foreach (var hitInfo in traceGround.hitInfos)
|
|
||||||
{
|
|
||||||
var dot = Float3.Dot(Physics.Gravity.Normalized, hitInfo.Normal);
|
|
||||||
if (-dot >= 0.7) //~45deg slope
|
|
||||||
{
|
|
||||||
//Console.Print("d: " + hitInfo.Distance);
|
|
||||||
Vector3 newPos = rigidBody.Position;
|
|
||||||
|
|
||||||
if (hitInfo.Distance > 0f)
|
|
||||||
newPos += Physics.Gravity.Normalized * (hitInfo.Distance - 0.01f);
|
|
||||||
else
|
|
||||||
newPos += hitInfo.Normal * 0.1f;
|
|
||||||
|
|
||||||
rigidBody.Position = newPos;
|
|
||||||
|
|
||||||
onGround = true;
|
|
||||||
currentVelocity.Y = 0f;
|
|
||||||
break;
|
|
||||||
|
|
||||||
//if (currentVelocity.Length > 0.01f)
|
|
||||||
// Console.Print("groundvel: " + currentVelocity.ToString());
|
|
||||||
//currentVelocity.Y = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// jump
|
|
||||||
if (onGround)
|
|
||||||
{
|
|
||||||
if (!jumped && InputManager.GetAction("Jump"))
|
|
||||||
{
|
|
||||||
jumped = true;
|
|
||||||
|
|
||||||
velocity += Float3.Up * jumpVelocity;
|
|
||||||
onGround = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (jumped && !InputManager.GetAction("Jump")) // jump released
|
|
||||||
jumped = false;
|
|
||||||
|
|
||||||
|
|
||||||
// ground friction
|
|
||||||
if (onGround)
|
|
||||||
{
|
|
||||||
float currentSpeed = velocity.Length;
|
|
||||||
|
|
||||||
float control = currentSpeed < stopspeed ? stopspeed : currentSpeed;
|
|
||||||
var drop = control * friction * Time.DeltaTime;
|
|
||||||
|
|
||||||
float newspeed = currentSpeed - drop;
|
|
||||||
if (newspeed < 0)
|
|
||||||
newspeed = 0;
|
|
||||||
|
|
||||||
if (currentSpeed < 0.0001f)
|
|
||||||
velocity *= 0;
|
|
||||||
else
|
|
||||||
velocity *= newspeed / currentSpeed;
|
|
||||||
}
|
|
||||||
|
|
||||||
//bool stepUp = false;
|
|
||||||
|
|
||||||
if (onGround) // ground acceleration
|
|
||||||
{
|
|
||||||
ApplyAcceleration(ref velocity, wishVelocity.Normalized, wishVelocity.Length, float.MaxValue, accelerationGround);
|
|
||||||
}
|
|
||||||
else // air acceleration
|
|
||||||
{
|
|
||||||
var wishspeed = wishVelocity.Length;
|
|
||||||
if (wishspeed > maxAirSpeed)
|
|
||||||
wishspeed = maxAirSpeed;
|
|
||||||
|
|
||||||
if (strafeAcceleration != 0f)
|
|
||||||
ApplyAcceleration(ref velocity, wishVelocity.Normalized, wishspeed, maxAirStrafeSpeed, strafeAcceleration);
|
|
||||||
|
|
||||||
//stepUp = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!onGround)
|
|
||||||
{
|
|
||||||
velocity += Physics.Gravity * Time.DeltaTime;
|
|
||||||
//Console.Print("grav");
|
|
||||||
}
|
|
||||||
//else
|
|
||||||
// Console.Print("Yv: " + currentVelocity.Y);
|
|
||||||
|
|
||||||
//SlideMove(ref position, false, ref velocity);
|
|
||||||
|
|
||||||
safePosition = rigidBody.Position;
|
|
||||||
if (rigidBody.EnableSimulation)
|
|
||||||
{
|
|
||||||
//rigidBody.Position = position;
|
|
||||||
//Vector3 force = velocity - rigidBody.LinearVelocity;
|
|
||||||
rigidBody.LinearVelocity = velocity;
|
|
||||||
//rigidBody.AddForce(force, ForceMode.Impulse);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//rigidBody.Position = position;
|
|
||||||
rigidBody.LinearVelocity = velocity;
|
|
||||||
}
|
|
||||||
|
|
||||||
//rigidBody.LinearVelocity = velocity;
|
|
||||||
|
|
||||||
//if (currentVelocity.Length > 0.01f)
|
|
||||||
// Console.Print("vel: " + currentVelocity.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ApplyAcceleration(ref Vector3 velocity, Vector3 wishDir, float wishspeed, float maxWishspeed, float acceleration)
|
|
||||||
{
|
|
||||||
float wishspeedOrig = wishspeed;
|
|
||||||
if (wishspeed > maxWishspeed)
|
|
||||||
wishspeed = maxWishspeed;
|
|
||||||
|
|
||||||
float currentSpeed = Float3.Dot(velocity, wishDir);
|
|
||||||
float addSpeed = wishspeed - currentSpeed;
|
|
||||||
if (addSpeed <= 0f)
|
|
||||||
return;
|
|
||||||
|
|
||||||
float accelSpeed = acceleration * wishspeedOrig * Time.DeltaTime;
|
|
||||||
if (accelSpeed > addSpeed)
|
|
||||||
accelSpeed = addSpeed;
|
|
||||||
|
|
||||||
velocity += accelSpeed * wishDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnUpdate()
|
|
||||||
{
|
|
||||||
float xAxis = InputManager.GetAxisRaw("Mouse X");
|
|
||||||
float yAxis = InputManager.GetAxisRaw("Mouse Y");
|
|
||||||
if (xAxis != 0.0f || yAxis != 0.0f)
|
|
||||||
{
|
|
||||||
var camera = rootActor.GetChild<Camera>();
|
|
||||||
|
|
||||||
viewPitch += yAxis;
|
|
||||||
viewYaw += xAxis;
|
|
||||||
|
|
||||||
viewPitch = Mathf.Clamp(viewPitch, -90.0f, 90.0f);
|
|
||||||
|
|
||||||
// root orientation must be set first
|
|
||||||
rootActor.Orientation = Quaternion.Euler(0, viewYaw, 0);
|
|
||||||
camera.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user