Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
- #include "Demo.h"
- #include "DemoCharacter.h"
- #include "DemoProjectile.h"
- #include "UsableActor.h"
- //////////////////////////////////////////////////////////////////////////
- // ADemoCharacter
- ADemoCharacter::ADemoCharacter(const class FPostConstructInitializeProperties& PCIP)
- : Super(PCIP)
- {
- // Set size for collision capsule
- CapsuleComponent->InitCapsuleSize(42.f, 96.0f);
- // set our turn rates for input
- BaseTurnRate = 45.f;
- BaseLookUpRate = 45.f;
- // Create a CameraComponent
- FirstPersonCameraComponent = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
- FirstPersonCameraComponent->AttachParent = CapsuleComponent;
- FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 64.f); // Position the camera
- // Default offset from the character location for projectiles to spawn
- GunOffset = FVector(100.0f, 30.0f, 10.0f);
- // Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
- Mesh1P = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("CharacterMesh1P"));
- Mesh1P->SetOnlyOwnerSee(true); // only the owning player will see this mesh
- Mesh1P->AttachParent = FirstPersonCameraComponent;
- Mesh1P->RelativeLocation = FVector(0.f, 0.f, -150.f);
- Mesh1P->bCastDynamicShadow = false;
- Mesh1P->CastShadow = false;
- // Note: The ProjectileClass and the skeletal mesh/anim blueprints for Mesh1P are set in the
- // derived blueprint asset named MyCharacter (to avoid direct content references in C++)
- MaxUseDistance = 800;
- bHasNewFocus = true;
- }
- /*
- Performs raytrace to find closest looked-at UsableActor.
- */
- AUsableActor* ADemoCharacter::GetUsableInView()
- {
- FVector camLoc;
- FRotator camRot;
- if (Controller == NULL)
- return NULL;
- Controller->GetPlayerViewPoint(camLoc, camRot);
- const FVector start_trace = camLoc;
- const FVector direction = camRot.Vector();
- const FVector end_trace = start_trace + (direction * MaxUseDistance);
- FCollisionQueryParams TraceParams(FName(TEXT("")), true, this);
- TraceParams.bTraceAsyncScene = true;
- TraceParams.bReturnPhysicalMaterial = false;
- TraceParams.bTraceComplex = true;
- FHitResult Hit(ForceInit);
- GetWorld()->LineTraceSingle(Hit, start_trace, end_trace, COLLISION_PROJECTILE, TraceParams);
- return Cast<AUsableActor>(Hit.GetActor());
- }
- /*
- Update actor currently being looked at by player.
- */
- void ADemoCharacter::Tick(float DeltaSeconds)
- {
- Super::Tick(DeltaSeconds);
- if (Controller && Controller->IsLocalController())
- {
- AUsableActor* usable = GetUsableInView();
- // End Focus
- if (FocusedUsableActor != usable)
- {
- if (FocusedUsableActor)
- {
- FocusedUsableActor->EndFocusItem();
- }
- bHasNewFocus = true;
- }
- // Assign new Focus
- FocusedUsableActor = usable;
- // Start Focus.
- if (usable)
- {
- if (bHasNewFocus)
- {
- usable->StartFocusItem();
- bHasNewFocus = false;
- }
- }
- }
- }
- /*
- Runs on Server. Perform "OnUsed" on currently viewed UsableActor if implemented.
- */
- void ADemoCharacter::Use_Implementation()
- {
- if (FocusedUsableActor)
- {
- FocusedUsableActor->OnUsed(this);
- }
- }
- bool ADemoCharacter::Use_Validate()
- {
- // No special server-side validation performed.
- return true;
- }
- //////////////////////////////////////////////////////////////////////////
- // Input
- void ADemoCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
- {
- // set up gameplay key bindings
- check(InputComponent);
- InputComponent->BindAction("Use", IE_Pressed, this, &ADemoCharacter::Use);
- InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
- InputComponent->BindAction("Fire", IE_Pressed, this, &ADemoCharacter::OnFire);
- InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ADemoCharacter::TouchStarted);
- InputComponent->BindAxis("MoveForward", this, &ADemoCharacter::MoveForward);
- InputComponent->BindAxis("MoveRight", this, &ADemoCharacter::MoveRight);
- // We have 2 versions of the rotation bindings to handle different kinds of devices differently
- // "turn" handles devices that provide an absolute delta, such as a mouse.
- // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
- InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
- InputComponent->BindAxis("TurnRate", this, &ADemoCharacter::TurnAtRate);
- InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
- InputComponent->BindAxis("LookUpRate", this, &ADemoCharacter::LookUpAtRate);
- }
- void ADemoCharacter::OnFire()
- {
- // try and fire a projectile
- if (ProjectileClass != NULL)
- {
- const FRotator SpawnRotation = GetControlRotation();
- // MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
- const FVector SpawnLocation = GetActorLocation() + SpawnRotation.RotateVector(GunOffset);
- UWorld* const World = GetWorld();
- if (World != NULL)
- {
- // spawn the projectile at the muzzle
- World->SpawnActor<ADemoProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);
- }
- }
- // try and play the sound if specified
- if (FireSound != NULL)
- {
- UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
- }
- // try and play a firing animation if specified
- if(FireAnimation != NULL)
- {
- // Get the animation object for the arms mesh
- UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
- if(AnimInstance != NULL)
- {
- AnimInstance->Montage_Play(FireAnimation, 1.f);
- }
- }
- }
- void ADemoCharacter::TouchStarted(const ETouchIndex::Type FingerIndex, const FVector Location)
- {
- // only fire for first finger down
- if (FingerIndex == 0)
- {
- OnFire();
- }
- }
- void ADemoCharacter::MoveForward(float Value)
- {
- if (Value != 0.0f)
- {
- // find out which way is forward
- const FRotator Rotation = GetControlRotation();
- FRotator YawRotation(0, Rotation.Yaw, 0);
- // Get forward vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
- // add movement in that direction
- AddMovementInput(Direction, Value);
- }
- }
- void ADemoCharacter::MoveRight(float Value)
- {
- if (Value != 0.0f)
- {
- // find out which way is right
- const FRotator Rotation = GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // Get right vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
- // add movement in that direction
- AddMovementInput(Direction, Value);
- }
- }
- void ADemoCharacter::TurnAtRate(float Rate)
- {
- // calculate delta for this frame from the rate information
- AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
- }
- void ADemoCharacter::LookUpAtRate(float Rate)
- {
- // calculate delta for this frame from the rate information
- AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement