コード例 #1
0
    internal void InitShot(Vector3 shotDirection, GravityDirection gravityDirection)
    {
        m_shotDirection   = shotDirection;
        m_shotDirection.z = 0;

        m_gravityDirection = gravityDirection;
    }
コード例 #2
0
        public void ChangeInput(int gravityDirectionValue)
        {
            GravityDirection gravityDirection = (GravityDirection)gravityDirectionValue;

            switch (gravityDirection)
            {
            case GravityDirection.Up:
                _inputAxis           = InputAxis.Horizontal;
                _directionMultiplier = -1;
                break;

            case GravityDirection.Down:
                _inputAxis           = InputAxis.Horizontal;
                _directionMultiplier = 1;
                break;

            case GravityDirection.Right:
                _inputAxis           = InputAxis.Vertical;
                _directionMultiplier = -1;
                break;

            case GravityDirection.Left:
                _inputAxis           = InputAxis.Vertical;
                _directionMultiplier = 1;
                break;

            default:
                break;
            }
        }
コード例 #3
0
 // Start is called before the first frame update
 void Start()
 {
     currentAngle     = 0;
     baseGravity      = Physics2D.gravity;
     gravityStrength  = 10;
     currentDirection = GravityDirection.Down;
 }
コード例 #4
0
        // 启动GTM
        public void ActiveGTM(GravityDirection gd)
        {
            // 保存重力值 重力置零
            float gValue = physicsMgr.World.Gravity.Length();
            physicsMgr.World.Gravity = Vector2.Zero;

            // 增大空气摩擦...

            // 储备下一个重力 & SetRoleUp & 按键映射
            switch (gd)
            {
                case GravityDirection.Up:
                    NextGravity = new Vector2(0,-gValue);
                    SetRoleRight((float)Math.PI);
                    break;
                case GravityDirection.Down:
                    NextGravity = new Vector2(0,gValue);
                    SetRoleRight(0);
                    break;
                case GravityDirection.Left:
                    NextGravity = new Vector2(-gValue,0);
                    SetRoleRight((float)Math.PI / 2);
                    break;
                case GravityDirection.Right:
                    NextGravity = new Vector2(gValue,0);
                    SetRoleRight((float)Math.PI * 3 / 2);
                    break;
            }
        }
コード例 #5
0
    public void SetGravity(GravityDirection direction)
    {
        switch (direction)
        {
        case GravityDirection.Up:
            gravity = new Vector2(0, 1);
            break;

        case GravityDirection.Down:
            gravity = new Vector2(0, -1);
            break;

        case GravityDirection.Left:
            gravity = new Vector2(-1, 0);
            break;

        case GravityDirection.Right:
            gravity = new Vector2(1, 0);
            break;

        default:
            goto case GravityDirection.Down;
        }

        gravity *= gravitySpeed;
    }
コード例 #6
0
    // Update is called once per frame
    void Update()
    {
        gravityChangeable = GridManager.AtRest();
        if (Input.GetKeyDown(KeyCode.UpArrow) && gravityChangeable == true)
        {
            gravityDirection  = GravityDirection.UP;
            gravityChangeable = false;
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow) && gravityChangeable == true)
        {
            gravityDirection  = GravityDirection.DOWN;
            gravityChangeable = false;
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow) && gravityChangeable == true)
        {
            gravityDirection  = GravityDirection.RIGHT;
            gravityChangeable = false;
        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow) && gravityChangeable == true)
        {
            gravityDirection  = GravityDirection.LEFT;
            gravityChangeable = false;
        }


        // CheckMotion();
    }
コード例 #7
0
    private void RotateMovementToGravity(GravityDirection gravityDirection)
    {
        // Depending on direction, set
        // Whether movement is on x or y
        // Whether movement direction is flipped or not
        // Whether jump direction is flipped or not
        switch (gravityDirection)
        {
        case GravityDirection.North:
            moveOnX          = true;
            directionFlipper = -1;
            jumpFlipper      = -1;
            break;

        case GravityDirection.East:
            moveOnX          = false;
            directionFlipper = 1;
            jumpFlipper      = -1;
            break;

        case GravityDirection.South:
        default:
            moveOnX          = true;
            directionFlipper = 1;
            jumpFlipper      = 1;
            break;

        case GravityDirection.West:
            moveOnX          = false;
            directionFlipper = -1;
            jumpFlipper      = 1;
            break;
        }
    }
コード例 #8
0
    private void Start()
    {
        Physics2D.gravity  = Vector2.zero;
        gravityScale       = defaultGravityScale;
        m_GravityDirection = GravityDirection.Down;
        coll = GetComponent <Collision>();

        Vector2 m_bottomOffset = coll.bottomOffset;
        Vector2 m_rightOffset  = coll.rightOffset;
        Vector2 m_leftOffset   = coll.leftOffset;
    }
