# © Spark Universe, 2023
# authors: destruc7i0n
# description: p3 of the tutorial main loop, run as instance marker
# called by: 

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

execute if score @s tut_timer matches 1 run tp @p[tag=instance_player] ~ ~ ~ 0 0
execute if score @s tut_timer matches 1 run fog @p[tag=instance_player] push spark:fog_tutorial tutorial_fog

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

# give the player a lawn mower if they don't have one
execute if score @s tut_timer matches 61 as @p[tag=instance_player] if entity @s[hasitem={item=spark:place_lawn_mower,quantity=0}] run give @s spark:place_lawn_mower 6 0 {"minecraft:item_lock": {"mode": "lock_in_slot"}, "minecraft:keep_on_death": {}, "minecraft:can_place_on": {"blocks": ["spark:tutorial_light", "spark:tutorial_arrow_down"]}}
# tag the lawn mower if its there
execute if score @s tut_timer matches 61 as @p[tag=instance_player] at @s as @e[type=spark:lawn_mower,r=6,tag=!tutorial_placeable] run function spark/tutorial/p3/tag_placeable

# remove lawn mower if it falls on the spark block
execute as @e[tag=tutorial_placeable] at @s if block ~ ~-0.1 ~ spark:spark_universe_color [] run event entity @s spark:despawn

# 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]
# 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:place_lawn_mower
# remove the placed lawn mower if its there
execute if score @s tut_timer matches 100 as @e[tag=tutorial_placeable] if score @s tut = .tut_player_id player_id run event entity @s spark:despawn
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]

execute if score @s tut_timer matches 130 run fog @p[tag=instance_player] pop tutorial_fog
execute if score @s tut_timer matches 140 run function spark/tutorial/p3/next
