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

scoreboard players add @s[scores={tut_timer=..60}] tut_timer 1
scoreboard players add @s[scores={tut_timer=70..89}] tut_timer 1
scoreboard players add @s[scores={tut_timer=200..}] tut_timer 1

# make the player not go on the line
execute positioned ~-5 ~-2 ~3 as @p[tag=instance_player,m=a,dx=10,dy=6,dz=1] 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

# spawn the first zombie
execute if score @s tut_timer matches 60 run function spark/tutorial/p4/first_zombie

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

# tag the projectile the player shoots
tag @e[x=~-5,y=~-1,z=~-2,dx=10,dy=7,dz=12,type=spark:fire_spew] add tutorial_projectile

# count the number of projectiles for the instance
scoreboard players set .proj_count tut 0
execute as @e[tag=tutorial_projectile] run scoreboard players add .proj_count tut 1

# give the player charge while there is still one zombie alive and there are no active projectiles
execute if score @s tut_timer matches 61 as @p[tag=instance_player] if score @s charge_ability matches ..9 if score .proj_count tut matches 0 if score .target_count tut matches 1 run function spark/tutorial/p4/charge_player

# despawn all projectiles when the zombie is killed
execute if score @s tut_timer matches 61 if score .target_count tut matches 0 run function spark/tutorial/p4/first_zombie_killed
execute if score @s tut_timer matches 61 if score .target_count tut matches 0 run scoreboard players set @s tut_timer 70

# remove all projectiles before the next zombie spawns
execute if score @s tut_timer matches 70..90 as @e[tag=tutorial_projectile] run event entity @s spark:despawn
# spawn the second zombie after 1 second
execute if score @s tut_timer matches 90 if score .target_count tut matches 0 if score .proj_count tut matches 0 run function spark/tutorial/p4/second_zombie

# give the player the charge potion while there is a zombie and no active projectiles
execute if score @s tut_timer matches 100 if score .target_count tut matches 1 if score .proj_count tut matches 0 run give @p[tag=instance_player,hasitem={item=spark:charge_potion,quantity=0},scores={charge_ability=..9}] spark:charge_potion 1 0 {"minecraft:item_lock": {"mode": "lock_in_slot"}, "minecraft:keep_on_death": {}}
# remove if they don't need it (since the potion charging is handled in anim controller we need this to remove it)
scoreboard players set @p[tag=instance_player,hasitem={item=spark:charge_potion},scores={charge_ability=10..}] SS_ui_tutorial 10
clear @p[tag=instance_player,hasitem={item=spark:charge_potion},scores={charge_ability=10..}] spark:charge_potion

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

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

execute if score @s tut_timer matches 240 run function spark/tutorial/p4/next

scoreboard players reset .proj_count tut
tag @e[tag=tutorial_projectile] remove tutorial_projectile