コード例 #9
0
ファイル: character.cs プロジェクト: zephyrz73/balloonEscape
    void Update()
    {
        switch (m_GravityDirection)
        {
        case GravityDirection.Down:
            //Change the gravity to be in a downward direction (default)
            Physics2D.gravity = new Vector2(0, -9.8f);
            //Press the space key to switch to the left direction
            break;

        case GravityDirection.Left:
            //Change the gravity to go to the left
            Physics2D.gravity = new Vector2(-9.8f, 0);
            //Press the space key to change the direction of gravity
            break;

        case GravityDirection.Up:
            //Change the gravity to be in a upward direction
            Physics2D.gravity = new Vector2(0, 9.8f);
            //Press the space key to change the direction
            break;

        case GravityDirection.Right:
            //Change the gravity to go in the right direction
            Physics2D.gravity = new Vector2(9.8f, 0);
            //Press the space key to change the direction

            break;
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            m_GravityDirection = GravityDirection.Left;
            Debug.Log("Left");
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            m_GravityDirection = GravityDirection.Up;
            Debug.Log("Up");
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            m_GravityDirection = GravityDirection.Right;
            Debug.Log("Right");
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            m_GravityDirection = GravityDirection.Down;
            Debug.Log("Down");
        }
    }
コード例 #10
0
    public GameObject GetNextBlockByDirection(GravityDirection gravityDirection)
    {
        switch (gravityDirection)
        {
        case GravityDirection.Up: return(upBlock);

        case GravityDirection.Down: return(downBlock);

        case GravityDirection.Left: return(leftBlock);

        case GravityDirection.Right: return(rightBlock);

        default: return(null);
        }
    }
コード例 #11
0
ファイル: Player.cs プロジェクト: renios/reverse_labyrinth
    GravityDirection GetRightDirection(GravityDirection direction)
    {
        switch (direction)
        {
        case GravityDirection.Up: return(GravityDirection.Right);

        case GravityDirection.Down: return(GravityDirection.Left);

        case GravityDirection.Left: return(GravityDirection.Up);

        case GravityDirection.Right: return(GravityDirection.Down);

        default: return(direction);
        }
    }
コード例 #12
0
ファイル: SolverForm.cs プロジェクト: Mekeater/PSDS
 private void cb_GravityDirection_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (this.cb_GravityDirection.SelectedIndex == 0)
     {
         gravityDirection = GravityDirection.X;
     }
     else if (this.cb_GravityDirection.SelectedIndex == 1)
     {
         gravityDirection = GravityDirection.Y;
     }
     else
     {
         gravityDirection = GravityDirection.Z;
     }
 }
コード例 #13
0
ファイル: LevelManager.cs プロジェクト: enchiridion11/myriad
 public void SwitchGravity()
 {
     if (gravity == GravityDirection.UP)
     {
         ScaleLevelsUp();
         gravity = GravityDirection.DOWN;
         Player.Instance.CeilingToFloor();
     }
     else if (gravity == GravityDirection.DOWN)
     {
         ScaleLevelsDown();
         gravity = GravityDirection.UP;
         Player.Instance.FloorToCeiling();
     }
 }
コード例 #14
0
    void Start()
    {
        health = START_HEALTH;
        player = GetComponent <Player>();
        isDead = false;

        gravityDirection = GravityDirection.DOWN;
        gravity          = GRAVITY_STRENGTH * -1;

        playerControl = GetComponent <Player>();

        health = START_HEALTH;
        isDead = false;

        roundNumber = 0;
    }
コード例 #15
0
 // Update is called once per frame
 void Update()
 {
     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, currentAngle), 0.2f);
     if (Input.GetKeyDown(KeyCode.Tab))
     {
         if (currentDirection == GravityDirection.Right)
         {
             currentDirection = GravityDirection.Down;
         }
         else
         {
             currentDirection++;
         }
         RotateGravity(currentDirection);
     }
 }
コード例 #16
0
    void Update()
    {
        // Left click to shoot
        if (Input.GetMouseButtonDown(0) && Time.timeScale > 0)
        {
            Shoot();
            animator.SetTrigger("recoil");
            audio.fire();
        }

        // Scroll to change gravity direction
        float scroll    = Input.GetAxis("Mouse ScrollWheel");
        int   direction = scroll > 0 ? -1 : scroll < 0 ? 1 : 0;

        gravityDirection = (GravityDirection)(((int)gravityDirection + direction + 4) % 4);
    }
コード例 #17
0
    public void ChangeGravity()
    {
        gravityDirection = (GravityDirection)Random.Range(0, 4);

        if (gravityDirection == GravityDirection.DOWN || gravityDirection == GravityDirection.LEFT)
        {
            gravity = (gravity > 0) ? gravity * -1 : gravity;
        }

        if (gravityDirection == GravityDirection.UP || gravityDirection == GravityDirection.RIGHT)
        {
            gravity = (gravity < 0) ? gravity * -1 : gravity;
        }

        Invoke("ResetGravity", 10f);
    }
