void ALD35Character::Tick(float deltaTime) { Super::Tick(deltaTime); if (Health <= 0) return; ShotCooldown -= deltaTime; HealthRegenStoppedFor -= deltaTime; if (IsFiring && ShotCooldown <= 0) { ShotCooldown = IsInAlternateForm ? 0.5f : 1.5f; OnFire(); } if (IsInAlternateForm) { Energy = FMath::Clamp(Energy - deltaTime / MaxTransformTime, 0.f, 1.f); if (Energy <= 0) { Transform(); } if (HealthRegenStoppedFor <= 0) Health = FMath::Clamp(Health + deltaTime, 0.f, 4.f); } for (auto& a : GetComponentsByTag(UPrimitiveComponent::StaticClass(), TEXT("Human"))) { Cast<UPrimitiveComponent>(a)->SetVisibility(!IsInAlternateForm); } for (auto& a : GetComponentsByTag(UPrimitiveComponent::StaticClass(), TEXT("Claw"))) { Cast<UPrimitiveComponent>(a)->SetVisibility(IsInAlternateForm); } for (auto& a : GetComponentsByClass(UPointLightComponent::StaticClass())) { Cast<UPointLightComponent>(a)->SetVisibility(!IsSomeoneTransformed); } }
// Called every frame void AWeapon::Tick( float DeltaTime ) { Super::Tick( DeltaTime ); if (!m_weaponOpening) { TArray<UActorComponent*> m_components = GetComponentsByTag(USceneComponent::StaticClass(), "WeaponOpening"); if (m_components.Num() > 0) { m_weaponOpening = Cast<USceneComponent>(m_components[0]); } } }
void ALD35Character::OnFire() { if (IsInAlternateForm) { FVector trgPos = FindComponentByClass<UCameraComponent>()->GetComponentLocation() + GetActorRotation().RotateVector(FVector(140, 0, 0)); //DrawDebugSphere(GetWorld(), trgPos, 150, 8, FColor::Red, false, 0.5f); TArray<FOverlapResult> res; if (GetWorld()->OverlapMultiByChannel(res, trgPos, FQuat::Identity, ECollisionChannel::ECC_WorldDynamic, FCollisionShape::MakeSphere(150))) { for (auto& a : res) { if (a.Actor.Get() && a.Actor != this) { a.Actor->TakeDamage(1, FDamageEvent(), this->GetController(), this); } } } if (FMath::Rand() % 3 == 0) { UGameplayStatics::PlaySoundAtLocation(this, TransformSound, GetActorLocation()); } return; } // try and fire a projectile if (ProjectileClass != NULL) { USceneComponent* cam = FindComponentByClass<UCameraComponent>(); if (cam) { FRotator SpawnRotation = cam->GetComponentRotation(); FVector SpawnLocation = cam->GetComponentLocation(); TArray<UActorComponent*> childComponents = GetComponentsByTag(USceneComponent::StaticClass(), "Gun"); for (auto& b : childComponents) { if (auto a = Cast<USceneComponent>(b)) { if (a->ComponentHasTag(TEXT("Gun"))) { SpawnRotation = a->GetComponentRotation(); SpawnLocation = a->GetComponentLocation(); //UE_LOG(LogTemp, Display, TEXT("NM %s %s %s %s %s"), *a->GetName(), *cam->GetName(), *SpawnLocation.ToString(), *cam->GetComponentLocation().ToString(), *a->GetRelativeTransform().ToString()); } } } UWorld* const World = GetWorld(); if (World != NULL) { // spawn the projectile at the muzzle FActorSpawnParameters params; params.Instigator = this; World->SpawnActor<ALD35Projectile>(ProjectileClass, SpawnLocation, SpawnRotation, params); } } } // try and play the sound if specified if (FireSound != NULL) { UGameplayStatics::PlaySoundAtLocation(this, ShootCrossbowSound, GetActorLocation()); } }