Exemple #1
0
 public GameObject(string name)
 {
     m_Name           = name;
     m_ComponentFlags = 0;
     m_Position       = new PositionComponent();
     m_Sprite         = new SpriteComponent();
     m_WorldBounds    = new WorldBoundsComponent();
     m_Move           = new MoveComponent();
 }
Exemple #2
0
        public void UpdateSystem(double time, float deltaTime)
        {
            WorldBoundsComponent bounds = S_Objects[boundsId].m_WorldBounds;

            for (int i = 0, n = entities.Count; i < n; i++)
            {
                PositionComponent pos  = S_Objects[i].m_Position;
                MoveComponent     move = S_Objects[i].m_Move;

                // update position based on movement velocity & delta time
                pos.x += move.velx * deltaTime;
                pos.y += move.vely * deltaTime;

                // check against world bounds; put back onto bounds and mirror the velocity component to "bounce" back
                if (pos.x < bounds.xMin)
                {
                    move.velx = -move.velx;
                    pos.x     = bounds.xMin;
                }
                if (pos.x > bounds.xMax)
                {
                    move.velx = -move.velx;
                    pos.x     = bounds.xMax;
                }
                if (pos.y < bounds.yMin)
                {
                    move.vely = -move.vely;
                    pos.y     = bounds.yMin;
                }
                if (pos.y > bounds.yMax)
                {
                    move.vely = -move.vely;
                    pos.y     = bounds.yMax;
                }
            }
        }
        public CompositionGame()
        {
            PerfMon.InitializeStarted();

            this.graphics = new GraphicsDeviceManager(this);
            this.Content.RootDirectory = "Content";
            this.IsMouseVisible        = true;
            this.IsFixedTimeStep       = false;

            this.texture2Ds = new Texture2D[2];
            var worldBoundsGameObject = new GameObject("World Bounds");
            var worldBoundsComponent  = new WorldBoundsComponent(this.graphics);

            worldBoundsGameObject.AddComponent(worldBoundsComponent);
            s_GameObjects.Add(worldBoundsGameObject);

            var random = new Random();

            for (var i = 0; i < BubbleCount; i++)
            {
                var gameObject = new GameObject($"Bubble #{i}");

                var positionComponent = new PositionComponent(random);
                gameObject.AddComponent(positionComponent);

                var velocityComponent = new VelocityComponent(random, Bubble.MinVelocity, Bubble.MaxVelocity);
                gameObject.AddComponent(velocityComponent);

                var velocityModifier = (float)random.NextDouble() * (Bubble.MaxModifier - Bubble.MinModifier) + Bubble.MinModifier;

                var spriteComponent = new SpriteComponent();
                spriteComponent.ColorR = MathB.Select(0, byte.MaxValue, velocityModifier < 0.0f);
                spriteComponent.ColorG = MathB.Select(0, byte.MaxValue, velocityModifier >= 0.0f);
                spriteComponent.ColorB = byte.MinValue;
                var scaleMax = MathF.Select(Bubble.MinModifier, Bubble.MaxModifier, velocityModifier >= 0.0f);
                spriteComponent.Alpha = (byte)(int)(128 * (velocityModifier / scaleMax));
                spriteComponent.Index = Sprites.Bubble;
                gameObject.AddComponent(spriteComponent);

                var velocityModifierComponent = new VelocityModifierComponent();
                velocityModifierComponent.VelocityModifier = velocityModifier;
                gameObject.AddComponent(velocityModifierComponent);

                var sizeComponent = new SizeComponent();
                sizeComponent.Size = 64;
                gameObject.AddComponent(sizeComponent);

                s_GameObjects.Add(gameObject);
            }

            for (int i = 0; i < DotCount; i++)
            {
                var gameObject = new GameObject($"Dot #{i}");

                var positionComponent = new PositionComponent(random);
                gameObject.AddComponent(positionComponent);

                var velocityComponent = new VelocityComponent(random, Dot.MinVelocity, Dot.MaxVelocity);
                gameObject.AddComponent(velocityComponent);

                var spriteComponent = new SpriteComponent();
                var colors          = new byte[3];
                random.NextBytes(colors);
                spriteComponent.ColorR = colors[0];
                spriteComponent.ColorG = colors[1];
                spriteComponent.ColorB = colors[2];
                spriteComponent.Alpha  = byte.MaxValue;
                spriteComponent.Index  = Sprites.Dot;
                gameObject.AddComponent(spriteComponent);

                var modifyVelocityComponent = new ModifyVelocityComponent();
                gameObject.AddComponent(modifyVelocityComponent);

                s_GameObjects.Add(gameObject);
            }
        }