예제 #1
0
        // all physics calculations are done within here
        // this is called from within MainManager
        public void PhysicsUpdate()
        {
            accum += deltaTime;

            //Calculate physics on the current frame
            while (accum > deltaTime)
            {
                colliderTest1        = null;
                colliderTest2        = null;
                currentCollisionPair = new List <CollisionPair>();
                depthX     = 0f;
                depthY     = 0f;
                xDistance  = 0f;
                yDistance  = 0f;
                xDirection = 0f;
                yDirection = 0f;

                // if no path simulate normally
                //calculate rigidbodies
                for (int i = 0; i < rigidBodiesInScene.Count; i++)
                {
                    // entity calc
                    if (rigidBodiesInScene[i].tag == "Entity")
                    {
                        bool charging = false;
                        recentHit = false;

                        // check if player attacked
                        // player charge attack
                        if (Input.GetMouseButtonDown(0))
                        {
                            chargeDmg  = 0;
                            chargeRate = 1000f;
                            charging   = true;

                            if (charging == true)
                            {
                                chargeDmg += chargeRate * Time.deltaTime;
                            }
                        }
                        if (Input.GetMouseButtonUp(0))
                        {
                            charging  = false;
                            chargeDmg = Mathf.Clamp(chargeDmg, 0f, 100f);
                            //create a player attack area and check if the enemy is inside it
                            GameObject   t       = Instantiate(Resources.Load("EntityAttack")) as GameObject;
                            GameObject   graphic = Instantiate(Resources.Load("AttackGraphic")) as GameObject;
                            EntityAttack et      = t.GetComponent <EntityAttack>();
                            et.InitAttack();
                            CustomRigidbody player = GameObject.FindGameObjectWithTag("Player").GetComponent <CustomRigidbody>();

                            Vector2 direction = et.GetDirection(player).normalized;
                            t.transform.position = (Vector2)player.transform.position + direction;

                            graphic.transform.position = (Vector2)player.transform.position + direction;
                            graphic.transform.SetParent(player.transform);

                            Transform[] q = graphic.GetComponentsInChildren <Transform>();

                            foreach (Transform tr in q)
                            {
                                if (tr.tag == "pointer")
                                {
                                    graphic.transform.rotation = tr.rotation;
                                }
                            }


                            if (et.IsColliding(rigidBodiesInScene[i]))
                            {
                                // if hit, set enemy velocity to the direction of where the player pointed
                                rigidBodiesInScene[i].m_IsIgnoringGravity = false;
                                rigidBodiesInScene[i].m_Velocity          = direction * chargeDmg;
                                rigidBodiesInScene[i].m_IsEnemyDead       = true;
                                Destroy(et.gameObject);
                                DestroyObject(graphic.gameObject, 0.5f);
                            }
                            else
                            {
                                DestroyObject(graphic.gameObject);
                                Destroy(et.gameObject, 0.5f);
                            }
                        }

                        // if entity has been attacked, dont path find
                        AStarPathfind a = new AStarPathfind();
                        if (rigidBodiesInScene[i].m_IsEnemyDead != true)
                        {
                            // calculate entity positions for pathfinding
                            playerPos = new Vector2(0, 0);
                            enemyPos  = new Vector2(0, 0);

                            for (int l = 0; l < entityBoundingBoxes.Count; l++)
                            {
                                if (entityBoundingBoxes[l].tag == "Player")
                                {
                                    playerPos = entityBoundingBoxes[l].transform.position;
                                }
                            }
                            // generate path to player
                            playerNode = grid.NodeFromWorldPoint(playerPos);
                            enemyNode  = grid.NodeFromWorldPoint(rigidBodiesInScene[i].transform.position);

                            path  = a.AStarSearch(grid, enemyNode, playerNode);
                            dPath = path;
                            //path = null;
                        }
                        else
                        {
                            Destroy(a);
                            path = null;
                        }

                        // if path exists move through it
                        if (path != null)
                        {
                            for (int j = 0; j < path.Count; j++)
                            {
                                rigidBodiesInScene[i].m_EnemyHasPath = false;
                                Vector2 direction = (path[j] - (Vector2)rigidBodiesInScene[i].transform.position);
                                direction.Normalize();
                                rigidBodiesInScene[i].m_EnemyMoveDirection = direction;
                                rigidBodiesInScene[i].m_EnemyHasPath       = true;
                                rigidBodiesInScene[i].EnemyPhysicsLoop();
                            }
                        }
                        // if entity has been attacked, re enable gravity
                        else if (path == null || rigidBodiesInScene[i].m_IsEnemyDead == true)
                        {
                            rigidBodiesInScene[i].m_EnemyHasPath      = false;
                            rigidBodiesInScene[i].m_IsIgnoringGravity = false;
                            rigidBodiesInScene[i].EnemyPhysicsLoop();
                        }
                    }
                    // calc player and other rigidbodies
                    if (rigidBodiesInScene[i].tag == "Player")
                    {
                        rigidBodiesInScene[i].PlayerPhysicsLoop();
                    }
                    else if (rigidBodiesInScene[i].tag != "Player" || rigidBodiesInScene[i].tag != "Entity")
                    {
                        rigidBodiesInScene[i].PlayerPhysicsLoop();
                    }
                }

                //calculate collisions
                for (int j = 0; j < entityBoundingBoxes.Count; j++)
                {
                    // this is the current entity we are checking colliders against
                    // we dont need to check collisions between platforms.
                    colliderTest1 = entityBoundingBoxes[j];

                    //collision between entity and ground
                    for (int k = 0; k < floorBoundingBoxes.Count; k++)
                    {
                        colliderTest2 = floorBoundingBoxes[k];

                        if (TestAABBOverlap(colliderTest1, colliderTest2))
                        {
                            if (colliderTest1.GetComponent <CustomRigidbody>().m_IsEnemyDead == true)
                            {
                                entityBoundingBoxes.Remove(colliderTest1);
                                rigidBodiesInScene.Remove(colliderTest1.GetComponent <CustomRigidbody>());
                                Destroy(colliderTest1.gameObject);
                                isEnemyActive = false;
                            }
                            colliderTest1.GetComponent <CustomRigidbody>().m_IsGrounded = true;
                            //Debug.Log(string.Format("Ground Collision between: {0} and {1}", colliderTest1, colliderTest2));
                            PopulateCollisionData(colliderTest1, colliderTest2);
                        }
                    }

                    //collision between entity and entity
                    for (int l = 0; l < entityBoundingBoxes.Count; l++)
                    {
                        colliderTest2 = entityBoundingBoxes[l];

                        //Don't collide with itself
                        if (colliderTest1 == colliderTest2)
                        {
                            continue;
                        }

                        if (TestAABBOverlap(colliderTest1, colliderTest2))
                        {
                            //Debug.Log(string.Format("Entity Collision between: {0} and {1}", colliderTest1, colliderTest2));
                            PopulateCollisionData(colliderTest1, colliderTest2);

                            float[] moveVals = { 12, -12 };

                            int chooseAxis = 0;
                            // enemy 'attacks'
                            // choose a random direction to set the players velocity in
                            if (colliderTest1.tag == "Player")
                            {
                                CustomRigidbody rb = colliderTest1.GetComponent <CustomRigidbody>();
                                rb.m_PlayerHasBeenHit = true;
                                float v = Random.Range(0, moveVals.Length);
                                chooseAxis = Random.Range(0, 1);
                                if (chooseAxis == 0)
                                {
                                    rb.m_Velocity = new Vector2(moveVals[Random.Range(0, moveVals.Length)], 0);
                                }
                                if (chooseAxis == 1)
                                {
                                    rb.m_Velocity = new Vector2(0, moveVals[Random.Range(0, moveVals.Length)]);
                                }
                            }
                            if (colliderTest2.tag == "Player")
                            {
                                CustomRigidbody rb = colliderTest2.GetComponent <CustomRigidbody>();
                                rb.m_PlayerHasBeenHit = true;
                                int v = Random.Range(0, moveVals.Length);
                                chooseAxis = Random.Range(0, 1);
                                if (chooseAxis == 0)
                                {
                                    rb.m_Velocity = new Vector2(moveVals[Random.Range(0, moveVals.Length)], 0);
                                }
                                if (chooseAxis == 1)
                                {
                                    rb.m_Velocity = new Vector2(0, moveVals[Random.Range(0, moveVals.Length)]);
                                }
                            }
                        }
                    }

                    // Finally check whether an entity has hit the boundaries of the screen
                    // and needs to be removed from scene
                    for (int i = 0; i < boundaryBoundingBoxes.Count; i++)
                    {
                        colliderTest2 = boundaryBoundingBoxes[i];

                        if (TestAABBOverlap(colliderTest1, colliderTest2))
                        {
                            if (colliderTest1.tag == "Player")
                            {
                                entityBoundingBoxes.Remove(colliderTest1);
                                rigidBodiesInScene.Remove(colliderTest1.GetComponent <CustomRigidbody>());
                                Destroy(colliderTest1.gameObject);
                                isPlayerActive = false;
                            }
                            if (colliderTest1.tag == "Entity")
                            {
                                entityBoundingBoxes.Remove(colliderTest1);
                                rigidBodiesInScene.Remove(colliderTest1.GetComponent <CustomRigidbody>());
                                Destroy(colliderTest1.gameObject);
                                isEnemyActive = false;
                            }
                        }
                    }
                }


                accum -= deltaTime;
            }
        }