コード例 #18
0
ファイル: MainGame.cs プロジェクト: dev4robi/portfolio
    // 게임 초기화
    public void InitializeGame()
    {
        Time.timeScale  = 1.000f;
        bPaused         = false;
        bGameOver       = false;
        iLastOctave     = 0;
        bScrClicked     = false;
        selectedDotList = new List <DotObject>();

        frameColor     = new Color(80.00f / 255.00f, 20.00f / 255.00f, 20.00f / 255.00f);
        enGravityDir   = GravityDirection.GRAVITY_DOWN;
        iNoMoreMoveCnt = 1;
        iScore         = 0;
        iMaxLink       = 0;
        iLinkCnt       = 0;
        iScoreBase     = 35;
    }
コード例 #19
0
        private void SetGravityDirection(GravityDirection direction)
        {
            _currentGravityDirection = direction;
            var directionSign = direction == GravityDirection.Down ? 1f : -1f;

            _rigidbody.gravityScale    = directionSign * GravityScale;
            playerSpriteRenderer.flipY = direction == GravityDirection.Up;

            var velocity = _rigidbody.velocity;

            velocity.y          = -FlipDirectionInitialVelocity * directionSign;
            _rigidbody.velocity = velocity;

            colliderObject.transform.localScale = new Vector3(1, directionSign, 1);

            GravitySwitchEvent?.Invoke(_currentGravityDirection);
        }
コード例 #20
0
    void GetRotationReady(GravityDirection i_direction)
    {
        switch (i_direction)
        {
        case GravityDirection.XPositive: fromRotation = transform.rotation; toRotation = Quaternion.Euler(-90, 0, 90); break;

        case GravityDirection.XNegtive: fromRotation = transform.rotation; toRotation = Quaternion.Euler(-90, 0, -90); break;

        case GravityDirection.YPositive: fromRotation = transform.rotation; toRotation = Quaternion.Euler(180, 0, 0); break;

        case GravityDirection.YNegtive: fromRotation = transform.rotation; toRotation = Quaternion.Euler(0, 0, 0); break;

        case GravityDirection.ZNegtive: fromRotation = transform.rotation; toRotation = Quaternion.Euler(90, 0, 0); break;

        case GravityDirection.ZPositive: fromRotation = transform.rotation; toRotation = Quaternion.Euler(-90, 0, 0); break;
        }
    }
コード例 #21
0
    // Events

    private void RotateSelfToGravity(GravityDirection gravityDirection)
    {
        // Store current time and angle
        startTime  = Time.time;
        startAngle = transform.eulerAngles;

        // Depending on direction, set
        // New entity angle
        // Whether movement is on x or y
        // Whether movement direction is flipped or not
        // Whether jump direction is flipped or not
        switch (gravityDirection)
        {
        case GravityDirection.North:
            destAngleZ = 180;
            break;

        case GravityDirection.East:
            destAngleZ = 90;
            break;

        case GravityDirection.South:
        default:
            destAngleZ = 0;
            // Smart rotate
            if (startAngle.z == 270)
            {
                destAngleZ = 360;
            }
            break;

        case GravityDirection.West:
            destAngleZ = 270;
            // Smart rotate
            if (startAngle.z == 0)
            {
                destAngleZ = -90;
            }
            break;
        }

        // Enable orienting state
        orienting = true;
        eventManager.InvokeEvent("Rotator_Orienting_" + orienting);
    }
コード例 #22
0
        public void ChangeMovement(Collider2D collider)
        {
            Gravity gravity = collider.GetComponent <Gravity>();

            if (gravity == null)
            {
                return;
            }
            GravityDirection newDirection = GravityDirectionExtensions.FromAngle(transform.eulerAngles.z);

            if (newDirection != gravity.Direction)
            {
                gravity.Direction = GravityDirectionExtensions.FromAngle(transform.eulerAngles.z);
                gravity.OnChange();
            }

            gravity.Force = _speed;
        }
コード例 #23
0
 /// <summary>
 /// 根据重力方向载入按键映射
 /// </summary>
 /// <param name="gd"></param>
 public void GDKeyMap(GravityDirection gd)
 {
     switch (gd)
     {
         case GravityDirection.Down:
             Keyboard.keyMapping = new Dictionary<Keyboard.EGameKey, Keys> { { Keyboard.EGameKey.Up, Keys.Up }, { Keyboard.EGameKey.Left, Keys.Left }, { Keyboard.EGameKey.Right, Keys.Right } };
             break;
         case GravityDirection.Up:
             Keyboard.keyMapping = new Dictionary<Keyboard.EGameKey, Keys> { { Keyboard.EGameKey.Up, Keys.Down }, { Keyboard.EGameKey.Left, Keys.Right }, { Keyboard.EGameKey.Right, Keys.Left } };
             break;
         case GravityDirection.Left:
             Keyboard.keyMapping = new Dictionary<Keyboard.EGameKey, Keys> { { Keyboard.EGameKey.Up, Keys.Right }, { Keyboard.EGameKey.Left, Keys.Up }, { Keyboard.EGameKey.Right, Keys.Down } };
             break;
         case GravityDirection.Right:
             Keyboard.keyMapping = new Dictionary<Keyboard.EGameKey, Keys> { { Keyboard.EGameKey.Up, Keys.Left }, { Keyboard.EGameKey.Left, Keys.Down }, { Keyboard.EGameKey.Right, Keys.Up } };
             break;
     }
 }
コード例 #24
0
        // 根据重力方向载入按键映射
        public void GDKeyMap(GravityDirection gd)
        {
            switch (gd)
            {
                case GravityDirection.Down:
                    game.SetKeyMap(new Dictionary<EGameKey, Keys> { { EGameKey.Up, Keys.Up }, { EGameKey.Left, Keys.Left }, { EGameKey.Right, Keys.Right } });

                    break;
                case GravityDirection.Up:
                    game.SetKeyMap(new Dictionary<EGameKey, Keys> { { EGameKey.Up, Keys.Down }, { EGameKey.Left, Keys.Right }, { EGameKey.Right, Keys.Left } });
                    break;
                case GravityDirection.Left:
                    game.SetKeyMap(new Dictionary<EGameKey, Keys> { { EGameKey.Up, Keys.Right }, { EGameKey.Left, Keys.Up }, { EGameKey.Right, Keys.Down } });
                    break;
                case GravityDirection.Right:
                    game.SetKeyMap(new Dictionary<EGameKey, Keys> { { EGameKey.Up, Keys.Left }, { EGameKey.Left, Keys.Down }, { EGameKey.Right, Keys.Up } });
                    break;
            }
        }
コード例 #25
0
 private void Update()
 {
     //transform.RotateAround(transform.position, Vector3.forward, 30 * Time.deltaTime);
     if (Input.GetKeyDown(KeyCode.Z) && m_isReadyChangeDirection)
     {
         m_isReadyChangeDirection = false;
         m_GravityDirection       = m_nextGravityDirection;
     }
     if (IsRotating)
     {
         ratio += Time.deltaTime * m_RotateSpeed;
         if (ratio > 1)
         {
             ratio      = 1;
             IsRotating = false;
         }
         print(toRotation + "," + ratio);
         transform.rotation = Quaternion.Slerp(fromRotation, toRotation, ratio);
     }
 }
コード例 #26
0
ファイル: LogicGravityBox.cs プロジェクト: zehro/Projects
 Vector3 gravityToVector(GravityDirection d)
 {
     switch (d)
     {
         case GravityDirection.Right:
             return Vector3.right;
         case GravityDirection.Left:
             return Vector3.left;
         case GravityDirection.Up:
             return Vector3.up;
         case GravityDirection.Down:
             return Vector3.down;
         case GravityDirection.Forward:
             return Vector3.forward;
         case GravityDirection.Back:
             return Vector3.back;
         default:
             return Vector3.zero;
     }
 }
コード例 #27
0
ファイル: Character1.cs プロジェクト: evandyer1234/Richard
    void Start()
    {
        Physics2D.gravity  = new Vector2(0, -9.8f);
        m_GravityDirection = GravityDirection.Down;
        time         = attackTime;
        rb           = GetComponent <Rigidbody2D>();
        anim         = GetComponent <Animator>();
        localScale   = transform.localScale;
        currentSpeed = moveSpeed;
        SpinBox.SetActive(false);
        currentDashTime = dashTime;
        downHitBox.SetActive(false);
        sideHitBox.SetActive(false);
        Swipe.SetActive(false);
        Swipe2.SetActive(false);
        meleeeffect.SetActive(false);
        currentDeathTime = deathTime;

        healthSlider.maxValue = health;
    }
コード例 #28
0
    void RotateGravity(GravityDirection direction)
    {
        switch (direction)
        {
        case GravityDirection.Down:
            Physics2D.gravity = Vector2.down * gravityStrength;
            break;

        case GravityDirection.Left:
            Physics2D.gravity = Vector2.left * gravityStrength;
            break;

        case GravityDirection.Up:
            Physics2D.gravity = Vector2.up * gravityStrength;
            break;

        case GravityDirection.Right:
            Physics2D.gravity = Vector2.right * gravityStrength;
            break;
        }
    }
コード例 #29
0
        public static Vector2 ToVector2(this GravityDirection gravityDirection)
        {
            switch (gravityDirection)
            {
            case GravityDirection.Up:
                return(Vector2.up);

            case GravityDirection.Down:
                return(Vector2.down);

            case GravityDirection.Right:
                return(Vector2.right);

            case GravityDirection.Left:
                return(Vector2.left);

            default:
                Debug.LogError("Invalid gravityDirection value.");
                return(Vector2.zero);
            }
        }
コード例 #30
0
ファイル: SolverForm.cs プロジェクト: Mekeater/PSDS
        private void MeshFlieForm_Load(object sender, EventArgs e)
        {
            cb_SolverType.Items.AddRange(new string[] { "基于压力法(压力基)", "基于密度法(密度基)" });
            cb_VelocityFormulation.Items.AddRange(new string[] { "绝对速度", "相对速度" });
            cb_TimeType.Items.AddRange(new string[] { "稳态", "瞬态" });
            cb_GravityDirection.Items.AddRange(new string[] { "X", "Y", "Z" });

            cb_SolverType.SelectedIndex          = 0;
            cb_VelocityFormulation.SelectedIndex = 0;
            cb_TimeType.SelectedIndex            = 0;
            cb_GravityDirection.SelectedIndex    = 2;

            solverType          = SolverType.PressureBased;
            velocityFormulation = VelocityFormulation.Absolute;
            timeType            = TimeType.Steady;

            isGravity        = false;
            gravityDirection = GravityDirection.Z;

            this.lb_help.Text = "帮助:\r\n";
        }
