Esempio n. 1
0
    public static State GetState(PhysicsModifyable pM)
    {
        State myState = new State();

        myState.timeElapsed = Player.instance.TimeElapsed;
        myState.active      = pM.gameObject.activeSelf;
        if (pM.gameObject.activeSelf)
        {
            myState.mass      = pM.Mass;
            myState.charge    = pM.Charge;
            myState.entangled = pM.Entangled;

            PhysicsAffected pA = pM.GetComponent <PhysicsAffected>();
            if (pA != null)
            {
                myState.velocity        = pA.Velocity;
                myState.angularVelocity = pA.AngularVelocity;
                myState.position        = pA.Position;
                myState.rotation        = pA.Rotation;
            }
            else
            {
                myState.position = pM.Position;
                myState.rotation = pM.Rotation;
            }

            Goal g = pM.GetComponent <Goal>();
            if (g != null)
            {
                myState.combined            = g.Combined;
                myState.numElementsCombined = g.numElementsCombined;
                myState.children            = LevelManager.Clone(g.Children);
            }

            Switch[] switches = pM.GetComponents <Switch>();
            myState.activated = new bool[switches.Length];
            foreach (Switch s in switches)
            {
                myState.activated[s.SwitchIndex] = s.activated;
            }
        }

        return(myState);
    }
Esempio n. 2
0
    public static void Bind(PhysicsModifyable pM1, PhysicsModifyable pM2)
    {
        PhysicsAffected pA1 = pM1.GetComponent <PhysicsAffected>();
        PhysicsAffected pA2 = pM2.GetComponent <PhysicsAffected>();

        if (pA1 != null && pA2 != null)
        {
            FixedJoint j1 = pA1.GetComponent <FixedJoint>();
            if (j1 == null || j1.connectedBody != pA2.GetComponent <Rigidbody>())
            {
                if (j1 != null)
                {
                    Destroy(j1);
                }

                j1 = pA1.gameObject.AddComponent <FixedJoint>();
                j1.connectedBody = pA2.GetComponent <Rigidbody>();
            }

            FixedJoint j2 = pA2.GetComponent <FixedJoint>();
            if (j2 == null || j2.connectedBody != pA1.GetComponent <Rigidbody>())
            {
                if (j2 != null)
                {
                    Destroy(j2);
                }

                j2 = pA2.gameObject.AddComponent <FixedJoint>();
                j2.connectedBody = pA1.GetComponent <Rigidbody>();
            }
        }
        else if (pA1 != null || pA2 != null)
        {
            if (pA1 != null)
            {
                pA1.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
            }
            else if (pA2 != null)
            {
                pA2.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
            }
        }
    }
Esempio n. 3
0
File: Goal.cs Progetto: kkiniaes/TBT
    // Update is called once per frame
    void Update()
    {
        //if(numElementsCombined > 1 && children.Count != 1) Debug.Log(children.Count);
        if (!combined)
        {
            if (Player.instance.timeScale > 0)
            {
                combineCooldown = Mathf.Max(0, combineCooldown - Time.deltaTime);
                foreach (Goal g in goals)
                {
                    if (combineCooldown <= 0 && Player.instance.timeScale > 0)
                    {
                        if (g != null && g.gameObject != null && g != this && !g.combined && touching(g))
                        {
                            float myCharge    = GetComponent <PhysicsModifyable>().charge;
                            float otherCharge = g.GetComponent <PhysicsModifyable>().charge;
                            if (g.numElementsCombined == numElementsCombined && (myCharge == 0 || otherCharge != myCharge))
                            {
                                Goal child  = null;
                                Goal parent = null;

                                if ((GetComponent <PhysicsAffected>() == null && g.GetComponent <PhysicsAffected>() != null) || g.id < id)
                                {
                                    child  = this;
                                    parent = g;
                                }
                                else
                                {
                                    child  = g;
                                    parent = this;
                                }

                                PhysicsAffected parentPA = parent.GetComponent <PhysicsAffected>();
                                PhysicsAffected childPA  = child.GetComponent <PhysicsAffected>();
                                if (parentPA != null && childPA != null)
                                {
                                    parentPA.Velocity = (parentPA.Velocity + childPA.Velocity) / 2f;
                                }
                                else if (parentPA != null)
                                {
                                    parentPA.Velocity /= 2f;
                                }

                                PhysicsModifyable parentPM = parent.GetComponent <PhysicsModifyable>();
                                PhysicsModifyable childPM  = child.GetComponent <PhysicsModifyable>();
                                if (parentPM.Entangled == null && childPM.Entangled != null)
                                {
                                    parentPM.Entangled = childPM.Entangled;
                                    if (parentPM.entangled != null)
                                    {
                                        parentPM.entangled.Entangled = parentPM;
                                    }
                                    childPM.Entangled = null;
                                }

                                parentPM.Mass   = 0;
                                childPM.Mass    = 0;
                                parentPM.Charge = 0;
                                childPM.Charge  = 0;

                                parent.children.Push(child);
                                parent.numElementsCombined++;
                                GameObject.Instantiate(combineEffect, parent.transform.position, Quaternion.identity);
                                child.Combine();
                            }
                        }
                    }
                }
            }
            else
            {
                combineCooldown = 0;
            }

            transform.localScale = hydrogenScale * Mathf.Sqrt(numElementsCombined);
            transform.GetChild(0).GetComponent <TextMesh>().text = System.Enum.GetNames(typeof(Element))[numElementsCombined - 1];
            if (GetComponent <PhysicsAffected>() != null)
            {
                GetComponent <PhysicsAffected>().Inertia = numElementsCombined / 2f;
            }

            if (!AntiMatterExplosion.exists && (int)LevelManager.instance.goalElement <= (numElementsCombined - 1))
            {
                Player.instance.LoadNextLevel();
            }
        }
    }
