Ejemplo n.º 1
0
    public static RewinderSnapshot Create()
    {
        RewinderSnapshot snapshot = RewinderSnapshotPool.Instance.Get(currentGroups);

        if (latest == null)
        {
            latest = oldest = snapshot;
        }
        else
        {
            snapshot.Next      = latest;
            snapshot.Next.Prev = snapshot;
            latest             = snapshot;
        }

        ++count;

        while (count > MaxSnapshots)
        {
            --count;

            RewinderSnapshot temp = oldest;
            oldest      = temp.Prev;
            oldest.Next = null;

            RewinderSnapshotPool.Instance.Return(temp);
        }

        return(snapshot);
    }
Ejemplo n.º 2
0
    void OnHit(List <RewinderHitboxHit> hits)
    {
        var firstHit = hits.FirstOrDefault();

        /* Keeping this for easy debugging
         * foreach (var hit in hits)
         * {
         *      Debug.Log("We hit: " + hit.Hitbox.Transform.gameObject.name);
         *      if (hit.Hitbox.Transform.parent != null)
         *              Debug.Log("Hits parent is: " + hit.Hitbox.Transform.transform.parent.gameObject.name);
         * }
         */
        RewinderSnapshot.Recycle(hits);

        if (firstHit.Transform.gameObject.tag == "Enemy")
        {
            //Check for a parent object, if one is found. Get it's Enemy script and apply damage
            if (firstHit.Transform.parent != null)
            {
                var enemyObj = firstHit.Transform.parent.gameObject;
                enemyObj.GetComponent <Enemy>().photonView.RPC("NetworkApplyDamageToEnemy", PhotonTargets.All, primaryWeapon.damage);
            }
            else
            {
                var enemyObj = firstHit.Transform.gameObject;
                enemyObj.GetComponent <Enemy>().photonView.RPC("NetworkApplyDamageToEnemy", PhotonTargets.All, primaryWeapon.damage);
            }
        }
    }
Ejemplo n.º 3
0
 private void HandleNewSnapshot(RewinderSnapshot obj)
 {
     if (photonView.isMine)
     {
         RewinderHitboxGroupSnapshot group = obj.Groups.Where(e => e.Group == hitboxGroup).First();            //obj.Groups.First();
         correctPos = group.Position;
         correctRot = group.Rotation;
     }
 }
Ejemplo n.º 4
0
    public static bool Raycast(float time, Vector3 origin, Vector3 direction, List <RewinderHitboxHit> result)
    {
        time = GetTime() - time;
        RewinderSnapshot snapshot = Find(time);

        if (snapshot != null)
        {
            snapshot.Raycast(origin, direction, result);
        }

        return(result.Count > 0);
    }
Ejemplo n.º 5
0
    public static bool OverlapSphere(float time, Vector3 origin, float radius, List <RewinderHitboxHit> result)
    {
        time = GetTime() - time;
        RewinderSnapshot snapshot = Find(time);

        if (snapshot != null)
        {
            snapshot.OverlapSphere(origin, radius, result);
        }

        return(result.Count > 0);
    }
Ejemplo n.º 6
0
    public static void Reset()
    {
        RewinderSnapshot current = latest;

        while (current != null)
        {
            RewinderSnapshot temp = current;
            current = current.Next;
            RewinderSnapshotPool.Instance.Return(temp);
        }

        count  = 0;
        oldest = null;
        latest = null;
    }
Ejemplo n.º 7
0
    IEnumerator CreateSnapshot()
    {
        if (running)
        {
            yield break;
        }

        running = true;

        int frameCounter    = 0;
        int snapshotCounter = 0;

        while (running)
        {
            switch (FrameType)
            {
            case FrameTypes.Update:
                yield return(new WaitForEndOfFrame());

                break;

            case FrameTypes.FixedUpdate:
                yield return(new WaitForFixedUpdate());

                break;
            }

            if (running)
            {
                if (++frameCounter >= SnapshotEveryNthFrame)
                {
                    RewinderSnapshot snapshot = RewinderSnapshot.Create();

                    if (++snapshotCounter >= RaiseEveryNthSnapshot)
                    {
                        if (NewSnapshot != null)
                        {
                            NewSnapshot(snapshot);
                        }

                        snapshotCounter = 0;
                    }

                    frameCounter = 0;
                }
            }
        }
    }
Ejemplo n.º 8
0
    public override void Fire()
    {
        PhotonNetwork.Instantiate(this.ammo.ammoResourceName, this.bulletOrigin.position, bulletOrigin.transform.rotation, 0);

        if (photonView.isMine)
        {
            Ray r = new Ray(bulletOrigin.position, bulletOrigin.forward);

            List <RewinderHitboxHit> hits = new List <RewinderHitboxHit>();
            RewinderSnapshot.Raycast(Time.time, r.origin, r.direction, out hits);

            if (hits.Count > 0)
            {
                FireOnHit(hits);
            }
        }
    }
Ejemplo n.º 9
0
    void Start()
    {
        if (ProximityHitbox != null)
        {
            ProximityHitbox.Initialize();
        }

        if (BodyHitboxes != null)
        {
            for (int i = 0; i < BodyHitboxes.Length; ++i)
            {
                BodyHitboxes[i].Initialize();
            }
        }

        RewinderSnapshot.RegisterGroup(this);
    }
Ejemplo n.º 10
0
 void Start()
 {
     photonView      = GetComponent <PhotonView>();
     snapshotCreator = GetComponent <RewinderSnapshotCreator>();
     if (photonView.isMine)
     {
         hitboxGroup = GetComponent <RewinderHitboxGroup>();
         RewinderSnapshot.RegisterGroup(hitboxGroup);
         if (snapshotCreator != null)
         {
             snapshotCreator.NewSnapshot += HandleNewSnapshot;
         }
     }
     else
     {
         if (snapshotCreator != null)
         {
             snapshotCreator.enabled = false;
         }
     }
 }
Ejemplo n.º 11
0
    public static RewinderSnapshot Find(float time)
    {
        // TODO: Currently O(n), needs to get faster

        float            foundTime       = float.MaxValue;
        RewinderSnapshot foundSnapshot   = latest;
        RewinderSnapshot currentSnapshot = latest;

        while (currentSnapshot != null)
        {
            float length = Mathf.Abs(currentSnapshot.Time - time);

            if (length < foundTime)
            {
                foundTime     = length;
                foundSnapshot = currentSnapshot;
            }

            currentSnapshot = currentSnapshot.Next;
        }

        return(foundSnapshot);
    }
Ejemplo n.º 12
0
 void OnDestroy()
 {
     RewinderSnapshot.UnregisterGroup(this);
 }
Ejemplo n.º 13
0
    void Update()
    {
        if (Application.isPlaying && RewinderSnapshot.Count > 0)
        {
            RewinderSnapshot snapshot = RewinderSnapshot.Find(Time.time - behind);

            if (snapshot != null)
            {
                accuracy = (float)System.Math.Round(Mathf.Abs((Time.time - behind) - snapshot.Time), 3);
                RewinderHitboxGroupSnapshot group = snapshot.Groups.First();

                // On first time, setup debug display

#if REWINDER_DEBUG
                if (debugDict.Count == 0)
                {
                    foreach (RewinderHitbox hitbox in group.Group.BodyHitboxes)
                    {
                        switch (hitbox.ColliderType)
                        {
                        case RewinderColliderType.Sphere:
                        {
                            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                            go.renderer.material    = Resources.Load("RewinderDebug", typeof(Material)) as Material;
                            go.transform.localScale = hitbox.Collider.bounds.size;
                            debugDict.Add(hitbox, go);
                        }
                        break;

                        case RewinderColliderType.Box:
                        {
                            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                            go.transform.localScale = hitbox.Bounds.size;
                            go.renderer.material    = Resources.Load("RewinderDebug", typeof(Material)) as Material;
                            debugDict.Add(hitbox, go);
                        }
                        break;
                        }
                    }
                }

                // Move debug display

                for (int i = 0; i < group.HitboxMatrices_Debug.Length; ++i)
                {
                    Matrix4x4      m      = group.HitboxMatrices_Debug[i];
                    RewinderHitbox hitbox = group.Group.BodyHitboxes[i];
                    GameObject     go     = debugDict[hitbox];
                    go.transform.position = m.MultiplyPoint(Vector3.zero);
                    go.transform.rotation = group.HitboxRotations_Debug[i];
                }
#endif

                // Do collision detection (if any)

                if (Input.GetMouseButtonDown(0))
                {
                    RaycastHit rHit;
                    Ray        r = Camera.main.ScreenPointToRay(Input.mousePosition);
                    System.Diagnostics.Stopwatch sw = null;

                    switch (selected)
                    {
                    case 0:
                    {
                        List <RewinderHitboxHit> hits;

                        // Time how long the raycast takes
                        sw = System.Diagnostics.Stopwatch.StartNew();
                        RewinderSnapshot.Raycast(behind, r.origin, r.direction, out hits);
                        sw.Stop();

                        hit = hits.FirstOrDefault();
                        RewinderSnapshot.Recycle(hits);
                        sphereExample.active = false;
                    }
                    break;

                    case 1:
                        if (Physics.Raycast(r, out rHit, 1024f, 1 << 8))
                        {
                            drawPosition = rHit.point;
                            List <RewinderHitboxHit> hits;

                            // Time how long the overlap takes
                            sw = System.Diagnostics.Stopwatch.StartNew();
                            RewinderSnapshot.OverlapSphere(behind, drawPosition, 0.5f, out hits);
                            sw.Stop();

                            hit = hits.FirstOrDefault();
                            RewinderSnapshot.Recycle(hits);
                            sphereExample.transform.position = drawPosition;
                            sphereExample.active             = true;
                        }
                        break;
                    }

                    sw.Stop();
                    time = (float)((double)sw.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency);
                }
            }
        }
    }