Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "MyCharacter.h"
- // Sets default values
- AMyCharacter::AMyCharacter()
- {
- // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- }
- AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
- {
- PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
- PlayerCamera->AttachTo(GetRootComponent());
- Diughlight = CreateDefaultSubobject<USpotLightComponent>(TEXT("Diughlight"));
- Diughlight->AttachTo(PlayerCamera);
- //Weapon = CreateDefaultSubobject<UChildActorComponent>(TEXT("Weapon"));
- //Weapon->AttachTo(PlayerCamera);
- //Weapon->SetChildActorClass(AWeaponBase::StaticClass());
- //Weapon->CreateChildActor();
- }
- // Called when the game starts or when spawned
- void AMyCharacter::BeginPlay()
- {
- Super::BeginPlay();
- Diughlight->SetIntensity(0);
- }
- // Called every frame
- void AMyCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- GEngine->AddOnScreenDebugMessage(-1, .01, FColor::Red, FString::Printf(TEXT("%f"), LightMeter), true);
- GetLightIntensity();
- if (bCrouchToggle)
- {
- Crouch();
- }
- else
- {
- UnCrouch();
- }
- if (bFlashlightToggle)
- {
- Diughlight->SetIntensity(5000);
- }
- else
- {
- Diughlight->SetIntensity(0);
- }
- }
- // Called to bind functionality to input
- void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- PlayerInputComponent->BindAxis("MoveForward",this,&AMyCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
- PlayerInputComponent->BindAxis("LookUp", this, &AMyCharacter::LookUp);
- PlayerInputComponent->BindAxis("LookRight", this, &AMyCharacter::LookRight);
- PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &AMyCharacter::ToggleCrouch);
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyCharacter::StartJump);
- PlayerInputComponent->BindAction("ToggleLight", IE_Pressed, this, &AMyCharacter::ToggleFlashlight);
- PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AMyCharacter::StartSprint);
- PlayerInputComponent->BindAction("Sprint", IE_Released, this, &AMyCharacter::EndSprint);
- //PlayerInputComponent->BindAction("Attack", IE_Pressed, this, &AWeaponBase::Attack);
- }
- void AMyCharacter::MoveForward(float Value)
- {
- FVector Direction = GetActorForwardVector();
- AddMovementInput(Direction, Value);
- }
- void AMyCharacter::MoveRight(float Value)
- {
- FVector Direction = GetActorRightVector();
- AddMovementInput(Direction, Value);
- }
- void AMyCharacter::LookUp(float Value)
- {
- AddControllerPitchInput(Value);
- }
- void AMyCharacter::LookRight(float Value)
- {
- AddControllerYawInput(Value);
- }
- void AMyCharacter::ToggleCrouch()
- {
- if (!bCrouchToggle)
- {
- bCrouchToggle = true;
- }
- else if(bCrouchToggle)
- {
- bCrouchToggle = false;
- }
- }
- void AMyCharacter::StartJump()
- {
- Jump();
- }
- void AMyCharacter::EndJump()
- {
- StopJumping();
- }
- void AMyCharacter::ToggleFlashlight()
- {
- if (!bFlashlightToggle)
- {
- bFlashlightToggle = true;
- }
- else if (bFlashlightToggle)
- {
- bFlashlightToggle = false;
- }
- }
- void AMyCharacter::StartSprint()
- {
- GetCharacterMovement()->MaxWalkSpeed = 800;
- }
- void AMyCharacter::EndSprint()
- {
- GetCharacterMovement()->MaxWalkSpeed = 300;
- }
- void AMyCharacter::GetLightIntensity()
- {
- LightMeter = 0;
- if (bFlashlightToggle)
- {
- LightMeter = 5000;
- }
- TArray<AActor*> PointLights; //Array of point lights in the world.
- UGameplayStatics::GetAllActorsOfClass(this, APointLight::StaticClass(), PointLights); //Gets all the point lights in the world.
- for (int32 i = 0; i < PointLights.Num(); i++) //Counts all the current lights
- {
- APointLight* Light = Cast<APointLight>(PointLights[i]);
- if (Light)
- {
- FVector EyesLocation;
- FRotator EyesRotation;
- GetActorEyesViewPoint(EyesLocation, EyesRotation);
- float Distance = (Light->GetActorLocation() - EyesLocation).Size();
- if (Distance < Light->PointLightComponent->AttenuationRadius)
- {
- FCollisionQueryParams QueryParams;
- QueryParams.AddIgnoredActor(this);
- QueryParams.bTraceComplex = true;
- FHitResult Hit;
- if (!GetWorld()->LineTraceSingleByChannel(Hit, EyesLocation, Light->GetActorLocation(), ECC_Visibility, QueryParams))
- {
- LightMeter = Light->PointLightComponent->Intensity;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement