# © Spark Universe, 2023
# authors: destruc7i0n
# description: p2 main loop
# called by: 

scoreboard players add @s[scores={tut_timer=..60}] tut_timer 1
scoreboard players add @s[scores={tut_timer=100..}] tut_timer 1

# make the player not go on the line
execute positioned ~-7 ~-1 ~7 as @p[tag=instance_player,m=a,dx=14,dy=6,dz=2] at @s facing entity @e[tag=current_instance,c=1] eyes run tp @s ^ ^ ^0.3

execute if score @s tut_timer matches 1 run tp @p[tag=instance_player] ~ ~ ~ 0 0

execute if score @s tut_timer matches 60 run function spark/tutorial/p2/spawn_targets
execute if score @s tut_timer matches 60 run scoreboard players set @p[tag=instance_player] SS_ui_tutorial 2
# execute if score @s tut_timer matches 60 run playsound mob.villager.haggle @p[tag=instance_player]

# make zombies face player
execute if score @s tut_timer matches 61 as @e[tag=instance_target] at @s run tp @s ~ ~ ~ facing @p[tag=instance_player]

# tag the projectile the player shoots
execute if score @s tut_timer matches 61.. as @p[tag=instance_player] at @s as @e[type=spark:sawblade,r=3,tag=!tutorial_projectile] run function spark/tutorial/p2/tag_projectile

# check if all are dead
execute if score @s tut_timer matches 61 if score .target_count tut matches 0 run scoreboard players set @s tut_timer 100

execute if score @s tut_timer matches 100 run clear @p[tag=instance_player] spark:sawblade_shooter
execute if score @s tut_timer matches 100 run scoreboard players set @p[tag=instance_player] SS_ui_tutorial 0
execute if score @s tut_timer matches 100 run playsound spark.zombies2.tutorial.success_minor @p[tag=instance_player]

# remove any projectiles that might get stuck in the wall
execute if score @s tut_timer matches 110 as @e[tag=tutorial_projectile] if score @s tut = .tut_player_id player_id run event entity @s spark:despawn

execute if score @s tut_timer matches 140 run function spark/tutorial/p2/next
