/// <summary>
    /// Handles the mouse input for the player
    /// </summary>
    void HandleMouseInput()
    {
        // if the cursor is not locked, just return
        if (Cursor.lockState == CursorLockMode.None)
        {
            return;
        }

        float xMovement = Input.GetAxisRaw("Mouse X") * sensitivity;
        float yMovement = Input.GetAxisRaw("Mouse Y") * sensitivity;

        // apply the x-movement (rotation around y-axis) to the rigidbody
        transform.Rotate(Vector3.up * xMovement);
        // apply the y-movement (rotation around the x-axis) to the camera)
        transform.GetChild(0).transform.Rotate(Vector3.right * -yMovement);

        // handle any click event
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit raycastHit;
            Transform  camera = transform.GetChild(0).transform;
            if (Physics.Raycast(camera.position, camera.forward, out raycastHit, 100))
            {
                Vector3 blockPos = EditTerrain.GetBlockPos(raycastHit);
                EditTerrain.SetBlock(raycastHit, new AirBlock(blockPos));
            }
        }
    }
示例#2
0
文件: Player.cs 项目: paupul/dugcra
    public void AttemptMove(int xDir, int yDir)
    {
        if (Time.timeScale == 1)
        {
            Vector2      end = rb2D.position + new Vector2(xDir, yDir);
            RaycastHit2D fogDetect;
            RaycastHit2D walldetect;

            fogDetect  = Physics2D.Linecast(rb2D.position, end, fog);
            walldetect = Physics2D.Linecast(rb2D.position, end, wall); //nekeisti

            WorldPos pos = EditTerrain.GetBlockPos(fogDetect);
            //Debug.Log(pos.x + " " + pos.y);

            if (fogDetect)
            {
                fogWorld.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty));
                scoreManager.AddPoints(1);
            }
            //if (fogDetect)
            //{
            //    GameObject.Find(fogDetect.transform.name).GetComponent<SpriteRenderer>().enabled = false;
            //    print(fogDetect.transform.name);
            //    GameObject.Find(fogDetect.transform.name).GetComponent<BoxCollider2D>().enabled = false;
            //    Destroy(GameObject.Find(fogDetect.transform.name).GetComponent<GameObject>());
            //}
            if (!walldetect && !fogDetect)
            {
                gameSounds.PlaySound(rnd.Next(0, 2));
                rb2D.MovePosition(end);
            }
        }
        StartCoroutine(Delay());
    }
示例#3
0
    void Mine()
    {
        if (anim)
        {
            anim.SetTrigger("Mine");
        }
        // anim.Play("Mining");
        RaycastHit hit;

        //if (Physics.Raycast(transform.position, transform.forward, out hit, 100))
        if (Physics.Raycast(myCam.transform.position, myCam.transform.forward, out hit, 5))
        {
            Vector3 pos   = new Vector3(EditTerrain.GetBlockPos(hit).x, EditTerrain.GetBlockPos(hit).y, EditTerrain.GetBlockPos(hit).z);
            Block   block = EditTerrain.GetBlock(hit);
            EditTerrain.SetBlock(hit, new BlockAir(), false, true);
            if (block is BlockGrass)
            {
                Instantiate(item_grass, pos, Quaternion.identity);
            }
            else if (block is BlockSand)
            {
                Instantiate(item_sand, pos, Quaternion.identity);
            }
            else if (block is BlockWood)
            {
                Instantiate(item_wood, pos, Quaternion.identity);
            }
        }
    }
示例#4
0
    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical   = Input.GetAxis("Vertical");

        horizontal = horizontal * 5 * Time.deltaTime;
        vertical   = vertical * 5 * Time.deltaTime;

        Vector3 position = new Vector3(transform.position.x + horizontal, transform.position.y + vertical, transform.position.z);

        transform.position = position;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Vector2      rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
            RaycastHit2D hit    = Physics2D.Raycast(rayPos, Vector2.zero);
            WorldPos     pos    = EditTerrain.GetBlockPos(hit);
            if (hit.collider != null && hit.collider.GetComponent <Grid>())
            {
                fog.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty));
            }
        }
        if (Input.GetMouseButtonDown(1))
        {
            cam.orthographicSize = 5;
        }
        if (Input.GetMouseButtonUp(1))
        {
            cam.orthographicSize = 8;
        }
    }
示例#5
0
文件: Player.cs 项目: yauver/Dugcra
    public void AttemptMove(int xDir, int yDir)
    {
        Vector2      end = rb2D.position + new Vector2(xDir, yDir);
        RaycastHit2D fogDetect;
        RaycastHit2D walldetect;

        fogDetect = Physics2D.Raycast(rb2D.position, end);
        Debug.Log(end.y);
        walldetect = Physics2D.Linecast(rb2D.position, end, wall);
        WorldPos pos = EditTerrain.GetBlockPos(end);

        Debug.Log(pos.y);
        if (fogDetect)
        {
            fogWorld.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty));
        }
        //if (fogDetect)
        //{
        //    GameObject.Find(fogDetect.transform.name).GetComponent<SpriteRenderer>().enabled = false;
        //    print(fogDetect.transform.name);
        //    GameObject.Find(fogDetect.transform.name).GetComponent<BoxCollider2D>().enabled = false;
        //    Destroy(GameObject.Find(fogDetect.transform.name).GetComponent<GameObject>());
        //}
        if (!walldetect)
        {
            //print(walldetect.transform.name);
            rb2D.MovePosition(end);
            //print("Moved");
        }
        StartCoroutine(Delay());
    }
