Ejemplo n.º 1
0
        public void Update(float deltaTime)
        {
            var positions  = new Span <PositionComponent>(this.positionComponents);
            var velocities = new Span <VelocityComponent>(this.velocityComponents);

            for (int i = 0; i < this.length; i++)
            {
                var position = positions[i];

                position.Value += velocities[i].Value * deltaTime;

                var velocity = velocities[i];

                var x        = MathF.Select(1, -1, position.Value.X <this.minX || position.Value.X> this.maxX);
                var y        = MathF.Select(1, -1, position.Value.Y <this.minY || position.Value.Y> this.maxY);
                var multiply = new Vector2(x, y);

                velocity.Value *= multiply;

                position.Value = new Vector2(position.Value.X.Clamp(this.minX, this.maxX), position.Value.Y.Clamp(this.minY, this.maxY));

                positions[i]  = position;
                velocities[i] = velocity;
            }
        }
Ejemplo n.º 2
0
        private static void RunJob(
            float deltaTime,
            int sliceLength,
            Bounds bounds,
            Memory <PositionComponent> positionsSlice,
            Memory <VelocityComponent> velocitiesSlice)
        {
            var positions  = positionsSlice.Span;
            var velocities = velocitiesSlice.Span;

            for (int i = 0; i < sliceLength; i++)
            {
                var position = positions[i];

                position.Value += velocities[i].Value * deltaTime;

                var velocity = velocities[i];

                var x        = MathF.Select(1, -1, position.Value.X <bounds.MinX || position.Value.X> bounds.MaxX);
                var y        = MathF.Select(1, -1, position.Value.Y <bounds.MinY || position.Value.Y> bounds.MaxY);
                var multiply = new Vector2(x, y);

                velocity.Value *= multiply;

                position.Value = new Vector2(position.Value.X.Clamp(bounds.MinX, bounds.MaxX), position.Value.Y.Clamp(bounds.MinY, bounds.MaxY));

                positions[i]  = position;
                velocities[i] = velocity;
            }
        }
Ejemplo n.º 3
0
        public override void Update(float deltaTime)
        {
            this.position.Value += this.Value * deltaTime;

            var x        = MathF.Select(1, -1, position.Value.X <this.bounds.MinX || position.Value.X> this.bounds.MaxX);
            var y        = MathF.Select(1, -1, position.Value.Y <this.bounds.MinY || position.Value.Y> this.bounds.MaxY);
            var multiply = new Vector2(x, y);

            this.Value *= multiply;

            this.position.Value = new Vector2(position.Value.X.Clamp(this.bounds.MinX, this.bounds.MaxX), position.Value.Y.Clamp(this.bounds.MinY, this.bounds.MaxY));
        }
Ejemplo n.º 4
0
        public override void Initialize(GraphicsDeviceManager graphics)
        {
            base.Initialize(graphics);

            var velocityMagnitude = (float)this.random.NextDouble() * (MaxVelocity - MinVelocity) + MinVelocity;

            this.Velocity = this.random.NextVector2() * velocityMagnitude;

            this.VelocityModifier = (float)this.random.NextDouble() * (MaxModifier - MinModifier) + MinModifier;

            var red      = MathB.Select(0, byte.MaxValue, this.VelocityModifier < 0.0f);
            var green    = MathB.Select(0, byte.MaxValue, this.VelocityModifier >= 0.0f);
            var scaleMax = MathF.Select(MinModifier, MaxModifier, this.VelocityModifier >= 0.0f);
            var alpha    = (byte)(int)(128 * (this.VelocityModifier / scaleMax));

            this.spriteColor = new Color(red, green, byte.MinValue, alpha);
        }
