Skip to content

Instantly share code, notes, and snippets.

/ASpellWorldProjectile.cpp Secret
Created Mar 16, 2015

Embed
What would you like to do?
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "SpellWorld.h"
#include "SpellWorldProjectile.h"
ASpellWorldProjectile::ASpellWorldProjectile(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
myShouldDestroyOnHit = false;
//Use a sphere as a simple collision representation
myCollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
myCollisionComponent->SetSphereRadius(5.0f);
myCollisionComponent->BodyInstance.SetCollisionProfileName("Projectile");
myCollisionComponent->OnComponentHit.AddDynamic(this, &ASpellWorldProjectile::OnHit); // set up a notification for when this component hits something blocking
//Players can't walk on it
myCollisionComponent->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
myCollisionComponent->CanCharacterStepUpOn = ECB_No;
//Set as root component
this->RootComponent = myCollisionComponent;
//Use a ProjectileMovementComponent to govern this projectile's movement
myProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
myProjectileMovementComponent->UpdatedComponent = myCollisionComponent;
myProjectileMovementComponent->InitialSpeed = 3000.0f;
myProjectileMovementComponent->MaxSpeed = 10000.0f;
myProjectileMovementComponent->bRotationFollowsVelocity = true;
myProjectileMovementComponent->bShouldBounce = true;
this->InitialLifeSpan = 3.0f;
}
//Getters
bool ASpellWorldProjectile::GetShouldBounce()
{
return myProjectileMovementComponent->bShouldBounce;
}
bool ASpellWorldProjectile::GetShouldDestroyOnHit()
{
return myShouldDestroyOnHit;
}
float ASpellWorldProjectile::GetInitialSpeed()
{
return myProjectileMovementComponent->InitialSpeed;
}
float ASpellWorldProjectile::GetMaxSpeed()
{
return myProjectileMovementComponent->MaxSpeed;
}
FVector ASpellWorldProjectile::GetScale()
{
return this->GetActorScale();
}
//FVector ASpellWorldProjectile::GetVelocity()
//{
// return myProjectileMovementComponent->Velocity;
//}
FVector ASpellWorldProjectile::GetDirection()
{
return myProjectileMovementComponent->Velocity.SafeNormal();
}
USphereComponent* ASpellWorldProjectile::GetCollisionComponent()
{
return myCollisionComponent;
}
UProjectileMovementComponent* ASpellWorldProjectile::GetProjectileMovementComponent()
{
return myProjectileMovementComponent;
}
//Setters
void ASpellWorldProjectile::SetShouldBounce(bool shouldBounce)
{
myProjectileMovementComponent->bShouldBounce = shouldBounce;
}
void ASpellWorldProjectile::SetShouldDestroyOnHit(bool destroyOnHit)
{
myShouldDestroyOnHit = destroyOnHit;
}
void ASpellWorldProjectile::SetScale(FVector scale)
{
this->SetActorScale3D(scale);
}
//void ASpellWorldProjectile::SetVelocity(FVector velocity)
//{
// myProjectileMovementComponent->Velocity = velocity;
//}
void ASpellWorldProjectile::SetSpeed(float speed)
{
myProjectileMovementComponent->Velocity = GetDirection() * speed;
}
//Functions
void ASpellWorldProjectile::OnHit(AActor* otherActor, UPrimitiveComponent* otherComp, FVector normalImpulse, const FHitResult& hit)
{
// Only add impulse and destroy projectile if we hit a physics
if ((otherActor != NULL) && (otherActor != this) && (otherComp != NULL) && otherComp->IsSimulatingPhysics())
{
otherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
}
if (myShouldDestroyOnHit == true)
{
Destroy();
}
}
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/Actor.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "SpellWorldProjectile.generated.h"
//!!!!!!THIS WILL ENENTUALY BECOME AN ABSTACT CLASS AND ONLY CHILDREN WILL BE ABLE TO BE CREATED!!!!!!
//Base class of any projectile.
//UE4 requires the word "class" to preceede a class name.
//UPROPERTY and UFUNCTION let UE4 know about the variable/function.
UCLASS(config=Game)
class ASpellWorldProjectile : public AActor
{
public:
GENERATED_BODY()
bool myShouldDestroyOnHit; //True if the projectile should be destroyed when it hits an object.
//Constructor
ASpellWorldProjectile(const FObjectInitializer& ObjectInitializer);
//Sphere collision component
UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
class USphereComponent* myCollisionComponent;
//Projectile movement component
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
class UProjectileMovementComponent* myProjectileMovementComponent;
//Called when projectile hits something
UFUNCTION()
void OnHit(AActor* otherActor, UPrimitiveComponent* otherComp, FVector normalImpulse, const FHitResult& hit);
//Getters
bool GetShouldBounce();
bool GetShouldDestroyOnHit();
float GetInitialSpeed();
float GetMaxSpeed();
FVector GetScale();
//FVector GetVelocity();
FVector GetDirection();
class USphereComponent* GetCollisionComponent();
class UProjectileMovementComponent* GetProjectileMovementComponent();
//Setters
void SetShouldBounce(bool shouldBounce);
void SetShouldDestroyOnHit(bool destroyOnHit);
void SetScale(FVector scale);
//void SetVelocity(FVector velocity);
void SetSpeed(float speed);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.