📄 gatherrule.java
字号:
package example.boids;
class GatherRule implements BoidRule, BoidsConstants{
private int totalX;
private int totalY;
private int count;
private Boid currentBoid;
private static final int ATTRACTION_FACTOR = (1 << FIXED_POINT_SHIFT) / 40;
GatherRule()
{
}
public void reset(Boid boid)
{
totalX = 0;
totalY = 0;
count = 0;
currentBoid = boid;
}
public void applyTo(Boid boid)
{
totalX += boid.getX();
totalY += boid.getY();
count++;
}
public int getVx()
{
int vx = 0;
if (count > 0)
{
vx = ((totalX / count - currentBoid.getX()) * ATTRACTION_FACTOR)
>> FIXED_POINT_SHIFT;
}
return vx;
}
public int getVy()
{
int vy = 0;
if (count > 0)
{
vy = ((totalY / count - currentBoid.getY()) * ATTRACTION_FACTOR)
>> FIXED_POINT_SHIFT;
}
return vy;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -