Example #1
0
 public void ObjectExitedRange(GravityObject gravityObject)
 {
     if (attractOthers)
     {
         objectsInRange.Remove(gravityObject);
     }
 }
Example #2
0
    private void FixedUpdate()
    {
        foreach (GravityObject GO1 in AllObjects)
        {
            foreach (GravityObject GO2 in AllObjects)
            {
                if (GO1 != GO2)
                {
                    //applyGravity(GO1, GO2);
                    //Debug.Log("applyGravity()");
                    applyFakeGravity(GO1, GO2);

                    if (GravityObject.verifyOverlap(GO1, GO2))
                    {
                        if (GO1.mass > GO2.mass)
                        {
                            for (int i = 0; i < consumeRate; i++)
                            {
                                GO1.consume(GO2);
                            }
                        }
                        else if (GO2.mass > GO1.mass)
                        {
                            for (int i = 0; i < consumeRate; i++)
                            {
                                GO2.consume(GO1);
                            }
                        }
                    }
                }
            }
        }
    }
Example #3
0
 public void InvertNewObject(GravityObject gravObject)
 {
     //Check if object is the same as current object, undo effect if so
     if (gravObject == currentlyInvertedObject)
     {
         currentlyInvertedObject.SwitchLocalGravity();
         currentlyInvertedObject = null;
         invertedActive          = false;
     }
     //Drop current object if present
     else if (invertedActive)
     {
         currentlyInvertedObject.SwitchLocalGravity();
         currentlyInvertedObject = gravObject;
         currentlyInvertedObject.SwitchLocalGravity();
         invertedActive = true;
     }
     //If nothing currently activated
     else
     {
         currentlyInvertedObject = gravObject;
         currentlyInvertedObject.SwitchLocalGravity();
         invertedActive = true;
     }
 }
