#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
#    This function has been created by Skilendarz Group.
#             https://skilendarz.com
#
#      GIVE MAP MAKERS THE CREDIT THEY DESERVE!
#             COPYING IS PROHIBITED!
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

scoreboard players operation $id id2 = @s id2
execute as @e[tag=minigun] if score @s id2 = $id id2 run tag @s add this_m

#animation
scoreboard players add @s time 1
execute if entity @s[scores={time=10}] run playsound minecraft:sound.minigun master @a[distance=..32] ~ ~ ~ 2
execute if entity @s[scores={time=10..12}] run scoreboard players add @e[tag=minigun_barrel,tag=this_m] time 5
execute if entity @s[scores={time=13..15}] run scoreboard players add @e[tag=minigun_barrel,tag=this_m] time 10
execute if entity @s[scores={time=16..18}] run scoreboard players add @e[tag=minigun_barrel,tag=this_m] time 15
execute if entity @s[scores={time=19..210}] run scoreboard players add @e[tag=minigun_barrel,tag=this_m] time 20
execute if entity @s[scores={time=19}] as @e[tag=this_m] run data merge entity @s {Glowing:1}

execute if entity @s[scores={time=19..239},team=minigun_red] at @p[tag=player,team=!red,scores={kill-timer=0}] run summon marker ~ ~-1 ~ {Tags:["minigun_target"]}
execute if entity @s[scores={time=19..239},team=minigun_blue] at @p[tag=player,team=!blue,scores={kill-timer=0}] run summon marker ~ ~-1 ~ {Tags:["minigun_target"]}
execute if entity @s[scores={time=19..239},team=minigun_green] at @p[tag=player,team=!green,scores={kill-timer=0}] run summon marker ~ ~-1 ~ {Tags:["minigun_target"]}
execute if entity @s[scores={time=19..239},team=minigun_yellow] at @p[tag=player,team=!yellow,scores={kill-timer=0}] run summon marker ~ ~-1 ~ {Tags:["minigun_target"]}
execute if entity @s[scores={time=19..239}] as @e[tag=minigun_tower,tag=this_m] at @s run tp @s ~ ~ ~ facing entity @e[tag=minigun_target,limit=1]
kill @e[tag=minigun_target]

execute if entity @s[scores={time=25..230}] at @e[tag=minigun_barrel,tag=this_m] rotated ~90 ~ run particle minecraft:smoke ^ ^1.55 ^0.9 0 0 0 0 1 force @a
execute if entity @s[scores={time=230}] as @e[tag=this_m] run data merge entity @s {Glowing:0}
execute if entity @s[scores={time=211..233}] run scoreboard players add @e[tag=minigun_barrel,tag=this_m] time 15
execute if entity @s[scores={time=214..236}] run scoreboard players add @e[tag=minigun_barrel,tag=this_m] time 10
execute if entity @s[scores={time=217..239}] run scoreboard players add @e[tag=minigun_barrel,tag=this_m] time 5

#end
execute if entity @s[scores={time=260}] run summon minecraft:creeper ~ ~ ~ {Fuse:0}
execute if entity @s[scores={time=260}] run setblock ~ ~0.6 ~ air
execute if entity @s[scores={time=260..}] run kill @e[tag=minigun,tag=this_m]

#elements position
execute at @e[tag=minigun_tower,tag=this_m] run tp @e[tag=minigun_barrel,tag=this_m] ^-0.17 ^0.37 ^-0.03 ~-90 ~
tp @e[tag=minigun_tower,tag=this_m] ~ ~-0.75 ~

#bullet spawn
execute if entity @s[scores={time=19..230}] at @e[tag=minigun_barrel,tag=this_m] positioned ^-0.3 ^-0.05 ^ run summon armor_stand ~ ~1.4 ~ {Invisible:1,NoGravity:1,Tags:["candy_bullet","this_m"],ArmorItems:[{},{},{},{id:"minecraft:lime_dye",Count:1b,tag:{CustomModelData:44}}]}
execute at @e[tag=minigun_barrel,tag=this_m] run tp @e[tag=candy_bullet,tag=this_m] ~ ~1.4 ~ ~90 ~

scoreboard players operation @e[tag=candy_bullet,tag=this_m] id = @s id
scoreboard players operation @e[tag=candy_bullet,tag=this_m] id2 = @s id2

#team
execute if entity @s[team=minigun_blue] run tag @e[tag=candy_bullet,tag=this_m] add blue
execute if entity @s[team=minigun_red] run tag @e[tag=candy_bullet,tag=this_m] add red
execute if entity @s[team=minigun_green] run tag @e[tag=candy_bullet,tag=this_m] add green
execute if entity @s[team=minigun_yellow] run tag @e[tag=candy_bullet,tag=this_m] add yellow

tag @e[tag=this_m] remove this_m