コード例 #31
0
        private Dictionary <Entity, int> CalculateCellsOffsets(GameFieldSize gameSettings)
        {
            GravityDirection         gravityDirection = _helper.Gravity;
            CellsMap                 map            = new CellsMap(EntityManager);
            Dictionary <Entity, int> offsettedCells = new Dictionary <Entity, int>();
            var i = new IntIterator(new IteratorSettings(0, gameSettings.Width - 1, 1));

            while (i.MoveNext())
            {
                var j = new IntIterator(new IteratorSettings(0, gameSettings.Height - 1, -(int)gravityDirection));
                while (j.MoveNext())
                {
                    if (map.GetCell(i, j, out var cell))
                    {
                        continue;
                    }

                    var k = new IntIterator(new IteratorSettings(
                                                j - (int)gravityDirection,
                                                IteratorSettings.GetTo(0, gameSettings.Height - 1, -(int)gravityDirection),
                                                -(int)gravityDirection));
                    while (k.MoveNext())
                    {
                        if (!map.GetCell(i, k, out var affectedCell))
                        {
                            continue;
                        }

                        if (!offsettedCells.TryGetValue(affectedCell, out var o))
                        {
                            offsettedCells[affectedCell] = 0;
                        }

                        offsettedCells[affectedCell] += (int)gravityDirection;
                    }
                }
            }

            return(offsettedCells);
        }
コード例 #32
0
    void FixedUpdate()
    {
        switch (m_GravityDirection)
        {
        case GravityDirection.Left:
            //Change the gravity to go to the left
            Physics2D.gravity = new Vector2(-2f, 0);
            //Press the space key to change the direction of gravity
            if (Input.GetKeyDown(KeyCode.Space))
            {
                m_GravityDirection = GravityDirection.Up;
                Debug.Log("Up");
            }
            break;

        case GravityDirection.Up:
            //Change the gravity to be in a upward direction
            Physics2D.gravity = new Vector2(0, 2f);
            //Press the space key to change the direction
            if (Input.GetKeyDown(KeyCode.Space))
            {
                m_GravityDirection = GravityDirection.Right;
                Debug.Log("Right");
            }
            break;

        case GravityDirection.Right:
            //Change the gravity to go in the right direction
            Physics2D.gravity = new Vector2(2f, 0);
            //Press the space key to change the direction
            if (Input.GetKeyDown(KeyCode.Space))
            {
                m_GravityDirection = GravityDirection.Down;
                Debug.Log("Down");
            }

            break;
        }
    }
コード例 #33
0
    private void Update()
    {
        xRaw = Input.GetAxisRaw(horrizontalCtrl);
        yRaw = Input.GetAxisRaw(verticalCtrl);

        if (Input.GetAxis(gravSwitchButton) > 0)
        {
            //SwitchGravityDirection();  // TODO taking grav swtich out in first playable
        }

        UpdatePlayerOrientation();
        ApplyGravityForce();

        //Debug.Log(coll.wrapCorner);
        if (coll.wrapCorner)
        {
            StartCoroutine(ExampleCoroutine());
            //rb.velocity = Vector2.zero; // TODO
            m_GravityDirection = GravityDirection.Right;
            coll.wrapCorner    = false;
        }
    }
コード例 #34
0
    public void ApplyGravity(GravityDirection direction)
    {
        if (cooldown > 0)
        {
            return;
        }

        int axisIndex = (int)direction;

        if (gravityTimer <= 0)
        {
            gravityTimer = effectTime;
            isFalling    = false;
        }

        if (direction == GravityDirection.DOWN)
        {
            gravityTimer = 0;
            isFalling    = true;
        }

        gravity = GravityDirections[axisIndex];
    }
コード例 #35
0
		public GravityBehaviour(float coefficient, GravityDirection direction, float limit = DefaultSpeedLimit)
		{
			_GravityVector = Globals.CreateVector(coefficient, direction.ToAngle());
			SpeedLimit = limit;
		}
