// Fill out your copyright notice in the Description page of Project Settings. #include "Characters/ExoPlayerCharacter.h" #include "Blueprint/UserWidget.h" #include "Characters/Components/ShootingComponent.h" #include "GameFramework/CharacterMovementComponent.h" #include "Items/AmmoBoxBase.h" #include "Items/HealthBoxBase.h" #include "Player/InteractionComponent.h" #include "Widget/WBP_PlayerUI.h" AExoPlayerCharacter::AExoPlayerCharacter() { GetCharacterMovement()->bSnapToPlaneAtStart = true; InteractionComponent = CreateDefaultSubobject(TEXT("Interaction Component")); ShootingComponent = CreateDefaultSubobject(TEXT("Shooting Component")); Weapon = CreateDefaultSubobject("Weapon"); Weapon->SetupAttachment(GetMesh()); Weapon->SetCollisionEnabled(ECollisionEnabled::NoCollision); Weapon->bCastDynamicShadow = false; Weapon->CastShadow = false; Weapon->SetRelativeLocation(FVector(-50.f, 0.f, -90.f)); bUseControllerRotationPitch = false; bUseControllerRotationYaw = true; bUseControllerRotationRoll = false; } void AExoPlayerCharacter::BeginPlay() { Super::BeginPlay(); GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &AExoPlayerCharacter::OnActorBeginOverlap); PlayerHud = CreateWidget(GetWorld(),PlayerHudClass); if (PlayerHud) { PlayerHud->AddToViewport(); PlayerHud->AddToPlayerScreen(); if (PlayerHud) { PlayerHud->SetHp(CurrentHealth,MaxHealth); PlayerHud->SetAmmo(10); } } } void AExoPlayerCharacter::OnActorBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { if (OtherActor && OtherActor != this) { if (OtherActor->IsA(AHealthBoxBase::StaticClass())) { AHealthBoxBase* HealthBox = Cast(OtherActor); if (HealthBox) { AddHealthPoints(HealthBox->HealthValue); HealthBox->Destroy(); UE_LOG(LogTemp, Warning, TEXT("Zebrano apteczkę")); } } else if (OtherActor->IsA(AAmmoBoxBase::StaticClass())) { AAmmoBoxBase* AmmoBox = Cast(OtherActor); if (AmmoBox) { if (ShootingComponent->AddAmmo(AmmoBox->AmmoType, AmmoBox->AmmoValue)) { AmmoBox->Destroy(); UE_LOG(LogTemp, Warning, TEXT("Zebrano amunicję")); } } } } } void AExoPlayerCharacter::AddHealthPoints(float addValue) { CurrentHealth += addValue; if (CurrentHealth > MaxHealth) CurrentHealth = MaxHealth; if (PlayerHud) PlayerHud->SetHp(CurrentHealth,MaxHealth); }