(Feat) Add mulitplayer
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "InventoryComponent.h"
|
||||
#include "ItemDataAsset.h"
|
||||
#include "Kismet/GameplayStatics.h"
|
||||
#include "Net/UnrealNetwork.h"
|
||||
|
||||
#define LOCTEXT_NAMESPACE "StorageContainer"
|
||||
|
||||
@@ -22,6 +23,12 @@ AStorageContainer::AStorageContainer()
|
||||
PrimaryActorTick.bCanEverTick = true;
|
||||
PrimaryActorTick.bStartWithTickEnabled = false;
|
||||
|
||||
// Acteur du monde : contenu et couvercle appartiennent au serveur.
|
||||
// Le UInventoryComponent, lui, se replique deja tout seul -- c'est la
|
||||
// classe entiere qui le fait, coffre comme sac.
|
||||
bReplicates = true;
|
||||
SetReplicateMovement(false);
|
||||
|
||||
BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BaseMesh"));
|
||||
SetRootComponent(BaseMesh);
|
||||
|
||||
@@ -166,15 +173,34 @@ void AStorageContainer::Interact_Implementation(AActor* Interactor)
|
||||
PlayerController->OpenStorage(this);
|
||||
}
|
||||
|
||||
void AStorageContainer::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
||||
{
|
||||
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
||||
|
||||
DOREPLIFETIME(AStorageContainer, bOpen);
|
||||
}
|
||||
|
||||
void AStorageContainer::SetOpen(bool bInOpen)
|
||||
{
|
||||
if (bOpen == bInOpen)
|
||||
// Le couvercle appartient au monde : seul le serveur en decide, et la
|
||||
// replication l'annonce aux autres. Un client qui forcerait bOpen verrait
|
||||
// son coffre s'ouvrir seul, et le prochain OnRep le refermerait.
|
||||
if (!HasAuthority() || bOpen == bInOpen)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bOpen = bInOpen;
|
||||
ApplyOpenState();
|
||||
}
|
||||
|
||||
void AStorageContainer::OnRep_Open()
|
||||
{
|
||||
ApplyOpenState();
|
||||
}
|
||||
|
||||
void AStorageContainer::ApplyOpenState()
|
||||
{
|
||||
if (USoundBase* Sound = bOpen ? OpenSound : CloseSound)
|
||||
{
|
||||
UGameplayStatics::PlaySoundAtLocation(this, Sound, GetActorLocation());
|
||||
|
||||
Reference in New Issue
Block a user