ffmap-backend/html/pacman.js

73 lines
1.7 KiB
JavaScript
Raw Normal View History

2012-06-12 01:02:49 +02:00
function pacman() {
var angle = d3.scale.linear()
.domain([0, 1, 2, 3])
.range([0.01, Math.PI/4, 0.01, Math.PI/4])
2012-06-10 18:55:18 +02:00
2012-06-12 01:02:49 +02:00
d3.timer(pacman_animate)
var a = 0
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
var p = {x: 0, y: 0}
var pm = vis.append("path")
.style("fill", "#ff0")
.style("stroke", "#000")
.style("stroke-width", 2.5)
.style("stroke-linejoin", "round")
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
function pacman_animate() {
var size = force.size()
var nodes = force.nodes()
var n = nodes.length
if (n == 0)
return
a = (a + 0.10)%2
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
pm.attr("d", d3.svg.arc().innerRadius(0)
.outerRadius(24).endAngle(-angle(a) + Math.PI/2 + 2*Math.PI).startAngle(angle(a) + Math.PI/2))
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
var closest = null
var dd = Infinity;
for (i = 0; i < n; i++) {
var o = nodes[i]
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
var d = Math.pow((o.x - p.x),2) + Math.pow( o.y - p.y, 2)
if (d < dd) {
dd = d
closest = o
}
2012-06-10 18:54:16 +02:00
}
2012-06-12 01:02:49 +02:00
var dx = closest.x - p.x
var dy = closest.y - p.y
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
var d = Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2))
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
dx = dx/d
dy = dy/d
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
if (d>8) {
p.x += dx * 2
p.y += dy * 6
2012-06-10 18:54:16 +02:00
} else {
2012-06-12 01:02:49 +02:00
var snd;
if (closest.flags.client) {
snd = new Audio("pacman_eatfruit.wav")
} else {
snd = new Audio("pacman_eatghost.wav")
}
snd.play()
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
data.nodes = data.nodes.filter(function (d) {
return d.id != closest.id
})
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
data.links = data.links.filter(function (d) {
return d.target.id != closest.id && d.source.id != closest.id
})
update()
}
2012-06-10 18:54:16 +02:00
2012-06-12 01:02:49 +02:00
pm.attr("transform", "matrix(" + [dx, dy, -dy, dx, p.x, p.y].join(",") + ")")
}
2012-06-10 18:54:16 +02:00
}