コード例 #36
0
ファイル: Player.cs プロジェクト: Express50/Bricklayer
        /// <summary>
        /// Calculates the Y velocity accounting for jumping and
        /// animates accordingly.
        /// </summary>
        /// <remarks>
        /// During the accent of a jump, the Y velocity is completely
        /// overridden by a power curve. During the decent, gravity takes
        /// over. The jump velocity is controlled by the jumpTime field
        /// which measures time into the accent of the current jump.
        /// </remarks>
        /// <param name="velocityY">
        /// The player's current velocity along the Y axis.
        /// </param>
        /// <returns>
        /// A new Y velocity if beginning or continuing a jump.
        /// Otherwise, the existing Y velocity.
        /// </returns>
        protected float DoJump(float velocityY, GameTime gameTime)
        {
            // If the player wants to jump
            if (IsJumping)
            {
                // Begin or continue a jump
                if ((!WasJumping && IsOnGround) || JumpTime > 0.0f)
                {
                    if (JumpTime == 0)
                        JumpDirection = GravityDirection; //Set the direction this jump started from
                    if (JumpTime == 0 && IsMine)
                    {
                        //Send message we are now jumping
                        PlayerStateMessage msg = new PlayerStateMessage(this);
                        msg.IsJumping = true;
                        Game.NetManager.Send(msg);
                    }
                    JumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds; //Incriment jump timer
                }

                //If we are in the ascent of the jump
                if (0.0f < JumpTime && JumpTime <= MaxJumpTime)
                {
                    //Fully override the vertical velocity with a power curve that gives players more control over the top of the jump
                    velocityY = (JumpDirection == GravityDirection.Up || JumpDirection == GravityDirection.Left ? -1 : 1) * (JumpLaunchVelocity * (1.0f - (float)Math.Pow(JumpTime / MaxJumpTime, JumpControlPower)));
                }
                else
                {
                    if (JumpTime > 0 && IsMine)
                    {
                        //Tell others we are falling now
                        PlayerStateMessage msg = new PlayerStateMessage(this);
                        msg.IsJumping = false;
                        Game.NetManager.Send(msg);
                    }
                    JumpTime = 0.0f;   // Reached the apex of the jump
                    IsJumping = false;
                }
            }
            else
            {
                //Tell others we have landed
                if (JumpTime > 0 && IsMine)
                    Game.NetManager.Send(new PlayerStateMessage(this));
                // Continues not jumping or cancels a jump in progress
                JumpTime = 0.0f;
            }
            WasJumping = IsJumping;

            return velocityY;
        }
