Main
using System;
using Sifteo;
using Libraries;


namespace App
{
    // Purpose:
    // Game cube.  Contains basics for game related cubes.  Derive from this for different cube types.
    public class PlayerCubeWrapper : GameCubeWrapper
    {
        // Current selection in the cube.
        public String m_selection;
   
        // Whether the cube has had its selection set.
        // A set selection shouldn't be changed until the game is reset.
        // Value is true if the selection is set, false if not.
        private bool m_selectionSet = false;
       
        // Whether the player has been eliminated from the current game.  Value is true if he has, false if not.
        public bool m_isEliminated = false;
       
        // Display game rules.
        private RulesDisplay m_rulesDisp;
   
        // If true, then the judge should be given the player count when the button is pressed.
        protected bool m_notifyJudgeOfCount = false;

        // Purpose:
        // Constructor.
        // app      -
        // cube     - Cube the properties are for.
        // cubeType - What type of cube it is.
        public PlayerCubeWrapper(RPSApp app,
                                 Cube cube,
                                 CubeType cubeType) : base(app, cube, cubeType)
        {
            // Set the initial image.
            if ((CubeType.e_player1 == m_cubeType) || (CubeType.e_player2 == m_cubeType) || (CubeType.e_player3 == m_cubeType) || (CubeType.e_player4 == m_cubeType))
            {
                SetSelection(m_noSelection);
                m_objs.GetAsSprite(m_spriteID).SetImage(null, true);
            }
        }

        // Purpose:
        // Get whether the cube has had its selection set.
        // Value is true if it has, false if not.
        public bool IsSelectionSet()
        {
            return m_selectionSet;
        }

        // Purpose:
        // Set the flag for weether the cube should tell the judge how many players are allowed.
        public void TellJudgeCountOnPress(bool notify)
        {
            m_notifyJudgeOfCount = notify;
        }
   
        // Purpose:
        // Set whether the cube is used to display the game rules.  Value is true if it should, false if not.
        public void SetUseGameRules(bool use)
        {
            if(use)
            {
                if(null == m_rulesDisp)
                {
                    m_rulesDisp = new RulesDisplay(this);
                    m_rulesDisp.GetRenderObj().SetLayer(1);
                    m_rulesDisp.GetRenderObj().SetRenderOrder(0);
                }
                else
                {
                    m_rulesDisp.GetRenderObj().m_needsRender = true;
                }
            }
            else
            {
                m_rulesDisp = null;
            }

            m_needRender = true;
        }

        // Purpose:
        // Update the renderer, adding render objects.
        // Do this in the update before a render.  Any sprites that can be rendered should go in here.
        // Sprites, etc should already be in the state they are for rendering.
        protected override void UpdateRenderer()
        {
            if(null != m_rulesDisp)
                m_rulesDisp.UseRenderer(m_renderer);

            // Text updated so the sprite has to render.
            if(m_renderer.ContainsRenderObj())
            {
                m_objs.GetAsSprite(m_spriteID).m_needsRender = true;
            }

            base.UpdateRenderer();
        }

        // Purpose:
        // Check if a new render is needed.  Override and check sprites/backgrounds/whatever.
        // Returns true if a render is needed, false if not.
        protected override bool CheckNeedsRender()
        {
            if(base.CheckNeedsRender())
            {
                return true;
            }
            else
            {
                if((null != m_rulesDisp) && m_rulesDisp.CheckNeedsRender())
                    return true;
            }

            return false;
        }

        // Handle updating anything needed for the cube.
        // If the cube is marked for render, add in anything that needs to be rendered.
        protected override void HandleUpdate()
        {
            base.HandleUpdate ();

            m_needRender = CheckNeedsRender();
            if(m_needRender)
            {
                if((null != m_rulesDisp) && (m_rulesDisp.CheckNeedsRender()))
                    m_renderer.Insert(m_rulesDisp.GetRenderObj());
            }
        }

        // Purpose:
        // Handle the button press for cubes.  Depending on the cube type and state, perform different actions.
        protected override void OnButton (Cube cube, bool pressed)
        {
            base.OnButton(cube, pressed);

            if (m_allowButtonPress)
            {
                if (!pressed)
                {
                    if(m_notifyJudgeOfCount)
                    {
                        // In player count selection state.  Tell the judge how many players there should be.
                        if(CubeType.e_player2 == m_cubeType)
                            m_app.GetJudgeWrapper().SetNumPlayers(2);
                        else if(CubeType.e_player3 == m_cubeType)
                            m_app.GetJudgeWrapper().SetNumPlayers(3);
                        else if(CubeType.e_player4 == m_cubeType)
                            m_app.GetJudgeWrapper().SetNumPlayers(4);
                        else
                            Log.Error("Invalid number of players");
                    }
                    else
                    {
                        // Update the current selection.
                        if(m_noSelection == m_selection)
                        {
                            // Random selection.
                            SetSelection(m_app.m_rules.RandomSelection(m_app.m_rand));
                        }
                        else
                        {  
                            // Next selection.
                            SetSelection(m_app.m_rules.NextSelection(m_selection));
                        }
                    }
                }
            }
        }
   
        // Purpose:
        // Set the current user selection in the cube.
        // Updates the displayed image to match the selection.
        //
        // selection - What selection type the cube should have.
        public void SetSelection (String selection)
        {
            m_selection = selection;
       
            if(m_noSelection == m_selection)
            {
                m_objs.GetAsSprite(m_spriteID).SetImage(m_app.m_images.m_playerChoose, true);
                m_allowButtonPress = true;
                m_selectionSet = false;
            }
            else
            {
                m_objs.GetAsSprite(m_spriteID).SetImage(m_app.m_rules.GetSelectionImage(m_selection), true);
            }
            m_objs.GetAsSprite(m_spriteID).m_needsRender = true;
        }
   
        // Purpose:
        // Event.  Handle shacking a cube.  Starts when shaking begins.  
        protected override void OnShakeStarted (Cube cube)
        {  
            if(m_app.IsPlayer(m_cubeType))
            {
                // Lock in the choice if needed.
                if ((m_noSelection != m_selection) && !m_selectionSet)
                {
                    m_selectionSet = true;
                    m_allowButtonPress = false;
                    m_objs.GetAsSprite(m_spriteID).SetImage(m_app.m_images.m_playerDecided, true); 
                }          
            }
        }
       
        // Purpose:
        // Tilting a cube with rules changes the rules displayed.  Tilt sideways to change the rules set viewed, tilt up/down to go to the next or previous page.
        protected override void OnTilt(Cube cube, int tiltX, int tiltY, int tiltZ)
        {
            if(null != m_rulesDisp)
            {
                if(0 == tiltX)
                    m_rulesDisp.ChangeRules(false, this);
                else if(2 == tiltX)
                    m_rulesDisp.ChangeRules(true, this);

                if(0 == tiltY)
                    m_rulesDisp.ChangePage(true, this);
                else if(2 == tiltY)
                    m_rulesDisp.ChangePage(false, this);
            }
        }
   
        // Purpose:
        // Render the cube.
        // paint - Value is true if the paint command should happen at the end of the function, false if not.
        public override void Render(bool paint)
        {
            base.Render(false);

            if(paint)
                m_cube.Paint();
        }
    }
}

 
Website copyright (c) 2012 Benjamin Millek, all rights reserved.