'NetSlime' Initial port finished and working with 2 players (at least)
This commit is contained in:
		| @@ -1,4 +1,5 @@ | ||||
| #include "EnemyCharacter.h" | ||||
| #include "Networking/GasaNetLibrary_Inlines.h" | ||||
|  | ||||
| AEnemyCharacter::AEnemyCharacter() | ||||
| { | ||||
|   | ||||
| @@ -11,13 +11,12 @@ | ||||
| #include "AbilitySystem/GasaAttributeSet.h" | ||||
| #include "Components/SkeletalMeshComponent.h" | ||||
| #include "Engine/PostProcessVolume.h" | ||||
| #include "Game/GasaGameInstance.h" | ||||
| #include "Game/GasaLevelScriptActor.h" | ||||
| #include "Game/GasaPlayerController.h" | ||||
| #include "Materials/MaterialInstanceDynamic.h" | ||||
|  | ||||
| void AGasaCharacter::SetHighlight(EHighlight Desired) | ||||
| { | ||||
| 	HighlightState = Desired; | ||||
| } | ||||
| #include "Networking/GasaNetLibrary_Inlines.h" | ||||
| using namespace Gasa; | ||||
|  | ||||
| AGasaCharacter::AGasaCharacter() | ||||
| { | ||||
| @@ -51,13 +50,112 @@ AGasaCharacter::AGasaCharacter() | ||||
| 		 | ||||
| 		Attributes = CreateDefaultSubobject<UGasaAttributeSet>("Attributes"); | ||||
| 	} | ||||
|  | ||||
| 	// Replication | ||||
| 	 | ||||
| 	bReplicates            = false; | ||||
| 	bNetLoadOnClient       = true; | ||||
| 	NetDormancy            = DORM_Awake; | ||||
| 	NetCullDistanceSquared = NetCullDist_Medium; | ||||
| 	NetUpdateFrequency     = 30.0f; | ||||
| 	MinNetUpdateFrequency  = 5.0f; | ||||
| 	NetPriority            = 2.0f; | ||||
|  | ||||
| 	ACharacter::SetReplicateMovement(true); | ||||
| } | ||||
|  | ||||
| #pragma region GameFramework | ||||
| void AGasaCharacter::Controller_OnPawnPossessed() | ||||
| { | ||||
| 	NetLog("Controller confirmed possession."); | ||||
|  | ||||
| 	// Do stuff here that you needed to wait for the player controller be aware of you for. | ||||
| 	BP_Controller_OnPawnPossessed(); | ||||
|  | ||||
| 	if (Event_OnPawnReady.IsBound()) | ||||
| 		Event_OnPawnReady.Broadcast();	 | ||||
| } | ||||
|  | ||||
| void AGasaCharacter::ServerRPC_R_NotifyClientPawnReady_Implementation() | ||||
| { | ||||
| 	Event_OnPawnReady.Broadcast(); | ||||
| } | ||||
| #pragma endregion GameFramework | ||||
|  | ||||
| #pragma region Highlight | ||||
| void AGasaCharacter::SetHighlight(EHighlight Desired) | ||||
| { | ||||
| 	HighlightState = Desired; | ||||
| } | ||||
| #pragma endregion Highlight | ||||
|  | ||||
| #pragma region Pawn | ||||
| void AGasaCharacter::OnRep_PlayerState() | ||||
| { | ||||
| 	Super::OnRep_PlayerState(); | ||||
| } | ||||
|  | ||||
| void AGasaCharacter::PossessedBy(AController* NewController) | ||||
| { | ||||
| 	Super::PossessedBy(NewController); | ||||
| 	NetLog("Pawn possessed."); | ||||
|  | ||||
| 	AController* OldController; | ||||
| 	 | ||||
| 	// APawn::PossessedBy | ||||
| 	{ | ||||
| 		SetOwner(NewController); | ||||
| 		OldController = Controller; | ||||
| 		Controller = NewController; | ||||
|  | ||||
| 		ForceNetUpdate(); | ||||
|  | ||||
| 	#if UE_WITH_IRIS | ||||
| 		// The owning connection depends on the Controller having the new value. | ||||
| 		UpdateOwningNetConnection(); | ||||
| 	#endif | ||||
| 		 | ||||
| 		if (Controller->PlayerState != nullptr) | ||||
| 			SetPlayerState(Controller->PlayerState); | ||||
|  | ||||
| 		if (APlayerController* PlayerController = Cast<APlayerController>(Controller)) | ||||
| 		{ | ||||
| 			if (GetNetMode() != NM_Standalone) | ||||
| 			{ | ||||
| 				SetReplicates(true); | ||||
| 				SetAutonomousProxy(true); | ||||
| 			} | ||||
| 		} | ||||
| 		else | ||||
| 			CopyRemoteRoleFrom(GetDefault<APawn>()); | ||||
| 	} | ||||
|  | ||||
| 	if (AGasaPlayerController* PC = Cast<AGasaPlayerController>(NewController)) | ||||
| 	{ | ||||
| 		PC->Event_OnPawnPossessed.AddUniqueDynamic(this, & ThisClass::Controller_OnPawnPossessed); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		NetLog("Controller assigned to GasaCharacter is not derived from GasaPlayerController.", ELogV::Warning); | ||||
| 		NetLog("Controller: Name: " + NewController->GetName() + " Class: " + NewController->GetClass()->GetName(), ELogV::Warning); | ||||
| 	} | ||||
|  | ||||
| 	// cont. APawn::PossessedBy | ||||
| 	{ | ||||
| 		// Dispatch Blueprint event if necessary | ||||
| 		if (OldController != NewController) | ||||
| 		{ | ||||
| 			ReceivePossessed(Controller); | ||||
| 			NotifyControllerChanged(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// ACharacter::PossessedBy | ||||
| 	{ | ||||
| 		// If we are controlled remotely, set animation timing to be driven by client's network updates. So timing and events remain in sync. | ||||
| 		if (GetMesh() && IsReplicatingMovement() && (GetRemoteRole() == ROLE_AutonomousProxy && GetNetConnection() != nullptr)) | ||||
| 			GetMesh()->bOnlyAllowAutonomousTickPose = true; | ||||
| 	} | ||||
| 	 | ||||
| 	if (bAutoAbilitySystem) | ||||
| 	{ | ||||
| 		// TODO(Ed): Do we need to do this for enemies? | ||||
| @@ -65,9 +163,14 @@ void AGasaCharacter::PossessedBy(AController* NewController) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void AGasaCharacter::OnRep_PlayerState() | ||||
| void AGasaCharacter::SetPlayerDefaults() | ||||
| { | ||||
| 	Super::OnRep_PlayerState(); | ||||
| 	Super::SetPlayerDefaults(); | ||||
| } | ||||
|  | ||||
| void AGasaCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) | ||||
| { | ||||
| 	Super::SetupPlayerInputComponent(PlayerInputComponent); | ||||
| } | ||||
| #pragma endregion Pawn | ||||
|  | ||||
|   | ||||
| @@ -4,7 +4,7 @@ | ||||
| #include "GameFramework/Character.h" | ||||
|  | ||||
| #include "GasaCommon.h" | ||||
| #include "Game/GasaPlayerState.h" | ||||
| #include "Game/GasaGameState.h" | ||||
| #include "Networking/GasaNetLibrary.h" | ||||
|  | ||||
| #include "GasaCharacter.generated.h" | ||||
| @@ -22,6 +22,9 @@ class GASA_API AGasaCharacter : public ACharacter | ||||
| { | ||||
| 	GENERATED_BODY() | ||||
| public: | ||||
| 	 | ||||
| 	AGasaCharacter(); | ||||
|  | ||||
| #pragma region Ability System | ||||
| 	UPROPERTY(EditAnywhere, Category="Ability System") | ||||
| 	bool bAutoAbilitySystem = true; | ||||
| @@ -38,6 +41,20 @@ public: | ||||
| 	TObjectPtr<USkeletalMeshComponent> Weapon; | ||||
| #pragma endregion Combat | ||||
|  | ||||
| #pragma region GameFramework | ||||
| 	UPROPERTY(BlueprintAssignable) | ||||
| 	FOnPawnSig Event_OnPawnReady; | ||||
| 	 | ||||
| 	UFUNCTION() | ||||
| 	void Controller_OnPawnPossessed(); | ||||
|  | ||||
| 	UFUNCTION(BlueprintImplementableEvent) | ||||
| 	void BP_Controller_OnPawnPossessed(); | ||||
|  | ||||
| 	UFUNCTION(Server, Reliable) | ||||
| 	void ServerRPC_R_NotifyClientPawnReady(); | ||||
| #pragma endregion GameFramework | ||||
|  | ||||
| #pragma region Highlighting | ||||
| 	static constexpr float HighlightStencilDepth = 256.0; | ||||
| 	 | ||||
| @@ -57,10 +74,6 @@ public: | ||||
| 	FORCEINLINE void Dehighlight() { SetHighlight(EHighlight::Disabled); }; | ||||
| #pragma endregion Highlighting | ||||
|  | ||||
| 	AGasaCharacter(); | ||||
|  | ||||
| 	FORCEINLINE AGasaPlayerState* GetGasaPlayerState() { return GetPlayerState<AGasaPlayerState>(); } | ||||
|  | ||||
| #pragma region NetSlime | ||||
| 	// NetSlime interface is generated by GasaGen/GasaGen_NetSlime.cpp | ||||
| 	FORCEINLINE ENetworkMode GetNetworkMode() const { return Gasa::GetNetworkMode( this ); } | ||||
| @@ -89,14 +102,14 @@ public: | ||||
| #pragma endregion IAbilitySystem | ||||
|  | ||||
| #pragma region Pawn | ||||
| 	void PossessedBy(AController* NewController) override; | ||||
|  | ||||
| 	void OnRep_PlayerState() override; | ||||
| 	void PossessedBy(AController* NewController) override; | ||||
| 	void SetPlayerDefaults() override; | ||||
| 	void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override; | ||||
| #pragma endregion Pawn | ||||
| 	 | ||||
| #pragma region Actor | ||||
| 	void BeginPlay() override; | ||||
|  | ||||
| 	void Tick(float DeltaSeconds) override; | ||||
| #pragma endregion Actor | ||||
| }; | ||||
| @@ -108,3 +121,4 @@ namespace Gasa | ||||
| 	// 	 | ||||
| 	// } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| #include "PlayerCharacter.h" | ||||
|  | ||||
| #include "Networking/GasaNetLibrary_Inlines.h" | ||||
| #include "AbilitySystemComponent.h" | ||||
| #include "Game/GasaPlayerController.h" | ||||
| #include "UI/GasaHUD.h" | ||||
| @@ -12,46 +13,12 @@ APlayerCharacter::APlayerCharacter() | ||||
| 	bAutoAbilitySystem = false; | ||||
| } | ||||
|  | ||||
| // TODO(Ed): We need to setup Net Slime... | ||||
| void APlayerCharacter::PossessedBy(AController* NewController) | ||||
| { | ||||
| 	Super::PossessedBy(NewController); | ||||
|  | ||||
| 	AGasaPlayerState* PS = GetGasaPlayerState(); | ||||
| 	// Server setup ability system (character side) | ||||
| 	{ | ||||
| 		AbilitySystem = PS->AbilitySystem; | ||||
| 		Attributes    = PS->Attributes; | ||||
| 		AbilitySystem->InitAbilityActorInfo(PS, this); | ||||
| 	} | ||||
|  | ||||
| 	if (IsLocallyControlled()) | ||||
| 	{ | ||||
| 		AGasaPlayerController* PC   = GetController<AGasaPlayerController>(); | ||||
| 		AGasaHUD*              HUD  = PC->GetHUD<AGasaHUD>(); | ||||
| 		FWidgetControllerData  Data = { PC, PS, AbilitySystem, Attributes }; | ||||
| 		HUD->InitHostWidget(& Data); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TODO(Ed): We need to setup Net Slime... | ||||
| void APlayerCharacter::OnRep_PlayerState() | ||||
| { | ||||
| 	Super::OnRep_PlayerState(); | ||||
| 	 | ||||
| 	AGasaPlayerState* PS = GetGasaPlayerState(); | ||||
| 	// Client setup ability system | ||||
| 	{ | ||||
| 		AbilitySystem = PS->AbilitySystem; | ||||
| 		Attributes    = PS->Attributes; | ||||
| 		AbilitySystem->InitAbilityActorInfo(PS, this); | ||||
| 	} | ||||
|  | ||||
| 	if (IsLocallyControlled()) | ||||
| 	{ | ||||
| 		AGasaPlayerController* PC   = GetController<AGasaPlayerController>(); | ||||
| 		AGasaHUD*              HUD  = PC->GetHUD<AGasaHUD>(); | ||||
| 		FWidgetControllerData  Data = { PC, PS, AbilitySystem, Attributes }; | ||||
| 		HUD->InitHostWidget(& Data); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user