Newer
Older
BlackoutClient / Assets / Player / Player.cs
@Mark Mark on 1 Feb 2020 4 KB View fade area
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    private Item netItem;
    public bool ForceLocal;

    public bool IsLocal
    {
        get
        {
            return ForceLocal || (netItem != null && netItem.IsLocal);
        }
    }

    private UIHookup uiHookup;

    private CharacterController charControl;

    private PlayerFade playerFade;
    private Camera playerFadeCam;
    // Start is called before the first frame update
    void Awake()
    {
        netItem = GetComponentInChildren<Item>();
        castLight = transform.Find("CastLight").gameObject;

        uiHookup = FindObjectOfType<UIHookup>();

        if (ForceLocal)
        {
            StartLocal();
        }
    }

    private GameObject castLight;
    private float bottomOffset;
    void StartLocal()
    {
        castLight.SetActive(true);
        charControl = GetComponentInChildren<CharacterController>();
        charControl.enabled = true;

        bottomOffset = -charControl.center.y + charControl.height * 0.5f;

        playerFade = FindObjectOfType<PlayerFade>();
        playerFadeCam = playerFade.GetComponent<Camera>();
    }

    public float Speed;

    private List<Touch> touches = new List<Touch>();

    public float MoveTouchFullZone = 0.7f;
    public float MoveTouchRadiusSize = 1.45f;

    private float viewRadius = 4;

    // Update is called once per frame
    void Update()
    {
        if (IsLocal && charControl != null)
        {
            RaycastHit hit;
            if (Physics.Raycast(new Ray(transform.position, Vector3.down), out hit, 10f))
            {
                Vector3 pos = transform.position;
                pos.y = hit.point.y + (bottomOffset + 0.01f);
                transform.position = pos;
            }

            Vector3 wantMove = new Vector3();

            if (Input.GetKey(KeyCode.UpArrow))
            {
                wantMove.z += 1;
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                wantMove.z -= 1;
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                wantMove.x += 1;
            }
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                wantMove.x -= 1;
            }

            wantMove.Normalize();

            if (uiHookup != null && uiHookup.MoveTouch != null && uiHookup.MainCanvas != null)
            {
                float radius = uiHookup.MoveTouch.rect.width * 0.5f * MoveTouchRadiusSize;

                touches.Clear();
                touches.AddRange(Input.touches);

                if (Input.GetMouseButton(0))
                {
                    touches.Add(new Touch { position = Input.mousePosition });
                }

                foreach (Touch touch in touches)
                {
                    Vector2 diff = new Vector2();
                    RectTransformUtility.ScreenPointToLocalPointInRectangle(uiHookup.MoveTouch, touch.position, null, out diff);
                    if (diff.magnitude < radius)
                    {
                        wantMove = new Vector3(diff.x, 0, diff.y) / (radius * MoveTouchFullZone);
                        if (wantMove.magnitude > 1) wantMove.Normalize();
                    }
                }
            }

            

            if (wantMove.sqrMagnitude > 0)
            {
                wantMove *= Speed * Time.deltaTime;
                wantMove.y = -0.1f * Time.deltaTime;
                CollisionFlags flags = charControl.Move(wantMove);
            }

            if (playerFade != null && playerFadeCam != null)
            {
                Vector3 playerScreen3 = playerFadeCam.WorldToScreenPoint(transform.position);
                Vector3 edgeScreen3 = playerFadeCam.WorldToScreenPoint(transform.position + playerFadeCam.transform.right * viewRadius);

                Vector2 playerScreen2 = new Vector2(playerScreen3.x / playerFadeCam.pixelWidth, playerScreen3.y / playerFadeCam.pixelHeight);
                Vector2 edgeScreen2 = new Vector2(edgeScreen3.x / playerFadeCam.pixelWidth, edgeScreen3.y / playerFadeCam.pixelHeight);

                float radius = (edgeScreen2 - playerScreen2).magnitude;

                playerFade.PlayerFadeMat.SetVector("_MainCameraPos", new Vector4(playerScreen2.x, playerScreen2.y, 0, 0));
                playerFade.PlayerFadeMat.SetFloat("_Radius", radius);
            }
        }
    }
}