Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SpaceDive.h"
- #include "MainPawn.h"
- #include "AsteroidSpawn.h"
- #include "SpaceDivePlayerState.h"
- #include "HighscoresSaveGame.h"
- AMainPawn::AMainPawn(const class FObjectInitializer& OI)
- : Super(OI), bDragActivated(false), LastTouchPoint(0.0f, 0.0f), bPlayerDied(false), PlayerLifesAtMultiplier(0), bUseMotionControl(false)
- {
- // Subobject creation
- MainCamera = OI.CreateDefaultSubobject<UCameraComponent>(this, TEXT("MainCamera"));
- CameraCapsule = OI.CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("CameraCapsule"));
- MultiplierSphere = OI.CreateDefaultSubobject<USphereComponent>(this, TEXT("MultiplierSphere"));
- InnerPointSphere = OI.CreateDefaultSubobject<USphereComponent>(this, TEXT("InnerPointSphere"));
- OuterPointSphere = OI.CreateDefaultSubobject<USphereComponent>(this, TEXT("OuterPointSphere"));
- CameraCapsule->AddRelativeRotation(FRotator(0.f, 90.f, 0.f));
- // Event subscription
- CameraCapsule->OnComponentBeginOverlap.AddDynamic(this, &AMainPawn::OnCollide);
- MultiplierSphere->OnComponentBeginOverlap.AddDynamic(this, &AMainPawn::OnMultiplierCollide);
- InnerPointSphere->OnComponentBeginOverlap.AddDynamic(this, &AMainPawn::OnInnerPointSphereCollide);
- OuterPointSphere->OnComponentBeginOverlap.AddDynamic(this, &AMainPawn::OnOuterPointSphereCollide);
- // Creating the object hierarchy
- RootComponent = MainCamera;
- CameraCapsule->AttachTo(MainCamera);
- MultiplierSphere->AttachTo(MainCamera);
- InnerPointSphere->AttachTo(MainCamera);
- OuterPointSphere->AttachTo(MainCamera);
- }
- void AMainPawn::SetupCapsule()
- {
- NearPlaneHeight = 2 * 10.0f * FMath::Tan(FMath::DegreesToRadians(MainCamera->FieldOfView / 2));
- NearPlaneWidth = MainCamera->AspectRatio * NearPlaneHeight;
- for (TActorIterator<AAsteroidSpawn> Itr(GetWorld()); Itr; ++Itr)
- {
- CameraBoundsWidth = Itr->GetActorScale3D().Y * 0.75f * 0.5f;
- CameraBoundsHeight = Itr->GetActorScale3D().Z * 0.75f * 0.5f;
- }
- const float Area = NearPlaneWidth * NearPlaneHeight;
- const float CollisionArea = Area / 9.0f;
- const float Radius = FMath::Sqrt(CollisionArea / PI);
- CameraCapsule->SetCapsuleSize(Radius, 0.01f);
- }
- void AMainPawn::SetupMultiplierSphere()
- {
- const float Area = NearPlaneWidth * NearPlaneHeight;
- const float HelperArea = Area / MultiplierSphereDivider;
- const float Radius = FMath::Sqrt(HelperArea / PI);
- MultiplierSphere->SetSphereRadius(Radius);
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::SanitizeFloat(Radius));
- }
- void AMainPawn::SetupInnerPointSphere()
- {
- const float Area = NearPlaneWidth * NearPlaneHeight;
- const float HelperArea = Area / InnerPointSphereDivider;
- const float Radius = FMath::Sqrt(HelperArea / PI);
- InnerPointSphere->SetSphereRadius(Radius);
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::SanitizeFloat(Radius));
- }
- void AMainPawn::SetupOuterPointSphere()
- {
- const float Area = NearPlaneWidth * NearPlaneHeight;
- const float HelperArea = Area / OuterPointSphereDivider;
- const float Radius = FMath::Sqrt(HelperArea / PI);
- OuterPointSphere->SetSphereRadius(Radius);
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::SanitizeFloat(Radius));
- }
- void AMainPawn::Tick(float Delta)
- {
- Super::Tick(Delta);
- if (bUseMotionControl)
- {
- TickMotionInput(Delta);
- }
- else
- {
- TickTouchInput(Delta);
- }
- TickPlayerMovement(Delta);
- TickCameraRoll(Delta);
- /*DrawDebugSphere(GetWorld(), MultiplierSphere->GetComponentLocation(), MultiplierSphere->GetScaledSphereRadius(), 32, FColor::Blue);
- DrawDebugSphere(GetWorld(), InnerPointSphere->GetComponentLocation(), InnerPointSphere->GetScaledSphereRadius(), 32, FColor::Red);
- DrawDebugSphere(GetWorld(), OuterPointSphere->GetComponentLocation(), OuterPointSphere->GetScaledSphereRadius(), 32, FColor::Yellow);
- DrawDebugCapsule(GetWorld(), CameraCapsule->GetComponentLocation(), CameraCapsule->GetScaledCapsuleHalfHeight(), CameraCapsule->GetScaledCapsuleRadius(), CameraCapsule->GetComponentRotation().Quaternion(), FColor::Green); */
- }
- void AMainPawn::TickPlayerMovement(float Delta)
- {
- // Move player
- FVector NewLoc = FVector(0.0f, MoveRightVal * FMath::Abs(MovementSpeedFactor) * Delta, MoveUpVal * MovementSpeedFactor * Delta);
- this->AddActorLocalOffset(NewLoc, true);
- // Ensure, that player is in the camera bounds
- NewLoc = this->GetActorLocation();
- NewLoc.Y = FMath::Clamp<float>(NewLoc.Y, 0.0f - CameraBoundsWidth, 0.0f + CameraBoundsWidth);
- NewLoc.Z = FMath::Clamp<float>(NewLoc.Z, 0.0f - CameraBoundsHeight, 0.0f + CameraBoundsHeight);
- this->SetActorLocation(NewLoc);
- }
- void AMainPawn::TickTouchInput(float Delta)
- {
- if (!bPlayerDied && bDragActivated)
- {
- const float FactorWeight = 0.2000;
- APlayerController* const PC = Cast<APlayerController>(GetController());
- check(PC);
- float LocationX;
- float LocationY;
- bool bPressed;
- PC->GetInputTouchState(ETouchIndex::Touch1, LocationX, LocationY, bPressed);
- const FVector2D TouchLoc(LocationX, LocationY);
- const FVector2D DeltaTouch((TouchLoc - LastTouchPoint) * FactorWeight);
- MoveRightVal = DeltaTouch.X;
- MoveUpVal = DeltaTouch.Y;
- LastTouchPoint = TouchLoc;
- }
- }
- void AMainPawn::TickCameraRoll(float Delta)
- {
- const FRotator NewRot(0.0f, 0.0f, RollRotationRate * Delta);
- GetController()->SetControlRotation(NewRot + GetControlRotation());
- }
- void AMainPawn::TickMotionInput(float Delta)
- {
- APlayerController* const PC = Cast<APlayerController>(Controller);
- FVector Tilt;
- FVector RotationRate;
- FVector Gravity;
- FVector Acceleration;
- PC->GetInputMotionState(Tilt, RotationRate, Gravity, Acceleration);
- /*
- if (GEngine)
- {
- GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Green, Tilt.ToString());
- GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, RotationRate.ToString());
- GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, Gravity.ToString());
- GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Blue, Acceleration.ToString());
- }
- */
- // The "normal" tilt value, so where the deadzone is
- const float TiltNormal = 2.0f;
- // The half width of the dead zone (1 to each side)
- const float TiltTolerance = 0.1f;
- // The "normal" gravity value, so the deadzone
- const float GravityYNormal = 0.0f;
- // The half width of the deadzone (1 to each side)
- const float GravityYTolerance = 0.5f;
- // Tilt max and min values, that can occur
- const float TiltMax = 3.0f;
- const float TiltMin = 0.0f;
- // Gravity max and min value, that can occur
- const float GravityYMin = -10.0f;
- const float GravityYMax = 10.0f;
- // Motion scaling factor
- const float MotionFactor = 10.0f;
- float TiltOffset = ((TiltMax - TiltMin) / 2) - TiltMax; // So the middle is on 0.0f
- MoveUpVal = MotionFactor * -(Tilt.Z + TiltOffset) / ((TiltMax - TiltMin) / 2); // Divide by the last part to clamp between 0 and 1
- MoveRightVal = MotionFactor * Gravity.Y / ((GravityYMax - GravityYMin) / 2); // Same as above without offset
- // DEADZONES
- if ((Tilt.Z <= TiltNormal + TiltTolerance) && (Tilt.Z >= TiltNormal - TiltTolerance))
- {
- MoveUpVal = 0.0f;
- }
- if ((Gravity.Y <= GravityYNormal + GravityYTolerance) && (Gravity.Y >= GravityYNormal - GravityYTolerance))
- {
- MoveRightVal = 0.0f;
- }
- }
- void AMainPawn::BeginPlay()
- {
- Super::BeginPlay();
- SetupCapsule();
- SetupMultiplierSphere();
- SetupInnerPointSphere();
- SetupOuterPointSphere();
- SetupMusic();
- Cast<ASpaceDivePlayerState>(PlayerState)->PlayerLifes = InitialPlayerLifes;
- GetController()->SetControlRotation(FRotator(0.0f, 0.0f, -85.0f));
- }
- void AMainPawn::SetupMusic()
- {
- ASpaceDivePlayerState* const State = Cast<ASpaceDivePlayerState>(PlayerState);
- const int SoundIndex = FMath::RandRange(0, MusicList.Num() - 1);
- USoundBase* const SoundToPlay = MusicList[SoundIndex];
- check(SoundToPlay);
- UGameplayStatics::PlaySoundAttached(SoundToPlay, MainCamera);
- }
- void AMainPawn::SetupPlayerInputComponent(UInputComponent* InputComponent)
- {
- InputComponent->BindAxis("MoveUp", this, &AMainPawn::MoveUp);
- InputComponent->BindAxis("MoveRight", this, &AMainPawn::MoveRight);
- }
- void AMainPawn::MoveUp(float Val)
- {
- MoveUpVal = Val;
- }
- void AMainPawn::MoveRight(float Val)
- {
- MoveRightVal = Val;
- }
- void AMainPawn::OnPlayerTouch(bool bTouch)
- {
- bDragActivated = bTouch;
- APlayerController* const Controller = Cast<APlayerController>(GetController());
- check(Controller);
- float LocationX;
- float LocationY;
- bool bIsPressed;
- Controller->GetInputTouchState(ETouchIndex::Touch1, LocationX, LocationY, bIsPressed);
- LastTouchPoint = FVector2D(LocationX, LocationY);
- }
- void AMainPawn::OnCollide(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bSweep, const FHitResult& SweepResult)
- {
- OnHit();
- OtherActor->Destroy();
- Cast<ASpaceDivePlayerState>(PlayerState)->ResetMultiplier();
- }
- void AMainPawn::OnMultiplierCollide(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bSweep, const FHitResult& SweepResult)
- {
- MultiplierCollides();
- //GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Multiplier");
- PlayerLifesAtMultiplier = (Cast<ASpaceDivePlayerState>(PlayerState))->PlayerLifes;
- GetWorldTimerManager().SetTimer(this, &AMainPawn::OnMultiplierDelayEnds, 0.25f, false);
- }
- void AMainPawn::OnMultiplierDelayEnds()
- {
- ASpaceDivePlayerState* const SDPlayerState = Cast<ASpaceDivePlayerState>(PlayerState);
- check(SDPlayerState);
- if (SDPlayerState->PlayerLifes == PlayerLifesAtMultiplier)
- {
- check(MultiplierSound);
- SDPlayerState->IncreaseMultiplier();
- UGameplayStatics::PlaySoundAtLocation(GetWorld(), MultiplierSound, FVector(0.0f, 0.0f, 0.0f));
- }
- }
- void AMainPawn::OnInnerPointSphereCollide(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bSweep, const FHitResult& SweepResult)
- {
- AddPlayerPoints(InnerPointSpherePoints);
- //GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Point");
- }
- void AMainPawn::OnOuterPointSphereCollide(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bSweep, const FHitResult& SweepResult)
- {
- AddPlayerPoints(OuterPointSpherePoints);
- //GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Less Points");
- }
- void AMainPawn::AddPlayerPoints(float Points)
- {
- if (PlayerState)
- PlayerState->Score += Points * Cast<ASpaceDivePlayerState>(PlayerState)->Multiplier;
- }
- void AMainPawn::OnHit()
- {
- PlayerHitEvent.Broadcast();
- ASpaceDivePlayerState* LocalPlayerState = Cast<ASpaceDivePlayerState>(PlayerState);
- PlayerHitsAsteroid();
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Hit");
- (LocalPlayerState->PlayerLifes)--;
- if (LocalPlayerState->PlayerLifes == 0)
- {
- GameOver();
- }
- }
- void AMainPawn::GameOver()
- {
- SaveScore();
- bPlayerDied = true;
- PlayerDies();
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Game Over");
- GameOverEvent.Broadcast();
- }
- void AMainPawn::SaveScore()
- {
- UHighscoresSaveGame* SaveGame = Cast<UHighscoresSaveGame>(UGameplayStatics::CreateSaveGameObject(UHighscoresSaveGame::StaticClass()));
- if (UGameplayStatics::DoesSaveGameExist(SaveGame->SaveSlotName, SaveGame->UserIndex))
- {
- SaveGame = Cast<UHighscoresSaveGame>(UGameplayStatics::LoadGameFromSlot(SaveGame->SaveSlotName, SaveGame->UserIndex));
- }
- ASpaceDivePlayerState* const SDPlayerState = Cast<ASpaceDivePlayerState>(PlayerState);
- for (int32 i = 0; i < SaveGame->HighscoreData.Num(); i++)
- {
- if (SDPlayerState->Score >= SaveGame->HighscoreData[i])
- {
- SaveGame->HighscoreData.Insert(SDPlayerState->Score, i);
- SaveGame->HighscoreData.RemoveAt(SaveGame->HighscoreData.Num() - 1); // Highscore list should keep a size of 10
- break;
- }
- }
- UGameplayStatics::SaveGameToSlot(SaveGame, SaveGame->SaveSlotName, SaveGame->UserIndex);
- }
- void AMainPawn::SetYAxisInverted(bool bInverted)
- {
- const bool bShouldInvert = bInverted && (MovementSpeedFactor > 0.0f);
- const bool bShouldRevert = !bInverted && (MovementSpeedFactor < 0.0f);
- if (bShouldInvert || bShouldRevert)
- {
- MovementSpeedFactor *= -1.f;
- }
- }
- void AMainPawn::SetUseMotionControl(bool bUseMotionControl)
- {
- this->bUseMotionControl = bUseMotionControl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement