class Snake extends Chain{
constructor(positions, distances, bodySizes){
super(positions, distances);
if (bodySizes.length != this.joints.length) throw new Error("bad arguments");
this.bodies = bodySizes;
this.angles = new Array(this.joints.length).fill(Math.PI);
}
resolve(facingAng){
super.resolve(0);
this.angles[0] = facingAng;
for (let i = 1; i < this.joints.length; ++i) {
let ang = vecHeading(vecFromSub(this.joints[i-1], this.joints[i]));
this.angles[i] = ang;
}
}
getShape(){
const getPoint = (idx, angOffset, sizeOffset) => {
const x = this.joints[idx].x + Math.cos(this.angles[idx] + angOffset) * (this.bodies[idx] / 2 + sizeOffset);
const y = this.joints[idx].y + Math.sin(this.angles[idx] + angOffset) * (this.bodies[idx] / 2 + sizeOffset);
return {x, y};
}
let points = [getPoint(0,Math.PI/6, 0), getPoint(0,0,0), getPoint(0, -Math.PI/6, 0)];
for (let i = 0; i < this.joints.length; ++i) {
points.push(getPoint(i, -Math.PI/2, 0));
points.unshift(getPoint(i,Math.PI/2, 0));
}
points.push(getPoint(this.joints.length-1, -5*Math.PI/6, 0));
points.unshift(getPoint(this.joints.length-1,5*Math.PI/6, 0));
points.unshift(getPoint(this.joints.length-1, Math.PI,0));
return points;
}
}