/Flash/NoisyShuffleboard/src/noisy/ShuffleRock.as

https://github.com/rmmccann/TP-data-games · ActionScript · 212 lines · 150 code · 33 blank · 29 comment · 6 complexity · 14c79ba3b292715bdfdea768fb5ac233 MD5 · raw file

  1. // ShuffleRock.as
  2. // Copyright (c) 2012 by University of Massachusetts and contributors
  3. // Project information: http://srri.umass.edu/datagames/
  4. // Released under the MIT License http://www.opensource.org/licenses/mit-license.php
  5. package noisy
  6. {
  7. import flash.display.Sprite;
  8. import mx.controls.Text;
  9. import spark.components.Label;
  10. import spark.core.SpriteVisualElement;
  11. public class ShuffleRock extends SpriteVisualElement
  12. {
  13. public static const COLOR_FLAG:Number = 0x08; // Bitflag 1 = Red else Blue.
  14. public static const PATTERN_FLAG:Number = 0x04; // Bitflag 1 = Striped else Solid.
  15. public static const SHAPE_FLAG:Number = 0x02; // Bitflag 1 = Ball else Cube.
  16. public static const SIZE_FLAG:Number = 0x01; // Bitflag 1 = Small else Large.
  17. public static const COLOR_RED:uint = 0xe02234; // Color red value.
  18. public static const COLOR_BLUE:uint = 0x1a6cc8; // Color blue value.
  19. public static const COLOR_RED_PALER:uint = 0xf4b5bb; // Color paler red value.
  20. public static const COLOR_BLUE_PALER:uint = 0xb4d2f5; // Color paler blue value.
  21. public static const COLOR_STRING:String = "Color"; // Color string.
  22. public static const PATTERN_STRING:String = "Pattern"; // Pattern string.
  23. public static const SHAPE_STRING:String = "Shape"; // Shape string.
  24. public static const SIZE_STRING:String = "Size"; // Size string.
  25. public static const COLOR_RED_STRING:String = "Red"; // Color string: Red
  26. public static const COLOR_BLUE_STRING:String = "Blue"; // Color string: Blue
  27. public static const PATTERN_STRIPED_STRING:String= "Striped"; // Pattern string: Striped
  28. public static const PATTERN_SOLID_STRING:String = "Solid"; // Pattern string: Solid
  29. public static const SHAPE_BALL_STRING:String = "Ball"; // Shape string: Ball
  30. public static const SHAPE_CUBE_STRING:String = "Cube"; // Shape string: Cube
  31. public static const SIZE_SMALL_STRING:String = "Small"; // Size string: Small
  32. public static const SIZE_LARGE_STRING:String = "Large"; // Size string: Large
  33. public static const RADIUS_SIZE_SMALL:uint = 5; // Radius for small rock.
  34. public static const RADIUS_SIZE_LARGE:uint = 7; // Radius for large rock.
  35. public static const OUTLINE_THICKNESS:uint = 1; // Thickness of outline around rock.
  36. private var mAttributeFlags:uint; // Originally, all flags live here, but added the following to ease sorting.
  37. public var mColor:Boolean; // Color flag: true = Red, false = Blue.
  38. public var mPattern:Boolean; // Pattern flag: true = Striped, false = Solid.
  39. public var mShape:Boolean; // Shape flag: true = Ball, false = Cube.
  40. public var mSize:Boolean; // Size flag: true = Small, false = Large.
  41. private var mXPosition:uint; // Stores X position of rock.
  42. private var mYPosition:uint; // Stores Y position of rock.
  43. private var mBody:Sprite; // Used for drawing the body of the rock.
  44. // Constructor takes in parameters indicating the attribute flags that control 4 attributes (color, pattern,
  45. // shape, and size), x position, y position, and status as rock currently being played.
  46. public function ShuffleRock(iAttributeFlags:Number = 0, iX:uint = 0, iY:uint = 0, iCurrent:Boolean = true)
  47. {
  48. super();
  49. // Originally, all flags lived in attributeFlags, but added individual flags to ease sorting.
  50. mAttributeFlags = iAttributeFlags;
  51. mColor = (iAttributeFlags & COLOR_FLAG) == COLOR_FLAG;
  52. mPattern = (iAttributeFlags & PATTERN_FLAG) == PATTERN_FLAG;
  53. mShape = (iAttributeFlags & SHAPE_FLAG) == SHAPE_FLAG;
  54. mSize = (iAttributeFlags & SIZE_FLAG) == SIZE_FLAG;
  55. mXPosition = iX;
  56. mYPosition = iY;
  57. mBody = new Sprite;
  58. this.hideMe(); // Remain hidden until drawn.
  59. this.addChild(mBody); // body must be child of Shufflerock to display.
  60. }
  61. // getRadius returns the radius of the rock based on it size.
  62. public function getRadius():uint
  63. {
  64. return mSize ? RADIUS_SIZE_SMALL : RADIUS_SIZE_LARGE;
  65. }
  66. // getRadius returns the radius of the rock based on it size.
  67. public function getCircumference():uint
  68. {
  69. var radius:uint = mSize ? RADIUS_SIZE_SMALL : RADIUS_SIZE_LARGE;
  70. return 2 * Math.PI * radius;
  71. }
  72. // setPosition() sets the x and y position of the ShuffleRock.
  73. public function setPosition(iX:uint, iY:uint):void
  74. {
  75. mXPosition = iX;
  76. mYPosition = iY;
  77. }
  78. // setPositionX() sets the x position of the ShuffleRock.
  79. public function setPositionX(iX:uint):void
  80. {
  81. mXPosition = iX;
  82. }
  83. // setPositionY() sets the y position of the ShuffleRock.
  84. public function setPositionY(iY:uint):void
  85. {
  86. mYPosition = iY;
  87. }
  88. // getPositionX() gets the x position of the ShuffleRock.
  89. public function getPositionX():uint
  90. {
  91. return mXPosition;
  92. }
  93. // getPositionY() gets the Y position of the ShuffleRock.
  94. public function getPositionY():uint
  95. {
  96. return mYPosition;
  97. }
  98. // getColor() returns the string describing the rock's color.
  99. public function getColor():String
  100. {
  101. return mColor ? COLOR_RED_STRING : COLOR_BLUE_STRING;
  102. }
  103. // getPattern() returns the string describing the rock's pattern.
  104. public function getPattern():String
  105. {
  106. return mPattern ? PATTERN_STRIPED_STRING : PATTERN_SOLID_STRING;
  107. }
  108. // getShape() returns the string describing the rock's shape.
  109. public function getShape():String
  110. {
  111. return mShape ? SHAPE_BALL_STRING : SHAPE_CUBE_STRING;
  112. }
  113. // getSize() returns the string describing the rock's size.
  114. public function getSize():String
  115. {
  116. return mSize ? SIZE_SMALL_STRING : SIZE_LARGE_STRING;
  117. }
  118. // setColor sets the rock's color equal to iNewColor.
  119. public function setColor(iNewColor:Boolean):Boolean
  120. {
  121. var oldColor:Boolean = mColor;
  122. mColor = iNewColor;
  123. return oldColor;
  124. }
  125. // setPattern sets the rock's pattern equal to iNewPattern.
  126. public function setPattern(iNewPattern:Boolean):Boolean
  127. {
  128. var oldPattern:Boolean = mPattern;
  129. mPattern = iNewPattern;
  130. return oldPattern;
  131. }
  132. // setShape sets the rock's shape equal to iNewShape.
  133. public function setShape(iNewShape:Boolean):Boolean
  134. {
  135. var oldShape:Boolean = mShape;
  136. mShape = iNewShape;
  137. return oldShape;
  138. }
  139. // setSize sets the rock's size equal to iNewSize.
  140. public function setSize(iNewSize:Boolean):Boolean
  141. {
  142. var oldSize:Boolean = mSize;
  143. mSize = iNewSize;
  144. return oldSize;
  145. }
  146. // hideMe() make the rock invisible.
  147. public function hideMe():void
  148. {
  149. this.visible = false;
  150. }
  151. // showMe() makes the rock visible.
  152. public function showMe():void
  153. {
  154. this.visible = true;
  155. }
  156. // drawMe() draws the rock using current settings.
  157. public function drawMe():void
  158. {
  159. var bodyColor:uint = mColor ? COLOR_RED : COLOR_BLUE;
  160. var lineColor:uint = mColor ? COLOR_RED_PALER : COLOR_BLUE_PALER;
  161. var radius:uint = mSize ? RADIUS_SIZE_SMALL : RADIUS_SIZE_LARGE;
  162. var diameter:uint = radius * 2;
  163. mBody.graphics.clear();
  164. mBody.graphics.beginFill(bodyColor);
  165. // Draw circle if shape is a sphere (this.shape == true), else draw square (this.shape == false).
  166. if (mShape)
  167. mBody.graphics.drawCircle(mXPosition, mYPosition, radius);
  168. else
  169. // Shift starting x and y drawing position so that square is drawn with center at x, y.
  170. mBody.graphics.drawRect(mXPosition - radius, mYPosition - radius, diameter, diameter);
  171. // Draw stripe if striped rock (this.mPattern == true), nothing for
  172. // solid (this.mPattern == false).
  173. mBody.graphics.beginFill(lineColor);
  174. if (mPattern)
  175. mBody.graphics.drawRect(mXPosition - radius, mYPosition - 1, diameter, 2);
  176. this.showMe();
  177. }
  178. }
  179. }