Main
using System;
using System.Collections;
using System.Collections.Generic;
using Sifteo;

namespace Libraries
{
    // Purpose:
    // Wrapper class to use for a cube.
    // Handles generic settings that most cubes would need, like background color and whether it needs to render.
    // This of this as the starting point of a cube. Derive for adding more functionality.
    public class BaseCubeWrapper
    {  
        // Used to seed the random number generator.  This has to be done once.
        // For this application, wait until a button is pressed.  The first time a button gets pressed the current tick count for that cube will be used to seed the random
        // number generator used by the entire application.  Will only be done once.
        private static bool m_needSeedRand = true;
        private static int m_randSeed = 0;
       
        // Store the application the cube is in.
        public BasicApp m_a;

        // Stores the cube that is contained in the wrapper.
        public Cube m_cube;
   
        // This flag tells the wrapper to redraw the current image on the cube. (See Tick, below).
        public bool m_needRender = false;
   
        // Wheither user input from the button is allowed.  Value is true if it is, false if not.
        public bool m_allowButtonPress = false;
   
        // Whether the fill color should be used at the start of a render.  Value is true if it should, false if not.
        public bool m_useFillColor = true;

        // Color to use for the cube background.
        public Color m_bgColor = Color.Black;
   
        // Tag for wheither the cube contained in the wrapper is actually usable.  Use when you have more cubes than are needed and want to ignore some.
        // Value should be true to ignore, false to not.  All ignoring must be handled in derived objects.  This is simply here for conveniance.
        public bool m_ignore = false;

        // If true, every other render frame is ignored.  Prevents input blocking.
        protected bool m_useRenderSkip = false;

        // Used in combination with m_useRenderSkip.  If true the next render frame will be ignored.
        private bool m_skipRender = false;
   

        // Purpose:
        // Constructor.
        // app      - Application for easy access.
        // cube     - Cube the properties are for.
        public BaseCubeWrapper(BasicApp app,
                               Cube cube)
        {
            m_a = app;
            m_cube = cube;
            m_cube.userData = this;
   
            // Attach event handlers for button and accelerometer actions.
            m_cube.ButtonEvent += OnButton;
            m_cube.ShakeStartedEvent += OnShakeStarted;
            m_cube.ShakeStoppedEvent += OnShakeStopped;
            m_cube.TiltEvent += OnTilt;
        }
   
        // ## Button ##
        // This is a handler for the Button event. It is triggered when a cube's
        // face button is either pressed or released. The `pressed` argument
        // is true when you press down and false when you release.
        protected virtual void OnButton (Cube cube, bool pressed)
        {
            if(m_needSeedRand)
            {
                m_needSeedRand = false;
                m_a.m_rand = new Random(m_randSeed);
            }
        }
   
        // ## Shake Started ##
        // This is a handler for the ShakeStarted event. It is triggered when the
        // player starts shaking a cube. When the player stops shaking, a
        // corresponding ShakeStopped event will be fired (see below).
        //
        // Note: while a cube is shaking, it will still fire tilt and flip events
        // as its internal accelerometer goes around and around. If your game wants
        // to treat shaking separately from tilting or flipping, you need to add
        // logic to filter events appropriately.
        protected virtual void OnShakeStarted (Cube cube)
        {
        }
   
        // ## Shake Stopped ##
        // This is a handler for the ShakeStarted event. It is triggered when the
        // player stops shaking a cube. The `duration` argument tells you
        // how long (in milliseconds) the cube was shaken.
        protected virtual void OnShakeStopped(Cube cube, int duration)
        {
        }
       
        // Purpose:
        // Event to use for tilting the cube.
        protected virtual void OnTilt(Cube cube, int tiltX, int tiltY, int tiltZ)
        {
        }
   
        // Purpose:
        // Render the cube.
        // paint - Value is true if the paint command should happen at the end of the function, false if not.
        public virtual void Render(bool paint)
        {
   
            if(m_useFillColor)
                m_cube.FillScreen(m_bgColor);
           
            if(paint)
                m_cube.Paint();
        }

        // Handle any updating that needs to occur.  Happens before rendering.
        protected virtual void HandleUpdate()
        {
        }
   
        // Purpose:
        // Update/render.
        public virtual void Tick()
        {
            // Increment the random seed.
            m_randSeed += 1;
   
            HandleUpdate();
           
            // Handle skipping a render.
            // Rendering each frame blocks input, so this will allow the regular game logic to progress without rendering blocking it.
            bool allowRender = true;

            if(m_useRenderSkip)
            {
                if(m_skipRender)
                {
                    m_skipRender = false;
                    allowRender = false;
                }
                else
                {
                    m_skipRender = true;
                }
            }

            // Redraw the image on the cube.
            if (allowRender && m_needRender)
            {
                m_needRender = false;
                Render(true);
            }  
        }
    }
}

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