GetNearestPlanet() public static method

public static GetNearestPlanet ( Vector3 pos ) : Gravity,
pos Vector3
return Gravity,
Ejemplo n.º 1
0
 public void CheckOrientation()
 {
     _io = GetComponent <InterplanetaryObject>();
     _io.NearestPlanet = InterplanetaryObject.GetNearestPlanet(transform.position);
     Initialize();
     UpdateOrientation();
     Deinitialize();
 }
    public void OrientToPlanet()
    {
        InterplanetaryObject io = gameObject.AddComponent <InterplanetaryObject>();

        if (PlanetOverride != null)
        {
            io.NearestPlanet = PlanetOverride;
        }
        else
        {
            io.NearestPlanet = InterplanetaryObject.GetNearestPlanet(transform.position);
        }

        transform.LookAt(io.NearestPlanet.transform.position);
        transform.Rotate(new Vector3(1.0f, 0, 0), 90);
        InvertOrientation();
        DestroyImmediate(io);
    }
Ejemplo n.º 3
0
 void Start()
 {
     this.NearestPlanet = InterplanetaryObject.GetNearestPlanet(this.transform.position);
 }
    public void DropToPlanet()
    {
        InterplanetaryObject io = gameObject.AddComponent <InterplanetaryObject>();

        Collider collider = GetComponent <Collider>();

        if (!collider)
        {
            Debug.Log(gameObject.name + " does not have a collider: cannot drop to planet surface");
            return;
        }

        if (PlanetOverride != null)
        {
            io.NearestPlanet = PlanetOverride;
        }
        else
        {
            io.NearestPlanet = InterplanetaryObject.GetNearestPlanet(transform.position);
        }

        Collider   planetCollider = io.NearestPlanet.GetComponent <MeshCollider>();
        RaycastHit hit            = new RaycastHit();

        // Look for planet's surface, which needs to be between the object's center and the planet's center
        int layer = LayerMask.GetMask("Planet");

        Physics.Raycast(transform.position, io.NearestPlanet.transform.position - transform.position, out hit, Mathf.Infinity, layer);
        if (hit.collider == null)
        {
            Debug.Log(gameObject.name + ": no raycast hit");
        }
        else if (hit.collider != planetCollider)
        {
            Debug.Log(gameObject.name + ": did not hit planet, hit " + hit.collider.gameObject.name);
        }
        else
        {
            int     i        = 1;
            Vector3 startPos = transform.position;
            float   move     = hit.distance / 2;

            while (i < 15)
            {
                transform.position = startPos - transform.up * move;

                //Vector3 closestPoint = rb.ClosestPointOnBounds(io.NearestPlanet.transform.position);
                RaycastHit closestPointHit;
                Ray        closestPointRay = new Ray(io.NearestPlanet.transform.position, transform.position - io.NearestPlanet.transform.position);
                collider.Raycast(closestPointRay, out closestPointHit, Mathf.Infinity);
                Vector3 closestPoint = closestPointHit.point;

                if (Physics.Raycast(closestPoint, io.NearestPlanet.transform.position - closestPoint, hit.distance * 2))
                {
                    move += hit.distance / ((float)Mathf.Pow(2, i));
                }
                else
                {
                    move -= hit.distance / ((float)Mathf.Pow(2, i));
                }

                i++;
            }

            transform.position -= transform.up * ExtraDropDistance;
        }

        DestroyImmediate(io);
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Tries to spawn an enemy
    /// </summary>
    void SpawnEnemy()
    {
        Dictionary <long, List <Vector3> > verticesInGrid = verticesInGridByPlanet [planetName];
        bool    foundVertex          = false;
        Vector3 position             = Vector3.zero;
        int     maxCellsAway         = 0;
        int     minCellsAway         = 0;
        int     spawnDirectionChoice = 0;

        if (EnemyPrefabs.Count == 0)
        {
            return;
        }

        int tries = 0;

        long playerLoc = getPlayerGridLoc();

        SystemLogger.write("Trying to spawn an enemy around grid " + playerLoc);

        spawnDirectionChoice = Random.Range(0, 4);
        List <long> possibleKeys = null;

        MinSpawnDistanceActual = MinSpawnDistanceEmpty + (MinSpawnDistancePopulated - MinSpawnDistanceEmpty) * (CurrentEnemyNumber / CurrentDifficulty);
        MaxSpawnDistanceActual = MinSpawnDistancePopulated + (MaxSpawnDistanceActual - MinSpawnDistancePopulated) * (CurrentEnemyNumber / CurrentDifficulty);

        // the minimum number of cells away
        maxCellsAway = 5;
        minCellsAway = 3;

        // choose a direction to spawn the enemies
        possibleKeys = getSpawnCells(playerLoc, maxCellsAway, minCellsAway);

        // tries x time to find a valid vertice
        while (!foundVertex && tries < MaxTries)
        {
            SystemLogger.write("Tries: " + tries);

            if (Player.Instance == null)
            {
                break;
            }

            // all the possible grids to look for a vertice
            if (possibleKeys.Count > 0)
            {
                long key = possibleKeys [Random.Range(0, possibleKeys.Count)];

                if (verticesInGrid.ContainsKey(key) && verticesInGrid [key].Count > 0)
                {
                    position = verticesInGrid [key] [Random.Range(0, verticesInGrid [key].Count)];

                    float dist = Vector2.Distance(position, Player.Instance.transform.position);

                    if (dist > MinSpawnDistanceActual && dist < MaxSpawnDistance)
                    {
                        Collider[] hits = Physics.OverlapSphere(position, CollisionCheckRadius);
                        if (hits.Length <= 1)
                        {
                            foundVertex = true;
                            break;
                        }
                    }
                }
            }

            tries++;
        }

        // Can't find a location to spawn
        if (tries >= MaxTries)
        {
            return;
        }

        List <GameObject> prefabsToUse;
        List <float>      weightsToUse;
        PlanetAISpawner   planetAISpawner = Player.Instance.getPlanetNavigation().GetComponent <PlanetAISpawner>();

        if (planetAISpawner)
        {
            prefabsToUse = planetAISpawner.AIOnThisPlanet;
            weightsToUse = planetAISpawner.AIWeights;
        }
        else
        {
            prefabsToUse = EnemyPrefabs;
            weightsToUse = EnemyProbabilities;
        }

        GameObject prefab = null;
        float      r      = Random.Range(0, 100) / 100.0f;

        float weightTotal = 0f;

        for (int i = 0; i < weightsToUse.Count; i++)
        {
            weightTotal += weightsToUse[i];
        }

        float sum = 0;

        for (int i = 0; i < prefabsToUse.Count; i++)
        {
            if (r < (sum + weightsToUse[i]) / weightTotal)
            {
                prefab = prefabsToUse[i];
                break;
            }
            sum += weightsToUse[i];
        }


        GameObject e = GameObject.Instantiate(prefab, position, new Quaternion()) as GameObject;

        // add to hierarchy
        e.transform.parent = transform;

        // TODO: test if we need this
        Gravity nearestPlanet = InterplanetaryObject.GetNearestPlanet(position);
        Vector3 angleToPlanet = position - nearestPlanet.transform.position;

        e.transform.position = e.transform.position + angleToPlanet.normalized * 1f;

        e.AddComponent <EnvironmentOrienter>();
        e.GetComponent <EnvironmentOrienter>().OrientToPlanet();

        CurrentEnemyNumber++;
    }
Ejemplo n.º 6
0
        public void Load()
        {
            //ScoreManager.Instance.score = Score;
            //ScoreManager.Instance.SetMultiplier(Multiplier);
            SaveSystem.Instance.TimesBeaten    = TimesBeaten;
            Player.Instance.transform.position = PlayerPosition;
            Player.Instance.transform.rotation = PlayerRotation;
            Player.Instance.GetComponent <InterplanetaryObject> ().NearestPlanet = InterplanetaryObject.GetNearestPlanet(PlayerPosition);
            PickupCache.Instance.LaserBeam.GetComponent <Weapon> ().ammo         = BeamAmmo;
            PickupCache.Instance.Mine.GetComponent <Weapon> ().ammo   = MineAmmo;
            PickupCache.Instance.Rocket.GetComponent <Weapon> ().ammo = RocketAmmo;

            if (TankBossDead)
            {
                Destroy(BossManager.Instance.Planet1Tank);
            }
            if (ScorpionDead)
            {
                Destroy(BossManager.Instance.Scorpion);
            }
            if (MineLayerDead)
            {
                Destroy(BossManager.Instance.MineLayer);
            }
            if (GoliathDead)
            {
                Destroy(BossManager.Instance.Goliath);
            }

            ProceduralGenerationOnMesh.serializedInformation desertInfo = new ProceduralGenerationOnMesh.serializedInformation();
            desertInfo.samplePointKeys      = desertSamplePointChunks;
            desertInfo.samplePointLocations = desertSamplePointLocations;
            desertInfo.samplePointObjects   = desertSamplePointObjectNames;
            desertInfo.triangleIndexes      = desertTriangleIndexes;
            desertInfo.samplePointSizes     = desertSamplePointSizes;

            ProceduralGenerationOnMesh.serializedInformation iceInfo = new ProceduralGenerationOnMesh.serializedInformation();
            iceInfo.samplePointKeys      = iceSamplePointChunks;
            iceInfo.samplePointLocations = iceSamplePointLocations;
            iceInfo.samplePointObjects   = iceSamplePointObjectNames;
            iceInfo.triangleIndexes      = iceTriangleIndexes;
            iceInfo.samplePointSizes     = iceSamplePointSizes;


            if (ProceduralGenerationOnMesh.serializedSamplePointsByPlanet.ContainsKey("DesertPlanet"))
            {
                ProceduralGenerationOnMesh.serializedSamplePointsByPlanet ["DesertPlanet"] = desertInfo;
            }
            else
            {
                ProceduralGenerationOnMesh.serializedSamplePointsByPlanet.Add("DesertPlanet", desertInfo);
            }

            if (ProceduralGenerationOnMesh.serializedSamplePointsByPlanet.ContainsKey("IcePlanet"))
            {
                Debug.Log(System.Environment.StackTrace);
                ProceduralGenerationOnMesh.serializedSamplePointsByPlanet ["IcePlanet"] = iceInfo;
            }
            else
            {
                ProceduralGenerationOnMesh.serializedSamplePointsByPlanet.Add("IcePlanet", iceInfo);
            }


            int multi = 1;

            for (int i = 0; i < TimesBeaten; i++)
            {
                multi = multi * 2;
            }

            ScoreManager.Instance.SetMultiplier(multi);
            loadFinished = true;
        }