Exemple #1
0
    private void UpdateOrientation(EntityOrientation orientation)
    {
        if (this.currentOrientation != orientation)
        {
            this.currentOrientation = orientation;

            var characterAnimator = this.GetNode <AnimatedSprite>("AnimatedSprite");
            characterAnimator.Scale = new Vector2(-characterAnimator.Scale.x, characterAnimator.Scale.y);

            foreach (var mask in this.availableMasks)
            {
                var spriteMask = mask.GetNode <Sprite>("Sprite");
                spriteMask.Scale = characterAnimator.Scale;
            }
        }
    }
Exemple #2
0
 public void Activate(EntityOrientation orientation)
 {
     if (IsDashReady())
     {
         timer         = duration;
         cooldownTimer = cooldown;
         character.Stun(duration);
         dashBoostTimer = dashBoostTime;
         if (character.IsOnFloor())
         {
             verticalDash = false;
         }
         else
         {
             verticalDash = true;
         }
         dashDir = character.GetOrientation();
     }
 }
Exemple #3
0
    public bool Activate(EntityOrientation orientation, bool boosted)
    {
        if (ShootReady())
        {
            var proj_instance = (Proj)proj.Instance();
            proj_instance.Rotation = Mathf.Deg2Rad((orientation == EntityOrientation.Right) ? 0 : 180);
            if (boosted)
            {
                proj_instance.SetBoosted();
            }
            GetParent().GetParent().AddChild(proj_instance);
            proj_instance.GlobalPosition = GlobalPosition;


            cooldown = 1 / rate;
            return(true);
        }

        return(false);
    }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            var mouseDelta = (Vector2)VoxelGame.Instance.MouseDelta;

            mouseDelta /= (float)gameTime.ElapsedGameTime.TotalSeconds;

            var elapsed = MathHelper.Min(1.0f / 20, (float)gameTime.ElapsedGameTime.TotalSeconds);

            mouseDelta *= elapsed;

            Vector3 move             = Vector3.Zero;
            bool    walkingIsPressed = false;
            bool    isSpacePressed   = false;

            if (!VoxelGame.Instance.Paused)
            {
                var keyboardState = Keyboard.GetState();

                walkingIsPressed = Mouse.GetState().MiddleButton == ButtonState.Pressed;
                if (walkingIsPressed && !_walkingWasPressed)
                {
                    Walking = !Walking;
                }

                isSpacePressed = keyboardState.IsKeyDown(Keys.Space);

                if (keyboardState.IsKeyDown(Keys.A))
                {
                    move.X -= 1;
                }
                if (keyboardState.IsKeyDown(Keys.D))
                {
                    move.X += 1;
                }
                if (keyboardState.IsKeyDown(Keys.S))
                {
                    move.Z -= 1;
                }
                if (keyboardState.IsKeyDown(Keys.W))
                {
                    move.Z += 1;
                }

                PlayerOrientation = PlayerOrientation.RotateYaw(mouseDelta.X * 0.0025f);
                PlayerOrientation = PlayerOrientation.RotatePitch(mouseDelta.Y * 0.0025f);
            }

            RenderManager.Instance.CameraForward = PlayerOrientation.Forward;

            if (Walking)
            {
                if (VoxelGame.Instance.Grid.saveInitializationPhase > 0 && VoxelGame.Instance.Grid.TileExists(PlayerPosition))
                {
                    if (move.LengthSquared() > 0)
                    {
                        move.Normalize();

                        PlayerPosition += (
                            PlayerOrientation.HorizontalForward * move.Z +
                            PlayerOrientation.HorizontalRight * move.X) * elapsed * 20;
                    }

                    // Gravity
                    _playerSpeed.Y -= 9.8f * elapsed;
                    PlayerPosition += new Vector3(0, _playerSpeed.Y * elapsed, 0);

                    var p0 = (PlayerPosition - new Vector3(0.4f, 0.4F, 0.4F)).GridPosition;
                    var q0 = (PlayerPosition + new Vector3(0.4f, 0.4F, 0.4F)).GridPosition;

                    var pos = new BlockPos(p0.X, int.MinValue, p0.Z);

                    for (int rz = p0.Z; rz <= q0.Z; rz++)
                    {
                        for (int rx = p0.X; rx <= q0.X; rx++)
                        {
                            int ry = VoxelGame.Instance.Grid.FindGround(new BlockPos(rx, q0.Y, rz));
                            if (ry > pos.Y)
                            {
                                pos = new BlockPos(rx, ry, rz);
                            }
                        }
                    }

                    var block = VoxelGame.Instance.Grid.GetBlock(pos);

                    float blockHeight = block.PhysicsMaterial.IsSolid ? block.PhysicsMaterial.Height : 0;

                    PlayerPositionTarget = EntityPosition.FromGrid(pos, new Vector3(0, blockHeight, 0));

                    var approach = elapsed / 0.25f;

                    var distance = PlayerPosition.RelativeTo(PlayerPositionTarget).Y;

                    if (distance < 0)
                    {
                        _playerSpeed.Y = 0;
                        if (isSpacePressed && !_wasSpacePressed)
                        {
                            PlayerPosition += new Vector3(0, -distance, 0);
                            _playerSpeed.Y  = 8;
                        }
                    }

                    if (distance < -1)
                    {
                        PlayerPosition += new Vector3(0, -distance, 0);
                    }
                    else if (distance > approach)
                    {
                        PlayerPosition += new Vector3(0, -approach, 0);
                    }
                    else if (distance < -approach)
                    {
                        PlayerPosition += new Vector3(0, approach, 0);
                    }
                    else
                    {
                        PlayerPosition += new Vector3(0, -distance, 0);
                    }
                }
            }
            else
            {
                if (move.LengthSquared() > 0)
                {
                    move.Normalize();

                    PlayerPosition += (
                        PlayerOrientation.Forward * move.Z +
                        PlayerOrientation.HorizontalRight * move.X) * elapsed * 25;
                }

                _playerSpeed = Vector3.Zero;
            }

            VoxelGame.Instance.Grid.SetPlayerPosition(PlayerPosition);
            PriorityScheduler.Instance.SetPlayerPosition(PlayerPosition);

            _walkingWasPressed = walkingIsPressed;
            _wasSpacePressed   = isSpacePressed;
        }
