view src/alice/test/topology/aquarium/AutoIncrement.java @ 138:87f1a30a8c82 working

change range automatic
author sugi
date Tue, 18 Sep 2012 11:46:38 +0900
parents 53aff28cde6b
children f995396f2af7
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 String key;
	private float min = -1.3f;
	private float max;
	
	public AutoIncrement(String key,int index){
		this.key = key;
		this.number.setKey("local","num");
		this.position.setKey("local", key,index);
	}

	@Override
	public void run() {
		max = this.number.asInteger()*2+1+0.3f;
		FishPoint fp = this.position.asClass(FishPoint.class);
		if (fp.getX()+0.01>max){
			fp.setXY(min, fp.getY());
		} else if (fp.getX()+0.01< min){
			fp.setXY(max, fp.getY());
		}
		else {
			fp.setXY(fp.getX()+0.01f, fp.getY());
		}
		
		ods.update("local", key, fp);
		synchronized(this){
			try {
				// TODO
				// Waiting should be done in Alice kernel
				// ids.create(CommandType.WAIT);
			 
				wait(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		new AutoIncrement(this.key,this.position.index);
	}
	
}