Advertisement
Guest User

MyCharacter.cpp

a guest
May 3rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "MyCharacter.h"
  4.  
  5.  
  6. // Sets default values
  7. AMyCharacter::AMyCharacter()
  8. {
  9. // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  10. PrimaryActorTick.bCanEverTick = true;
  11. }
  12.  
  13. AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
  14. {
  15.  
  16. PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
  17. PlayerCamera->AttachTo(GetRootComponent());
  18.  
  19. Diughlight = CreateDefaultSubobject<USpotLightComponent>(TEXT("Diughlight"));
  20. Diughlight->AttachTo(PlayerCamera);
  21.  
  22. //Weapon = CreateDefaultSubobject<UChildActorComponent>(TEXT("Weapon"));
  23. //Weapon->AttachTo(PlayerCamera);
  24. //Weapon->SetChildActorClass(AWeaponBase::StaticClass());
  25. //Weapon->CreateChildActor();
  26. }
  27.  
  28. // Called when the game starts or when spawned
  29. void AMyCharacter::BeginPlay()
  30. {
  31. Super::BeginPlay();
  32.  
  33. Diughlight->SetIntensity(0);
  34.  
  35. }
  36.  
  37. // Called every frame
  38. void AMyCharacter::Tick(float DeltaTime)
  39. {
  40. Super::Tick(DeltaTime);
  41.  
  42. GEngine->AddOnScreenDebugMessage(-1, .01, FColor::Red, FString::Printf(TEXT("%f"), LightMeter), true);
  43. GetLightIntensity();
  44.  
  45. if (bCrouchToggle)
  46. {
  47. Crouch();
  48. }
  49. else
  50. {
  51. UnCrouch();
  52. }
  53.  
  54. if (bFlashlightToggle)
  55. {
  56. Diughlight->SetIntensity(5000);
  57. }
  58. else
  59. {
  60. Diughlight->SetIntensity(0);
  61. }
  62.  
  63. }
  64.  
  65. // Called to bind functionality to input
  66. void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  67. {
  68. Super::SetupPlayerInputComponent(PlayerInputComponent);
  69.  
  70.  
  71. PlayerInputComponent->BindAxis("MoveForward",this,&AMyCharacter::MoveForward);
  72. PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
  73. PlayerInputComponent->BindAxis("LookUp", this, &AMyCharacter::LookUp);
  74. PlayerInputComponent->BindAxis("LookRight", this, &AMyCharacter::LookRight);
  75.  
  76. PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &AMyCharacter::ToggleCrouch);
  77. PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyCharacter::StartJump);
  78. PlayerInputComponent->BindAction("ToggleLight", IE_Pressed, this, &AMyCharacter::ToggleFlashlight);
  79. PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AMyCharacter::StartSprint);
  80. PlayerInputComponent->BindAction("Sprint", IE_Released, this, &AMyCharacter::EndSprint);
  81. //PlayerInputComponent->BindAction("Attack", IE_Pressed, this, &AWeaponBase::Attack);
  82.  
  83. }
  84.  
  85. void AMyCharacter::MoveForward(float Value)
  86. {
  87. FVector Direction = GetActorForwardVector();
  88. AddMovementInput(Direction, Value);
  89. }
  90.  
  91. void AMyCharacter::MoveRight(float Value)
  92. {
  93. FVector Direction = GetActorRightVector();
  94. AddMovementInput(Direction, Value);
  95. }
  96.  
  97. void AMyCharacter::LookUp(float Value)
  98. {
  99. AddControllerPitchInput(Value);
  100. }
  101.  
  102. void AMyCharacter::LookRight(float Value)
  103. {
  104. AddControllerYawInput(Value);
  105. }
  106.  
  107. void AMyCharacter::ToggleCrouch()
  108. {
  109. if (!bCrouchToggle)
  110. {
  111. bCrouchToggle = true;
  112. }
  113. else if(bCrouchToggle)
  114. {
  115. bCrouchToggle = false;
  116. }
  117. }
  118.  
  119. void AMyCharacter::StartJump()
  120. {
  121. Jump();
  122. }
  123.  
  124. void AMyCharacter::EndJump()
  125. {
  126. StopJumping();
  127. }
  128.  
  129. void AMyCharacter::ToggleFlashlight()
  130. {
  131. if (!bFlashlightToggle)
  132. {
  133. bFlashlightToggle = true;
  134. }
  135. else if (bFlashlightToggle)
  136. {
  137. bFlashlightToggle = false;
  138. }
  139. }
  140.  
  141. void AMyCharacter::StartSprint()
  142. {
  143. GetCharacterMovement()->MaxWalkSpeed = 800;
  144. }
  145.  
  146. void AMyCharacter::EndSprint()
  147. {
  148. GetCharacterMovement()->MaxWalkSpeed = 300;
  149. }
  150.  
  151. void AMyCharacter::GetLightIntensity()
  152. {
  153. LightMeter = 0;
  154. if (bFlashlightToggle)
  155. {
  156. LightMeter = 5000;
  157. }
  158. TArray<AActor*> PointLights; //Array of point lights in the world.
  159. UGameplayStatics::GetAllActorsOfClass(this, APointLight::StaticClass(), PointLights); //Gets all the point lights in the world.
  160. for (int32 i = 0; i < PointLights.Num(); i++) //Counts all the current lights
  161. {
  162. APointLight* Light = Cast<APointLight>(PointLights[i]);
  163. if (Light)
  164. {
  165. FVector EyesLocation;
  166. FRotator EyesRotation;
  167. GetActorEyesViewPoint(EyesLocation, EyesRotation);
  168.  
  169. float Distance = (Light->GetActorLocation() - EyesLocation).Size();
  170. if (Distance < Light->PointLightComponent->AttenuationRadius)
  171. {
  172. FCollisionQueryParams QueryParams;
  173. QueryParams.AddIgnoredActor(this);
  174. QueryParams.bTraceComplex = true;
  175.  
  176. FHitResult Hit;
  177. if (!GetWorld()->LineTraceSingleByChannel(Hit, EyesLocation, Light->GetActorLocation(), ECC_Visibility, QueryParams))
  178. {
  179. LightMeter = Light->PointLightComponent->Intensity;
  180. }
  181. }
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement