litesoft
@ 950
litesoft / trunk / GWT_Sandbox / MultiModule / anywhere / src / org / litesoft / sandbox / anywhere / die / State.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
package org.litesoft.sandbox.anywhere.die; import java.util.*; import static org.litesoft.sandbox.anywhere.die.View.*; public class State { private Map<View, Integer> mViewValues = new EnumMap<View, Integer>( View.class ); private final Orientation mOrientation; /** * Initialize to Identifier 1?2 */ public State( Orientation pOrientation ) { mViewValues.put( Result, 1 ); mViewValues.put( Other, 6 ); mViewValues.put( North, 2 ); mViewValues.put( South, 5 ); if ( Orientation.L.equals( mOrientation = pOrientation ) ) { mViewValues.put( East, 3 ); mViewValues.put( West, 4 ); } else { mViewValues.put( West, 3 ); mViewValues.put( East, 4 ); } } public synchronized Identifier getIdentifier() { return new Identifier( mOrientation, getValue(), getValue( North ) ); } public int getValue() { return getValue( Result ); } public synchronized Integer getValue( View pView ) { return mViewValues.get( pView ); } public synchronized View getViewForValue( int pValue ) { for ( View zView : View.values() ) { if ( pValue == getValue( zView ) ) { return zView; } } return null; } public synchronized void adjustTo( Identifier pIdentifier ) { if ( pIdentifier == null ) { return; } if ( mOrientation != pIdentifier.getOrientation() ) { throw new IllegalStateException( "Orientation Incompatible" ); } makeResultEqual( pIdentifier.getFace() ); makeNorthEqual( pIdentifier.getNorthSide() ); } public void makeNorthResult() { rollToSide( North ); // Move Down } public void makeSouthResult() { rollToSide( South ); // Move Up } public void makeWestResult() { rollToSide( West ); // Move Right } public void makeEastResult() { rollToSide( East ); // Move Left } public void makeWestNorth() // Rotate Clockwise { rotateEastOrWestToNorth( West ); } public void makeEastNorth() // Rotate Counter-Clockwise { rotateEastOrWestToNorth( East ); } public void rotate180degrees() { moveSides( North, South, // East, West, // West, East, // South, North ); } public void rollOver() { moveSides( Result, Other, // Rotate around the East-West Axis North, South, // South, North, // Other, Result ); } private void rotateEastOrWestToNorth( View pFromSide ) { View zOpposite = pFromSide.opposite(); moveSides( pFromSide, North, // North, zOpposite, // zOpposite, South, // South, pFromSide ); } private void rollToSide( View pFromSide ) { View zOpposite = pFromSide.opposite(); moveSides( Other, pFromSide, // pFromSide, Result, // Result, zOpposite, // zOpposite, Other ); } private void makeResultEqual( int pValue ) { switch ( getViewForValue( pValue ) ) { case North: makeNorthResult(); break; case East: makeEastResult(); break; case West: makeWestResult(); break; case South: makeSouthResult(); break; case Other: rollOver(); break; default: // & Result break; } } private void makeNorthEqual( int pValue ) { switch ( getViewForValue( pValue ) ) { default: // & North break; case East: makeEastNorth(); break; case West: makeWestNorth(); break; case South: rotate180degrees(); break; case Other: makeNorthResult(); // Result changed! break; case Result: makeSouthResult(); // Result changed! break; } } private synchronized void moveSides( View... pFromToViewPairs ) { Map<View, Integer> zViewValues = new EnumMap<View, Integer>( mViewValues ); for ( int i = 0; i < pFromToViewPairs.length; ) { // **** Do NOT inline the following View zFromView = pFromToViewPairs[i++]; View zToView = pFromToViewPairs[i++]; zViewValues.put( zToView, mViewValues.get( zFromView ) ); // Copy Value "From" to "To" } mViewValues = zViewValues; } } |