コード例 #37
0
ファイル: Player.cs プロジェクト: Express50/Bricklayer
        /// <summary>
        /// Apply client-side physics to the character, calculate velocity from movement and gravity, handle collisions
        /// </summary>
        /// <param name="gameTime"></param>
        private void ApplyPhysics(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; //Time in seconds, since last frame
            float delta = elapsed * 60;

            //Face the direction the player is moving
            if (SimulationState.Movement.X > 0)
                Direction = FacingDirection.Right;
            else if (SimulationState.Movement.X < 0)
                Direction = FacingDirection.Left;

            if (Mode == PlayerMode.Normal)
            {
                if (!IsMine)
                    IsJumping = VirtualJump;
                //The next area applies the movement and gravity, based on the current direction the player is being pushed by arrows (if so)

                //If gravity is up or down
                if (GravityDirection == GravityDirection.Default || GravityDirection == GravityDirection.Down || GravityDirection == GravityDirection.Up)
                {
                    //Apply horizontal movement
                    //If not movement detected from keys, apply acceleration to move, if not, slowly stop
                    if (SimulationState.Movement.X != 0)
                        SimulationState.Velocity.X += SimulationState.Movement.X * MoveSpeed;
                    else
                        SimulationState.Velocity.X = MathHelper.Lerp(SimulationState.Velocity.X, 0, MoveSlowDownFactor);
                    SimulationState.Velocity.X = MathHelper.Clamp(SimulationState.Velocity.X, -MaxVelocity, MaxVelocity);

                    //Apply vertical movement (jump, gravity)
                    if (GravityDirection == GravityDirection.Up)
                    {
                        SimulationState.Velocity.Y = MathHelper.Clamp((SimulationState.Velocity.Y - GravityAcceleration * .0166f) - GravityAcceleration * .0166f, -MaxFallSpeed, MaxFallSpeed);
                        SimulationState.Velocity.Y = DoJump(SimulationState.Velocity.Y, gameTime);
                        SimulationState.Velocity.Y = MathHelper.Clamp((SimulationState.Velocity.Y - GravityAcceleration * .0166f), -MaxFallSpeed, MaxFallSpeed);
                    }
                    else if (GravityDirection == GravityDirection.Down)
                    {
                        SimulationState.Velocity.Y = MathHelper.Clamp(((SimulationState.Velocity.Y + (GravityAcceleration * 2.5f) * .0166f) + (GravityAcceleration * 2.5f) * .0166f), -MaxFallSpeed, MaxFallSpeed);
                        SimulationState.Velocity.Y = DoJump(SimulationState.Velocity.Y, gameTime);
                        SimulationState.Velocity.Y = MathHelper.Clamp(((SimulationState.Velocity.Y + (GravityAcceleration * 2.5f) * .0166f)), -MaxFallSpeed, MaxFallSpeed);
                    }
                    else
                    {
                        SimulationState.Velocity.Y = MathHelper.Clamp((SimulationState.Velocity.Y + GravityAcceleration * .0166f) + GravityAcceleration * .0166f, -MaxFallSpeed, MaxFallSpeed);
                        SimulationState.Velocity.Y = DoJump(SimulationState.Velocity.Y, gameTime);
                        SimulationState.Velocity.Y = MathHelper.Clamp(SimulationState.Velocity.Y + GravityAcceleration * .0166f, -MaxFallSpeed, MaxFallSpeed);
                    }
                }
                //If gravity is left or right
                if (GravityDirection == GravityDirection.Left || GravityDirection == GravityDirection.Right)
                {
                    //Apply vertical movement
                    //If not movement detected from keys, apply acceleration to move, if not, slowly stop
                    if (SimulationState.Movement.Y != 0)
                        SimulationState.Velocity.Y += SimulationState.Movement.Y * MoveSpeed;
                    else
                        SimulationState.Velocity.Y = MathHelper.Lerp(SimulationState.Velocity.Y, 0, MoveSlowDownFactor);
                    SimulationState.Velocity.Y = MathHelper.Clamp(SimulationState.Velocity.Y, -MaxVelocity, MaxVelocity);

                    //Apply vertical movement (jump, gravity)
                    if (GravityDirection == GravityDirection.Left)
                    {
                        SimulationState.Velocity.X = MathHelper.Clamp((SimulationState.Velocity.X - GravityAcceleration * .0166f) - GravityAcceleration * .0166f, -MaxFallSpeed, MaxFallSpeed);
                        SimulationState.Velocity.X = DoJump(SimulationState.Velocity.X, gameTime);
                        SimulationState.Velocity.X = MathHelper.Clamp((SimulationState.Velocity.X - GravityAcceleration * .0166f), -MaxFallSpeed, MaxFallSpeed);
                    }
                    else if (GravityDirection == GravityDirection.Right)
                    {
                        SimulationState.Velocity.X = MathHelper.Clamp(((SimulationState.Velocity.X + GravityAcceleration * .0166f) + GravityAcceleration * .0166f), -MaxFallSpeed, MaxFallSpeed);
                        SimulationState.Velocity.X = DoJump(SimulationState.Velocity.X, gameTime);
                        SimulationState.Velocity.X = MathHelper.Clamp(((SimulationState.Velocity.X + GravityAcceleration * .0166f)), -MaxFallSpeed, MaxFallSpeed);
                    }
                }

                //Apply pseudo-drag horizontally.
                if (IsOnGround)
                    SimulationState.Velocity.X *= GroundDragFactor;
                else
                    SimulationState.Velocity.X *= AirDragFactor;

                 //If gravity is up or down, apply horizontal collision, then vertical
                if (GravityDirection == GravityDirection.Default || GravityDirection == GravityDirection.Down || GravityDirection == GravityDirection.Up)
                {
                    GravityDirection = GravityDirection.Default;
                    HorizontalCollision(gameTime, elapsed); //Horizontal Collison, X Axis
                    VerticalCollision(gameTime, elapsed); //Vertical Collision, Y Axis
                }
                //If gravity is left or right, apply vertical collision, then horizontal
                else if (GravityDirection == GravityDirection.Left || GravityDirection == GravityDirection.Right)
                {
                    GravityDirection = GravityDirection.Default;
                    VerticalCollision(gameTime, elapsed); //Vertical Collision, Y Axis
                    HorizontalCollision(gameTime, elapsed); //Horizontal Collison, X Axis
                }

                //If the collision stopped us from moving, reset the velocity to zero.
                if (SimulationState.Position.X == PreviousState.Position.X)
                    SimulationState.Velocity.X = 0;
                if (SimulationState.Position.Y == PreviousState.Position.Y)
                    SimulationState.Velocity.Y = 0;
            }
            else if (Mode == PlayerMode.God)
            {
                //If not movement detected from keys, apply acceleration to move, if not, slowly stop
                if (SimulationState.Movement.X != 0)
                    SimulationState.Velocity.X += SimulationState.Movement.X * GodMoveSpeed;
                else
                    SimulationState.Velocity.X = MathHelper.Lerp(SimulationState.Velocity.X, 0, GodMoveSlowDownFactor);

                if (SimulationState.Movement.Y != 0)
                    SimulationState.Velocity.Y += SimulationState.Movement.Y * GodMoveSpeed;
                else
                    SimulationState.Velocity.Y = MathHelper.Lerp(SimulationState.Velocity.Y, 0, GodMoveSlowDownFactor);

                SimulationState.Velocity.X = MathHelper.Clamp(SimulationState.Velocity.X, -MaxGodVelocity, MaxGodVelocity);
                SimulationState.Velocity.Y = MathHelper.Clamp(SimulationState.Velocity.Y, -MaxGodVelocity, MaxGodVelocity);
                //X Axis
                SimulationState.Velocity.X *= delta;
                Vector2 change = SimulationState.Velocity.X * Vector2.UnitX * .0166f;
                change.X = MathHelper.Clamp(change.X, -10, 10);
                SimulationState.Position += change;
                SimulationState.Position = new Vector2((float)Math.Round(SimulationState.Position.X), SimulationState.Position.Y);

                //Y Axis
                SimulationState.Velocity.Y *= delta;
                change = SimulationState.Velocity.Y * Vector2.UnitY * .0166f;
                change.Y = MathHelper.Clamp(change.Y, -10, 10);
                SimulationState.Position += change;
                SimulationState.Position = new Vector2(SimulationState.Position.X, (float)Math.Round(SimulationState.Position.Y));

                //Clamp position in bounds
                SimulationState.Position.X = MathHelper.Clamp(SimulationState.Position.X, Tile.Width, (Map.Width * Tile.Width) - (Tile.Width * 2));
                SimulationState.Position.Y = MathHelper.Clamp(SimulationState.Position.Y, Tile.Height, (Map.Height * Tile.Height) - (Tile.Height * 2));
            }

            //Set idle states
            if (SimulationState.Position == PreviousState.Position)
                IdleTime += elapsed;
            else
                IdleTime = 0;
        }
