Esempio n. 1
0
        public override void Update()
        {
            // Movement.
            var movementVector3 = new Vector3(
                Input.CheckButton(LeftButton).ToInt() - Input.CheckButton(RightButton).ToInt(),
                Input.CheckButton(UpButton).ToInt() - Input.CheckButton(DownButton).ToInt(),
                0
                );

            movementVector3 = Vector3.Transform(
                movementVector3,
                Matrix.CreateRotationZ(MathHelper.ToRadians(Camera.Rotation))
                );         // Rotating by the camera's rotation, so camera will always move relatively to screen.

            var rotatedMovementVector = new Vector2(movementVector3.X, movementVector3.Y);

            Camera.Position += TimeKeeper.GlobalTime(_cameraSpeed / Camera.Zoom) * rotatedMovementVector;
            // Movement.

            // Zoom.
            var zoomDirection = Input.CheckButton(ZoomInButton).ToInt() - Input.CheckButton(ZoomOutButton).ToInt();

            Camera.Zoom += TimeKeeper.GlobalTime(_zoomSpeed) * zoomDirection;

            if (Camera.Zoom < _minZoom)
            {
                Camera.Zoom = _minZoom;
            }
            if (Camera.Zoom > _maxZoom)
            {
                Camera.Zoom = _maxZoom;
            }
            // Zoom.
        }
Esempio n. 2
0
 public override void Update(List <Component> components)
 {
     foreach (BotComponent bot in components)
     {
         var actor = bot.Owner.GetComponent <ActorComponent>();
         actor.Move       = true;
         actor.Direction += TimeKeeper.GlobalTime(bot.TurningSpeed);                 // ni-ni-ni-ni-ni-ni-ni-ni-ni-ni-ni-ni-ni-ni
     }
 }
Esempio n. 3
0
 public override void Update()
 {
     // Basic animation code.
     _animation += TimeKeeper.GlobalTime(_animationSpeed);
     if (_animation > 1)
     {
         _animation -= 1;
     }
 }
Esempio n. 4
0
        public override void Update()
        {
            var position = GetComponent <PositionComponent>();

            position.Position.Y += TimeKeeper.GlobalTime(_vspeed);

            if (_deathAlarm.Update())
            {
                DestroyEntity();
            }
        }
Esempio n. 5
0
        public override void Update()
        {
            RetrieveStopperPoints();

            if (Target != null)
            {
                var targetPosition = Target.Position - Vector2.UnitY * OffsetY;
                var targetDistance = GameMath.Distance(Camera.Position, targetPosition);
                var targetVector   = (Camera.Position - targetPosition).GetSafeNormalize();

                if (targetDistance > MaxDistance && MaxDistanceEnabled)
                {
                    _position      = (targetPosition + targetVector * MaxDistance);
                    targetDistance = MaxDistance;
                }

                if (targetDistance > PullbackDistance)
                {
                    var distanceToMax = targetDistance - PullbackDistance;

                    distanceToMax *= (float)Math.Pow(PullbackMultiplier, TimeKeeper.GlobalTime());
                    _position      = targetPosition + targetVector * (distanceToMax + PullbackDistance);
                }
            }


            // Restricting the camera.
            var adjustedTopLeftPoint     = _topLeftPoint + Camera.Size / 2;
            var adjustedBottomRightPoint = _bottomRightPoint - Camera.Size / 2;

            if (_position.X < adjustedTopLeftPoint.X)
            {
                _position.X = adjustedTopLeftPoint.X;
            }
            if (_position.Y < adjustedTopLeftPoint.Y)
            {
                _position.Y = adjustedTopLeftPoint.Y;
            }
            if (_position.X > adjustedBottomRightPoint.X)
            {
                _position.X = adjustedBottomRightPoint.X;
            }
            if (_position.Y > adjustedBottomRightPoint.Y)
            {
                _position.Y = adjustedBottomRightPoint.Y;
            }
            // Restricting the camera.

            Camera.Position = _position.Round();
        }
