///////////////////////////////////////////////////////////////////////////////// // Copyright © 2008-2010 W. Kowarschick // // Permission is granted to copy, distribute and/or modify this document // under the terms of the WK License, Version 1.0 (WKL 1.0) or any later version // published by Wolfgang Kowarschick. ///////////////////////////////////////////////////////////////////////////////// package die.model { import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; import wk.mvcs.model.WK_Model; [Bindable] public class ModelDie extends WK_Model implements IModelDie { ///////////////////////////////////////////////////////////////////////////// // Private Members ///////////////////////////////////////////////////////////////////////////// private var v_state: String = ConstantsDice.NO_VALUE; private var v_value: uint = 0; private var v_timer: Timer = new Timer(ConstantsDice.DELAY, ConstantsDice.NUMBER_OF_ROLLS); private function mh_timer(event: Event): void { value = Math.floor(Math.random() * 6) + 1; } private function mh_timer_complete(event: Event): void { state = ConstantsDice.THROWN; } ///////////////////////////////////////////////////////////////////////////// // Constructor ///////////////////////////////////////////////////////////////////////////// public function ModelDie() { v_timer.addEventListener(TimerEvent.TIMER, mh_timer); v_timer.addEventListener(TimerEvent.TIMER_COMPLETE, mh_timer_complete); } ///////////////////////////////////////////////////////////////////////////// // Attributes ///////////////////////////////////////////////////////////////////////////// /** * The state of the die: * ROLLING, i.e., the die is currently rolling, * or THROWN, i.e., the die has been thrown * and until the next throw the value does not change, * or NO_VALUE, i.e. the die is in a box and no value * (0, to be more precisely) is displayed. */ public function get state(): String { return v_state; } public function set state(p_state: String): void { v_state = p_state; } /** * The value of the die: 1 .. 6 * and 0 (when state == NO_VALUE). * It can only be set if the die currently is not ROLLING. */ public function get value(): uint { return v_value; } public function set value(p_value: uint): void { if (1 <= p_value && p_value <= 6) v_value = p_value; else if (p_value == 0) { state = ConstantsDice.NO_VALUE; v_value = 0; } else throw new Error("The value " + p_value + " is greater than 6."); } ///////////////////////////////////////////////////////////////////////////// // Public Methods ///////////////////////////////////////////////////////////////////////////// /** * If the die is currently not ROLLING, it starts * ROLLING. For NUMBER_OF_ROLLS times * (with a delay time of DELAY ms after each roll) * a new random value is computed for the die. Thereafter the * state is switched to THROWN. */ public function rollTheDie(): void { if (v_state != ConstantsDice.ROLLING) { state = ConstantsDice.ROLLING; v_timer.reset(); v_timer.start(); }; } ///////////////////////////////////////////////////////////////////////////// // EOC ///////////////////////////////////////////////////////////////////////////// } } ///////////////////////////////////////////////////////////////////////////////// // EOF /////////////////////////////////////////////////////////////////////////////////