Ejemplo n.º 5
0
        public virtual void Update(GameTime gameTime)
        {
            this.Position += this.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

            const int minX = 0;
            var       maxX = this.boundX;
            const int minY = 0;
            var       maxY = this.boundY;

            var x        = MathF.Select(1, -1, this.Position.X <minX || this.Position.X> maxX);
            var y        = MathF.Select(1, -1, this.Position.Y <minY || this.Position.Y> maxY);
            var multiply = new Vector2(x, y);

            this.Velocity *= multiply;

            this.Position = new Vector2(this.Position.X.Clamp(minX, maxX), this.Position.Y.Clamp(minY, maxY));
        }
Ejemplo n.º 6
0
        protected override void Initialize()
        {
            var displaySize = this.graphics.GraphicsDevice.DisplayMode.TitleSafeArea;

            this.graphics.PreferredBackBufferHeight = displaySize.Height - 100;
            this.graphics.PreferredBackBufferWidth  = displaySize.Width;
            this.graphics.ApplyChanges();

            const int minX = 0;
            var       maxX = this.graphics.PreferredBackBufferWidth;
            const int minY = 0;
            var       maxY = this.graphics.PreferredBackBufferHeight;

            this.moveSystem             = new MoveSystem(this.graphics, Scene.EntityCount, Scene.PositionComponents, Scene.VelocityComponents);
            this.velocityModifierSystem = new VelocityModifierSystem(Scene.EntityCount, Scene.VelocityModifierCount, Scene.PositionComponents,
                                                                     Scene.VelocityConstraintComponents, Scene.VelocityComponents, Scene.SizeComponents, Scene.VelocityModifierComponents);

            var random = new Random();

            for (int i = 0; i < Scene.EntityCount; i++)
            {
                var position = new PositionComponent();
                position.Value = new Vector2(random.Next(minX, maxX), random.Next(minY, maxY));
                Scene.PositionComponents[i] = position;

                var entityTypePredicate = i >= Constants.DotCount;
                var velMin = MathF.Select(Constants.Dot.MinVelocity, Constants.Bubble.MinVelocity, entityTypePredicate);
                var velMax = MathF.Select(Constants.Dot.MaxVelocity, Constants.Bubble.MaxVelocity, entityTypePredicate);

                Scene.VelocityConstraintComponents[i] = new VelocityConstraintComponent(velMin, velMax);

                var velocity = new VelocityComponent();
                velocity.Value = random.NextVelocity(velMin, velMax);
                Scene.VelocityComponents[i] = velocity;

                var velocityModifierValue = (float)random.NextDouble() * (Constants.Bubble.MaxModifier - Constants.Bubble.MinModifier) + Constants.Bubble.MinModifier;
                var scaleMax     = MathF.Select(Constants.Bubble.MinModifier, Constants.Bubble.MaxModifier, velocityModifierValue >= 0.0f);
                var bubbleAlpha  = (byte)(int)(128 * (velocityModifierValue / scaleMax));
                var bubbleColorR = MathB.Select(0, byte.MaxValue, velocityModifierValue < 0.0f);
                var bubbleColorG = MathB.Select(0, byte.MaxValue, velocityModifierValue >= 0.0f);

                var dotColors = new byte[3];
                random.NextBytes(dotColors);
                var colorR = MathB.Select(dotColors[0], bubbleColorR, entityTypePredicate);
                var colorG = MathB.Select(dotColors[1], bubbleColorG, entityTypePredicate);
                var colorB = MathB.Select(dotColors[2], byte.MinValue, entityTypePredicate);
                var alpha  = MathB.Select(byte.MaxValue, bubbleAlpha, entityTypePredicate);
                var index  = Math.Select(Constants.Sprites.Dot, Constants.Sprites.Bubble, entityTypePredicate);
                Scene.SpriteComponents[i] = new SpriteComponent(colorR, colorG, colorB, alpha, index);

                if (entityTypePredicate)
                {
                    var modifierIndex = i - Constants.DotCount;

                    Scene.SizeComponents[modifierIndex] = new SizeComponent(64);

                    Scene.VelocityModifierComponents[modifierIndex] = new VelocityModifierComponent(velocityModifierValue);
                }
            }

            base.Initialize();
        }
Ejemplo n.º 7
0
        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);
            }
        }