tag @s add self101

#get rotation of human player
tag @s add validRotation
execute store result score @s rot_pitch run data get entity @s Rotation[1] 1
execute store result score @s rot_yaw run data get entity @s Rotation[0] 1
scoreboard players add @s[scores={rot_yaw=..-1}] rot_yaw 360

#check for valid rotation
execute if entity @s[tag=!freecam] unless score @s rot_yaw matches 173..187 run tag @s remove validRotation
execute if entity @s[tag=!freecam] unless score @s rot_pitch matches 40..60 run tag @s remove validRotation
tag @s[tag=alwaysvalid] add validRotation

#not valid rotation? no movement, no glowing
execute unless entity @s[tag=validRotation] as @e[tag=my_model,limit=1] run data merge entity @s {Glowing:1b}
execute if entity @s[tag=validRotation] as @e[tag=my_model,limit=1] run data merge entity @s {Glowing:0b}
execute unless entity @s[tag=validRotation] run effect give @s fire_resistance 15 1 false
execute if entity @s[tag=validRotation] run effect clear @s fire_resistance

#up/down/left/right
scoreboard players set @s left_right 0
scoreboard players set @s up_down 0
scoreboard players set @s[tag=validRotation,scores={xin=1000..}] left_right -1
scoreboard players set @s[tag=validRotation,scores={xin=..-1000}] left_right 1
scoreboard players set @s[tag=validRotation,scores={zin=1000..}] up_down 1
scoreboard players set @s[tag=validRotation,scores={zin=..-1000}] up_down -1

#convert movement directions when "as seen by bulldozer" is enabled
execute if entity @s[tag=alternate_controls] unless entity @s[scores={left_right=0,up_down=0}] run function bd:movement/alternate_control/_face_index

#can't move if dead
scoreboard players set @s[scores={deathTime=1..}] left_right 0
scoreboard players set @s[scores={deathTime=1..}] up_down 0

#can't move if locked in a cutscene or something
scoreboard players set @s[scores={haltMovement=1..}] left_right 0
scoreboard players set @s[scores={haltMovement=1..}] up_down 0

#global movement
scoreboard players operation global up_down = @s up_down
scoreboard players operation global left_right = @s left_right
execute if entity @s[tag=!freecam] run scoreboard players set global freecam 0
execute if entity @s[tag=freecam] run scoreboard players set global freecam 1

#visualize front end of bulldozer
execute as @e[tag=my_char,limit=1] at @s run particle minecraft:smoke ~ ~1 ~ 0 0 0 .01 1 force

#try to move
execute if score global moveCooldown matches ..0 as @e[tag=my_char,limit=1] at @s run function bd:movement/face_index

#scoreboard players set @s[scores={up_down=1,left_right=0}] faceDirection 1
#scoreboard players set @s[scores={up_down=1,left_right=1}] faceDirection 2
#scoreboard players set @s[scores={up_down=0,left_right=1}] faceDirection 3
#scoreboard players set @s[scores={up_down=-1,left_right=1}] faceDirection 4
#scoreboard players set @s[scores={up_down=-1,left_right=0}] faceDirection 5
#scoreboard players set @s[scores={up_down=-1,left_right=-1}] faceDirection 6
#scoreboard players set @s[scores={up_down=0,left_right=-1}] faceDirection 7
#scoreboard players set @s[scores={up_down=1,left_right=-1}] faceDirection 8
#^ this code that was commented out was used in No More Diamonds where player could move freely in 8 directions
#doze off borrowed the tech I wrote for NMD, which itself was borrowed from my earlier map Bliss which was a 2D platformer

#max movespeed...
scoreboard players set @s[scores={xin=1748..}] xin 1747
scoreboard players set @s[scores={xin=-1747}] xin -1748
scoreboard players set @s[scores={zin=1748..}] zin 1747
scoreboard players set @s[scores={zin=-1747}] zin -1748

#last held direction
#execute unless entity @s[scores={left_right=0,up_down=0}] run scoreboard players operation @s lastDirection = @s faceDirection
scoreboard players operation @s lastDirection = @s faceDirection

#player char gets coords
execute as @e[tag=my_char,limit=1] at @s store result score @s char_x run data get entity @s Pos[0] 1
execute as @e[tag=my_char,limit=1] at @s store result score @s char_y run data get entity @s Pos[1] 1
execute as @e[tag=my_char,limit=1] at @s store result score @s char_z run data get entity @s Pos[2] 1



#last direction stored on my_char
scoreboard players operation @e[tag=my_char,limit=1,sort=nearest] lastDirection = @s lastDirection

#get y motion
#execute as @e[tag=my_char,limit=1] store result score @e[limit=1,tag=self101] motion_y run data get entity @s Motion[1] 1000

#clean up tag
tag @s remove self101