コード例 #1
0
    void HandleInput()
    {
        float moveSpeed = MovementSpeed * Time.deltaTime;
        float rotSpeed  = TurnSpeed * Time.deltaTime;

        float vert = Input.GetAxisRaw("Player" + PlayerNumber + "_Vertical");
        float hori = Input.GetAxisRaw("Player" + PlayerNumber + "_Horizontal");

        if (vert > 0) //up
        {
            if (World.worldMap[(int)(Y), (int)(X + dirX * moveSpeed)] <= 0)
            {
                X += dirX * moveSpeed;
            }
            if (World.worldMap[(int)(Y + dirY * moveSpeed), (int)(X)] <= 0)
            {
                Y += dirY * moveSpeed;
            }
        }
        //move backwards if no wall behind you
        if (vert < 0) //down
        {
            if (World.worldMap[(int)(Y), (int)(X - dirX * moveSpeed)] <= 0)
            {
                X -= dirX * moveSpeed;
            }
            if (World.worldMap[(int)(Y - dirY * moveSpeed), (int)(X)] <= 0)
            {
                Y -= dirY * moveSpeed;
            }
        }
        //rotate to the right
        if (hori > 0) //right
        {
            //both camera direction and camera plane must be rotated
            float oldDirX = dirX;
            dirX = dirX * Mathf.Cos(-rotSpeed) - dirY * Mathf.Sin(-rotSpeed);
            dirY = oldDirX * Mathf.Sin(-rotSpeed) + dirY * Mathf.Cos(-rotSpeed);
            float oldPlaneX = planeX;
            planeX = planeX * Mathf.Cos(-rotSpeed) - planeY * Mathf.Sin(-rotSpeed);
            planeY = oldPlaneX * Mathf.Sin(-rotSpeed) + planeY * Mathf.Cos(-rotSpeed);
        }
        //rotate to the left
        if (hori < 0) //left
        {
            //both camera direction and camera plane must be rotated
            float oldDirX = dirX;
            dirX = dirX * Mathf.Cos(rotSpeed) - dirY * Mathf.Sin(rotSpeed);
            dirY = oldDirX * Mathf.Sin(rotSpeed) + dirY * Mathf.Cos(rotSpeed);
            float oldPlaneX = planeX;
            planeX = planeX * Mathf.Cos(rotSpeed) - planeY * Mathf.Sin(rotSpeed);
            planeY = oldPlaneX * Mathf.Sin(rotSpeed) + planeY * Mathf.Cos(rotSpeed);
        }

        bool canshoot = FpsGameModeInstance.IsOpened;

        if (canshoot && Input.GetButtonDown("Player" + PlayerNumber + "_Fire"))
        {
            lastFireTime = Time.time;
            int clipnum = UnityEngine.Random.Range(0, ShotSounds.Length - 1);
            audioSource.PlayOneShot(ShotSounds[clipnum]);

            Vector2     start = new Vector2(X, Y);
            LineSegment line  = new LineSegment(start, start + new Vector2(dirX, dirY) * ShotRange);

            Vector2         dir    = new Vector2(dirX, dirY) * ShotRange;
            ContactFilter2D filter = new ContactFilter2D();
            RaycastHit2D[]  hits   = new RaycastHit2D[20];



            Vector2 hitVector = new Vector2();
            if (GridSystem.IntersectsElement(start, dir, World, Renderer.Rect.rect.width, out hitVector) == true)
            {
                int amount = Physics2D.Raycast(line.Start, dir, filter, hits, (hitVector - line.Start).magnitude);
                if (amount > 0)
                {
                    for (var i = 0; i < amount; i++)
                    {
                        var hit = hits[i];
                        if (hit.collider == collider2D)
                        {
                            continue;
                        }

                        m_pointsystem.ShotPlayer();
                        //-----------------------------------------Add Points shooting------------------------------//
                        Debug.Log("Shot someone!!!");
                        var player = hit.collider.GetComponent <FPSPlayer>();
                        if (player)
                        {
                            //player.Health -= DamagePerShot;
                            player.Hit(DamagePerShot, this);
                            player.Pointsystem.GotShot();
                        }
                    }
                }
                else
                {
                    return;
                }
            }
        }
    }