Files
GoakeFlax/Source/Game/Player/PlayerActor.cs
2023-03-07 17:51:43 +02:00

127 lines
3.9 KiB
C#

using System.Collections.Generic;
using FlaxEngine;
using FlaxEngine.Networking;
#if FLAX_EDITOR
using FlaxEditor.CustomEditors.Dedicated;
using FlaxEditor.Scripting;
#endif
namespace Game
{
#if FLAX_EDITOR
[CustomEditor(typeof(PlayerActor))]
public class PlayerActorEditor : ActorEditor
{
protected override List<ItemInfo> GetItemsForType(ScriptType type)
{
var items = GetItemsForType(type, type.IsClass, true);
// Remove all Rigid Body options
items.RemoveAll(x => x.Display?.Group == "Rigid Body");
// Inject scripts editor
ScriptMemberInfo scriptsMember = type.GetProperty("Scripts");
if (scriptsMember != ScriptMemberInfo.Null)
{
ItemInfo item = new ItemInfo(scriptsMember)
{
CustomEditor = new CustomEditorAttribute(typeof(ScriptsEditor))
};
items.Add(item);
}
return items;
}
}
#endif
public class PlayerActor : RigidBody
{
private PlayerMovement playerMovement;
private RigidBody playerRigidBody;
[NetworkReplicated]
public uint PlayerId = uint.MaxValue;
/*public PlayerActor()
{
// Default internal values for RigidBody
IsKinematic = true;
EnableGravity = false;
LinearDamping = 0f;
AngularDamping = 0f;
Constraints = RigidbodyConstraints.LockRotation;
}*/
public override void OnBeginPlay()
{
// Default internal values for RigidBody
IsKinematic = true;
EnableGravity = false;
LinearDamping = 0f;
AngularDamping = 0f;
Constraints = RigidbodyConstraints.LockRotation;
base.OnBeginPlay();
playerMovement = FindScript<PlayerMovement>();
playerRigidBody = FindActor<RigidBody>();
}
public override void OnEnable()
{
// Trigger OnEnable manually, does not seem 8to propagate when parent gets enabled/disabled
playerMovement.Enabled = true;
//NetworkReplicator.AddObject(this);
}
public override void OnDisable()
{
playerMovement.Enabled = false;
//NetworkReplicator.RemoveObject(this);
}
[NetworkRpc(client: true)]
public void Initialize(uint playerId)
{
PlayerId = playerId;
playerMovement.SetInput(playerId);
if (playerId == NetworkManager.LocalClientId)
{
FindActor("CameraHolder").IsActive = true;
//FindActor("ViewModelHolder").IsActive = true;
FindActor("PlayerModel").IsActive = false;
}
else
FindActor("PlayerModel").IsActive = true;
IsActive = true;
}
[NetworkRpc(server: true)]
public void UpdateNetworkInput(ulong frame, Float4 viewDeltaXYMoveForwardRight, bool attacking, bool jumping)
{
if (playerMovement.input is not PlayerInputNetwork playerInputNetwork)
return;
PlayerInputState inputState = new PlayerInputState(frame, viewDeltaXYMoveForwardRight.X, viewDeltaXYMoveForwardRight.Y, viewDeltaXYMoveForwardRight.Z, viewDeltaXYMoveForwardRight.W, attacking, jumping);
playerInputNetwork.currentState.input = inputState;
}
public void SetPosition(Float3 newPosition)
{
Position = newPosition;
}
public void SetRotation(Float3 eulerAngles)
{
playerMovement.ResetRotation(eulerAngles);
}
[NetworkRpc(client: true)]
public void Teleport(Float3 newPosition, Float3 eulerAngles)
{
SetPosition(newPosition);
SetRotation(eulerAngles);
}
}
}