<commit_before>// Copyright (c) 2017 Bytecode Bits

#include "ZynapsReloaded.h"
#include "PlayerPawn.h"
#include "ProjectionUtil.h"
#include "ZynapsWorldSettings.h"
#include "FuelCapsule.h"

// Log category
DEFINE_LOG_CATEGORY(LogPlayerPawn);

// Sets default values
APlayerPawn::APlayerPawn() : Super()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// Set up root component
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

	// Set up the collision capsule
	CapsuleComponent = CreateCapsuleComponent(RootComponent);

	// Set up the mesh component
	MeshComponent = CreateMeshComponent(CapsuleComponent);

	// Set up the engine thrust particle system
	EngineThrustSocketName = TEXT("EngineThrust");
	EnginePartSystemComponent = CreateEngineThrustParticleSystem(MeshComponent, EngineThrustSocketName);

	// Set up the movement component
	MovementComponent = CreateMovementComponent();

	// Set up the explosion particle system
	ExplosionPartSystem = CreateExplosionParticleSystem();

	// Sets this pawn to be controlled by the lowest-numbered player
	AutoPossessPlayer = EAutoReceiveInput::Player0;

	// Init shooting vars
	ProjectileClass = APlayerProjectile::StaticClass();
	LeftCannonSocketName = FName("LeftCannon");
	RightCannonSocketName = FName("RightCannon");
	TopCannonSocketName = FName("TopCannon");
	NextCannon = RightCannon;
}

// Creates the capsule component used for collision detection
UCapsuleComponent* APlayerPawn::CreateCapsuleComponent(USceneComponent* Parent)
{
	UCapsuleComponent* Component = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleComponent"));
	Component->InitCapsuleSize(18.0f, 32.0f);
	Component->SetWorldRotation(FRotator(0.0f, 180.0f, -90.0f));
	Component->SetWorldScale3D(FVector(12.0f, 10.0f, 10.0f));
	Component->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	Component->bGenerateOverlapEvents = true;
	Component->SetNotifyRigidBodyCollision(true);
	Component->SetCollisionObjectType(ECollisionChannel::ECC_Pawn);
	Component->SetCanEverAffectNavigation(false);
	Component->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
	Component->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
	Component->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldDynamic, ECollisionResponse::ECR_Block);
	Component->SetCollisionResponseToChannel(ECollisionChannel::ECC_PhysicsBody, ECollisionResponse::ECR_Overlap);
	Component->SetSimulatePhysics(true);
	Component->SetEnableGravity(false);
	Component->SetAngularDamping(0.0f);
	Component->SetLinearDamping(0.0f);
	Component->BodyInstance.bLockXRotation = true;
	Component->BodyInstance.bLockZRotation = true;
	Component->BodyInstance.bLockXTranslation = true;
	//Component->SetHiddenInGame(false);
	Component->WakeRigidBody();
	Component->OnComponentBeginOverlap.AddDynamic(this, &APlayerPawn::BeginOverlap);
	Component->OnComponentEndOverlap.AddDynamic(this, &APlayerPawn::EndOverlap);
	Component->OnComponentHit.AddDynamic(this, &APlayerPawn::Hit);
	Component->SetupAttachment(Parent);

	return Component;
}

// Creates the mesh component which models the ship
UStaticMeshComponent* APlayerPawn::CreateMeshComponent(USceneComponent* Parent)
{
	UStaticMeshComponent* Component = CreateDefaultSubobject<UStaticMeshComponent>(
		TEXT("MeshComponent"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(
		TEXT("StaticMesh'/Game/Models/Ship/zynaps_ship.zynaps_ship'"));
	if (MeshObj.Succeeded())
	{
		Component->SetStaticMesh(MeshObj.Object);
		Component->SetRelativeLocation(FVector(0.0f, -3.5f, -7.0f));
		Component->SetRelativeRotation(FRotator(0.0f, 0.0f, 90.0f));
		Component->SetRelativeScale3D(FVector(1.0f / 12.0f, 0.1f, 0.1f));
		Component->SetCollisionEnabled(ECollisionEnabled::NoCollision);
		Component->SetSimulatePhysics(false);
		Component->SetupAttachment(Parent);
	}
	else
	{
		UE_LOG(LogPlayerPawn, Error, 
			TEXT("The asset StaticMesh'/Game/Models/Ship/zynaps_ship.zynaps_ship' was not found"));
		Component = nullptr;
	}

	return Component;
}

// Creates the particle system for the engine thrust
UParticleSystemComponent* APlayerPawn::CreateEngineThrustParticleSystem(USceneComponent* Parent, FName SocketName)
{
	UParticleSystemComponent* Component = 
		CreateDefaultSubobject<UParticleSystemComponent>(TEXT("EnginePartSystemComponent"));
	static ConstructorHelpers::FObjectFinder<UParticleSystem> PartSystem(
		TEXT("ParticleSystem'/Game/Models/ThrustParticle/EngineThrustSystem.EngineThrustSystem'"));
	if (PartSystem.Succeeded())
	{
		Component->SetTemplate(PartSystem.Object);
		Component->SetRelativeScale3D(FVector(1.5f, 0.5f, 1.5f));
		Component->bAutoActivate = true;
		Component->SetCollisionEnabled(ECollisionEnabled::NoCollision);
		Component->SetSimulatePhysics(false);
		Component->SetupAttachment(Parent, SocketName);
	}
	else
	{
		UE_LOG(LogPlayerPawn, Error,
			TEXT("The asset ParticleSystem'/Game/Models/ThrustParticle/EngineThrustSystem.EngineThrustSystem' was not found"));
		Component = nullptr;
	}

	return Component;
}

// Creates the component which manages the movement of the ship
UFly2DMovementComponent* APlayerPawn::CreateMovementComponent()
{
	UFly2DMovementComponent* Component = CreateDefaultSubobject<UFly2DMovementComponent>(TEXT("MovementComponent"));
	Component->UpdatedComponent = CapsuleComponent;
	return Component;
}

// Creates the explosion particle system
UParticleSystem* APlayerPawn::CreateExplosionParticleSystem()
{
	UParticleSystem* Result;
	static ConstructorHelpers::FObjectFinder<UParticleSystem> PartSystem(
		TEXT("ParticleSystem'/Game/Models/Explosion/ExplosionSystem.ExplosionSystem'"));
	if (PartSystem.Succeeded())
	{
		Result = PartSystem.Object;
	}
	else
	{
		UE_LOG(LogPlayerPawn, Error,
			TEXT("The asset ParticleSystem'/Game/Models/Explosion/ExplosionSystem.ExplosionSystem' was not found"));
		Result = nullptr;
	}

	return Result;
}

// Called after component initialization
void APlayerPawn::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	// Convert the first material of the mesh in a dynamic instance in order to change its color
	// during the power-up activation mode
	DynMaterial = MeshComponent->CreateAndSetMaterialInstanceDynamic(0);
	if (!DynMaterial)
	{
		UE_LOG(LogPlayerPawn, Error, TEXT("The dynamic material instance could not be created"));
	}
}

// Called when the game starts or when spawned
void APlayerPawn::BeginPlay()
{
	Super::BeginPlay();

	// Init the player state
	AZynapsPlayerState* State = GetZynapsPlayerState();
	if (!State)
	{
		UE_LOG(LogPlayerPawn, Error, TEXT("Failed to retrieve the player state"));
		return;
	}
	State->SetCurrentState(EPlayerState::Playing);
}

// Called every frame
void APlayerPawn::Tick(float DeltaSeconds)
{<commit_msg>Dynamic material to allow color changes during power-up activation mode<commit_after>// Copyright (c) 2017 Bytecode Bits

#include "ZynapsReloaded.h"
#include "PlayerPawn.h"
#include "ProjectionUtil.h"
#include "ZynapsWorldSettings.h"
#include "FuelCapsule.h"

// Log category
DEFINE_LOG_CATEGORY(LogPlayerPawn);

// Sets default values
APlayerPawn::APlayerPawn() : Super()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// Set up root component
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

	// Set up the collision capsule
	CapsuleComponent = CreateCapsuleComponent(RootComponent);

	// Set up the mesh component
	MeshComponent = CreateMeshComponent(CapsuleComponent);

	// Set up the engine thrust particle system
	EngineThrustSocketName = TEXT("EngineThrust");
	EnginePartSystemComponent = CreateEngineThrustParticleSystem(MeshComponent, EngineThrustSocketName);

	// Set up the movement component
	MovementComponent = CreateMovementComponent();

	// Set up the explosion particle system
	ExplosionPartSystem = CreateExplosionParticleSystem();

	// Sets this pawn to be controlled by the lowest-numbered player
	AutoPossessPlayer = EAutoReceiveInput::Player0;

	// Init shooting vars
	ProjectileClass = APlayerProjectile::StaticClass();
	LeftCannonSocketName = FName("LeftCannon");
	RightCannonSocketName = FName("RightCannon");
	TopCannonSocketName = FName("TopCannon");
	NextCannon = RightCannon;
}

// Creates the capsule component used for collision detection
UCapsuleComponent* APlayerPawn::CreateCapsuleComponent(USceneComponent* Parent)
{
	UCapsuleComponent* Component = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleComponent"));
	Component->InitCapsuleSize(18.0f, 32.0f);
	Component->SetWorldRotation(FRotator(0.0f, 180.0f, -90.0f));
	Component->SetWorldScale3D(FVector(12.0f, 10.0f, 10.0f));
	Component->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	Component->bGenerateOverlapEvents = true;
	Component->SetNotifyRigidBodyCollision(true);
	Component->SetCollisionObjectType(ECollisionChannel::ECC_Pawn);
	Component->SetCanEverAffectNavigation(false);
	Component->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
	Component->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
	Component->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldDynamic, ECollisionResponse::ECR_Block);
	Component->SetCollisionResponseToChannel(ECollisionChannel::ECC_PhysicsBody, ECollisionResponse::ECR_Overlap);
	Component->SetSimulatePhysics(true);
	Component->SetEnableGravity(false);
	Component->SetAngularDamping(0.0f);
	Component->SetLinearDamping(0.0f);
	Component->BodyInstance.bLockXRotation = true;
	Component->BodyInstance.bLockZRotation = true;
	Component->BodyInstance.bLockXTranslation = true;
	//Component->SetHiddenInGame(false);
	Component->WakeRigidBody();
	Component->OnComponentBeginOverlap.AddDynamic(this, &APlayerPawn::BeginOverlap);
	Component->OnComponentEndOverlap.AddDynamic(this, &APlayerPawn::EndOverlap);
	Component->OnComponentHit.AddDynamic(this, &APlayerPawn::Hit);
	Component->SetupAttachment(Parent);

	return Component;
}