示例#6
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        WorldPos pos = EditTerrain.GetBlockPos(collision.contacts[0].point);

        if (collision.collider.GetComponent <Grid>())
        {
            fog.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty));
        }
    }
示例#7
0
 public void AddToGarden(string name, int numStock)
 {
     Debug.Log(name);
     Debug.Log(numStock);
     Debug.Log(companyGardenPositions);
     if (companyGardenPositions.Contains(name))
     {
         Vector3 pos   = (Vector3)companyGardenPositions[name];
         Block   block = CreateBlock(name);
         block.selling = true;
         SetBlock(EditTerrain.GetBlockPos(pos).x, EditTerrain.GetBlockPos(pos).y + numStock, EditTerrain.GetBlockPos(pos).z, block);
     }
 }
    public static void SetBlock(RaycastHit hit, Block block, bool adjacent = false)
    {
        if (!singleton.isConnected)
        {
            Debug.LogError("Cannont set block client is not connected");
            return;
        }
        WorldPos pos = EditTerrain.GetBlockPos(hit, adjacent);
        var      msg = new MessaageTypes.SetBlockMessage();

        msg.pos     = pos;
        msg.blockID = BlockIDManager.GetID(block);
        singleton.client.Send(MessaageTypes.SetBlockID, msg);
    }
示例#9
0
    void FixedUpdate()
    {
        if (Vector3.Magnitude(rb.velocity) > 0.001f)
        {
            timer = 0;
        }

        //Debug.Log(rb.velocity);
        // Debug.Log(timer);
        if (thrown)
        {
            rb.AddForce(Physics.gravity * rb.mass * gravityMultiplier);
            timer += Time.deltaTime;
            if (timer > 5)
            {
                gameObject.SetActive(false);
                GameObject.Find("World").GetComponent <World>().SetBlock(EditTerrain.GetBlockPos(transform.position).x, EditTerrain.GetBlockPos(transform.position).y, EditTerrain.GetBlockPos(transform.position).z, CreateBlock());
            }
        }
    }
示例#10
0
    //TODO clean this up to reduce raycasts
    void Update()
    {
        //if (GetComponent<FirstPersonControllerCustom>().inputLocked)
        //return;

        //Block selector
        RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))
        {
            if (!blockSelector.gameObject.activeSelf)
            {
                blockSelector.gameObject.SetActive(true);
            }
            hit.point += (-hit.normal * 0.1f); //Smudging in a bit to fix edge case
            Vector3Int pos = EditTerrain.GetBlockPos(hit);
            blockSelector.position = pos.ToVector3();
        }
        else
        {
            if (blockSelector.gameObject.activeSelf)
            {
                blockSelector.gameObject.SetActive(false);
            }
        }

        if (currentBlockPlaceCooldown > 0f)
        {
            currentBlockPlaceCooldown -= Time.deltaTime;
            return;
        }

        //Break block
        if (PlayerInputManager.input.Attack.IsPressed)
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))//, int.MaxValue, QueryTriggerInteraction.Ignore)) {
            {
                EditTerrain.BreakBlock(hit);
                currentBlockPlaceCooldown = blockPlaceCooldown;
            }
        }

        //Place block
        if (PlayerInputManager.input.Use.IsPressed)
        {
//            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))//, int.MaxValue, QueryTriggerInteraction.Ignore)) {
            {
                if (GetComponent <PlayerInventory>().CurrentActiveItem != null && GetComponent <PlayerInventory>().CurrentActiveItem.placeable)
                {
                    hit.point += hit.normal;
                    Vector3Int pos  = EditTerrain.GetBlockPos(hit);
                    Collider[] cols = Physics.OverlapBox(pos.ToVector3(), Vector3.one * 0.45f, Quaternion.identity, LayerMask.GetMask("Default", "Blocks"), QueryTriggerInteraction.Ignore);
                    if (cols.Length > 0)
                    {
                        //Something in way
                        foreach (Collider col in cols)
                        {
                            if (col.GetComponentInChildren <Renderer>() != null)
                            {
                                StartCoroutine(TempFlashRed(col.GetComponentInChildren <Renderer>()));
                            }
                        }
                    }
                    else
                    {
                        //                    RaycastHit hit2;
                        //                    if (Physics.BoxCast(pos.ToVector3(), Vector3.one * 0.35f, Vector3.up * 0.001f, out hit2))
                        //                    {
                        //                        Debug.LogError(pos.ToVector3() + " - " + hit2.collider.gameObject.name + " - " + hit2.distance);
                        //                        ExtDebug.DrawBoxCastOnHit(pos.ToVector3(), Vector3.one * 0.35f, Quaternion.identity, Vector3.up * 0.001f, 0f, Color.red);
                        //                    }

                        //TODO fix
                        EditTerrain.PlaceBlock(hit, FindObjectOfType <PlayerInventory>().CurrentActiveItem.placeableBlockID);
                        GetComponent <PlayerInventory>().ConsumeCurrentItem();
                        currentBlockPlaceCooldown = blockPlaceCooldown;
                    }
                }
            }
        }
    }