Example #4
0
    private void GenerateObjects(List <GravityObject> L_GOs)
    {
        for (int i = 0; i < L_GOs.Capacity; i++)
        {
            //Debug.Log("GenerateObjects()#1: L_GOs.Capacity = " + L_GOs.Capacity);
            //Debug.Log("GenerateObjects()#2: for(): i = " + i);
            L_GOs[i].mass = Random.Range(minimumMass, maximumMass); // randomize radius via mass
            L_GOs[i].Refresh();                                     // update radius, localScale
            L_GOs[i].rigidbody = L_GOs[i].GetComponent <Rigidbody>();

            float   maximumEndToEnd = GravityObject.calcRadius(maximumMass) * L_GOs.Capacity;
            Vector3 location        = new Vector3(Random.Range(-maximumEndToEnd, maximumEndToEnd),
                                                  Random.Range(-maximumEndToEnd, maximumEndToEnd),
                                                  Random.Range(-maximumEndToEnd, maximumEndToEnd));
            L_GOs[i].transform.position = location;
            //L_GOs[i].transform.position = new Vector3(0,0,0);

            /*
             * // check for any overlaps so far, and resolve if so
             * for(int h = 0; h < i; h++) {
             *  if(GravityObject.verifyOverlap(L_GOs[h], L_GOs[i])) {
             *      D6Relocate(L_GOs[h], L_GOs[i]);
             *      Debug.Log("Relocated a GravityObject that generated inside another one!");
             *  }
             * }
             */
            L_GOs[i].rigidbody.velocity = new Vector3(Random.Range(-.1f, .1f),
                                                      Random.Range(-.1f, .1f), Random.Range(-.1f, .1f));
            L_GOs[i].gameObject.SetActive(true); // reactivate after positioning
        }
    }
    //private Vector2 targetVelocity;
    //private float currentHorizontalSpeed = 0, previousHorizontalSpeed = 0;


    private void Awake()
    {
        _rb                    = GetComponent <Rigidbody2D>();
        _gravityObject         = GetComponent <GravityObject>();
        _animator              = GetComponent <Animator>();
        _playerSoundController = GetComponent <PlayerSoundController>();
    }
 void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.tag == "Bullet" || other.gameObject.tag == "Player") {
         globalGravityOn = true;
         mainObject = this;
     }
 }
    public PlayerController(InputController _input, Rigidbody _rb, GravityObject _gravObj, Animator _animator, Collider coll)
    {
        input     = _input;
        rb        = _rb;
        animator  = _animator;
        transform = rb.transform;

        //set gravobj corners
        gravObj = _gravObj;
        gravObj.SetCorners(
            new Vector3[4] {
            new Vector3(coll.bounds.extents.x - boundsAdjust, 0, 0),
            new Vector3(0, 0, coll.bounds.extents.z - boundsAdjust),
            new Vector3(-coll.bounds.extents.x + boundsAdjust, 0, 0),
            new Vector3(0, 0, -coll.bounds.extents.z + boundsAdjust)
        }
            );

        //init state machine
        Dictionary <string, State> states = new Dictionary <string, State>();

        states.Add("Grounded", new State(
                       new State.StateAction(GroundedEnter),
                       new State.StateAction(GroundedUpdate),
                       new State.StateAction(GroundedExit)
                       )
                   );
        states.Add("Jumping", new State(
                       new State.StateAction(JumpingEnter),
                       new State.StateAction(JumpingUpdate),
                       new State.StateAction(JumpingExit)
                       )
                   );
        fsm = new StateMachine(states, "Grounded");
    }
    void OnTriggerExit(Collider other)
    {
        float angleToStartDirection = Vector3.Angle(startGravityDirection, Physics.gravity.normalized);
        float angleToEndDirection   = Vector3.Angle(endGravityDirection, Physics.gravity.normalized);

        Vector3 exitGravity = angleToStartDirection < angleToEndDirection ? startGravityDirection : endGravityDirection;

        GravityObject gravityObj = other.gameObject.GetComponent <GravityObject>();

        if (other.TaggedAsPlayer())
        {
            Physics.gravity = baseGravMagnitude * exitGravity;

            float angleBetween = Vector3.Angle(playerMovement.transform.up, -Physics.gravity.normalized);
            if (treatedAsADownStairForPlayer)
            {
                angleBetween = -angleBetween;
            }
            playerMovement.transform.rotation =
                Quaternion.FromToRotation(playerMovement.transform.up, -Physics.gravity.normalized) *
                playerMovement.transform.rotation;

            PlayerLook playerLook = PlayerLook.instance;
            playerLook.rotationY -= angleBetween * Vector3.Dot(
                playerMovement.transform.forward,
                playerMovement.ProjectedHorizontalVelocity().normalized
                );
            playerLook.rotationY = Mathf.Clamp(playerLook.rotationY, -playerLook.yClamp, playerLook.yClamp);
        }
        else if (gravityObj != null)
        {
            gravityObj.gravityDirection = exitGravity;
        }
    }
Example #9
0
    // Update is called once per frame
    void Update()
    {
        // if we are simulating
        if (Simulating)
        {
            // get good scaled delta time so things dont move too slow
            float GoodDeltaTime = TimeMultiplier * Time.deltaTime;
            // update objects
            GravityObject.CalculateForces(CreatedObjs);
            GravityObject.UpdateObjects(CreatedObjs, GoodDeltaTime);
            GravityObject.HandleCollisions(CreatedObjs, GoodDeltaTime);
            GravityObject.HandleRots(CreatedObjs, GoodDeltaTime);


            // If we press F, speed up the sim otherwise set to default
            if (Input.GetKey(KeyCode.F))
            {
                TimeMultiplier = 4000;
            }
            else
            {
                TimeMultiplier = 40;
            }
        }
    }
Example #10
0
    private void OnTriggerStay2D(Collider2D collision)
    {
        if (!influences)
        {
            return;
        }

        GravityObject obj = collision.gameObject.GetComponent <GravityObject>();

        if (obj)
        {
            if (obj.isInfluenced)
            {
                Vector2 v     = transform.position - obj.gameObject.transform.position;
                float   force = InitRadius / Mathf.Pow(v.magnitude, 2f);

                float mass;
                if (rb)
                {
                    mass = rb.mass;
                }
                else
                {
                    mass = noRigidBodyMass;
                }
                obj.ApplyGravity(v.normalized * force * mass);
            }
        }
    }
Example #11
0
 public void ObjectEnteredRange(GravityObject gravityObject)
 {
     if (attractOthers && !objectsInRange.Contains(gravityObject))
     {
         objectsInRange.Add(gravityObject);
     }
 }
