Пример #1
0
        public override void Update(float elapsedSeconds)
        {
            foreach (Entity e in this.actives.Values)
            {
                TransformComponent transform = e.GetComponent <TransformComponent>();
                MotionComponent    motion    = e.GetComponent <MotionComponent>();
                TargetComponent    target    = e.GetComponent <TargetComponent>();

                System.Diagnostics.Debug.Assert(transform != null, "PositionComponent not found");
                System.Diagnostics.Debug.Assert(motion != null, "VelocityComponent not found");
                System.Diagnostics.Debug.Assert(target != null, "TargetComponent not found");

                if (EntityWorld.EntityManager.IsAlive(target.TargetId))
                {
                    TransformComponent targetTransform = this.EntityWorld.ComponentManager.GetComponent <TransformComponent>(target.TargetId);

                    if (targetTransform != null)
                    {
                        Vector2 delta = targetTransform.Position - transform.Position;
                        float   destinationDirection = VectorExtensions.Vector2ToAngle(delta);
                        int     accelFactor          = 10;
                        float   newRotation          = MathExtensions.LerpAngle(MathHelper.WrapAngle(transform.Rotation), MathHelper.WrapAngle(destinationDirection), /*0.1f*/ 2 * elapsedSeconds);
                        transform.Rotation = newRotation;
                        motion.AddAcceleration(VectorExtensions.AngleToVector2(transform.Rotation) * GameConfig.Projectile.Velocity * accelFactor);
                        //motion.AddAngularAcceleration((destinationDirection - transform.Rotation) * 0.5f);
                    }
                }
                //else
                //{
                //    //target lost
                //    motion.AddAcceleration(VectorExtensions.AngleToVector2(transform.Rotation) * GameConfig.Projectile.Velocity * accelFactor);
                //}
            }
        }
        private bool RepellFromBorderInward(Entity e, float acceleration)
        {
            TransformComponent transform = e.GetComponent <TransformComponent>();
            MotionComponent    motion    = e.GetComponent <MotionComponent>();

            motion.AddAcceleration(Vector2.Normalize(GameConfig.PlayArea.Center.ToVector2() - transform.Position) * acceleration);

            return(true);
        }
Пример #3
0
        public override void Update(float elapsedSeconds)
        {
            foreach (Entity e in this.actives.Values)
            {
                TransformComponent    transform    = e.GetComponent <TransformComponent>();
                MotionComponent       motion       = e.GetComponent <MotionComponent>();
                AccelerationComponent acceleration = e.GetComponent <AccelerationComponent>();
                IntentComponent       intent       = e.GetComponent <IntentComponent>();

                System.Diagnostics.Debug.Assert(transform != null, "PositionComponent not found");
                System.Diagnostics.Debug.Assert(motion != null, "VelocityComponent not found");
                System.Diagnostics.Debug.Assert(acceleration != null, "MotionComponent not found");
                System.Diagnostics.Debug.Assert(intent != null, "IntentComponent not found");

                if (intent.IntentManager.HasIntent(Intent.Accelerate))
                {
                    // add force to center of object;
                    // TODO: add rocketengine/boostercomponent with offset from origin
                    motion.AddAcceleration(VectorExtensions.AngleToVector2(transform.Rotation) * acceleration.AccelerationFactor);
                }

                if (intent.IntentManager.HasIntent(Intent.Decelerate))
                {
                    motion.Drag = 0.05f;
                }
                else
                {
                    motion.Drag = 0.005f; //stop after a while
                }

                if (intent.IntentManager.HasIntent(Intent.RotateLeft))
                {
                    motion.AngularDrag = 0f;
                    motion.AddAngularAcceleration(-acceleration.RotationAccelerationFactor);
                }

                if (intent.IntentManager.HasIntent(Intent.RotateRight))
                {
                    motion.AngularDrag = 0f;
                    motion.AddAngularAcceleration(acceleration.RotationAccelerationFactor);
                }

                if (!(intent.IntentManager.HasIntent(Intent.RotateLeft) || intent.IntentManager.HasIntent(Intent.RotateRight)))
                {
                    motion.AngularDrag = 0.75f;
                }
            }
        }