001         package com.croftsoft.apps.cyborg;
002         
003         import java.util.*;
004         
005         import com.croftsoft.core.lang.NullArgumentException;
006         import com.croftsoft.core.math.MathConstants;
007         
008         /*********************************************************************
009         * Simulates user input.
010         * 
011         * @version
012         *   $Id: CyborgOperator.java,v 1.10 2008/04/19 21:30:58 croft Exp $
013         * @since
014         *   2005-08-24
015         * @author
016         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
017         *********************************************************************/
018    
019         public final class  CyborgOperator
020         //////////////////////////////////////////////////////////////////////
021         //////////////////////////////////////////////////////////////////////
022         {
023           
024         private static final int  ARRAY_LENGTH = 1 + ( int )
025           ( ( CyborgConfig.OPERATOR_DELAY_MILLIS * CyborgConfig.FRAME_RATE )
026           / MathConstants.MILLISECONDS_PER_SECOND ); 
027         
028         //
029           
030         private final CyborgModel  cyborgModel;
031         
032         private final Random       random;
033         
034         private final double [ ]
035           arrayX,
036           arrayY,
037           targetCenterXs,
038           targetCenterYs;
039         
040         //
041         
042         private int  index;
043           
044         //////////////////////////////////////////////////////////////////////
045         //////////////////////////////////////////////////////////////////////
046         
047         public  CyborgOperator ( CyborgModel  cyborgModel )
048         //////////////////////////////////////////////////////////////////////
049         {
050           NullArgumentException.check (
051             this.cyborgModel = cyborgModel );
052           
053           random = new Random ( );
054           
055           arrayX = new double [ ARRAY_LENGTH ];
056           
057           arrayY = new double [ ARRAY_LENGTH ];
058           
059           targetCenterXs = new double [ ARRAY_LENGTH ];
060           
061           targetCenterYs = new double [ ARRAY_LENGTH ];
062           
063           
064         }
065         
066         //////////////////////////////////////////////////////////////////////
067         //////////////////////////////////////////////////////////////////////
068         
069         public void  update ( )
070         //////////////////////////////////////////////////////////////////////
071         {
072           arrayX [ index ] = cyborgModel.getX ( );
073           
074           arrayY [ index ] = cyborgModel.getY ( );
075           
076           targetCenterXs [ index ] = cyborgModel.getTargetCenterX ( );
077           
078           targetCenterYs [ index ] = cyborgModel.getTargetCenterY ( );
079           
080           index++;
081           
082           if ( index == ARRAY_LENGTH )
083           {
084             index = 0;
085           }
086           
087           double  x = arrayX [ index ];
088           
089           double  y = arrayY [ index ];
090           
091           double  targetCenterX = targetCenterXs [ index ];
092           
093           double  targetCenterY = targetCenterYs [ index ];
094           
095           double  aimX = cyborgModel.getAimX ( );
096           
097           double  aimY = cyborgModel.getAimY ( );
098           
099           double  deltaX
100             = CyborgConfig.OPERATOR_DELTA * ( targetCenterX - x );
101           
102           double  deltaY
103             = CyborgConfig.OPERATOR_DELTA * ( targetCenterY - y );
104           
105           aimX += deltaX; // + 0.01 * ( 2.0 * random.nextDouble ( ) - 1.0 );       
106           
107           aimY += deltaY; // + 0.01 * ( 2.0 * random.nextDouble ( ) - 1.0 );
108           
109           if ( aimX < -1.0 )
110           {
111             aimX = -1.0;
112           }
113           else if ( aimX > 1.0 )
114           {
115             aimX = 1.0;
116           }
117           
118           if ( aimY < -1.0 )
119           {
120             aimY = -1.0;
121           }
122           else if ( aimY > 1.0 )
123           {
124             aimY = 1.0;
125           }       
126           
127           cyborgModel.setAimX ( aimX );
128           
129           cyborgModel.setAimY ( aimY );
130         }
131         
132         //////////////////////////////////////////////////////////////////////
133         //////////////////////////////////////////////////////////////////////
134         }