Example #12
0
 public void consume(GravityObject smallerObject)
 {
     if (mass > smallerObject.mass)
     {
         float half = smallerObject.mass / 2;
         smallerObject.mass -= half;
         mass += half;
         Refresh();
         if (smallerObject.mass < 0.1f)
         {
             smallerObject.gameObject.SetActive(false);
             total--;
             Debug.Log(total + " GravityObjects left!");
         }
         else
         {
             smallerObject.Refresh();
             smallerObject.rigidbody.velocity /= half; // slow down to prevent slingshotting
         }
         if (half > 1)
         {
             rigidbody.velocity /= half; // slow down based on mass consumed, because now heavier
         }
     }
 }
Example #13
0
 void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.tag == "Bullet" || other.gameObject.tag == "Player")
     {
         globalGravityOn = true;
         mainObject      = this;
     }
 }
Example #14
0
 // Start is called before the first frame update
 void Start()
 {
     next_fire = Time.time;
     home_pos  = transform.position;
     myGo      = GetComponent <GravityObject>();
     //Physics2D.IgnoreCollision(GetComponent<Collider2D>(), bullet.GetComponent<Collider2D>());
     //target = GameObject.FindWithTag("player").transform;
 }
Example #15
0
        public void UpdateHistory(GravityObject obj)
        {
            if (!PositionHistory.ContainsKey(obj))
            {
                PositionHistory.Add(obj, new List <Vector2>());
            }

            PositionHistory[obj].Add(obj.Position.Clone());
        }
Example #16
0
        public static float GravityForce(GravityObject first, GravityObject second)
        {
            var G        = 6.6726f * (float)Math.Pow(10, -11) * 1000000000;
            var distance = Vector2.Distance(first.Position, second.Position);

            distance = distance == 0 ? 1 : distance;

            return(G * (first.Mass * second.Mass) / distance);
        }
Example #17
0
    private void OnTriggerExit2D(Collider2D collision)
    {
        GravityObject exitedObject = collision.GetComponent <GravityObject>();

        if (exitedObject != null)
        {
            exitedObject.ObjectExitedRange(this);
        }
    }
Example #18
0
    private void CreateBody(NBodySystem system, GravityObjectInitial init)
    {
        GameObject obj = Instantiate(system.GravityObjectPrefab);

        obj.transform.parent        = system.gameObject.transform;
        obj.transform.localPosition = init.StartPos;
        GravityObject gObj = obj.GetComponent <GravityObject>();

        gObj.SetInit(init);
    }
    public static void AddGravityObject(GameObject sphere)
    {
        var objectsAsList = new List<GravityObject>();
        objectsAsList.AddRange(gravityObjects);

        var body = sphere.GetComponent<Rigidbody2D>();
        var gravityObject = new GravityObject(sphere.transform, body.mass, body);
        objectsAsList.Add(gravityObject);
        gravityObjects = objectsAsList.ToArray();
    }
Example #20
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        GravityObject go = collision.gameObject.GetComponent <GravityObject>();

        if (go != null)
        {
            //if (!collision.gameObject.GetComponent<CharactorController>().IsLanded)
            go.UseGravity(this);
        }
    }
Example #21
0
 public void DoneEditingBody()
 {
     EnableTopLayer();
     editingBody = false;
     dropBackEdit.gameObject.SetActive(false);
     objectToEdit.gameObject.transform.Find("HaloGlow").gameObject.SetActive(false);
     objectToEdit = null;
     gravitySystem.PopulateList();
     dragOrigin = Vector3.zero;
 }
Example #22
0
 private void OnTriggerExit2D(Collider2D collision)
 {
     if (collision.tag.Equals("Player"))
     {
         GravityObject go = collision.gameObject.GetComponent <GravityObject>();
         if (go.GetGravitySource().Equals(this))
         {
             go.ResetGravity();
         }
     }
 }
Example #23
0
    public static void AddGravityObject(GameObject sphere)
    {
        var objectsAsList = new List <GravityObject>();

        objectsAsList.AddRange(gravityObjects);

        var body          = sphere.GetComponent <Rigidbody2D>();
        var gravityObject = new GravityObject(sphere.transform, body.mass, body);

        objectsAsList.Add(gravityObject);
        gravityObjects = objectsAsList.ToArray();
    }