Exemple #5
0
        public EntityCollision Move(EntityOrientation direction, double speed)
        {
            this.Orientation = direction;

            EntityPosition newPosition = new EntityPosition(this.Position.PosX, this.Position.PosY);

            switch (direction)
            {
            case EntityOrientation.Left:
                newPosition.PosX -= speed;
                break;

            case EntityOrientation.UpLeft:
                newPosition.PosX -= speed / 2;
                newPosition.PosY -= speed / 2;
                break;

            case EntityOrientation.Up:
                newPosition.PosY -= speed;
                break;

            case EntityOrientation.UpRight:
                newPosition.PosX += speed / 2;
                newPosition.PosY -= speed / 2;
                break;

            case EntityOrientation.Right:
                newPosition.PosX += speed;
                break;

            case EntityOrientation.DownRight:
                newPosition.PosX += speed / 2;
                newPosition.PosY += speed / 2;
                break;

            case EntityOrientation.Down:
                newPosition.PosY += speed;
                break;

            case EntityOrientation.DownLeft:
                newPosition.PosX -= speed / 2;
                newPosition.PosY += speed / 2;
                break;
            }

            Entity collisionEntity = ParentCollection.Items.FirstOrDefault(x => x != this && x.IsColliding(newPosition, this.CollisionDistanceX, this.CollisionDistanceY));

            if (collisionEntity != null)
            {
                if (this is Projectile)
                {
                    if (((Projectile)this).IsValidCollision(collisionEntity))
                    {
                        return(new EntityCollision(collisionEntity));
                    }
                }
                else
                {
                    return(new EntityCollision(collisionEntity));
                }
            }

            this.Position.PosX = newPosition.PosX;
            this.Position.PosY = newPosition.PosY;

            return(null);
        }
Exemple #6
0
 public EntityCollision Move(EntityOrientation direction)
 {
     return(Move(direction, Speed));
 }
Exemple #7
0
    private void UpdateVelocity(float delta)
    {
        #region horizontal velocity

        if (IsOnWall() && finalVelocity.x == 0)
        {
            snapDir = currentOrientation;
        }

        var horizontalSpeed = 0f;


        if (!IsStunned())
        {
            horizontalSpeed -= Input.GetActionStrength("character_move_left") * speed;
            horizontalSpeed += Input.GetActionStrength("character_move_right") * speed;
        }

        if (horizontalSpeed == 0)
        {
            this.UpdateState(EntityState.Idle);
        }
        else
        {
            this.UpdateState(EntityState.Running);
            this.UpdateOrientation(horizontalSpeed > 0 ? EntityOrientation.Right : EntityOrientation.Left);
        }

        if (jump && dash.IsBoosting())
        {
            acceleration.x    += dashJumpBoost * ((dash.dashDir == EntityOrientation.Right) ? 1 : -1);
            horizontalVelocity = 0;
        }

        horizontalVelocity += acceleration.x;

        var hdrag = horizontalDrag;
        if (!IsOnFloor())
        {
            hdrag = airHorizontalDrag;
        }

        if (horizontalVelocity > 0)
        {
            horizontalVelocity -= Mathf.Min(hdrag, horizontalVelocity);
        }
        if (horizontalVelocity < 0)
        {
            horizontalVelocity += Mathf.Max(hdrag, horizontalVelocity);
        }

        velocity.x = horizontalSpeed + horizontalVelocity;

        #endregion

        #region vertical velocity

        var vdrag = 0f;
        if (IsOnWall())
        {
            wallJumpSnapTimer = wallJumpSnapDuration;
            vdrag             = verticalWallDrag;
        }
        else
        {
            wallJumpSnapTimer -= delta;
        }

        if (wallJump)
        {
            this.UpdateState(EntityState.Jumping);
            velocity.y      = 0;
            acceleration.y += -wallJumpVForce;
            wallJump        = false;
        }

        if (jump)
        {
            this.UpdateState(EntityState.Jumping);
            velocity.y      = 0;
            acceleration.y += -jumpStrength;
            jump            = false;
        }

        if (!jump && !wallJump)
        {
            if (noGravityTimer > 0)
            {
                noGravityTimer -= delta;
            }
            else
            {
                acceleration.y += gravity;
            }
        }

        if (IsJumping())
        {
            if (Input.IsActionPressed("activate_power") && CurrentMask.Type == MaskType.Jump)
            {
                acceleration.y += -jumpSecondaryStrength;
            }
            jumpTimer -= delta;
        }

        velocity.y += acceleration.y;

        if (velocity.y > 0)
        {
            velocity.y -= Mathf.Min(vdrag, velocity.y);
        }
        if (velocity.y > maximumVerticalVelocity)
        {
            velocity.y = maximumVerticalVelocity;
        }

        if (IsOnWall())
        {
            if (velocity.y > maximumVerticalVelocityWall)
            {
                velocity.y = maximumVerticalVelocityWall;
            }
            this.UpdateState(EntityState.Wall);
        }

        #endregion

        // reset acceleration
        acceleration = Vector2.Zero;
    }
Exemple #8
0
 public static float ToRotationAngle(this EntityOrientation o)
 {
     return(45f * (int)o);
 }