view src/main/java/alice/test/topology/aquarium/AutoIncrement.java @ 655:1c93e82e05c6 default tip

fix timestamp
author suruga
date Sat, 17 Feb 2018 00:33:00 +0900
parents aefbe41fcf12
children 15eeb439830c
line wrap: on
line source

package alice.test.topology.aquarium;

import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;

public class AutoIncrement extends CodeSegment {

    private Receiver position = ids.create(CommandType.PEEK);
    private Receiver number = ids.create(CommandType.PEEK);
    private static final float MIN = -1.3f;

    public AutoIncrement(String key, int index) {
        this.number.setKey("maxsize");
        this.position.setKey(key, index);
    }

    @Override
    public void run() {
        float max = this.number.asInteger() * 2 - 1 + 0.3f;
        FishPoint fp = this.position.asClass(FishPoint.class);
        if (fp.getX() + 0.01 > max) {
            fp.setXYZ(MIN, fp.getY(), fp.getZ());
        } else if (fp.getX() + 0.01 < MIN) {
            fp.setXYZ(max, fp.getY(), fp.getZ());
        }
        else {
            fp.setXYZ(fp.getX() + 0.01f, fp.getY(), fp.getZ());
        }

        ods.update(position.key, fp);
        synchronized (this) {
            try {
                // TODO
                // Waiting should be done in Alice kernel
                // ids.create(CommandType.WAIT);

                wait(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        new AutoIncrement(this.position.key, this.position.index);
    }

}