コード例 #38
0
ファイル: Player.cs プロジェクト: Express50/Bricklayer
        /// <summary>
        /// Handles collisions for a given block
        /// </summary>
        private bool HandleCollisions(GameTime gameTime, CollisionDirection direction, BlockCollision collision, Rectangle tileBounds, Vector2 depth, bool intersects, int x, int y)
        {
            if (collision != BlockCollision.Passable && intersects)
            {
                // If we crossed the top of a tile, we are on the ground.
                if (PreviousState.Bounds.Bottom <= tileBounds.Top)
                {
                    if (collision == BlockCollision.Platform)
                    {
                        IsOnGround = true;
                    }
                }
                if (collision == BlockCollision.Gravity)
                {
                    Tile tile = Map.Tiles[x, y, 1];
                    if (tile.Block == BlockType.UpArrow)
                        GravityDirection = GravityDirection.Up;
                    else if (tile.Block == BlockType.DownArrow)
                        GravityDirection = GravityDirection.Down;
                    else if (tile.Block == BlockType.RightArrow)
                        GravityDirection = GravityDirection.Right;
                    else if (tile.Block == BlockType.LeftArrow)
                        GravityDirection = GravityDirection.Left;
                    return true;
                }
                if (collision == BlockCollision.Impassable || IsOnGround)
                {
                    //Now that we know we hit something, resolve the collison
                    if (direction == CollisionDirection.Horizontal)
                    {
                        SimulationState.Position.X += depth.X;
                        IsOnGround = true;
                    }
                    if (direction == CollisionDirection.Vertical)
                    {
                        //Cancel jump if hit something (Ie, when you jump and hit the roof)
                        IsJumping = false;
                        JumpTime = 0;
                        //Obviously hit ground or roof
                        IsOnGround = true;

                        SimulationState.Position.Y += depth.Y;
                    }
                }
            }
            return false;
        }
コード例 #39
0
ファイル: Player.cs プロジェクト: JaakkoLipsanen/WVVW
 private void CheckIfNeedsPreOrLateJumping(UpdateContext updateContext)
 {
     _timeSinceLastJumpRequest += updateContext.DeltaSeconds;
     if (this.IsOnGround)
     {
         _timeSinceWasOnGround = 0f;
         if (_timeSinceLastJumpRequest < Player.JumpTimeBias)
         {
             _gravityDirection = _gravityDirection.Inverse();
             _timeSinceLastJumpRequest = float.MaxValue;
         }
     }
     else
     {
         _timeSinceWasOnGround += updateContext.DeltaSeconds;
     }
 }
コード例 #40
0
ファイル: Player.cs プロジェクト: JaakkoLipsanen/WVVW
        private void CheckIfIsCollidingWithSpring(UpdateContext updateContext)
        {
            bool collidedWithAny = false;
            foreach (Spring spring in _world.Springs)
            {
                if (this.Area.Intersects(spring.Area))
                {
                    collidedWithAny = true;
                    if (spring != _previouslyCollidingSpring)
                    {
                        _previouslyCollidingSpring = spring;
                        spring.IsTriggered = true;
                        _gravityDirection = _gravityDirection.Inverse();

                        break;
                    }
                }
            }

            if (!collidedWithAny && _previouslyCollidingSpring != null)
            {
                _previouslyCollidingSpring.IsTriggered = false;
                _previouslyCollidingSpring = null;
            }
        }
コード例 #41
0
ファイル: Player.cs プロジェクト: JaakkoLipsanen/WVVW
 public void Jump()
 {
     if (_isAlive)
     {
         if (this.IsOnGround)
         {
             _gravityDirection = _gravityDirection.Inverse();
         }
         else
         {
             _timeSinceLastJumpRequest = 0f;
             if (_timeSinceWasOnGround < Player.JumpTimeBias)
             {
                 _gravityDirection = _gravityDirection.Inverse();
             }
         }
     }
 }
 /// <summary>
 /// Start this instance.
 /// </summary>
 public void Start()
 {
     _currentGravityDirection = GravityDirection.DOWN;
 }
 /// <summary>
 /// Toggles the direction of gravity on this object.
 /// </summary>
 public GravityDirection Swtich()
 {
     return _currentGravityDirection = (_currentGravityDirection == GravityDirection.DOWN ? GravityDirection.UP : GravityDirection.DOWN);
 }