001         package com.croftsoft.apps.mars.ai;
002    
003         import java.util.*;
004    
005         import com.croftsoft.core.lang.NullArgumentException;
006         import com.croftsoft.core.math.geom.Point2DD;
007         import com.croftsoft.core.math.geom.PointXY;
008         import com.croftsoft.core.math.geom.ShapeLib;
009         import com.croftsoft.core.util.NullIterator;
010    
011         /*********************************************************************
012         * Default TankOperator implementation.
013         *
014         * @version
015         *   2003-05-12
016         * @since
017         *   2003-04-17
018         * @author
019         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020         *********************************************************************/
021    
022         public final class  PlayerTankOperator
023           implements TankOperator
024         //////////////////////////////////////////////////////////////////////
025         //////////////////////////////////////////////////////////////////////
026         {
027    
028         /** milliseconds */
029         private static final long  AUTO_PILOT_DELAY = 15 * 1000;
030    
031         //
032    
033         private final TankOperator    autoPilotTankOperator;
034    
035         private final List            pathList;
036    
037         private final StateSpaceNode  stateSpaceNode;
038    
039         //
040    
041         private TankConsole  tankConsole;
042    
043         private boolean      autoPilotMode;
044    
045         private boolean      fireRequested;
046    
047         private PointXY      destination;
048    
049         private boolean      destinationRequested;
050    
051         private long         lastInputTime;
052    
053         //////////////////////////////////////////////////////////////////////
054         //////////////////////////////////////////////////////////////////////
055    
056         public  PlayerTankOperator ( TankOperator  autoPilotTankOperator )
057         //////////////////////////////////////////////////////////////////////
058         {
059           NullArgumentException.check (
060             this.autoPilotTankOperator = autoPilotTankOperator );
061    
062           pathList = new ArrayList ( 1 );
063    
064           stateSpaceNode = new StateSpaceNode ( );
065    
066           pathList.add ( stateSpaceNode );
067    
068           lastInputTime = System.currentTimeMillis ( );
069         }
070    
071         //////////////////////////////////////////////////////////////////////
072         //////////////////////////////////////////////////////////////////////
073    
074         public void  fire ( )
075         //////////////////////////////////////////////////////////////////////
076         {
077           fireRequested = true;
078         }
079    
080         public void  go ( PointXY  destination )
081         //////////////////////////////////////////////////////////////////////
082         {
083           destinationRequested = true;
084    
085           this.destination = destination;
086    
087           stateSpaceNode.setPointXY ( destination );
088         }
089    
090         public Iterator  getPath ( )
091         //////////////////////////////////////////////////////////////////////
092         {
093           if ( autoPilotMode )
094           {
095             return autoPilotTankOperator.getPath ( );
096           }
097    
098           if ( destination == null )
099           {
100             return NullIterator.INSTANCE;
101           }
102    
103           return pathList.iterator ( );
104         }
105    
106         //////////////////////////////////////////////////////////////////////
107         //////////////////////////////////////////////////////////////////////
108    
109         public void  setTankConsole ( TankConsole  tankConsole )
110         //////////////////////////////////////////////////////////////////////
111         {
112           NullArgumentException.check ( this.tankConsole = tankConsole );
113    
114           autoPilotTankOperator.setTankConsole ( tankConsole );
115         }
116    
117         public void  update ( double  timeDelta )
118         //////////////////////////////////////////////////////////////////////
119         {
120           if ( fireRequested || destinationRequested )
121           {
122             autoPilotMode = false;
123    
124             lastInputTime = System.currentTimeMillis ( );
125    
126             if ( fireRequested )
127             {
128               tankConsole.fire ( );
129    
130               fireRequested = false;
131             }
132    
133             if ( destinationRequested )
134             {
135               tankConsole.go ( destination );
136    
137               tankConsole.rotateTurret ( destination );
138    
139               destinationRequested = false;
140             }
141           }
142           else if ( autoPilotMode )
143           {
144             autoPilotTankOperator.update ( timeDelta );
145           }
146           else if (
147             System.currentTimeMillis ( ) > lastInputTime + AUTO_PILOT_DELAY )
148           {
149             autoPilotMode = true;
150           }
151         }
152    
153         //////////////////////////////////////////////////////////////////////
154         //////////////////////////////////////////////////////////////////////
155         }