// 3D Snake - was going to have it travel thru hyperspace but distorting the body // was just too much hassle, I quite like the result anyway. class Snake{ Vector body; Node head; int len, col; float scale; Snake(Node n, int len, int col, float scale){ body = new Vector(); head = n; this.len = len; this.scale = scale; this.col = col; } void draw(){ move(); stroke(0); fill(col); beginShape(QUADS); for(int i = 0; i < body.size(); i++){ Node n = (Node)body.get(i); float s = scale * 0.5; vertex(n.x()-s, n.y()-s, n.z()); vertex(n.x()+s, n.y()-s, n.z()); vertex(n.x()+s, n.y()+s, n.z()); vertex(n.x()-s, n.y()+s, n.z()); vertex(n.x(), n.y()-s, n.z()-s); vertex(n.x(), n.y()+s, n.z()-s); vertex(n.x(), n.y()+s, n.z()+s); vertex(n.x(), n.y()-s, n.z()+s); vertex(n.x()-s, n.y(), n.z()-s); vertex(n.x()+s, n.y(), n.z()-s); vertex(n.x()+s, n.y(), n.z()+s); vertex(n.x()-s, n.y(), n.z()+s); // This next line is a hack to stop the bodies ghosting // It just seemed easier that whilst I have the node to hand that I double check // it's unwalkable. n.walkable = false; } endShape(); pushMatrix(); translate(head.x(), head.y(), head.z()); box(scale * 1.5, scale * 1.5, scale * 1.5); popMatrix(); } void move(){ Vector path = aStar.getPath(head, egg.n); if(path.size() > 1){ body.add(0, head); head = (Node)path.get(path.size()-2); if(len < body.size()){ Node n = (Node)body.remove(body.size()-1); n.walkable = true; } } if(head == egg.n){ len += (int)random(5, 10); egg = new Egg(); } } }