001 package model.fish; 002 003 import model.fish.display.NarrowFishDisplay; 004 import model.ILambda; 005 import sysModel.fish.AFish; 006 import sysModel.env.IBlockedCommand; 007 import sysModel.env.IOpenCommand; 008 009 import java.awt.*; 010 011 /** 012 * Class for a darter fish. 013 * 014 * @author Mathias G. Ricken 015 */ 016 public class DarterFish extends AFish { 017 /** 018 * Create a new darter fish. 019 */ 020 public DarterFish(Color fishColor) { 021 super(fishColor, NarrowFishDisplay.Singleton); 022 } 023 024 /** 025 * Carry out movement behavior for the fish. 026 */ 027 protected void move() { 028 // attempt to move forward 029 tryMoveFwd(new IBlockedCommand() { 030 public Object apply(Object param) { 031 // could not move forward at all 032 // turn PI radians 033 turnRight(Math.PI); 034 return null; 035 } 036 }, 037 new IOpenCommand() { 038 public Object apply(Object param) { 039 // the field ahead is open, move 040 ((ILambda)param).apply(null); 041 // attempt to move forward again 042 tryMoveFwd(new IBlockedCommand() { 043 public Object apply(Object param) { 044 // could not move forward any more 045 // do nothing 046 return null; 047 } 048 }, 049 new IOpenCommand() { 050 public Object apply(Object param) { 051 // second field ahead is open, move 052 ((ILambda)param).apply(null); 053 return null; 054 } 055 }); 056 return null; 057 } 058 }); 059 } 060 }