# Summon a ray at the caster


scoreboard players set @s invulnerable 1
execute if entity @s[nbt={SelectedItem:{tag:{weapon:"plasmarifle"}}}] run scoreboard players remove @s[scores={magPlasmaRifle=1..}] magPlasmaRifle 1

execute if entity @s[nbt={SelectedItem:{tag:{weapon:"plasmarifle"}}}] run summon area_effect_cloud ~ ~ ~ {Duration: 1, Tags: ["ray","rayplasmarifle","plasmarifleray"]}

execute if entity @s[nbt={SelectedItem:{tag:{weapon:"plasmarifle"}}}] at @s run playsound minecraft:weapon.plasmarifle player @a ~ ~ ~ 0.4


# Position the ray at the caster's eyes and copy their facing
execute store result score @s bloom run random value -7..9
execute anchored eyes rotated as @s[scores={bloom=-7}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~-2 ~
execute anchored eyes rotated as @s[scores={bloom=-6}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~-3 ~
execute anchored eyes rotated as @s[scores={bloom=-5}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~ ~-2
execute anchored eyes rotated as @s[scores={bloom=-4}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~ ~-3
execute anchored eyes rotated as @s[scores={bloom=-3}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~-2 ~-2
execute anchored eyes rotated as @s[scores={bloom=-2}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~-3 ~-3
execute anchored eyes rotated as @s[scores={bloom=-1}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~-2 ~-3
execute anchored eyes rotated as @s[scores={bloom=0}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~-3 ~-2
execute anchored eyes rotated as @s[scores={bloom=1}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~ ~
execute anchored eyes rotated as @s[scores={bloom=2}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~2 ~
execute anchored eyes rotated as @s[scores={bloom=3}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~3 ~
execute anchored eyes rotated as @s[scores={bloom=4}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~ ~2
execute anchored eyes rotated as @s[scores={bloom=5}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~ ~3
execute anchored eyes rotated as @s[scores={bloom=6}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~2 ~2
execute anchored eyes rotated as @s[scores={bloom=7}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~3 ~3
execute anchored eyes rotated as @s[scores={bloom=8}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~2 ~3
execute anchored eyes rotated as @s[scores={bloom=9}] run tp @e[tag=ray,limit=1,sort=nearest] ^ ^ ^1 ~3 ~2
scoreboard players reset @s bloom

execute store result score @s recoil run random value 1..4
tp @s[nbt={SelectedItem:{tag:{weapon:"plasmarifle"}}},scores={recoil=1}] ^ ^ ^ ~0.4 ~-0.3
tp @s[nbt={SelectedItem:{tag:{weapon:"plasmarifle"}}},scores={recoil=2}] ^ ^ ^ ~-0.4 ~-0.4
tp @s[nbt={SelectedItem:{tag:{weapon:"plasmarifle"}}},scores={recoil=3}] ^ ^ ^ ~0.5 ~-0.3
tp @s[nbt={SelectedItem:{tag:{weapon:"plasmarifle"}}},scores={recoil=4}] ^ ^ ^ ~-0.5 ~-0.4
scoreboard players reset @s recoil
# Process the ray
execute as @e[tag=ray,limit=1,sort=nearest] run function ray_demo:ray/process