Esempio n. 6
0
        public override void Update(List <Component> components)
        {
            // During update system picks up all the components on the scene at once.
            foreach (ActorComponent actor in components)
            {
                if (actor.Move)
                {
                    // Retrieving the position component from entity.
                    var position = actor.Owner.GetComponent <PositionComponent>();

                    position.PreviousPosition = position.Position;
                    position.Position        += TimeKeeper.GlobalTime(actor.Speed) * GameMath.DirectionToVector2(actor.Direction);
                }
            }
        }
Esempio n. 7
0
            public void Update()
            {
                var valueDelta = CurrentValue - TargetValue;
                var speedDelta = TimeKeeper.GlobalTime(Speed);

                if (Math.Abs(valueDelta) > speedDelta)
                {
                    CurrentValue -= speedDelta * Math.Sign(valueDelta);
                }
                else
                {
                    CurrentValue = TargetValue;
                    Complete     = true;
                }
            }
Esempio n. 8
0
        bool UpdateAnimation(StateMachine <ActorAnimationStates> stateMachine, StackableActorComponent actor, ActorAnimationStates newState)
        {
            actor.Animation += TimeKeeper.GlobalTime(actor.AnimationSpeed);

            if (actor.Animation >= 1 || actor.Animation < 0)
            {
                actor.Animation -= 1;

                if (newState != stateMachine.CurrentState)
                {
                    stateMachine.ChangeState(newState);
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 9
0
        public override void Update()
        {
            if (_mode == ShootingMode.Auto)
            {
                _initialDelayAlarm.Update();

                if (!_initialDelayAlarm.Running && _fireAlarm.Update())
                {
                    Shoot();
                }
            }

            if (_mode == ShootingMode.Trigger)
            {
                if (_myButton != null)
                {
                    if (_myButton.Pressed)
                    {
                        Shoot();
                    }
                }
                else
                {
                    if (TryGetComponent(out LinkComponent link))
                    {
                        if (link.Pair != null)
                        {
                            _myButton = (Button)link.Pair.Owner;
                            RemoveComponent <LinkComponent>();
                        }
                    }
                }
            }


            if (_shootingAnimationRunning)
            {
                _shootingAnimationProgress += TimeKeeper.GlobalTime(_shootingAnimationSpeed);

                if (_shootingAnimationProgress > 1)
                {
                    _shootingAnimationRunning  = false;
                    _shootingAnimationProgress = 0;
                }
            }
        }
Esempio n. 10
0
        public override void Update()
        {
            // Spawn, if watermelon been destroyed.
            if (_spawnedEntity == null || _spawnedEntity.Destroyed)
            {
                SpawnEntity();
            }
            // Spawn, if watermelon been destroyed.

            // Spawn, if watermelon been stacked.
            if (
                _spawnedEntity != null &&
                _spawnMode == SpawnMode.OnStacked &&
                _spawnedEntity.GetComponent <StackableActorComponent>().LogicStateMachine.CurrentState == ActorStates.Stacked
                )
            {
                SpawnEntity();
            }
            // Spawn, if watermelon been stacked.

            // Spawn, if watermelon is dead.
            if (
                _spawnedEntity != null &&
                _spawnMode == SpawnMode.OnDead &&
                _spawnedEntity.GetComponent <StackableActorComponent>().LogicStateMachine.CurrentState == ActorStates.Dead
                )
            {
                SpawnEntity();
            }
            // Spawn, if watermelon is dead.


            // Animation.
            if (_spawnAnimation)
            {
                _spawnAnimationProgress += TimeKeeper.GlobalTime(_spawnAnimationSpeed);
                if (_spawnAnimationProgress >= 1f)
                {
                    _spawnAnimation        = false;
                    _spawnedEntity.Enabled = true;
                    _spawnedEntity.Visible = true;
                }
            }
            // Animation.
        }
Esempio n. 11
0
        public override void Update()
        {
            var position = GetComponent <PositionComponent>();

            if (!_dead)
            {
                position.Position += _direction * TimeKeeper.GlobalTime(_speed);

                _collider.Position         = position.Position;
                _collider.PreviousPosition = position.PreviousPosition;

                if (!CollisionDetector.CheckCollision(_collider, _myCannon.GetComponent <SolidComponent>().Collider))
                {
                    foreach (SolidComponent solid in Scene.GetComponentList <SolidComponent>())
                    {
                        var solidPosition = solid.Owner.GetComponent <PositionComponent>();
                        solid.Collider.Position         = solidPosition.Position;
                        solid.Collider.PreviousPosition = solidPosition.PreviousPosition;
                        if (CollisionDetector.CheckCollision(_collider, solid.Collider))
                        {
                            Die();
                        }
                    }
                }

                var actors = SceneMgr.CurrentScene.GetEntityListByComponent <StackableActorComponent>();
                foreach (var actorEntity in actors)
                {
                    var playerActor = actorEntity.GetComponent <StackableActorComponent>();

                    var playerPosition = actorEntity.GetComponent <PositionComponent>();
                    var playerPhysics  = actorEntity.GetComponent <PhysicsComponent>();

                    // Setting up colliders.
                    playerPhysics.Collider.Position         = playerPosition.Position;
                    playerPhysics.Collider.PreviousPosition = playerPosition.PreviousPosition;
                    // Seting up colliders.

                    if (CollisionDetector.CheckCollision(_collider, playerPhysics.Collider))
                    {
                        StackableActorSystem.Damage(actorEntity.GetComponent <StackableActorComponent>());
                        Die();
                        break;
                    }
                }
            }
            else
            {
                _deadSpeed.Y      += TimeKeeper.GlobalTime(_deadGravity);
                position.Position += TimeKeeper.GlobalTime(_deadSpeed);
            }

            if (_dead)
            {
                var inBounds = false;
                foreach (var camera in SceneMgr.CurrentScene.GetEntityList <GameCamera>())
                {
                    if (camera.InBounds(position.Position))
                    {
                        inBounds = true;
                        break;
                    }
                }

                if (!inBounds)
                {
                    DestroyEntity();
                }
            }

            if (_lifetimeAlarm.Update())
            {
                DestroyEntity();
            }
        }
Esempio n. 12
0
        public override void Update(List <Component> components)
        {
            foreach (PathComponent path in components)
            {
                // Updating path.
                path.PointProgress += TimeKeeper.GlobalTime(path.Speed);

                var l = BoneLength(path, path.PointID);
                if (path.PointProgress > l)
                {
                    path.PointID += 1;
                    if (!path.Looped)
                    {
                        // Non-looped paths.
                        if (path.PointID >= path.Points.Count - 1)
                        {
                            path.Speed         *= -1;
                            path.PointID       -= 1;
                            path.PointProgress -= (path.PointProgress - l) * 2;
                        }
                        else
                        {
                            path.PointProgress -= l;
                        }
                        // Non-looped paths.
                    }
                    else
                    {
                        // Looped paths.
                        if (path.PointID >= path.Points.Count)
                        {
                            path.PointID = 0;
                        }
                        path.PointProgress -= l;
                        // Looped paths.
                    }
                }

                if (path.PointProgress < 0)
                {
                    path.PointID -= 1;

                    if (!path.Looped)
                    {
                        // Non-looped paths.
                        if (path.PointID < 0)
                        {
                            path.Speed   *= -1;
                            path.PointID += 1;

                            path.PointProgress -= path.PointProgress * 2;
                        }
                        else
                        {
                            path.PointProgress += BoneLength(path, path.PointID);
                        }
                        // Non-looped paths.
                    }
                    else
                    {
                        // Looped paths.
                        if (path.PointID < 0)
                        {
                            path.PointID = path.Points.Count - 1;
                        }
                        path.PointProgress += BoneLength(path, path.PointID);
                        // Looped paths.
                    }
                }
                // Updating path.


                // Updating owner entity position.
                var position = path.Owner.GetComponent <PositionComponent>();
                var solid    = path.Owner.GetComponent <SolidComponent>();

                var pathPosition = GetCurrentPosition(path);
                if (TimeKeeper.GlobalTime() != 0)
                {
                    solid.Speed = (pathPosition - position.Position) / (float)TimeKeeper.GlobalTime();
                }
                // Updating owner entity position.
            }
        }
Esempio n. 13
0
        void UpdateSpeed(StackableActorComponent actor, PhysicsComponent physics)
        {
            var horMovement = actor.RightAction.ToInt() - actor.LeftAction.ToInt();

            var sliding = false;

            if (
                horMovement == 0 ||
                (
                    Math.Abs(physics.Speed.X) > actor.MaxMovementSpeed &&
                    ((!actor.RightAction && !actor.LeftAction) || actor.Crouching)
                )
                )
            {
                // Slowing down.
                if (physics.Speed.X != 0)
                {
                    var spdSign = Math.Sign(physics.Speed.X);
                    physics.Speed.X -= TimeKeeper.GlobalTime(spdSign * actor.Deceleration);
                    if (Math.Sign(physics.Speed.X) != Math.Sign(spdSign))
                    {
                        physics.Speed.X = 0;
                    }
                }
                // Slowing down.


                sliding = (physics.Speed.X != 0 && actor.Crouching);
            }
            else
            {
                // Speeding up.
                if (Math.Abs(physics.Speed.X) < actor.MaxMovementSpeed || Math.Sign(physics.Speed.X) != Math.Sign(horMovement))
                {
                    physics.Speed.X += TimeKeeper.GlobalTime(horMovement * actor.Acceleration);

                    if (Math.Abs(physics.Speed.X) > actor.MaxMovementSpeed)
                    {
                        physics.Speed.X = horMovement * actor.MaxMovementSpeed;
                    }
                }
                // Speeding up.
            }


            var speedRatio = Math.Abs(physics.Speed.X / 2) / actor.MaxMovementSpeed;

            if (sliding)
            {
                if (actor.SlideSound == null)
                {
                    if (speedRatio > 1.1f)
                    {
                        actor.SlideSound = SoundController.PlaySound(Resources.Sounds.Slide);
                    }
                }
                else
                {
                    actor.SlideSound.Volume = Math.Min(1, speedRatio);
                }
            }

            if (actor.SlideSound != null && !sliding)
            {
                actor.SlideSound.Stop();
                actor.SlideSound = null;
            }
        }
Esempio n. 14
0
        void StackedUpdate(StackableActorComponent actor, float baseDirection, int stackIndex, Entity owner)
        {
            var position = actor.Owner.GetComponent <PositionComponent>();
            var physics  = actor.Owner.GetComponent <PhysicsComponent>();

            var masterPosition = actor.StackedPrevious.GetComponent <PositionComponent>();
            var masterPhysics  = actor.StackedPrevious.GetComponent <PhysicsComponent>();
            var masterActor    = actor.StackedPrevious.GetComponent <StackableActorComponent>();

            actor.StackOwner = owner;

            // Pendulum.

            // We are applying force to the pendulum, and it tries to push back.
            // Jiggly stacking stuff happens here.

            var pendulumForce       = (masterPosition.Position.X - masterPosition.PreviousPosition.X) * actor.PendulumForceMultiplier;
            var pendulumSpringForce = (float)Math.Pow(actor.StackDirectionOffset, 3) * actor.PendulumRigidity;

            actor.PendulumMomentum += TimeKeeper.GlobalTime(pendulumForce - pendulumSpringForce);
            actor.PendulumMomentum -= Math.Sign(actor.PendulumMomentum) * TimeKeeper.GlobalTime(actor.PendulumEnergyLossRate);

            if (Math.Abs(actor.PendulumMomentum) > actor.PendulumMomentumMax)
            {
                actor.PendulumMomentum = actor.PendulumMomentumMax * Math.Sign(actor.PendulumMomentum);
            }
            actor.StackDirectionOffset += TimeKeeper.GlobalTime(actor.PendulumMomentum);

            if (Math.Abs(actor.StackDirectionOffset) > actor.StackDirectionMaxOffset)
            {
                actor.StackDirectionOffset = actor.StackDirectionMaxOffset * Math.Sign(actor.StackDirectionOffset);
            }

            var dir = MathHelper.ToRadians(baseDirection + actor.StackDirectionOffset);

            // Pendulum.


            // Crouch transfer.
            if (masterActor.Crouching && !actor.Crouching)
            {
                Crouch(position, physics, actor);
            }
            if (!masterActor.Crouching && actor.Crouching)
            {
                Uncrouch(position, physics, actor);
            }
            // Crouch transfer.


            // Nice y delaying.
            actor.StackedTargetPosition = masterPosition.Position
                                          + new Vector2(
                (float)Math.Cos(dir),
                (float)-Math.Sin(dir)
                ) * (masterPhysics.Collider.Size.Y);


            // TODO: Figure out exact formula.
            position.Position.Y += TimeKeeper.GlobalTime((-position.Position.Y + actor.StackedTargetPosition.Y) / actor.StackPositionDelayDivider);
            //position.Position.Y += (-position.Position.Y + actor.StackedTargetPosition.Y) / actor.StackPositionDelayDivider;
            position.Position.X = actor.StackedTargetPosition.X;
            // Nice y delaying.



            if (actor.StackedNext != null)
            {
                StackedUpdate(
                    actor.StackedNext.GetComponent <StackableActorComponent>(),
                    baseDirection + actor.StackDirectionOffset,
                    stackIndex + 1,
                    owner
                    );
            }

            if (stackIndex > actor.StackLimit)
            {
                actor.LogicStateMachine.ChangeState(ActorStates.Dead);
            }
        }
Esempio n. 15
0
        private void UpdatePhysicsXAxis(List <Component> components)
        {
            // Moving solid objects.
            foreach (var solidEntity in _solidEntities)
            {
                var position = solidEntity.GetComponent <PositionComponent>();
                var solid    = solidEntity.GetComponent <SolidComponent>();
                position.Position.X += solid.Speed.X * (float)TimeKeeper.GlobalTime();
            }
            // Moving solid objects.


            foreach (PhysicsComponent cPhysics in components)
            {
                var cPosition = cPhysics.Owner.GetComponent <PositionComponent>();
                cPosition.Position.X += TimeKeeper.GlobalTime(cPhysics.Speed.X) + cPhysics.OneFrameMovement.X;


                // Setting up the collider.
                var collider = cPhysics.Collider;
                collider.Position         = cPosition.Position;
                collider.PreviousPosition = cPosition.PreviousPosition;
                // Setting up the collider.

                cPhysics.CollidedSolidH = CheckCollision(cPhysics.Owner, collider);
                if (cPhysics.CollidedSolidH != null)
                {
                    var solidEntityPosition = cPhysics.CollidedSolidH.GetComponent <PositionComponent>();

                    // Solid objects are assumed to be stationary, so we're just accomodating
                    // for their speed in physics objects speed.
                    var relativeSpeed = (cPosition.Position - cPosition.PreviousPosition)
                                        - (solidEntityPosition.Position - solidEntityPosition.PreviousPosition);

                    var sign = 1;
                    if (relativeSpeed.X < 0)
                    {
                        sign = -1;
                    }

                    cPhysics.Squashed = true;

                    for (var x = 0; x <= Math.Abs(relativeSpeed.X) + 1; x += 1)
                    {
                        collider.Position -= Vector2.UnitX * sign;

                        var solidEntity = CheckCollision(cPhysics.Owner, collider);
                        if (solidEntity == null)
                        {
                            if (Math.Sign(cPhysics.Speed.X) == sign)
                            {
                                cPhysics.Speed.X = 0;
                            }

                            cPosition.Position.X = collider.Position.X;

                            cPhysics.CollisionH = sign;
                            cPhysics.Squashed   = false;

                            var solid = cPhysics.CollidedSolidH.GetComponent <SolidComponent>();
                            solid.CollidedObjectH = cPhysics.Owner;
                            solid.CollisionH      = -sign;

                            break;
                        }
                        else
                        {
                            cPhysics.CollidedSolidH = solidEntity;
                        }
                    }
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Main physics code. Far from the best, but gets job done.
        /// </summary>
        public override void Update(List <Component> components)
        {
            _solidEntities = SceneMgr.CurrentScene.GetEntityListByComponent <SolidComponent>();

            foreach (var solidEntity in _solidEntities)
            {
                var solid = solidEntity.GetComponent <SolidComponent>();
                solid.CollisionH      = 0;
                solid.CollisionV      = 0;
                solid.CollidedObjectH = null;
                solid.CollidedObjectV = null;
            }


            // Gravity.
            foreach (PhysicsComponent cPhysics in components)
            {
                cPhysics.OneFrameMovement = Vector2.Zero;
                cPhysics.CollisionH       = 0;
                cPhysics.CollisionV       = 0;
                cPhysics.CollidedSolidH   = null;
                cPhysics.CollidedSolidV   = null;
                cPhysics.Squashed         = false;


                var cPosition = cPhysics.Owner.GetComponent <PositionComponent>();

                // Setting up the collider.
                var collider = cPhysics.Collider;
                collider.Position         = cPosition.Position + Vector2.UnitY;
                collider.PreviousPosition = cPosition.Position;
                // Setting up the collider.

                // Checking space under the entity.
                cPhysics.StandingOn = CheckCollision(cPhysics.Owner, collider);
                cPhysics.InAir      = (cPhysics.StandingOn == null);

                if (cPhysics.InAir)
                {
                    // In air.
                    if (cPhysics.Speed.Y < cPhysics.MaxFallSpeed)
                    {
                        cPhysics.Speed.Y += TimeKeeper.GlobalTime(cPhysics.Gravity * GravityMultiplier);

                        if (cPhysics.Speed.Y > cPhysics.MaxFallSpeed)
                        {
                            cPhysics.Speed.Y = cPhysics.MaxFallSpeed;
                        }
                    }
                    // In air.
                }
                else
                {
                    // On the ground.

                    // Moving entity along with solid object.
                    cPhysics.OneFrameMovement = cPhysics.StandingOn.GetComponent <SolidComponent>().Speed *(float)TimeKeeper.GlobalTime();

                    // On the ground.
                }
            }
            // Gravity.

            UpdatePhysicsXAxis(components);

            UpdatePhysicsYAxis(components);
        }
Esempio n. 17
0
        private void UpdatePhysicsYAxis(List <Component> components)
        {
            foreach (var solidEntity in _solidEntities)
            {
                var position = solidEntity.GetComponent <PositionComponent>();
                var solid    = solidEntity.GetComponent <SolidComponent>();
                position.Position.Y += TimeKeeper.GlobalTime(solid.Speed.Y);
            }

            foreach (PhysicsComponent cPhysics in components)
            {
                var cPosition = cPhysics.Owner.GetComponent <PositionComponent>();
                var collider  = cPhysics.Collider;

                cPosition.Position.Y += TimeKeeper.GlobalTime(cPhysics.Speed.Y) + cPhysics.OneFrameMovement.Y;

                collider.Position         = cPosition.Position;
                collider.PreviousPosition = cPosition.PreviousPosition;

                cPhysics.CollidedSolidV = CheckCollision(cPhysics.Owner, collider);

                if (cPhysics.CollidedSolidV != null)
                {
                    var solidEntityPosition = cPhysics.CollidedSolidV.GetComponent <PositionComponent>();
                    var relativeSpeed       = (cPosition.Position - cPosition.PreviousPosition)
                                              - (solidEntityPosition.Position - solidEntityPosition.PreviousPosition);

                    var sign = 1;
                    if (relativeSpeed.Y < 0)
                    {
                        sign = -1;
                    }

                    cPhysics.Squashed = true;

                    for (var y = 0; y <= Math.Abs(relativeSpeed.Y) + 10; y += 1)
                    {
                        collider.Position -= Vector2.UnitY * sign;

                        var solidEntity = CheckCollision(cPhysics.Owner, collider);
                        if (solidEntity == null)
                        {
                            if (Math.Sign(cPhysics.Speed.Y) == sign)
                            {
                                cPhysics.Speed.Y = 0;
                            }

                            cPosition.Position.Y = collider.Position.Y;

                            cPhysics.CollisionV = sign;

                            var solid = cPhysics.CollidedSolidV.GetComponent <SolidComponent>();
                            solid.CollidedObjectV = cPhysics.Owner;
                            solid.CollisionV      = -sign;

                            cPhysics.Squashed = false;
                            break;
                        }
                        else
                        {
                            cPhysics.CollidedSolidV = solidEntity;
                        }
                    }
                }
            }
        }