// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Engine/TargetPoint.h"
#include "TargetSpawner.generated.h"

UCLASS()
class GAMEJAMTEMPLATE_API ATargetSpawner : public ATargetPoint
{
	GENERATED_BODY()
	
public:

	ATargetSpawner();

	void Tick(float DeltaTime) override;

	/**
	* Sets the number of lanes to use for this spawning cycle.
	* @param NewNumberOfLanes The new number of lanes.
	*/
	void SetNumberOfLanes(int NewNumberOfLanes);

	/**
	* Sets the frequency at which the targets will be spawned.
	* @param NewSpawningInterval The new spawning interval.
	* @param NewDeltaInterval The new delta interval.
	*/
	void SetSpawningParameters(float NewSpawningInterval, float NewDeltaInterval);

	/**
	* Sets the speed at which the targets should move.
	* @param NewTargetMovementSpeed The target's new movement speed.
	*/
	void SetTargetMovementSpeed(float NewTargetMovementSpeed);

	/**
	* Starts a new spawning cycle.
	*/
	void Start();

	/**
	* Stops the current spawning cycle.
	*/
	void Stop();

	/**
	* Finds all targets within a blast radius and returns them.
	* Takes into consideration all of the targets in all of the lanes.
	* @param TargetsInRange The targets within the blast radius.
	* @param CenterOfBlast The world space location of the center of the blast.
	* @param BlastRadius The radius of the blast.
	*/
	void FindTargets(TArray<class ATarget*>* TargetsInRange, FVector CenterOfBlast, float BlastRadius);

protected:

	void BeginPlay() override;

private:

	// The name of the object fetched by the UObjectBaseUtility::GetName method
	const FString ObjectName;

	// Indicates if the spawner is currently spawning.
	bool bIsSpawning;

	// The lanes within the level.
	TMap<int, class ALane*> Lanes;

	// The number of lanes to use.
	int NumberOfLanes;

	// The time that elapses between two targets being spawned.
	float SpawningInterval;

	// The amount of time the spawning interval can flux.
	float DeltaInterval;

	// The movement speed of the spawned targets.
	float TargetMovementSpeed;

	// The amount of time that elapsed since the last target was spawned.
	float ElapsedTime;

	// The amount of time that needs to pass before the next target is spawned.
	float TimeTillNextSpawn;

	/**
	* Finds all of the lanes in the level.
	*/
	void FindLanes();

	/**
	* Spawns the targets according to the specified parameters.
	* @param DeltaTime The elapsed time between the previous two ticks.
	*/
	void Spawn(float DeltaTime);

};