Example #24
0
 /// <summary>
 /// Kills the Entity
 /// </summary>
 internal virtual void Kill()
 {
     // TODO - Kill the Entity
     currentEntityState = EntityState.Dead;
     rb.velocity        = Vector2.zero;
     rb.gravityScale    = GravityObject.GetNormalGravity();
     if (col != null)
     {
         col.isTrigger = true;
     }
     // TODO - Sound effect here
 }
    void OnTriggerStay(Collider other)
    {
        GravityObject gravityObj = other.gameObject.GetComponent <GravityObject>();

        if (other.TaggedAsPlayer())
        {
            t = GetLerpPositionOfPoint(playerMovement.bottomOfPlayer);
            if (treatedAsADownStairForPlayer)
            {
                t = 1 - t;
            }

            float gravAmplificationFactor = 1;
            if (treatedAsADownStairForPlayer)
            {
                Vector3 projectedPlayerPos = ClosestPointOnLine(
                    startPosition,
                    endPosition,
                    playerMovement.bottomOfPlayer
                    );
                float distanceFromPlayerToStairs = Vector3.Distance(playerMovement.bottomOfPlayer, projectedPlayerPos);
                gravAmplificationFactor = 1 + gravAmplificationMagnitude * Mathf.InverseLerp(
                    minDistanceForGravAmplification,
                    maxDistanceForGravAmplification,
                    distanceFromPlayerToStairs
                    );
            }

            Physics.gravity = baseGravMagnitude * gravAmplificationFactor *
                              Vector3.Lerp(startGravityDirection, endGravityDirection, t).normalized;

            float angleBetween = Vector3.Angle(playerMovement.transform.up, -Physics.gravity.normalized);
            if (treatedAsADownStairForPlayer)
            {
                angleBetween = -angleBetween;
            }
            playerMovement.transform.rotation =
                Quaternion.FromToRotation(playerMovement.transform.up, -Physics.gravity.normalized) *
                playerMovement.transform.rotation;

            PlayerLook playerLook = PlayerLook.instance;
            playerLook.rotationY -= angleBetween * Vector3.Dot(
                playerMovement.transform.forward,
                playerMovement.ProjectedHorizontalVelocity().normalized
                );
            playerLook.rotationY = Mathf.Clamp(playerLook.rotationY, -playerLook.yClamp, playerLook.yClamp);
        }
        else if (gravityObj != null)
        {
            float objT = GetLerpPositionOfPoint(other.transform.position);
            gravityObj.gravityDirection = Vector3.Lerp(startGravityDirection, endGravityDirection, objT).normalized;
        }
    }
Example #26
0
    void OnCollisionEnter(Collision collision)
    {
        Instantiate(soundEffect, collision.transform.position, Quaternion.identity);

        GravityObject collisionGravObj = collision.gameObject.GetComponent <GravityObject>();

        if (collisionGravObj.GetID() < GetID())
        {
            AddMass(collisionGravObj.GetMass());
            Destroy(collision.gameObject);
        }
    }
