diff --git a/Content/CTP/01_Level/L_Default.umap b/Content/CTP/01_Level/L_Default.umap index c957fa7..d006b8e 100644 Binary files a/Content/CTP/01_Level/L_Default.umap and b/Content/CTP/01_Level/L_Default.umap differ diff --git a/Content/CTP/04_Mesh/SM_Projectile.uasset b/Content/CTP/04_Mesh/SM_Projectile.uasset new file mode 100644 index 0000000..62b8258 Binary files /dev/null and b/Content/CTP/04_Mesh/SM_Projectile.uasset differ diff --git a/Content/CTP/05_Material/M_Player.uasset b/Content/CTP/05_Material/M_Player.uasset index 8e79efb..5b464fe 100644 Binary files a/Content/CTP/05_Material/M_Player.uasset and b/Content/CTP/05_Material/M_Player.uasset differ diff --git a/Source/M4_CPP/private/M4_PlayerPawn.cpp b/Source/M4_CPP/private/M4_PlayerPawn.cpp index 664ea3e..c9a6c28 100644 --- a/Source/M4_CPP/private/M4_PlayerPawn.cpp +++ b/Source/M4_CPP/private/M4_PlayerPawn.cpp @@ -21,7 +21,6 @@ AM4_PlayerPawn::AM4_PlayerPawn() MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName); MeshComponent->SetRelativeScale3D(FVector(1.0f, MeshScale.Y, MeshScale.X)); - static ConstructorHelpers::FObjectFinder DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube")); if (DefaultMeshRef.Succeeded()) @@ -85,22 +84,26 @@ void AM4_PlayerPawn::Move(const FInputActionInstance& Instance) void AM4_PlayerPawn::Shoot(const FInputActionInstance& Inst) { - if (Inst.GetValue().Get() == true) - { - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Using the Wand!")); - UWorld* const World = GetWorld(); - if (!Proj && !World) + if (Proj) { - FVector TargetPosition = GetActorLocation(); - FRotator SpawnRotation = GetActorRotation(); + UWorld* World = GetWorld(); + GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("Using the Wand!")); - FActorSpawnParameters ActorSpawnParams; - ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding; - ActorSpawnParams.Owner = this; + if (World) + { + FActorSpawnParameters SpawnParams; + SpawnParams.Owner = this; + const FVector InitialLocation = FVector(0.0f, GetActorLocation().Y, GetActorLocation().Z); - GetWorld()->SpawnActor(Proj, TargetPosition, SpawnRotation, ActorSpawnParams); + AM4_Projectile* Projectile = World->SpawnActor(Proj, InitialLocation, FRotator(), SpawnParams); + FVector LaunchDirection = FVector(0.0f, InitialLocation.Y + 1.f,InitialLocation.Z); + + if (Projectile) + { + Projectile->ShootDirection(LaunchDirection); + GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("Spell Casted!")); + } + } } - - } -}; +}; diff --git a/Source/M4_CPP/private/M4_Projectile.cpp b/Source/M4_CPP/private/M4_Projectile.cpp index 1bdc24e..894a635 100644 --- a/Source/M4_CPP/private/M4_Projectile.cpp +++ b/Source/M4_CPP/private/M4_Projectile.cpp @@ -1,26 +1,71 @@ -// Fill out your copyright notice in the Description page of Project Settings. - - -#include "M4_Projectile.h" +#include "M4_Projectile.h" #include "GameFramework/ProjectileMovementComponent.h" #include "Components/MeshComponent.h" // Sets default values AM4_Projectile::AM4_Projectile() { - ProjectileMesh = CreateDefaultSubobject(TEXT("MeshComp")); - ProjectileMesh-> SetRelativeScale3D(FVector(1.f, .1f,.1f)); - ProjectileMesh->BodyInstance.SetCollisionProfileName(TEXT("Projectile")); - ProjectileMesh->OnComponentHit.AddDynamic(this, &AM4_Projectile::OnHit); - ProjectileMesh->SetupAttachment(RootComponent); - ProjectileMesh->SetMobility(EComponentMobility::Movable); - ProjectileMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); - + PrimaryActorTick.bCanEverTick = true; + if(!RootComponent) + { + RootComponent = CreateDefaultSubobject(TEXT("ProjectileSceneComponent")); + } + + if(!CollisionComp) + { + CollisionComp = CreateDefaultSubobject(TEXT("MeshComponent")); + RootComponent = CollisionComp; + } + + + if(!ProjectileMovementComponent) + { + ProjectileMovementComponent = CreateDefaultSubobject(TEXT("ProjectileMovementComponent")); + ProjectileMovementComponent->SetUpdatedComponent(CollisionComp); + ProjectileMovementComponent->InitialSpeed = 700.0f; + ProjectileMovementComponent->MaxSpeed = 700.0f; + ProjectileMovementComponent->bRotationFollowsVelocity = true; + ProjectileMovementComponent->bShouldBounce = false; + ProjectileMovementComponent->ProjectileGravityScale = 0.0f; + } + + if (!ProjectileMovementComponent) + { + ProjectileMeshComponent = CreateDefaultSubobject(TEXT("ProjectileMeshComponent")); + + static ConstructorHelpers::FObjectFinderMesh(TEXT("'/Script/Engine.StaticMesh'/Game/CTP/04_Mesh/SM_Projectile.SM_Projectile'")); + if(Mesh.Succeeded()) + { + ProjectileMeshComponent->SetStaticMesh(Mesh.Object); + } + + static ConstructorHelpers::FObjectFinderMaterial(TEXT("'/Script/Engine.Material'/Game/CTP/05_Material/M_Player.M_Player'")); + if (Material.Succeeded()) + { + ProjectileMat = UMaterialInstanceDynamic::Create(Material.Object, ProjectileMeshComponent); + } + + ProjectileMeshComponent->SetMaterial(0, ProjectileMat); + ProjectileMeshComponent->SetRelativeScale3D(FVector(.2f, .2f, .2f)); + ProjectileMeshComponent->SetupAttachment(RootComponent); + } + +} + +void AM4_Projectile::ShootDirection(const FVector& ShootDir) +{ + ProjectileMovementComponent->Velocity = ShootDir * ProjectileMovementComponent->InitialSpeed; } void AM4_Projectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector Impulse, const FHitResult& Hit) { - OtherComp->AddImpulseAtLocation(GetVelocity() * BulletSpeed, GetActorLocation()); - Destroy(); + //OtherComp->AddImpulseAtLocation(GetVelocity() * BulletSpeed, GetActorLocation()); +} + +// Called every frame +void AM4_Projectile::Tick( float DeltaTime ) +{ + Super::Tick( DeltaTime ); + } \ No newline at end of file diff --git a/Source/M4_CPP/public/M4_PlayerPawn.h b/Source/M4_CPP/public/M4_PlayerPawn.h index a9f9be6..ebbde98 100644 --- a/Source/M4_CPP/public/M4_PlayerPawn.h +++ b/Source/M4_CPP/public/M4_PlayerPawn.h @@ -21,6 +21,9 @@ public: virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override; void Move(const FInputActionInstance& Instance); void Shoot(const FInputActionInstance& Inst); + + UPROPERTY(VisibleAnywhere) + TSubclassOf Proj = AM4_Projectile::StaticClass(); protected: @@ -32,9 +35,6 @@ protected: UPROPERTY() UInputAction* ShootAction; - - UPROPERTY(VisibleAnywhere) - TSubclassOf Proj; float MoveSpeed = 500.f; FVector2D MeshScale = FVector2D(0.6f, 0.5f); diff --git a/Source/M4_CPP/public/M4_Projectile.h b/Source/M4_CPP/public/M4_Projectile.h index bb81f33..1c5b982 100644 --- a/Source/M4_CPP/public/M4_Projectile.h +++ b/Source/M4_CPP/public/M4_Projectile.h @@ -1,4 +1,4 @@ -// Fill out your copyright notice in the Description page of Project Settings. +#include "GameFramework/ProjectileMovementComponent.h" #pragma once @@ -6,40 +6,33 @@ #include "GameFramework/Actor.h" #include "M4_Projectile.generated.h" -class UProjectileMovementComponent; class UMeshComponent; UCLASS() class M4_CPP_API AM4_Projectile : public AActor { GENERATED_BODY() - UPROPERTY(VisibleDefaultsOnly, Category = Projectile) - UMeshComponent* MeshComp; - UPROPERTY(VisibleAnywhere,BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true")) - UProjectileMovementComponent* ProjectileMovem; + UPROPERTY(VisibleAnywhere, Category = Movement) + UProjectileMovementComponent* ProjectileMovementComponent; + public: AM4_Projectile(); + + virtual void Tick( float DeltaTime ) override; + + UPROPERTY(VisibleDefaultsOnly, Category = Projectile) + UMeshComponent* CollisionComp; + + UPROPERTY(VisibleDefaultsOnly, Category = Projectile) + UStaticMeshComponent* ProjectileMeshComponent; - // Called every frame - //virtual void Tick(float DeltaTime) override; - - // Despawn after 5 seconds by default - UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Lifespan") - float ProjectileLifespan = 3.0f; - - // The amount of force this projectile imparts on objects it collides with - UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Physics") - float BulletSpeed = 100.0f; - - // Mesh of the projectile in the world - UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Mesh") - TObjectPtr ProjectileMesh; + UPROPERTY(VisibleDefaultsOnly, Category = Movement) + UMaterialInstanceDynamic* ProjectileMat; - UFUNCTION() + void ShootDirection(const FVector& ShootDir); void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,FVector Impulse, const FHitResult& Hit); - UMeshComponent* GetCollisionComp() const { return MeshComp; } - UProjectileMovementComponent* GetMovementComp() const { return ProjectileMovem; } + };