// Creates the mesh component which models the ship
UStaticMeshComponent* APlayerPawn::CreateMeshComponent(USceneComponent* Parent)
{
	UStaticMeshComponent* Component = CreateDefaultSubobject<UStaticMeshComponent>(
		TEXT("MeshComponent"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(
		TEXT("StaticMesh'/Game/Models/Ship/zynaps_ship.zynaps_ship'"));
	if (MeshObj.Succeeded())
	{
		Component->SetStaticMesh(MeshObj.Object);
		Component->SetRelativeLocation(FVector(0.0f, -3.5f, -7.0f));
		Component->SetRelativeRotation(FRotator(0.0f, 0.0f, 90.0f));
		Component->SetRelativeScale3D(FVector(1.0f / 12.0f, 0.1f, 0.1f));
		Component->SetCollisionEnabled(ECollisionEnabled::NoCollision);
		Component->SetSimulatePhysics(false);
		Component->SetupAttachment(Parent);
	}
	else
	{
		UE_LOG(LogPlayerPawn, Error, 
			TEXT("The asset StaticMesh'/Game/Models/Ship/zynaps_ship.zynaps_ship' was not found"));
		Component = nullptr;
	}

	return Component;
}

// Creates the particle system for the engine thrust
UParticleSystemComponent* APlayerPawn::CreateEngineThrustParticleSystem(USceneComponent* Parent, FName SocketName)
{
	UParticleSystemComponent* Component = 
		CreateDefaultSubobject<UParticleSystemComponent>(TEXT("EnginePartSystemComponent"));
	static ConstructorHelpers::FObjectFinder<UParticleSystem> PartSystem(
		TEXT("ParticleSystem'/Game/Models/ThrustParticle/EngineThrustSystem.EngineThrustSystem'"));
	if (PartSystem.Succeeded())
	{
		Component->SetTemplate(PartSystem.Object);
		Component->SetRelativeScale3D(FVector(1.5f, 0.5f, 1.5f));
		Component->bAutoActivate = true;
		Component->SetCollisionEnabled(ECollisionEnabled::NoCollision);
		Component->SetSimulatePhysics(false);
		Component->SetupAttachment(Parent, SocketName);
	}
	else
	{
		UE_LOG(LogPlayerPawn, Error,
			TEXT("The asset ParticleSystem'/Game/Models/ThrustParticle/EngineThrustSystem.EngineThrustSystem' was not found"));
		Component = nullptr;
	}

	return Component;
}

// Creates the component which manages the movement of the ship
UFly2DMovementComponent* APlayerPawn::CreateMovementComponent()
{
	UFly2DMovementComponent* Component = CreateDefaultSubobject<UFly2DMovementComponent>(TEXT("MovementComponent"));
	Component->UpdatedComponent = CapsuleComponent;
	return Component;
}

// Creates the explosion particle system
UParticleSystem* APlayerPawn::CreateExplosionParticleSystem()
{
	UParticleSystem* Result;
	static ConstructorHelpers::FObjectFinder<UParticleSystem> PartSystem(
		TEXT("ParticleSystem'/Game/Models/Explosion/ExplosionSystem.ExplosionSystem'"));
	if (PartSystem.Succeeded())
	{
		Result = PartSystem.Object;
	}
	else
	{
		UE_LOG(LogPlayerPawn, Error,
			TEXT("The asset ParticleSystem'/Game/Models/Explosion/ExplosionSystem.ExplosionSystem' was not found"));
		Result = nullptr;
	}

	return Result;
}

// Called after component initialization
void APlayerPawn::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	// Convert the first material of the mesh in a dynamic instance in order to change its color
	// during the power-up activation mode
	DynMaterial = MeshComponent->CreateAndSetMaterialInstanceDynamic(0);
	if (!DynMaterial)
	{
		UE_LOG(LogPlayerPawn, Error, TEXT("The dynamic material instance could not be created"));
		return;
	}
	DynMaterial->SetScalarParameterValue(FName("HighlightAlpha"), 0.0f);
}

// Called when the game starts or when spawned
void APlayerPawn::BeginPlay()
{
	Super::BeginPlay();

	// Init the player state
	AZynapsPlayerState* State = GetZynapsPlayerState();
	if (!State)
	{
		UE_LOG(LogPlayerPawn, Error, TEXT("Failed to retrieve the player state"));
		return;
	}
	State->SetCurrentState(EPlayerState::Playing);
}

// Called every frame
void APlayerPawn::Tick(float DeltaSeconds)
{
{'fraction_non_alphanumeric': 0.10857101906263437, 'fraction_numerical': 0.009889637379962735, 'mean_word_length': 7.233038348082596, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1205036', 'n_tokens_mistral': 4493, 'n_tokens_neox': 4279, 'n_words': 804}