Example #27
0
    /*
     * private bool D6Relocate(GravityObject GO1, GravityObject GO2) {
     *  // move GO2 around 6 faces of a die measuring 2 * GO1.radius per side, centered on GO1
     *  Vector3 center = GO1.gameObject.transform.position;
     *  List<Vector3> faces = new List<Vector3>(6) {
     *      center - new Vector3(-1.5f * GO1.radius, 0, 0), // -X, +0, +0
     *      center - new Vector3(+1.5f * GO1.radius, 0, 0), // +X, +0, +0
     *      center - new Vector3(0, -1.5f * GO1.radius, 0), // +0, -Y, +0
     *      center - new Vector3(0, +1.5f * GO1.radius, 0), // +0, +Y, +0
     *      center - new Vector3(0, 0, -1.5f * GO1.radius), // +0, +0, -Z
     *      center - new Vector3(0, 0, +1.5f * GO1.radius)  // +0, +0, +Z
     *  };
     *
     *  // step through possible faces and attempt to reposition GO2 to an open face
     *  for(int i = 0; i < 6; i++) {
     *      GO2.transform.position = faces[i];
     *      if(GravityObject.verifyOverlap(GO1, GO2) == false) { // if no overlap
     *          GO2.gameObject.transform.position = faces[i];
     *          return true; // success flag
     *      }
     *  }
     *  return false; // failure flag; not able to relocate on any of 6 faces
     * }
     */

    /*
     * private void applyGravity(GravityObject GO1, GravityObject GO2) {
     *  Vector3 radiusVector = (GO2.transform.position - GO1.transform.position);
     *  float r = radiusVector.magnitude; // * Mathf.Pow(10, 6);
     *  float m1 = GO1.mass; // * Mathf.Pow(10, 24);
     *  float m2 = GO2.mass; // * Mathf.Pow(10, 24);
     *  float gravityForce = (G * GO1.mass * GO2.mass * Mathf.Pow(10, 36)) / (r * r);
     *
     *  radiusVector = radiusVector.normalized * gravityForce;
     *  GO2.rigidbody.AddForce(-radiusVector); // scalable with slider
     *  GO1.rigidbody.AddForce(radiusVector); // scalable with slider
     * }
     */

    private void applyFakeGravity(GravityObject GO1, GravityObject GO2)
    {
        Vector3 radiusVector = (GO2.transform.position - GO1.transform.position);
        float   r            = radiusVector.magnitude;
        float   m1           = GO1.mass;
        float   m2           = GO2.mass;
        float   gravityForce = (G * GO1.mass * GO2.mass) / (r * r);

        radiusVector = radiusVector.normalized * gravityForce;
        GO2.rigidbody.AddForce(-radiusVector); // scalable with slider
        GO1.rigidbody.AddForce(radiusVector);  // scalable with slider
    }
Example #28
0
    private void OnTriggerExit2D(Collider2D collision)
    {
        GravityObject go = collision.gameObject.GetComponent <GravityObject>();

        if (go != null && go.isActiveAndEnabled)
        {
            if (this.Equals(go.GetGravitySource()))
            {
                go.ResetGravity();
            }
        }
    }
Example #29
0
    private void ApplyGravity(GravityObject objectToAttract)
    {
        Rigidbody2D rbToAttract = objectToAttract.rb;

        Vector2 direction = rb.position - rbToAttract.position;
        float   distance  = direction.magnitude;

        float   forceMagnitude = (rb.mass * rbToAttract.mass) / Mathf.Pow(distance, 1.8f);
        Vector2 force          = direction.normalized * forceMagnitude * 10;

        rbToAttract.AddForce(force);
    }
Example #30
0
    //readonly float invincibleTime = 3f;

    // Start is called before the first frame update
    void Start()
    {
        myMat      = GetComponent <Renderer>().material;
        myRig      = GetComponent <Rigidbody2D>();
        animator   = GetComponent <Animator>();
        go         = GetComponent <GravityObject>();
        IsFloating = go.IsFloating;
        InvincibleFor(3f);
        Health      = maxHealth;
        Energy      = maxEnergy;
        deadSound   = GameObject.FindGameObjectWithTag("DeadSound").GetComponent <AudioSource>();
        getHitSound = GameObject.FindGameObjectWithTag("HitSound").GetComponent <AudioSource>();
        kittySound  = GameObject.FindGameObjectWithTag("KittySound").GetComponent <AudioSource>();
    }
Example #31
0
    private void OnTriggerStay2D(Collider2D collision)
    {
        //if (collision.tag.Equals("Player")) {
        //GravityObject go = collision.gameObject.GetComponent<GravityObject>();
        GravityObject go = collision.gameObject.GetComponent <GravityObject>();

        if (go != null)
        {
            if (go.GetGravitySource() == null)
            {
                go.UseGravity(this);
            }
        }
    }
Example #32
0
    void Attract(GravityObject toAttract)
    {
        Rigidbody rbToAttract = toAttract.rb;

        Vector3 direction = rb.position - rbToAttract.position;

        toAttract.gravityDirection = direction;
        float distance = direction.magnitude;

        float   forceMagnitude = (rb.mass * rbToAttract.mass) / (distance * distance);
        Vector3 force          = direction.normalized * forceMagnitude;

        rbToAttract.AddForce(force);
    }