Esempio n. 4
0
    /// <summary>
    /// Perform logical explosion
    /// On server, this deals damage to stuff. On clients, it just applies forces
    /// </summary>
    private void DoLogicalExplosion(Vector3 explosionPosition, Vector3 explosionNormal, GameObject ignoreObject, int damageOwnerId, ExplosionSettings explosionConfig)
    {
        // Collect all the colliders in a sphere from the explosion's current position to a radius of the explosion radius.
        Collider[] colliders = Physics.OverlapSphere(explosionPosition, Mathf.Max(explosionConfig.explosionRadius, explosionConfig.physicsRadius), m_PhysicsMask);

        // Go through all the colliders...
        for (int i = 0; i < colliders.Length; i++)
        {
            Collider struckCollider = colliders[i];

            // Skip ignored object
            if (struckCollider.gameObject == ignoreObject)
            {
                continue;
            }

            // Create a vector from the shell to the target.
            Vector3 explosionToTarget = struckCollider.transform.position - explosionPosition;

            // Calculate the distance from the shell to the target.
            float explosionDistance = explosionToTarget.magnitude;

            // Server deals damage to objects
//			if (isServer)
            {
                // Find the DamageObject script associated with the rigidbody.
                IDamageObject targetHealth = struckCollider.GetComponentInParent <IDamageObject>();

                // If there is one, deal it damage
                if (targetHealth != null &&
                    //targetHealth.isAlive &&
                    explosionDistance < explosionConfig.explosionRadius)
                {
                    // Calculate the proportion of the maximum distance (the explosionRadius) the target is away.
                    float normalizedDistance =
                        Mathf.Clamp01((explosionConfig.explosionRadius - explosionDistance) / explosionConfig.explosionRadius);

                    // Calculate damage as this proportion of the maximum possible damage.
                    float damage = normalizedDistance * explosionConfig.damage;

                    // Deal this damage to the tank.
                    if (damageOwnerId == struckCollider.GetComponentInParent <Shooting>().m_PlayerNumber)
                    {
                        myAcademy.AddRewardToPlayer(damageOwnerId, -1 * damage);
                    }
                    else
                    {
                        myAcademy.AddRewardToPlayer(damageOwnerId, damage);
                    }
                    targetHealth.SetDamagedBy(damageOwnerId, explosionConfig.id);
                    targetHealth.Damage(damage);
                }
            }

            // Apply force onto PhysicsAffected objects, for anything we have authority on, or anything that's client only
            PhysicsAffected physicsObject = struckCollider.GetComponentInParent <PhysicsAffected>();
            NetworkIdentity identity      = struckCollider.GetComponentInParent <NetworkIdentity>();

            if (physicsObject != null && physicsObject.enabled && explosionDistance < explosionConfig.physicsRadius &&
                (identity == null || identity.hasAuthority))
            {
                physicsObject.ApplyForce(explosionConfig.physicsForce, explosionPosition, explosionConfig.physicsRadius);
            }
        }

        DoShakeForExplosion(explosionPosition, explosionConfig);
    }
