package net.mitloehner.flocki;

import java.util.Random;

public class Flocki {
    float x, y, dx, dy, tx = 0, ty = 0, w, h, speed = 5;
    Random rand;
    int marg = 30;

    Flocki() {
        rand = new Random();
    }

    public void init(float wi, float he) {
        w = wi;
        h = he;
        x = rand.nextFloat() * w;
        y = rand.nextFloat() * h;
    }

    public void run() {
        if (tx == 0 && ty == 0) {
            dx = (float)rand.nextDouble() * speed - speed/2;
            dy = (float)rand.nextDouble() * speed - speed/2;
        }
        if (x + dx < w-marg && x + dx > 0) x += dx;
        if (y + dy < h-marg && y + dy > 0) y += dy;
    }
    public void target(float x2, float y2) {
        tx = x2;
        ty = y2;
        float c = (float)Math.sqrt( square(tx - x) + square(ty - y));
        dx = speed * (tx - x) / c;
        dy = speed * (ty - y) / c;

    }
    float square(float x) { return x * x; }
}