コード例 #1
0
 public void attachEdge(EdgeComponent inEdge = null, EdgeComponent outEdge = null)
 {
     if (inEdge)
     {
         InEdges.Add(inEdge);
     }
     if (outEdge)
     {
         OutEdges.Add(outEdge);
     }
 }
コード例 #2
0
        public static void UpdateParticle(ParticleManager <VelocityParticleInfo> .Particle particle, float dt)
        {
            Vector2 velocity = particle.UserInfo.Velocity;

            particle.Position += velocity * dt;
            particle.Rotation  = (float)Math.Atan2(velocity.Y, velocity.X);

            for (int i = 0; i < particle.UserInfo.EdgeEntities.Count; i++)
            {
                Entity             entity        = particle.UserInfo.EdgeEntities[i];
                TransformComponent transformComp = entity.GetComponent <TransformComponent>();
                EdgeComponent      edgeComp      = entity.GetComponent <EdgeComponent>();

                // If withing the bounds of the edge
                if (Math.Abs(particle.Position.X) <= Constants.Pong.EDGE_WIDTH / 2)
                {
                    // If top edge
                    if (edgeComp.Normal.Y < 0 &&
                        particle.Position.Y >= transformComp.Position.Y)
                    {
                        velocity.Y = -Math.Abs(velocity.Y);
                    }
                    else if (edgeComp.Normal.Y > 0 &&
                             particle.Position.Y <= transformComp.Position.Y)
                    {
                        velocity.Y = Math.Abs(velocity.Y);
                    }
                }
            }

            float speed = velocity.Length();
            float alpha = Math.Min(1, Math.Min(particle.PercentLife * 2, speed * dt));

            alpha *= alpha;

            particle.Color.A = (byte)(255 * alpha);

            particle.Scale.X = particle.UserInfo.LengthMultiplier * Math.Min(Math.Min(1, 0.2f * speed * dt + 0.1f), alpha);

            // If within tolerance, set the velocity to zero.
            // We don't have to use sqrt here, so we shouldn't
            if (Math.Abs(velocity.X) + Math.Abs(velocity.Y) < 0.00000000001f)
            {
                velocity = Vector2.Zero;
            }

            float speedMul = 0.9f; // Particles gradually slow down

            velocity *= speedMul;

            particle.UserInfo.Velocity = velocity;
        }
コード例 #3
0
        void processCollision(Entity ballEntity)
        {
            TransformComponent transformComp = ballEntity.GetComponent <TransformComponent>();
            BallComponent      ballComp      = ballEntity.GetComponent <BallComponent>();

            // Wrap ball angle
            while (ballComp.Direction < 0)
            {
                ballComp.Direction += 2 * MathHelper.Pi;
            }
            while (ballComp.Direction > 2 * MathHelper.Pi)
            {
                ballComp.Direction -= 2 * MathHelper.Pi;
            }

            BoundingRect ballAABB = new BoundingRect(transformComp.Position - ballComp.Bounds / 2,
                                                     transformComp.Position + ballComp.Bounds / 2);

            // Paddles
            foreach (Entity paddleEntity in _paddleEntities)
            {
                PaddleComponent    paddleComp          = paddleEntity.GetComponent <PaddleComponent>();
                TransformComponent paddleTransformComp = paddleEntity.GetComponent <TransformComponent>();

                BoundingRect paddleAABB = new BoundingRect(paddleTransformComp.Position - paddleComp.Bounds / 2,
                                                           paddleTransformComp.Position + paddleComp.Bounds / 2);

                if (ballAABB.Intersects(paddleAABB))
                {
                    if (!paddleComp.IgnoreCollisions)
                    {
                        {
                            Vector2 ballEdge = transformComp.Position
                                               + ballComp.Bounds * -paddleComp.Normal;
                            Vector2 paddleEdge = paddleTransformComp.Position
                                                 + paddleComp.Bounds * paddleComp.Normal;

                            Vector2 bouncePosition = (ballEdge + paddleEdge) / 2;
                            EventManager.Instance.QueueEvent(new BallBounceEvent(ballEntity, paddleEntity, bouncePosition));
                        }

                        // Determine alpha of ball relative to the paddle's heigh
                        float relativeY = transformComp.Position.Y - paddleTransformComp.Position.Y;
                        float alpha     = (relativeY + (paddleComp.Bounds.Y / 2)) / paddleComp.Bounds.Y;
                        alpha = MathHelper.Clamp(alpha, 0, 1);

                        // Determine new direction
                        float newDir = MathHelper.Lerp(Constants.Pong.PADDLE_BOUNCE_MIN,
                                                       Constants.Pong.PADDLE_BOUNCE_MAX,
                                                       alpha);
                        newDir = MathHelper.ToRadians(newDir);

                        // Set ball direction
                        if (paddleComp.Normal.X > 0)
                        {
                            ballComp.Direction = newDir;
                        }
                        else if (paddleComp.Normal.X < 0)
                        {
                            ballComp.Direction = MathHelper.Pi - newDir;
                        }

                        // Make sure ball does not get stuck inside paddle
                        paddleComp.IgnoreCollisions = true;
                    }
                }
                else
                {
                    paddleComp.IgnoreCollisions = false;
                }
            }
            // Edges
            foreach (Entity edgeEntity in _edgeEntities)
            {
                EdgeComponent      edgeComp          = edgeEntity.GetComponent <EdgeComponent>();
                TransformComponent edgeTransformComp = edgeEntity.GetComponent <TransformComponent>();

                BoundingRect edgeAABB = new BoundingRect(edgeTransformComp.Position.X - Constants.Pong.PLAYFIELD_WIDTH / 2,
                                                         edgeTransformComp.Position.Y - Constants.Pong.EDGE_HEIGHT / 2,
                                                         Constants.Pong.EDGE_WIDTH,
                                                         Constants.Pong.EDGE_HEIGHT);

                if (ballAABB.Intersects(edgeAABB))
                {
                    {
                        Vector2 ballEdge = transformComp.Position
                                           + ballComp.Bounds * -edgeComp.Normal;
                        Vector2 edgeEdge = edgeTransformComp.Position
                                           + new Vector2(Constants.Pong.EDGE_WIDTH,
                                                         Constants.Pong.EDGE_HEIGHT)
                                           * edgeComp.Normal;

                        Vector2 bouncePosition = (ballEdge + edgeEdge) / 2;
                        EventManager.Instance.QueueEvent(new BallBounceEvent(ballEntity, edgeEntity, bouncePosition));
                    }

                    // Determine directional vector of ball
                    Vector2 ballDirection = new Vector2((float)Math.Cos(ballComp.Direction),
                                                        (float)Math.Sin(ballComp.Direction));

                    // Determine reflection vector of ball
                    Vector2 ballReflectionDir = getReflectionVector(ballDirection, edgeComp.Normal);

                    // Set angle of new directional vector
                    ballComp.Direction = (float)Math.Atan2(ballReflectionDir.Y,
                                                           ballReflectionDir.X);
                }
            }
        }