Esempio n. 5
0
    // Use this for initialization
    void Awake()
    {
        rand = new System.Random();
        HashSet <Edge> connections = getRandomConnections(new Point(columns, rows));

        int   extraBorderSpace = (border) ? 2 : 0;
        Point size             = new Point(columns * 2 + extraBorderSpace - 1, rows * 2 + extraBorderSpace - 1);

        bool[,] maze = initializedBoolArray(size, true);
        int offset = (border) ? 1 : 0;

        foreach (Edge e in connections)
        {
            int betweenX = Mathf.Min(e.P1.x * 2 + offset, e.P2.x * 2 + offset) + Mathf.Abs((e.P2.x * 2 + offset) - (e.P1.x * 2 + offset)) / 2;
            int betweenY = Mathf.Min(e.P1.y * 2 + offset, e.P2.y * 2 + offset) + Mathf.Abs((e.P2.y * 2 + offset) - (e.P1.y * 2 + offset)) / 2;
            maze[e.P1.x * 2 + offset, e.P1.y * 2 + offset] = false;
            maze[betweenX, betweenY] = false;
            maze[e.P2.x * 2 + offset, e.P2.y * 2 + offset] = false;
        }

        for (int x = offset; x < maze.GetLength(0) - offset; x++)
        {
            for (int y = offset; y < maze.GetLength(1) - offset; y++)
            {
                if (!((x - offset) % 2 != 0 && (y - offset) % 2 != 0) && rand.NextDouble() < loopingAmount)
                {
                    maze[x, y] = false;
                }
            }
        }

        blocks = new List <GameObject> ();
        for (int x = 0; x < maze.GetLength(0); x++)
        {
            for (int y = 0; y < maze.GetLength(1); y++)
            {
                if (maze[x, y])
                {
                    float posX = origin.x + x * blockSize.x;
                    float posY = origin.y;
                    float posZ = origin.z + y * blockSize.z;
                    blocks.Add(Instantiate(blockPrefab, new Vector3(posX, posY, posZ), Quaternion.identity) as GameObject);
                    blocks[blocks.Count - 1].transform.localScale = blockSize;
                    PhysicsAffected.TryAddPM(blocks[blocks.Count - 1].GetComponent <PhysicsModifyable>());
                }
            }
        }

        if (addGoals)
        {
            //float ySign = Mathf.Sign(origin.y);

            float      posX1 = origin.x + extraBorderSpace * blockSize.x / 2;
            float      posY1 = origin.y;       // + blockSize.y / 2 * ySign;
            float      posZ1 = origin.z + extraBorderSpace * blockSize.z / 2;
            GameObject goal1 = Instantiate(goalPrefab, new Vector3(posX1, posY1, posZ1), Quaternion.identity) as GameObject;
            goal1.transform.localScale = blockSize * 0.4f;

            float      posX2 = origin.x + (size.x - extraBorderSpace) * blockSize.x;
            float      posY2 = origin.y;       // + blockSize.y / 2 * ySign;
            float      posZ2 = origin.z + (size.y - extraBorderSpace) * blockSize.z;
            GameObject goal2 = Instantiate(goalPrefab, new Vector3(posX2, posY2, posZ2), Quaternion.identity) as GameObject;
            goal2.transform.localScale = blockSize * 0.4f;
        }
    }
Esempio n. 6
0
    private void setPMToState(PhysicsModifyable pM, PhysicsAffected pA, State state)
    {
        pM.gameObject.SetActive(state.active);

        if (state.active)
        {
            pM.Entangled = state.entangled;
            pM.Mass      = state.mass;
            pM.Charge    = state.charge;

            if (pA != null)
            {
                pA.Velocity        = state.velocity;
                pA.AngularVelocity = state.angularVelocity;
            }

            pM.Position = state.position;
            pM.Rotation = state.rotation;

            Goal g = pM.GetComponent <Goal>();
            if (g != null)
            {
                if (state.combined)
                {
                    g.Combine();
                }
                else
                {
                    g.UnCombine();
                }

                g.numElementsCombined = state.numElementsCombined;
                if (!g.Children.Equals(state.children))
                {
                    Stack <Goal> newChildren = new Stack <Goal>();
                    while (state.children.Count > 0)
                    {
                        state.children.Peek().Combine();
                        newChildren.Push(state.children.Pop());
                    }
                    while (g.Children.Count > 0)
                    {
                        if (!newChildren.Contains(g.Children.Peek()))
                        {
                            g.Children.Peek().UnCombine();
                        }
                        g.Children.Pop();
                    }

                    g.Children = ReverseClone(newChildren);
                }
            }

            if (state.activated.Length > 0)
            {
                foreach (Switch s in pM.GetComponents <Switch>())
                {
                    s.activated = state.activated[s.SwitchIndex];
                    // s.transform.FindChild("SwitchParticles" + s.SwitchIndex).gameObject.SetActive(s.activated);
                }
            }
        }
    }