Beispiel #1
0
    public void SetLockOnLineIntersection()
    {
        TSCollider tsCollider            = GetComponent <TSCollider>();
        bool       beforeUnlockedFlag    = tsCollider.enabled;
        bool       isUnlockedFlag        = true;
        FP         radius                = GetComponent <Renderer>().bounds.extents.magnitude;
        FP         distanceToWorldCenter = tsTransform.position.magnitude;

        foreach (TSRigidBody rb in Line.LineList)
        {
            // Where is the nearest point on line?
            TSVector nearestPointOnLine = rb.tsTransform.forward * distanceToWorldCenter;
            if (TSVector.Distance(nearestPointOnLine, tsTransform.position) < radius)
            {
                isUnlockedFlag = false;
                break;
            }
        }

        if (beforeUnlockedFlag != isUnlockedFlag) // Check for state change
        {
            if (isUnlockedFlag)                   // State has been unlocked (now accessible for activation)
            {
                tsCollider.enabled = true;
                GetComponent <Renderer>().material.color = ActivationUnlocked;
                OnUnlockForActivation();
            }
            else // State has been locked (now accessible for activation)
            {
                tsCollider.enabled = false;
                GetComponent <Renderer>().material.color = ActivationLocked;
                OnLockedForActivation();
            }
        }
    }
    private TSVector findAvailableStatusEffectPosition(FP selectedStatusEffectRadius)
    {
        // Find a location where random status effect will not overlap
        TSVector randomPos     = getRandomPosition();
        bool     positionFound = true;

        do
        {
            positionFound = true; // default to found position
            foreach (StatusEffect effect in spawnedStatusEffects)
            {
                if (TSVector.Distance(effect.tsTransform.position, randomPos) <= selectedStatusEffectRadius + effect.GetComponent <TSSphereCollider>().radius)
                {
                    randomPos     = getRandomPosition();
                    positionFound = false;
                    break;
                }
            }
            if (positionFound)
            {
                break;
            }
        } while (!positionFound);

        return(randomPos);
    }
        void Move(FP h, FP v)
        {
            // Set the movement vector based on the axis input.
            TSVector cacheMove = new TSVector(h, 0, v);

            if (TSVector.Distance(cacheMove, Movement) > 0.001f)
            {
//                Debug.Log("PlayerMovement::Movement:" + Movement);
                Movement = cacheMove;


                KBEngine.Event.fireIn("reqFrameChange", FrameProto.encode(new FrameUser(SyncFrame.CMD.USER, Movement)));
//                startTime = System.DateTime.Now;
            }



            // Normalise the movement vector and make it proportional to the speed per second.
            //movement = movement.normalized * speed * Time.deltaTime;

            // Move the player to it's current position plus the movement.
            //            playerRigidbody.MovePosition (transform.position + movement);

            //             Vector3 destposition = transform.position + movement;
            //
        }
    /// <summary>
    ///  get closest ship to ensure nothing collides.
    /// </summary>
    /// <returns>Returns the TSTransform class attached to the closest ship</returns>
    TSTransform getclosesttarget()
    {
        if (unitcom)
        {
            List <TSTransform> output = new List <TSTransform>();

            if (unitcom.allshipststransform.Length != 0)
            {
                output       = unitcom.allshipststransform.ToList();
                allshipstemp = output;
            }
            else
            {
                output = allshipstemp;
            }
            if (output.Contains(transformts))
            {
                output.Remove(transformts);
            }

            if (output.Count != 0)
            {
                List <TSTransform> removable = new List <TSTransform>();
                foreach (TSTransform gam in output)
                {
                    if (gam == null || gam.gameObject == null)
                    {
                        removable.Add(gam);
                    }
                }
                foreach (TSTransform gam in removable)
                {
                    output.Remove(gam);
                }
                output = output.OrderBy(x => TSVector.Distance(transformts.position, x.position)).ToList();
                if (output.Count > 0)
                {
                    return(output[0]);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
        else
        {
            return(null);
        }
    }
Beispiel #5
0
 /// <summary>
 // checks if target is in range and the same team.
 /// <summary>
 void fireweaponcheckshield()
 {
     if (TSVector.Distance(parentscript.WeaponTarget.GetComponent <TSTransform>().position, parentscript.fac.transformts.position) < Range && UnitMovementcommandcontroller.getteam(parentscript.WeaponTarget.shipscript.ShipColor, unittargetcontrol.crosslevelholder.Gamemode) == UnitMovementcommandcontroller.getteam(parentscript.ShipColor, unittargetcontrol.crosslevelholder.Gamemode))
     {
         fireweapon(parentscript.WeaponTarget.transform.position);
     }
     else
     {
         parentscript.WeaponTarget = null;
     }
 }
Beispiel #6
0
    /// <summary>
    /// OnTapLocation - call this to handle distributing and processing taps across a synchronized network game session
    /// Responds to taps and decides what to do
    /// </summary>
    /// <param name="tapLocation">location where tap/click took place</param>
    private void OnTapLocation(TSVector tapLocation)
    {
        // Handle powerup taps
        GameObject tlsc = TrueSyncManager.SyncedInstantiate(Resources.Load("Prefabs/TapLocationSphereCheck") as GameObject, tapLocation, TSQuaternion.identity);

        tlsc.GetComponent <TapLocationSphereCheck>().Owner = localOwner;

        // Find any status effect that may have been tapped
        FP           shortestDistance = 1000;
        StatusEffect nearestSe        = null;

        foreach (StatusEffect se in StatusEffectSystem.spawnedStatusEffects)
        {
            FP dist = TSVector.Distance(se.tsTransform.position, tapLocation);
            if (dist <= se.GetComponent <TSSphereCollider>().radius&& dist < shortestDistance)
            {
                shortestDistance = dist;
                nearestSe        = se;
            }
        }
        if (nearestSe != null) // Found a status effect to handle
        {
            return;            // exit this method to handle status effect within its own framework
        }
        // Shows where click took place (position marker):
        GameObject positionMarker = TrueSyncManager.SyncedInstantiate(PositionMarkerGO, tapLocation, TSQuaternion.identity);

        positionMarker.transform.position = tapLocation.ToVector();
        positionMarker.name = "Position Marker";    // Identify this marker game object in unity editor

        TSTransform tst = positionMarker.GetComponent <TSTransform>();

        tst.scale = TSVector.one * PlayerConfig.Instance.MinTapDistance;
        Renderer rend = positionMarker.GetComponent <Renderer>();

        if (TSVector.Distance(lastValidTapLocation, tapLocation) >= PlayerConfig.Instance.MinTapDistanceFromPrevious)
        {
            TapNearLine(tapLocation);
            rend.material.color = Color.black;
        }
        else
        {
            rend.material.color = Color.red;
        }

        DOTween.ToAlpha(() => rend.material.color, x => rend.material.color = x, 0, 5f).OnComplete(() =>
        {
            TrueSyncManager.Destroy(positionMarker);
        });

        lastValidTapLocation = tapLocation;
    }
Beispiel #7
0
    /// <summary>
    // travel time of the projectile for the simulation
    /// <summary>
    public FP hittime(TSVector start, TSVector end, FP speed)
    {
        FP a = TSVector.Distance(start, end) / speed;

        if (guntypemain != guntype.Lazer)
        {
            return(a + 0.5);
        }
        else
        {
            return(3);
        }
    }
Beispiel #8
0
    /// <summary>
    // get closest targfet to main ship to give attack order at.
    /// <summary>
    TSTransform getclosesttarget()
    {
        int randomtemp            = randominst.Next(0, 2);
        List <TSTransform> output = new List <TSTransform>();

        if (enemyships.Length != 0)
        {
            output = enemyships.ToList();
        }
        if (output.Count != 0)
        {
            List <TSTransform> removable = new List <TSTransform>();
            foreach (TSTransform gam in output)
            {
                if (gam == null || gam.gameObject == null)
                {
                    removable.Add(gam);
                }
            }
            foreach (TSTransform gam in removable)
            {
                output.Remove(gam);
            }
            if (output.Count != 0)
            {
                if (ships.Count > 0 && ships[0].gameObject != null)
                {
                    output = output.OrderBy(x => TSVector.Distance(ships[0].GetComponent <TSTransform>().position, x.GetComponent <TSTransform>().position)).ToList();
                }
                return(output[0]);
            }
            else
            {
                return(null);
            }
        }
        else
        {
            return(null);
        }
    }
Beispiel #9
0
    /// <summary>
    // called every second to update the AIs orders to adapt to the situation.
    /// <summary>
    public void GiveOrderwait(int waittime)
    {
        if (TrueSyncManager.Time > 0 && randominst != null)
        {
            ships.Clear();
            ships = unitcontrol.teammembersout(team);
            List <TSTransform> temp           = unitcontrol.targetsout(team);
            List <GameObject>  removableships = new List <GameObject>();
            List <TSTransform> enemyremovable = new List <TSTransform>();
            foreach (GameObject ship in ships)
            {
                if (ship == null || ship.gameObject == null)
                {
                    removableships.Add(ship);
                }
            }
            foreach (GameObject remov in removableships)
            {
                ships.Remove(remov);
            }
            foreach (TSTransform ship in temp)
            {
                if (ship == null || ship.gameObject == null)
                {
                    enemyremovable.Add(ship);
                }
            }
            foreach (TSTransform remov in enemyremovable)
            {
                temp.Remove(remov);
            }
            debugmaxcount = ships.Count.ToString() + " out of " + UnitMovementcommandcontroller.getmaxshipnumbers().ToString();
            if (startpos == new TSVector(0, 0, 0) && ships.Count > 0)
            {
                startpos = ships[0].GetComponent <TSTransform>().position + new TSVector(1, 0, 0);
            }
            Target = getclosesttarget();

            if (((ismission == false && ships.Count > 0) || (crosslevelvar.campaign == true && crosslevelvar.campaignlevel.objective == MainMenuCampaignControlScript.eMissionObjective.Survive)) && unitcontrol)
            {
                BuyShip();
            }

            if (temp.Count != 0)
            {
                enemyships = temp.ToArray();
            }
            if (ships != null && ships.Count != 0 && ships.Count > 4)
            {
                state = AIstate.attacking;
            }
            if (ships != null && ships.Count != 0 && ships.Count <= 3)
            {
                state = AIstate.retreating;
            }
            if (ships != null && ships.Count != 0 && ships.Count > 2 && ships.Count < 4)
            {
                state = AIstate.maintain;
            }
            if (ships != null && ships.Count != 0 && state == AIstate.retreating && TSVector.Distance(ships[0].GetComponent <TSTransform>().position, startpos) < 1000)
            {
                state = AIstate.holding;
            }
            if (ships != null && ships != null && ships.Count != 0 && ships[0] && Target != null && ships[0].gameObject != null && state == AIstate.holding && TSVector.Distance(ships[0].GetComponent <TSTransform>().position, Target.GetComponent <TSTransform>().position) < 1000)
            {
                state = AIstate.attacking;
            }
            if (ships != null && ships.Count != 0 && state == AIstate.maintain && ships.Count > 0 && TSVector.Distance(ships[0].GetComponent <TSTransform>().position, startpos) < 1000)
            {
                state = AIstate.retreating;
            }
            if (ships != null && ships.Count != 0 && state == AIstate.maintain && ships.Count > 0 && TSVector.Distance(ships[0].GetComponent <TSTransform>().position, startpos) > 1000)
            {
                state = AIstate.attacking;
            }
            if (ismission && timepassedactual > 30)
            {
                state = AIstate.attacking;
            }
            if (ismission && timepassedactual < 30)
            {
                state = AIstate.holding;
            }
            if (ismission && unitcontrol.crosslevelholder.campaignlevel.objective == MainMenuCampaignControlScript.eMissionObjective.Survive)
            {
                state = AIstate.attacking;
            }
            int i = 0;

            foreach (GameObject ship in ships)
            {
                if (Target != null && ship.gameObject != null && state == AIstate.attacking)
                {
                    ship.GetComponent <_Ship>().asignMoveOrder(Target.GetComponent <TSTransform>().position, Target.gameObject, RightHand_triggerInstantOrder.calculate_average_speed(ships.ToArray()), false);
                }
                if (ship.gameObject != null && state == AIstate.retreating)
                {
                    ship.GetComponent <_Ship>().asignMoveOrder(TargetPosition(i, startpos, ships.Count), null, RightHand_triggerInstantOrder.calculate_average_speed(ships.ToArray()), false);
                }
                if (ship.gameObject != null && state == AIstate.holding)
                {
                    _Ship shipscript = ship.GetComponent <_Ship>();
                    if (shipscript.HullType == eHullType.Light)
                    {
                        shipscript.asignMoveOrder(startpos, null, RightHand_triggerInstantOrder.calculate_average_speed(ships.ToArray()), false);
                    }
                    else
                    {
                        shipscript.asignMoveOrder(ship.GetComponent <TSTransform>().position, null, RightHand_triggerInstantOrder.calculate_average_speed(ships.ToArray()), false);
                    }
                }
                i++;
            }
        }
    }
Beispiel #10
0
    // Update is called once per frame
    void UpdateOrigin()
    {
        if (!isAvatar)
        {
            return;
        }

        FP dis       = TSVector.Distance(Position, destPosition);
        FP currSpeed = DestDuration <= 0 ? Speed : (Speed * playTime / DestDuration);

        if (dis <= currSpeed * Time.deltaTime)
        {
            Position = destPosition;
//           Debug.LogError("----------diff time------------------:" + (playTime - FrameDuration));
        }
        else
        {
            TSVector tempDirection = destPosition - Position;

            Position += tempDirection.normalized * currSpeed * Time.deltaTime;
        }



        FrameDuration += Time.deltaTime;

        if (FrameDuration >= DestDuration)
        {
            Position = destPosition;

            if (framePool.Count > 0)
            {
                DestDuration = playTime / (framePool.Count <= ThresholdFrame ? 1: framePool.Count / ThresholdFrame);

//                 if(framePool.Count > 8)
//                     Debug.LogError("framePool.Count too big:" + +framePool.Count);

                var framedata = framePool.Dequeue();



                emptyFramesTime = 0.0f;
                //               ThresholdFrame -= 1;

                //               Debug.Log("frame.id:"+ framedata.Key + " framePool.Count" + framePool.Count );

                TSVector movement = TSVector.zero;
                bool     space    = false;

                foreach (var item in framedata.Value.operation)
                {
                    if (item.cmd_type != (UInt32)CMD.BOX)
                    {
                        continue;
                    }

                    FrameBox msg = FrameProto.decode(item) as FrameBox;
                    movement = msg.movement;
                    space    = msg.space;
                }


                //               Debug.Log("d_point:" + point);

                destPosition += Speed * movement * playTime;

                FrameDuration = 0.0f;
            }
            else if (lastFrameData != null)
            {
//                Debug.Log("emptyFramesTime," + emptyFramesTime);

                emptyFramesTime += Time.deltaTime;

                if (emptyFramesTime >= playTime)
                {
//                    ThresholdFrame = (int)(emptyFramesTime / playTime);

                    //                   Debug.LogError("one frame time out,emptyFramesTime:" + emptyFramesTime + ",ThresholdFrame:"+ ThresholdFrame);
                }
            }
        }
    }
    /// <summary>
    /// Deterministic update function that, well , deterministically updates everything.
    /// </summary>
    public void SpecUpdate()
    {
        timepassedsincespawn++;
        if (turnspeed < shipscript.MaxAngularSpeed)
        {
            turnspeed = turnspeed + 0.003;
        }
        else
        {
            turnspeed = shipscript.MaxAngularSpeed;
        }
        thisposdif = Lastpos - transform.position;
        Lastpos    = transform.position;
        if (timepassedsincespawn <= 180)
        {
            int i = 0;
            foreach (GameObject ship in shipscript.meshes)
            {
                Vector3 starttrans = ship.transform.localPosition;
                starttrans.z = Mathf.Lerp(-3000, startmeshpos[i].z, (1 / (180 / timepassedsincespawn)));
                debugtest    = (1 / (180 / timepassedsincespawn));
                ship.transform.localPosition = starttrans;
                i++;
            }
        }
        if (shipscript && timepassedsincespawn > 180)
        {
            distancetotarget = TSVector.Distance(transformts.position, localtarget);
            if (started == false)
            {
                maxspeed = shipscript.MaxSpeed * 30;
                started  = true;
            }

            if (shipscript.WeaponTarget != null)
            {
                maxspeed = shipscript.MaxSpeed * 30;
            }
            else
            {
                maxspeed = shipscript.averagespeed * 30;
            }

            if (TrueSyncManager.Time > 0)
            {
                currentlymoving = false;

                if (distancetotarget > 10 || shipscript.HullType == eHullType.Light || shipscript.HullType == eHullType.Corvette)
                {
                    if (localtarget != null && distancetotarget < mindecdistance)
                    {
                        if (speed > 0)
                        {
                            speed = speed - acceleration / 2;
                        }
                    }
                    else
                    {
                        if (accelerating == true && speed < maxspeed)
                        {
                            speed += acceleration;
                        }
                        else if (speed > maxspeed)
                        {
                            accelerating = false;
                        }
                    }
                    if (speed > maxspeed)
                    {
                        speed = maxspeed;
                    }
                    if (shipscript.HullType == eHullType.Light || shipscript.HullType == eHullType.Corvette)
                    {
                        speed = maxspeed;
                    }
                    if (Target != new TSVector(0, 0, 0) && moving == true && distancetotarget > 150)
                    {
                        Look(Target);
                    }
                    if ((Target != new TSVector(0, 0, 0) && moving == true) || shipscript.HullType == eHullType.Light || shipscript.HullType == eHullType.Corvette)
                    {
                        Move(speed);
                    }
                }
                else if (shipscript.HullType != eHullType.Light && shipscript.HullType != eHullType.Corvette && speed > 0)
                {
                    Move(speed);
                }

                if (distancetotarget < 400)
                {
                    shipscript.movetonextstoredorder();
                }
            }
        }
        else
        {
            shipscript = GetComponent <_Ship>();
        }
        waitforpushaway++;
        if ((Closestoverall && shipscript.HullType != eHullType.Light && shipscript.HullType != eHullType.Corvette) && TSVector.Distance(Closestoverall.position, transformts.position) < 300 && Closestoverall.shipscript.HullType != eHullType.Light && Closestoverall.shipscript.HullType != eHullType.Corvette)
        {
            TSVector forwards = transformts.position - Closestoverall.position;
            transformts.position = transformts.position + (forwards * 0.005);
        }

        if (waitforpushaway > 10 && shipscript.HullType != eHullType.Light)
        {
            waitforpushaway = 0;
            TSTransform closest = getclosesttarget();
            if (closest != null)
            {
                Closestoverall = closest;
            }
        }
    }
Beispiel #12
0
        public static List <TSVector> SmoothSimple(List <TSVector> path, List <TSVector> subdivided)
        {
            if (path.Count < 2)
            {
                return(path);
            }

            //List<TSVector> subdivided;

            if (uniformLength)
            {
                maxSegmentLength = Math.Max(maxSegmentLength, 1);

                FP pathLength = 0;
                for (int i = 0; i < path.Count - 1; i++)
                {
                    pathLength += TSVector.Distance(path[i], path[i + 1]);
                }
                int estimatedNumberOfSegments = (pathLength / maxSegmentLength).AsInt();
                // Get a list with an initial capacity high enough so that we can add all points
                //subdivided = ListPool<TSVector>.Claim(estimatedNumberOfSegments+2);
                subdivided.Capacity = estimatedNumberOfSegments + 2;

                FP distanceAlong = FP.Zero;

                // Sample points every [maxSegmentLength] world units along the path
                int count = path.Count - 1;
                for (int i = 0; i < count; i++)
                {
                    var start = path[i];
                    var end   = path[i + 1];

                    FP length = TSVector.Distance(start, end);

                    while (distanceAlong < length)
                    {
                        subdivided.Add(TSVector.Lerp(start, end, distanceAlong / length));
                        distanceAlong += maxSegmentLength;
                    }

                    distanceAlong -= length;
                }
            }
            else
            {
                subdivisions = Math.Max(subdivisions, 0);

                if (subdivisions > 10)
                {
#if UNITY_EDITOR && PATHMANAGER_DEBUG
                    UnityEngine.Debug.LogWarning("Very large number of subdivisions. Cowardly refusing to subdivide every segment into more than " + (1 << subdivisions) + " subsegments");
#endif
                    subdivisions = 10;
                }

                int steps = 1 << subdivisions;
                //subdivided = ListPool<TSVector>.Claim();
                subdivided.Capacity = (path.Count - 1) * steps + 1;//

                for (int i = 0; i < path.Count - 1; i++)
                {
                    for (int j = 0; j < steps; j++)
                    {
                        subdivided.Add(TSVector.Lerp(path[i], path[i + 1], j * FP.One / steps));
                    }
                }
            }

            // Make sure we get the exact position of the last point
            // (none of the branches above will add it)
            subdivided.Add(path[path.Count - 1]);

            if (strength > 0)
            {
                for (int it = 0; it < iterations; it++)
                {
                    TSVector prev = subdivided[0];

                    for (int i = 1; i < subdivided.Count - 1; i++)
                    {
                        TSVector tmp = subdivided[i];

                        // prev is at this point set to the value that subdivided[i-1] had before this loop started
                        // Move the point closer to the average of the adjacent points
                        subdivided[i] = TSVector.Lerp(tmp, (prev + subdivided[i + 1]) / 2, strength);
                        prev          = tmp;
                    }
                }
            }
            return(subdivided);
        }
Beispiel #13
0
 public static FP GetDistanceToLineFromPosition(TSRigidBody lineRB, TSVector position)
 {
     return(TSVector.Distance(lineRB.tsTransform.forward * position.magnitude, position));
 }