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

116 lines
3.3 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;
[NetworkReplicated]
public uint hai = 1;
public PlayerActor()
{
// Default internal values for RigidBody
IsKinematic = true;
EnableGravity = false;
LinearDamping = 0f;
AngularDamping = 0f;
Constraints = RigidbodyConstraints.LockRotation;
}
public override void OnBeginPlay()
{
base.OnBeginPlay();
playerMovement = FindScript<PlayerMovement>();
playerRigidBody = FindActor<RigidBody>();
}
public override void OnEnable()
{
// Trigger OnEnable manually, does not seem to propagate when parent gets enabled/disabled
playerMovement.Enabled = true;
//NetworkReplicator.AddObject(this);
}
public override void OnDisable()
{
playerMovement.Enabled = false;
//NetworkReplicator.RemoveObject(this);
}
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;
}
public void UpdateNetworkInput(PlayerInputState inputState)
{
if (playerMovement.input is PlayerInputNetwork)
(playerMovement.input as PlayerInputNetwork).currentState.input = inputState;
}
public void SetPosition(Float3 newPosition)
{
Position = newPosition;
}
public void SetRotation(Float3 eulerAngles)
{
playerMovement.ResetRotation(eulerAngles);
}
public void Teleport(Float3 newPosition, Float3 eulerAngles)
{
SetPosition(newPosition);
SetRotation(eulerAngles);
}
}
}