################
# > Minecraft Spider: A Minecraft spider that will follow the nearest person.
# > How to use: Spawn it and let the Minecraft spider run after people.
################

@name Minecraft Spider
@persist Scale Speed I Body:entity
@persist Ranger:ranger Floor Clip Gravity A
@persist Colour:vector TeethColour:vector FaceColour:vector

interval(10)

if (first()|duped()){
    Scale=1
    Speed=Scale
    Colour=vec(25,25,25) # 25,25,25 default
    TeethColour=vec(100,0,0) # 100,0,0 default
    FaceColour=vec(0,0,0) # 0,0,0 default
    
    #Body
    holoCreate(1)
    holoScale(1,vec(1.5,1.2,1.2)*Scale)
    holoPos(1,entity():pos()+vec(0,20,15)*Scale)
    holoAng(1,ang(0,-90,0))
    Body = holoEntity(1)
    
    #Head
    holoCreate(2)
    holoScale(2,vec(1,1,1)*Scale)
    holoPos(2,entity():pos()+vec(0,0,15)*Scale)
    holoAng(2,ang(0,-90,0))
    
    #Legs
    holoCreate(3)
    holoScale(3,vec(2,0.3,0.3)*Scale)
    holoPos(3,entity():pos()+vec(15,5,10)*Scale)
    holoAng(3,ang(20,-40,0))
    holoCreate(4)
    holoScale(4,vec(2,0.3,0.3)*Scale)
    holoPos(4,entity():pos()+vec(-15,5,10)*Scale)
    holoAng(4,ang(-20,40,0))
    
    holoCreate(5)
    holoScale(5,vec(2,0.3,0.3)*Scale)
    holoPos(5,entity():pos()+vec(15,10,10)*Scale)
    holoAng(5,ang(20,-15,0))
    holoCreate(6)
    holoScale(6,vec(2,0.3,0.3)*Scale)
    holoPos(6,entity():pos()+vec(-15,10,10)*Scale)
    holoAng(6,ang(-20,15,0))
    
    holoCreate(7)
    holoScale(7,vec(2,0.3,0.3)*Scale)
    holoPos(7,entity():pos()+vec(15,15,10)*Scale)
    holoAng(7,ang(20,15,0))
    holoCreate(8)
    holoScale(8,vec(2,0.3,0.3)*Scale)
    holoPos(8,entity():pos()+vec(-15,15,10)*Scale)
    holoAng(8,ang(-20,-15,0))
    
    holoCreate(9)
    holoScale(9,vec(2,0.3,0.3)*Scale)
    holoPos(9,entity():pos()+vec(15,20,10)*Scale)
    holoAng(9,ang(20,40,0))
    holoCreate(10)
    holoScale(10,vec(2,0.3,0.3)*Scale)
    holoPos(10,entity():pos()+vec(-15,20,10)*Scale)
    holoAng(10,ang(-20,-40,0))
    
    #Eyes
    holoCreate(11)
    holoScale(11,vec(0.15*Scale,0.1,0.15*Scale))
    holoPos(11,entity():pos()+vec(2,-6,18)*Scale)
    holoCreate(12)
    holoScale(12,vec(0.15*Scale,0.1,0.15*Scale))
    holoPos(12,entity():pos()+vec(-2,-6,18)*Scale)
    
    #Mouth
    holoCreate(13)
    holoScale(13,vec(0.6*Scale,0.1,0.3*Scale))
    holoPos(13,entity():pos()+vec(0,-6,13)*Scale)
    
    #Teeth
    holoCreate(14)
    holoScale(14,vec(0.15*Scale,0.15,0.15*Scale))
    holoColor(14,vec(50,0,0))
    holoPos(14,entity():pos()+vec(-2.2,-6,14)*Scale)
    holoCreate(15)
    holoScale(15,vec(0.15*Scale,0.15,0.15*Scale))
    holoColor(15,vec(50,0,0))
    holoPos(15,entity():pos()+vec(1,-6,14)*Scale)
    
    holoColor(1,Colour)
    for (I=2,10) {
        holoColor(I,Colour)
        holoParent(I,Body)
    }
    for (I=11,15) {
        holoParent(I,holoEntity(2))
    }
    for (I=1,15) {
        holoMaterial(I,"models/debug/debugwhite")
    }
    holoColor(11,FaceColour)
    holoColor(12,FaceColour)
    holoColor(13,FaceColour)
    holoColor(14,TeethColour)
    holoColor(15,TeethColour)
    holoPos(1,Body:pos()+vec(0,0,-(4*Scale)))
    holoEntity(2):soundPlay("spider",0,"ambient/creatures/leech_bites_loop1.wav")
    soundVolume("spider",0.25)
}
findByClass("player*")
Players = findToArray()
NearestPly = findClosest(Body:pos())

holoAng(2,((NearestPly:pos()+vec(0,0,64))-holoEntity(2):pos()):toAngle())

if (Body:pos():distance(NearestPly:pos())>(Scale*50)){
    holoAng(1,((NearestPly:pos()+vec(0,0,15*Scale)+vec(0,0,-(4*Scale)))-Body:pos()):toAngle())
    holoAng(1,ang(0,Body:angles():yaw(),0))
    holoPos(1,Body:pos()+Body:forward()*Speed)
    A+=Speed*10
}

Ranger = Body:ranger(-999999)
Floor = Ranger:distance()
Clip = (Scale*10)+0.25
Gravity = Scale*2

if (Floor>Clip&Floor<500000){
    if ((Floor-Clip)<Gravity) {
        holoPos(1,Body:pos()-(Floor-Clip))
    }
    else {
        holoPos(1,Body:pos()-vec(0,0,Gravity))
    }
}
elseif (Floor<Clip|Floor>=500000) {
    holoPos(1,Body:pos()+vec(0,0,Gravity))
}

holoAng(3,Body:angles()+ang(0,90,0)+ang(20,cos(A)*10-40,0))
holoAng(4,Body:angles()+ang(0,90,0)+ang(-20,cos(A)*10+40,0))
holoAng(5,Body:angles()+ang(0,90,0)+ang(20,sin(A)*10-15,0))
holoAng(6,Body:angles()+ang(0,90,0)+ang(-20,sin(A)*10+15,0))
holoAng(7,Body:angles()+ang(0,90,0)+ang(15,cos(A)*10+15,0))
holoAng(8,Body:angles()+ang(0,90,0)+ang(-15,cos(A)*10-15,0))
holoAng(9,Body:angles()+ang(0,90,0)+ang(20,sin(A)*10+40,0))
holoAng(10,Body:angles()+ang(0,90,0)+ang(-20,sin(A)*10-40,0))
