Implement basic combat system
This commit is contained in:
@@ -148,3 +148,61 @@ ConnectionType=USBOnly
|
|||||||
bUseManualIPAddress=False
|
bUseManualIPAddress=False
|
||||||
ManualIPAddress=
|
ManualIPAddress=
|
||||||
|
|
||||||
|
[/Script/Engine.CollisionProfile]
|
||||||
|
-Profiles=(Name="NoCollision",CollisionEnabled=NoCollision,ObjectTypeName="WorldStatic",CustomResponses=((Channel="Visibility",Response=ECR_Ignore),(Channel="Camera",Response=ECR_Ignore)),HelpMessage="No collision",bCanModify=False)
|
||||||
|
-Profiles=(Name="BlockAll",CollisionEnabled=QueryAndPhysics,ObjectTypeName="WorldStatic",CustomResponses=,HelpMessage="WorldStatic object that blocks all actors by default. All new custom channels will use its own default response. ",bCanModify=False)
|
||||||
|
-Profiles=(Name="OverlapAll",CollisionEnabled=QueryOnly,ObjectTypeName="WorldStatic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility",Response=ECR_Overlap),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldStatic object that overlaps all actors by default. All new custom channels will use its own default response. ",bCanModify=False)
|
||||||
|
-Profiles=(Name="BlockAllDynamic",CollisionEnabled=QueryAndPhysics,ObjectTypeName="WorldDynamic",CustomResponses=,HelpMessage="WorldDynamic object that blocks all actors by default. All new custom channels will use its own default response. ",bCanModify=False)
|
||||||
|
-Profiles=(Name="OverlapAllDynamic",CollisionEnabled=QueryOnly,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility",Response=ECR_Overlap),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldDynamic object that overlaps all actors by default. All new custom channels will use its own default response. ",bCanModify=False)
|
||||||
|
-Profiles=(Name="IgnoreOnlyPawn",CollisionEnabled=QueryOnly,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Pawn",Response=ECR_Ignore),(Channel="Vehicle",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that ignores Pawn and Vehicle. All other channels will be set to default.",bCanModify=False)
|
||||||
|
-Profiles=(Name="OverlapOnlyPawn",CollisionEnabled=QueryOnly,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Pawn",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that overlaps Pawn, Camera, and Vehicle. All other channels will be set to default. ",bCanModify=False)
|
||||||
|
-Profiles=(Name="Pawn",CollisionEnabled=QueryAndPhysics,ObjectTypeName="Pawn",CustomResponses=((Channel="Visibility",Response=ECR_Ignore)),HelpMessage="Pawn object. Can be used for capsule of any playerable character or AI. ",bCanModify=False)
|
||||||
|
-Profiles=(Name="Spectator",CollisionEnabled=QueryOnly,ObjectTypeName="Pawn",CustomResponses=((Channel="WorldStatic",Response=ECR_Block),(Channel="Pawn",Response=ECR_Ignore),(Channel="Visibility",Response=ECR_Ignore),(Channel="WorldDynamic",Response=ECR_Ignore),(Channel="Camera",Response=ECR_Ignore),(Channel="PhysicsBody",Response=ECR_Ignore),(Channel="Vehicle",Response=ECR_Ignore),(Channel="Destructible",Response=ECR_Ignore)),HelpMessage="Pawn object that ignores all other actors except WorldStatic.",bCanModify=False)
|
||||||
|
-Profiles=(Name="CharacterMesh",CollisionEnabled=QueryOnly,ObjectTypeName="Pawn",CustomResponses=((Channel="Pawn",Response=ECR_Ignore),(Channel="Vehicle",Response=ECR_Ignore),(Channel="Visibility",Response=ECR_Ignore)),HelpMessage="Pawn object that is used for Character Mesh. All other channels will be set to default.",bCanModify=False)
|
||||||
|
-Profiles=(Name="PhysicsActor",CollisionEnabled=QueryAndPhysics,ObjectTypeName="PhysicsBody",CustomResponses=,HelpMessage="Simulating actors",bCanModify=False)
|
||||||
|
-Profiles=(Name="Destructible",CollisionEnabled=QueryAndPhysics,ObjectTypeName="Destructible",CustomResponses=,HelpMessage="Destructible actors",bCanModify=False)
|
||||||
|
-Profiles=(Name="InvisibleWall",CollisionEnabled=QueryAndPhysics,ObjectTypeName="WorldStatic",CustomResponses=((Channel="Visibility",Response=ECR_Ignore)),HelpMessage="WorldStatic object that is invisible.",bCanModify=False)
|
||||||
|
-Profiles=(Name="InvisibleWallDynamic",CollisionEnabled=QueryAndPhysics,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Visibility",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that is invisible.",bCanModify=False)
|
||||||
|
-Profiles=(Name="Trigger",CollisionEnabled=QueryOnly,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility",Response=ECR_Ignore),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldDynamic object that is used for trigger. All other channels will be set to default.",bCanModify=False)
|
||||||
|
-Profiles=(Name="Ragdoll",CollisionEnabled=QueryAndPhysics,ObjectTypeName="PhysicsBody",CustomResponses=((Channel="Pawn",Response=ECR_Ignore),(Channel="Visibility",Response=ECR_Ignore)),HelpMessage="Simulating Skeletal Mesh Component. All other channels will be set to default.",bCanModify=False)
|
||||||
|
-Profiles=(Name="Vehicle",CollisionEnabled=QueryAndPhysics,ObjectTypeName="Vehicle",CustomResponses=,HelpMessage="Vehicle object that blocks Vehicle, WorldStatic, and WorldDynamic. All other channels will be set to default.",bCanModify=False)
|
||||||
|
-Profiles=(Name="UI",CollisionEnabled=QueryOnly,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility",Response=ECR_Block),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldStatic object that overlaps all actors by default. All new custom channels will use its own default response. ",bCanModify=False)
|
||||||
|
+Profiles=(Name="NoCollision",CollisionEnabled=NoCollision,bCanModify=False,ObjectTypeName="WorldStatic",CustomResponses=((Channel="Visibility",Response=ECR_Ignore),(Channel="Camera",Response=ECR_Ignore)),HelpMessage="No collision")
|
||||||
|
+Profiles=(Name="BlockAll",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="WorldStatic",CustomResponses=,HelpMessage="WorldStatic object that blocks all actors by default. All new custom channels will use its own default response. ")
|
||||||
|
+Profiles=(Name="OverlapAll",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="WorldStatic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility",Response=ECR_Overlap),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldStatic object that overlaps all actors by default. All new custom channels will use its own default response. ")
|
||||||
|
+Profiles=(Name="BlockAllDynamic",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="WorldDynamic",CustomResponses=,HelpMessage="WorldDynamic object that blocks all actors by default. All new custom channels will use its own default response. ")
|
||||||
|
+Profiles=(Name="OverlapAllDynamic",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility",Response=ECR_Overlap),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldDynamic object that overlaps all actors by default. All new custom channels will use its own default response. ")
|
||||||
|
+Profiles=(Name="IgnoreOnlyPawn",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Pawn",Response=ECR_Ignore),(Channel="Vehicle",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that ignores Pawn and Vehicle. All other channels will be set to default.")
|
||||||
|
+Profiles=(Name="OverlapOnlyPawn",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Pawn",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that overlaps Pawn, Camera, and Vehicle. All other channels will be set to default. ")
|
||||||
|
+Profiles=(Name="Pawn",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="Pawn",CustomResponses=((Channel="Visibility",Response=ECR_Ignore)),HelpMessage="Pawn object. Can be used for capsule of any playerable character or AI. ")
|
||||||
|
+Profiles=(Name="Spectator",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="Pawn",CustomResponses=((Channel="WorldStatic"),(Channel="Pawn",Response=ECR_Ignore),(Channel="Visibility",Response=ECR_Ignore),(Channel="WorldDynamic",Response=ECR_Ignore),(Channel="Camera",Response=ECR_Ignore),(Channel="PhysicsBody",Response=ECR_Ignore),(Channel="Vehicle",Response=ECR_Ignore),(Channel="Destructible",Response=ECR_Ignore)),HelpMessage="Pawn object that ignores all other actors except WorldStatic.")
|
||||||
|
+Profiles=(Name="CharacterMesh",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="Pawn",CustomResponses=((Channel="Pawn",Response=ECR_Ignore),(Channel="Vehicle",Response=ECR_Ignore),(Channel="Visibility",Response=ECR_Ignore)),HelpMessage="Pawn object that is used for Character Mesh. All other channels will be set to default.")
|
||||||
|
+Profiles=(Name="PhysicsActor",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="PhysicsBody",CustomResponses=,HelpMessage="Simulating actors")
|
||||||
|
+Profiles=(Name="Destructible",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="Destructible",CustomResponses=,HelpMessage="Destructible actors")
|
||||||
|
+Profiles=(Name="InvisibleWall",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="WorldStatic",CustomResponses=((Channel="Visibility",Response=ECR_Ignore)),HelpMessage="WorldStatic object that is invisible.")
|
||||||
|
+Profiles=(Name="InvisibleWallDynamic",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Visibility",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that is invisible.")
|
||||||
|
+Profiles=(Name="Trigger",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility",Response=ECR_Ignore),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldDynamic object that is used for trigger. All other channels will be set to default.")
|
||||||
|
+Profiles=(Name="Ragdoll",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="PhysicsBody",CustomResponses=((Channel="Pawn",Response=ECR_Ignore),(Channel="Visibility",Response=ECR_Ignore)),HelpMessage="Simulating Skeletal Mesh Component. All other channels will be set to default.")
|
||||||
|
+Profiles=(Name="Vehicle",CollisionEnabled=QueryAndPhysics,bCanModify=False,ObjectTypeName="Vehicle",CustomResponses=,HelpMessage="Vehicle object that blocks Vehicle, WorldStatic, and WorldDynamic. All other channels will be set to default.")
|
||||||
|
+Profiles=(Name="UI",CollisionEnabled=QueryOnly,bCanModify=False,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="WorldStatic",Response=ECR_Overlap),(Channel="Pawn",Response=ECR_Overlap),(Channel="Visibility"),(Channel="WorldDynamic",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Overlap),(Channel="PhysicsBody",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Destructible",Response=ECR_Overlap)),HelpMessage="WorldStatic object that overlaps all actors by default. All new custom channels will use its own default response. ")
|
||||||
|
+Profiles=(Name="Projectile",CollisionEnabled=QueryOnly,bCanModify=True,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Pawn",Response=ECR_Overlap),(Channel="Player",Response=ECR_Ignore)),HelpMessage="Needs description")
|
||||||
|
+DefaultChannelResponses=(Channel=ECC_GameTraceChannel1,DefaultResponse=ECR_Block,bTraceType=False,bStaticObject=False,Name="Player")
|
||||||
|
-ProfileRedirects=(OldName="BlockingVolume",NewName="InvisibleWall")
|
||||||
|
-ProfileRedirects=(OldName="InterpActor",NewName="IgnoreOnlyPawn")
|
||||||
|
-ProfileRedirects=(OldName="StaticMeshComponent",NewName="BlockAllDynamic")
|
||||||
|
-ProfileRedirects=(OldName="SkeletalMeshActor",NewName="PhysicsActor")
|
||||||
|
-ProfileRedirects=(OldName="InvisibleActor",NewName="InvisibleWallDynamic")
|
||||||
|
+ProfileRedirects=(OldName="BlockingVolume",NewName="InvisibleWall")
|
||||||
|
+ProfileRedirects=(OldName="InterpActor",NewName="IgnoreOnlyPawn")
|
||||||
|
+ProfileRedirects=(OldName="StaticMeshComponent",NewName="BlockAllDynamic")
|
||||||
|
+ProfileRedirects=(OldName="SkeletalMeshActor",NewName="PhysicsActor")
|
||||||
|
+ProfileRedirects=(OldName="InvisibleActor",NewName="InvisibleWallDynamic")
|
||||||
|
-CollisionChannelRedirects=(OldName="Static",NewName="WorldStatic")
|
||||||
|
-CollisionChannelRedirects=(OldName="Dynamic",NewName="WorldDynamic")
|
||||||
|
-CollisionChannelRedirects=(OldName="VehicleMovement",NewName="Vehicle")
|
||||||
|
-CollisionChannelRedirects=(OldName="PawnMovement",NewName="Pawn")
|
||||||
|
+CollisionChannelRedirects=(OldName="Static",NewName="WorldStatic")
|
||||||
|
+CollisionChannelRedirects=(OldName="Dynamic",NewName="WorldDynamic")
|
||||||
|
+CollisionChannelRedirects=(OldName="VehicleMovement",NewName="Vehicle")
|
||||||
|
+CollisionChannelRedirects=(OldName="PawnMovement",NewName="Pawn")
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Content/Input/IA_Shoot.uasset
LFS
Normal file
BIN
Content/Input/IA_Shoot.uasset
LFS
Normal file
Binary file not shown.
Binary file not shown.
@@ -1,24 +1,33 @@
|
|||||||
#include "EnemySpaceship.h"
|
#include "EnemySpaceship.h"
|
||||||
#include "SpaceshipPawn.h"
|
|
||||||
#include "SpaceshipProjectile.h"
|
|
||||||
#include "Kismet/GameplayStatics.h"
|
#include "Kismet/GameplayStatics.h"
|
||||||
|
|
||||||
AEnemySpaceship::AEnemySpaceship()
|
AEnemySpaceship::AEnemySpaceship()
|
||||||
{
|
{
|
||||||
PrimaryActorTick.bCanEverTick = true;
|
PrimaryActorTick.bCanEverTick = true;
|
||||||
|
|
||||||
ShipMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipMesh"));
|
// Create and setup the enemy mesh
|
||||||
RootComponent = ShipMesh;
|
EnemyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("EnemyMesh"));
|
||||||
|
RootComponent = EnemyMesh;
|
||||||
|
|
||||||
CurrentHealth = MaxHealth;
|
// Disable gravity and physics simulation
|
||||||
|
EnemyMesh->SetSimulatePhysics(false);
|
||||||
|
EnemyMesh->SetEnableGravity(false);
|
||||||
|
|
||||||
|
// Set up collision for the enemy mesh
|
||||||
|
EnemyMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
||||||
|
EnemyMesh->SetCollisionObjectType(ECC_Pawn);
|
||||||
|
EnemyMesh->SetCollisionResponseToAllChannels(ECR_Block);
|
||||||
|
EnemyMesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore); // Ignore other pawns
|
||||||
|
EnemyMesh->SetCollisionResponseToChannel(ECC_GameTraceChannel1, ECR_Ignore); // Ignore player
|
||||||
|
EnemyMesh->SetGenerateOverlapEvents(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AEnemySpaceship::BeginPlay()
|
void AEnemySpaceship::BeginPlay()
|
||||||
{
|
{
|
||||||
Super::BeginPlay();
|
Super::BeginPlay();
|
||||||
|
|
||||||
PlayerPawn = Cast<ASpaceshipPawn>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
|
// Find the player pawn
|
||||||
GetWorldTimerManager().SetTimer(FireTimerHandle, this, &AEnemySpaceship::FireAtPlayer, FireRate, true);
|
PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AEnemySpaceship::Tick(float DeltaTime)
|
void AEnemySpaceship::Tick(float DeltaTime)
|
||||||
@@ -27,66 +36,43 @@ void AEnemySpaceship::Tick(float DeltaTime)
|
|||||||
|
|
||||||
if (PlayerPawn)
|
if (PlayerPawn)
|
||||||
{
|
{
|
||||||
// Calculate distance to player
|
// Move towards the player
|
||||||
float DistanceToPlayer = FVector::Dist(GetActorLocation(), PlayerPawn->GetActorLocation());
|
FVector Direction = (PlayerPawn->GetActorLocation() - GetActorLocation()).GetSafeNormal();
|
||||||
|
FVector NewLocation = GetActorLocation() + Direction * MovementSpeed * DeltaTime;
|
||||||
|
SetActorLocation(NewLocation);
|
||||||
|
|
||||||
if (DistanceToPlayer <= DetectionRange)
|
// Face towards the player
|
||||||
{
|
FRotator NewRotation = Direction.Rotation();
|
||||||
// Look at player
|
SetActorRotation(NewRotation);
|
||||||
FVector Direction = PlayerPawn->GetActorLocation() - GetActorLocation();
|
|
||||||
FRotator NewRotation = Direction.Rotation();
|
|
||||||
SetActorRotation(FMath::RInterpTo(GetActorRotation(), NewRotation, DeltaTime, 2.0f));
|
|
||||||
|
|
||||||
// Move towards player while maintaining some distance
|
|
||||||
float TargetDistance = 1000.0f;
|
|
||||||
if (DistanceToPlayer > TargetDistance)
|
|
||||||
{
|
|
||||||
FVector NewLocation = GetActorLocation() + (Direction.GetSafeNormal() * MovementSpeed * DeltaTime);
|
|
||||||
SetActorLocation(NewLocation);
|
|
||||||
}
|
|
||||||
else if (DistanceToPlayer < TargetDistance - 100.0f)
|
|
||||||
{
|
|
||||||
FVector NewLocation = GetActorLocation() - (Direction.GetSafeNormal() * MovementSpeed * DeltaTime);
|
|
||||||
SetActorLocation(NewLocation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AEnemySpaceship::FireAtPlayer()
|
float AEnemySpaceship::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent,
|
||||||
{
|
|
||||||
if (PlayerPawn && FVector::Dist(GetActorLocation(), PlayerPawn->GetActorLocation()) <= DetectionRange)
|
|
||||||
{
|
|
||||||
UWorld* World = GetWorld();
|
|
||||||
if (World)
|
|
||||||
{
|
|
||||||
FVector SpawnLocation = GetActorLocation() + (GetActorForwardVector() * 100.0f);
|
|
||||||
FRotator SpawnRotation = GetActorRotation();
|
|
||||||
FActorSpawnParameters SpawnParams;
|
|
||||||
SpawnParams.Owner = this;
|
|
||||||
SpawnParams.Instigator = GetInstigator();
|
|
||||||
|
|
||||||
ASpaceshipProjectile* Projectile = World->SpawnActor<ASpaceshipProjectile>(
|
|
||||||
ASpaceshipProjectile::StaticClass(),
|
|
||||||
SpawnLocation,
|
|
||||||
SpawnRotation,
|
|
||||||
SpawnParams
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float AEnemySpaceship::TakeDamage(float Damage, FDamageEvent const& DamageEvent,
|
|
||||||
AController* EventInstigator, AActor* DamageCauser)
|
AController* EventInstigator, AActor* DamageCauser)
|
||||||
{
|
{
|
||||||
float ActualDamage = Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);
|
float DamageToApply = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
|
||||||
|
|
||||||
CurrentHealth -= ActualDamage;
|
CurrentHealth -= DamageToApply;
|
||||||
if (CurrentHealth <= 0)
|
|
||||||
|
// Debug message
|
||||||
|
if (GEngine)
|
||||||
{
|
{
|
||||||
// Destroy the enemy ship
|
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red,
|
||||||
Destroy();
|
FString::Printf(TEXT("Enemy Health: %f"), CurrentHealth));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ActualDamage;
|
if (CurrentHealth <= 0)
|
||||||
|
{
|
||||||
|
Die();
|
||||||
|
}
|
||||||
|
|
||||||
|
return DamageToApply;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AEnemySpaceship::Die()
|
||||||
|
{
|
||||||
|
// Add any death effects here
|
||||||
|
|
||||||
|
// Destroy the enemy
|
||||||
|
Destroy();
|
||||||
}
|
}
|
||||||
@@ -1,41 +1,40 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "CoreMinimal.h"
|
#include "CoreMinimal.h"
|
||||||
#include "GameFramework/Pawn.h"
|
#include "GameFramework/Actor.h"
|
||||||
#include "EnemySpaceship.generated.h"
|
#include "EnemySpaceship.generated.h"
|
||||||
|
|
||||||
UCLASS()
|
UCLASS()
|
||||||
class MYPROJECT3_API AEnemySpaceship : public APawn
|
class MYPROJECT3_API AEnemySpaceship : public AActor
|
||||||
{
|
{
|
||||||
GENERATED_BODY()
|
GENERATED_BODY()
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AEnemySpaceship();
|
AEnemySpaceship();
|
||||||
virtual void Tick(float DeltaTime) override;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void BeginPlay() override;
|
virtual void BeginPlay() override;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
|
||||||
class UStaticMeshComponent* ShipMesh;
|
UStaticMeshComponent* EnemyMesh;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
|
||||||
float DetectionRange = 1500.0f;
|
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
|
|
||||||
float FireRate = 2.0f;
|
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
float MovementSpeed = 500.0f;
|
float MovementSpeed = 300.0f;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
||||||
float MaxHealth = 100.0f;
|
float MaxHealth = 100.0f;
|
||||||
|
|
||||||
float CurrentHealth;
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
||||||
FTimerHandle FireTimerHandle;
|
float CurrentHealth = 100.0f;
|
||||||
class ASpaceshipPawn* PlayerPawn;
|
|
||||||
|
|
||||||
void FireAtPlayer();
|
public:
|
||||||
virtual float TakeDamage(float Damage, struct FDamageEvent const& DamageEvent,
|
virtual void Tick(float DeltaTime) override;
|
||||||
AController* EventInstigator, AActor* DamageCauser) override;
|
|
||||||
|
// Override TakeDamage to handle damage taken
|
||||||
|
virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent,
|
||||||
|
class AController* EventInstigator, AActor* DamageCauser) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
AActor* PlayerPawn;
|
||||||
|
void Die();
|
||||||
};
|
};
|
||||||
@@ -12,6 +12,13 @@ ASpaceShooterGameMode::ASpaceShooterGameMode()
|
|||||||
DefaultPawnClass = PlayerPawnBPClass.Class;
|
DefaultPawnClass = PlayerPawnBPClass.Class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find enemy blueprint class
|
||||||
|
static ConstructorHelpers::FClassFinder<AEnemySpaceship> EnemyBPClass(TEXT("/Game/Blueprints/BP_EnemySpaceship"));
|
||||||
|
if (EnemyBPClass.Class != nullptr)
|
||||||
|
{
|
||||||
|
EnemyClass = EnemyBPClass.Class;
|
||||||
|
}
|
||||||
|
|
||||||
// Enable Tick()
|
// Enable Tick()
|
||||||
PrimaryActorTick.bCanEverTick = true;
|
PrimaryActorTick.bCanEverTick = true;
|
||||||
|
|
||||||
@@ -52,14 +59,15 @@ void ASpaceShooterGameMode::SpawnEnemy()
|
|||||||
if (FoundEnemies.Num() < MaxEnemies)
|
if (FoundEnemies.Num() < MaxEnemies)
|
||||||
{
|
{
|
||||||
UWorld* World = GetWorld();
|
UWorld* World = GetWorld();
|
||||||
if (World)
|
if (World && EnemyClass)
|
||||||
{
|
{
|
||||||
FVector SpawnLocation = GetRandomSpawnLocation();
|
FVector SpawnLocation = GetRandomSpawnLocation();
|
||||||
FRotator SpawnRotation = FRotator::ZeroRotator;
|
FRotator SpawnRotation = FRotator::ZeroRotator;
|
||||||
FActorSpawnParameters SpawnParams;
|
FActorSpawnParameters SpawnParams;
|
||||||
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
||||||
|
|
||||||
World->SpawnActor<AEnemySpaceship>(AEnemySpaceship::StaticClass(), SpawnLocation,
|
// Spawn using the Blueprint class instead of the C++ class directly
|
||||||
|
AEnemySpaceship* NewEnemy = World->SpawnActor<AEnemySpaceship>(EnemyClass, SpawnLocation,
|
||||||
SpawnRotation, SpawnParams);
|
SpawnRotation, SpawnParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,10 +83,13 @@ FVector ASpaceShooterGameMode::GetRandomSpawnLocation()
|
|||||||
// Generate random angle
|
// Generate random angle
|
||||||
float Angle = FMath::RandRange(0.0f, 2.0f * PI);
|
float Angle = FMath::RandRange(0.0f, 2.0f * PI);
|
||||||
|
|
||||||
|
// Generate random radius between min and max
|
||||||
|
float Radius = FMath::RandRange(MinSpawnRadius, MaxSpawnRadius);
|
||||||
|
|
||||||
// Calculate spawn position in a circle around the player
|
// Calculate spawn position in a circle around the player
|
||||||
FVector SpawnLocation;
|
FVector SpawnLocation;
|
||||||
SpawnLocation.X = PlayerLocation.X + SpawnRadius * FMath::Cos(Angle);
|
SpawnLocation.X = PlayerLocation.X + Radius * FMath::Cos(Angle);
|
||||||
SpawnLocation.Y = PlayerLocation.Y + SpawnRadius * FMath::Sin(Angle);
|
SpawnLocation.Y = PlayerLocation.Y + Radius * FMath::Sin(Angle);
|
||||||
SpawnLocation.Z = PlayerLocation.Z;
|
SpawnLocation.Z = PlayerLocation.Z;
|
||||||
|
|
||||||
return SpawnLocation;
|
return SpawnLocation;
|
||||||
|
|||||||
@@ -17,13 +17,19 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
float EnemySpawnInterval = 3.0f;
|
TSubclassOf<class AEnemySpaceship> EnemyClass;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
float SpawnRadius = 2000.0f;
|
float EnemySpawnInterval = 2.0f;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
int32 MaxEnemies = 5;
|
int32 MaxEnemies = 10;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
|
float MinSpawnRadius = 1000.0f;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
|
float MaxSpawnRadius = 2000.0f;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FTimerHandle EnemySpawnTimer;
|
FTimerHandle EnemySpawnTimer;
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ ASpaceshipPawn::ASpaceshipPawn()
|
|||||||
ShipMesh->SetSimulatePhysics(false);
|
ShipMesh->SetSimulatePhysics(false);
|
||||||
ShipMesh->SetEnableGravity(false);
|
ShipMesh->SetEnableGravity(false);
|
||||||
|
|
||||||
|
// Create projectile spawn point
|
||||||
|
ProjectileSpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("ProjectileSpawnPoint"));
|
||||||
|
ProjectileSpawnPoint->SetupAttachment(ShipMesh);
|
||||||
|
|
||||||
// Create camera spring arm
|
// Create camera spring arm
|
||||||
CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
|
CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
|
||||||
CameraSpringArm->SetupAttachment(RootComponent);
|
CameraSpringArm->SetupAttachment(RootComponent);
|
||||||
@@ -46,12 +50,40 @@ ASpaceshipPawn::ASpaceshipPawn()
|
|||||||
CurrentYaw = 0.0f;
|
CurrentYaw = 0.0f;
|
||||||
TargetRotation = FRotator::ZeroRotator;
|
TargetRotation = FRotator::ZeroRotator;
|
||||||
LastMouseDelta = FVector2D::ZeroVector;
|
LastMouseDelta = FVector2D::ZeroVector;
|
||||||
|
|
||||||
|
if (ShipMesh)
|
||||||
|
{
|
||||||
|
ShipMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
||||||
|
ShipMesh->SetCollisionObjectType(ECC_GameTraceChannel1); // This is the "Player" channel
|
||||||
|
ShipMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
|
||||||
|
ShipMesh->SetCollisionResponseToChannel(ECC_GameTraceChannel1, ECollisionResponse::ECR_Ignore);
|
||||||
|
ShipMesh->SetCollisionResponseToChannel(ECC_Pawn, ECollisionResponse::ECR_Ignore);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ASpaceshipPawn::BeginPlay()
|
void ASpaceshipPawn::BeginPlay()
|
||||||
{
|
{
|
||||||
Super::BeginPlay();
|
Super::BeginPlay();
|
||||||
|
|
||||||
|
// Debug messages for setup verification
|
||||||
|
if (GEngine)
|
||||||
|
{
|
||||||
|
if (ProjectileClass)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("ProjectileClass is set"));
|
||||||
|
else
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ProjectileClass is NOT set"));
|
||||||
|
|
||||||
|
if (ShootAction)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("ShootAction is set"));
|
||||||
|
else
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ShootAction is NOT set"));
|
||||||
|
|
||||||
|
if (ProjectileSpawnPoint)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("ProjectileSpawnPoint is set"));
|
||||||
|
else
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ProjectileSpawnPoint is NOT set"));
|
||||||
|
}
|
||||||
|
|
||||||
// Store player controller reference
|
// Store player controller reference
|
||||||
PlayerControllerRef = Cast<APlayerController>(Controller);
|
PlayerControllerRef = Cast<APlayerController>(Controller);
|
||||||
|
|
||||||
@@ -217,8 +249,8 @@ void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo
|
|||||||
// Bind mouse control
|
// Bind mouse control
|
||||||
EnhancedInputComponent->BindAction(MouseLookAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleMouseLook);
|
EnhancedInputComponent->BindAction(MouseLookAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleMouseLook);
|
||||||
|
|
||||||
// Bind fire action
|
// Add shooting binding
|
||||||
EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleFire);
|
EnhancedInputComponent->BindAction(ShootAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleShoot);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,27 +275,64 @@ void ASpaceshipPawn::HandleMouseLook(const FInputActionValue& Value)
|
|||||||
LastMouseDelta = MouseDelta;
|
LastMouseDelta = MouseDelta;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ASpaceshipPawn::HandleFire(const FInputActionValue& Value)
|
void ASpaceshipPawn::HandleShoot(const FInputActionValue& Value)
|
||||||
{
|
{
|
||||||
|
// Debug message
|
||||||
|
if (GEngine)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Yellow, TEXT("HandleShoot Called"));
|
||||||
|
|
||||||
|
if (bCanFire)
|
||||||
|
{
|
||||||
|
Fire();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASpaceshipPawn::Fire()
|
||||||
|
{
|
||||||
|
// Debug messages
|
||||||
|
if (!ProjectileClass)
|
||||||
|
{
|
||||||
|
if (GEngine)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("ProjectileClass not set!"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
UWorld* World = GetWorld();
|
UWorld* World = GetWorld();
|
||||||
if (World)
|
if (World)
|
||||||
{
|
{
|
||||||
FVector SpawnLocation = ShipMesh->GetSocketLocation(TEXT("ProjectileSpawn"));
|
FVector SpawnLocation = ProjectileSpawnPoint->GetComponentLocation();
|
||||||
if (SpawnLocation == FVector::ZeroVector)
|
|
||||||
{
|
|
||||||
SpawnLocation = GetActorLocation() + (GetActorForwardVector() * 100.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
FRotator SpawnRotation = GetActorRotation();
|
FRotator SpawnRotation = GetActorRotation();
|
||||||
|
|
||||||
FActorSpawnParameters SpawnParams;
|
FActorSpawnParameters SpawnParams;
|
||||||
SpawnParams.Owner = this;
|
SpawnParams.Owner = this;
|
||||||
SpawnParams.Instigator = GetInstigator();
|
SpawnParams.Instigator = GetInstigator();
|
||||||
|
|
||||||
World->SpawnActor<ASpaceshipProjectile>(
|
// Spawn the projectile
|
||||||
ASpaceshipProjectile::StaticClass(),
|
ASpaceshipProjectile* Projectile = World->SpawnActor<ASpaceshipProjectile>(
|
||||||
|
ProjectileClass,
|
||||||
SpawnLocation,
|
SpawnLocation,
|
||||||
SpawnRotation,
|
SpawnRotation,
|
||||||
SpawnParams
|
SpawnParams
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (Projectile)
|
||||||
|
{
|
||||||
|
if (GEngine)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, TEXT("Projectile Spawned"));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (GEngine)
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Failed to spawn projectile"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start fire rate timer
|
||||||
|
bCanFire = false;
|
||||||
|
GetWorldTimerManager().SetTimer(FireTimerHandle, this, &ASpaceshipPawn::ResetFire, FireRate, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ASpaceshipPawn::ResetFire()
|
||||||
|
{
|
||||||
|
bCanFire = true;
|
||||||
|
}
|
||||||
@@ -38,9 +38,6 @@ protected:
|
|||||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
|
||||||
class UInputAction* MouseLookAction;
|
class UInputAction* MouseLookAction;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
|
|
||||||
class UInputAction* FireAction;
|
|
||||||
|
|
||||||
// Movement Parameters
|
// Movement Parameters
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||||
float MaxThrust = 2000.0f;
|
float MaxThrust = 2000.0f;
|
||||||
@@ -67,11 +64,24 @@ protected:
|
|||||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta = (ClampMin = "0.0", ClampMax = "1.0", ToolTip = "How much of the current velocity is maintained when changing direction (0 = none, 1 = full)"))
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta = (ClampMin = "0.0", ClampMax = "1.0", ToolTip = "How much of the current velocity is maintained when changing direction (0 = none, 1 = full)"))
|
||||||
float DirectionalInertia = 0.3f;
|
float DirectionalInertia = 0.3f;
|
||||||
|
|
||||||
|
// Shooting properties
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
||||||
|
TSubclassOf<class ASpaceshipProjectile> ProjectileClass;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
||||||
|
float FireRate = 0.2f;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat")
|
||||||
|
USceneComponent* ProjectileSpawnPoint;
|
||||||
|
|
||||||
|
// Input action for shooting
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
|
||||||
|
class UInputAction* ShootAction;
|
||||||
|
|
||||||
// Input functions
|
// Input functions
|
||||||
void HandleThrottleStarted(const FInputActionValue& Value);
|
void HandleThrottleStarted(const FInputActionValue& Value);
|
||||||
void HandleThrottleReleased(const FInputActionValue& Value);
|
void HandleThrottleReleased(const FInputActionValue& Value);
|
||||||
void HandleMouseLook(const FInputActionValue& Value);
|
void HandleMouseLook(const FInputActionValue& Value);
|
||||||
void HandleFire(const FInputActionValue& Value);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Movement state
|
// Movement state
|
||||||
@@ -91,4 +101,11 @@ private:
|
|||||||
|
|
||||||
bool bWasThrottlePressed;
|
bool bWasThrottlePressed;
|
||||||
FVector DesiredVelocityDirection;
|
FVector DesiredVelocityDirection;
|
||||||
|
|
||||||
|
FTimerHandle FireTimerHandle;
|
||||||
|
bool bCanFire = true;
|
||||||
|
|
||||||
|
void HandleShoot(const FInputActionValue& Value);
|
||||||
|
void Fire();
|
||||||
|
void ResetFire();
|
||||||
};
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "SpaceshipProjectile.h"
|
#include "SpaceshipProjectile.h"
|
||||||
#include "GameFramework/ProjectileMovementComponent.h"
|
#include "GameFramework/ProjectileMovementComponent.h"
|
||||||
#include "Components/StaticMeshComponent.h"
|
#include "Components/StaticMeshComponent.h"
|
||||||
|
#include "EnemySpaceship.h"
|
||||||
|
|
||||||
ASpaceshipProjectile::ASpaceshipProjectile()
|
ASpaceshipProjectile::ASpaceshipProjectile()
|
||||||
{
|
{
|
||||||
@@ -12,6 +13,17 @@ ASpaceshipProjectile::ASpaceshipProjectile()
|
|||||||
ProjectileMesh->SetCollisionProfileName(TEXT("Projectile"));
|
ProjectileMesh->SetCollisionProfileName(TEXT("Projectile"));
|
||||||
ProjectileMesh->OnComponentHit.AddDynamic(this, &ASpaceshipProjectile::OnHit);
|
ProjectileMesh->OnComponentHit.AddDynamic(this, &ASpaceshipProjectile::OnHit);
|
||||||
|
|
||||||
|
// Set up collision
|
||||||
|
ProjectileMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
||||||
|
ProjectileMesh->SetCollisionObjectType(ECC_WorldDynamic);
|
||||||
|
ProjectileMesh->SetCollisionResponseToAllChannels(ECR_Ignore);
|
||||||
|
ProjectileMesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Block); // Block pawns (enemies)
|
||||||
|
ProjectileMesh->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Block); // Block world static
|
||||||
|
ProjectileMesh->SetGenerateOverlapEvents(true);
|
||||||
|
|
||||||
|
// Bind hit event
|
||||||
|
ProjectileMesh->OnComponentHit.AddDynamic(this, &ASpaceshipProjectile::OnHit);
|
||||||
|
|
||||||
// Create and setup projectile movement
|
// Create and setup projectile movement
|
||||||
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
|
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
|
||||||
ProjectileMovement->UpdatedComponent = ProjectileMesh;
|
ProjectileMovement->UpdatedComponent = ProjectileMesh;
|
||||||
@@ -32,12 +44,32 @@ void ASpaceshipProjectile::BeginPlay()
|
|||||||
void ASpaceshipProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
|
void ASpaceshipProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
|
||||||
FVector NormalImpulse, const FHitResult& Hit)
|
FVector NormalImpulse, const FHitResult& Hit)
|
||||||
{
|
{
|
||||||
if (OtherActor && OtherActor != this)
|
// Debug message to verify hit detection
|
||||||
|
if (GEngine)
|
||||||
{
|
{
|
||||||
// Apply damage to the hit actor (if it implements damage interface)
|
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow,
|
||||||
FDamageEvent DamageEvent;
|
FString::Printf(TEXT("Projectile hit: %s"), *OtherActor->GetName()));
|
||||||
OtherActor->TakeDamage(DamageAmount, DamageEvent, nullptr, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (OtherActor && OtherActor != this)
|
||||||
|
{
|
||||||
|
// Check if we hit an enemy
|
||||||
|
AEnemySpaceship* Enemy = Cast<AEnemySpaceship>(OtherActor);
|
||||||
|
if (Enemy)
|
||||||
|
{
|
||||||
|
// Apply damage
|
||||||
|
FDamageEvent DamageEvent;
|
||||||
|
Enemy->TakeDamage(DamageAmount, DamageEvent, nullptr, this);
|
||||||
|
|
||||||
|
// Debug message for damage
|
||||||
|
if (GEngine)
|
||||||
|
{
|
||||||
|
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red,
|
||||||
|
FString::Printf(TEXT("Dealt %f damage to enemy"), DamageAmount));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy the projectile
|
||||||
Destroy();
|
Destroy();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user