Example #1
0
 public void CheckOrientation()
 {
     _io = GetComponent <InterplanetaryObject>();
     _io.NearestPlanet = InterplanetaryObject.GetNearestPlanet(transform.position);
     Initialize();
     UpdateOrientation();
     Deinitialize();
 }
 public void CheckOrientation()
 {
     _io = GetComponent<InterplanetaryObject>();
     _io.NearestPlanet = InterplanetaryObject.GetNearestPlanet(transform.position);
     Initialize();
     UpdateOrientation();
     Deinitialize();
 }
Example #3
0
    /// <summary>
    /// Gets the planet VertexNavigation instance.
    /// </summary>
    /// <returns>The planet navigation.</returns>
    public VertexNavigation getPlanetNavigation()
    {
        InterplanetaryObject ipo = this.GetComponent <InterplanetaryObject>();
        Gravity          grav    = ipo.NearestPlanet;
        VertexNavigation vertNav = grav.GetComponent <VertexNavigation>();

        return(vertNav);
//		return (VertexNavigation) this.GetComponent<InterplanetaryObject>().NearestPlanet.GetComponent<VertexNavigation>();
    }
    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);
    }
    void FixedUpdate()
    {
        Collider[]       cols = Physics.OverlapSphere(transform.position, range, GravityEffectedObjects);
        List <Rigidbody> rbs  = new List <Rigidbody>();

        foreach (Collider c in cols)
        {
            Rigidbody rb = c.attachedRigidbody;
            if (rb != null && rb != _rigidbody && !rbs.Contains(rb))
            {
                rbs.Add(rb);
                Vector3 offset = transform.position - c.transform.position;
                Vector3 force  = offset / offset.sqrMagnitude * GetComponent <Rigidbody>().mass;
                rb.AddForce(force);

                InterplanetaryObject io = c.gameObject.GetComponent <InterplanetaryObject>();
                if (io && force.magnitude > io.NearestPlanetForce)
                {
                    io.NearestPlanet      = this;
                    io.NearestPlanetForce = force.magnitude;
                }
            }
        }
    }
Example #6
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);
    }
    /// <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++;
    }
Example #9
0
    public void Initialize()
    {
        _io       = GetComponent <InterplanetaryObject>();
        _collider = GetComponent <BoxCollider>();

        _orientationMarker = new GameObject("OrientationMarker");
        _orientationMarker.transform.SetParent(transform);
        _orientationMarker.transform.position      = Vector3.zero;
        _orientationMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        _orientationMarker.transform.localScale    = new Vector3(1, 1, 1);

        PlanetMarker = new GameObject("PlanetMarker");
        PlanetMarker.transform.SetParent(transform);
        PlanetMarker.transform.position      = Vector3.zero;
        PlanetMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        PlanetMarker.transform.localScale    = new Vector3(1, 1, 1);

        DownMarker = new GameObject("DownMarker");
        DownMarker.transform.SetParent(transform);
        DownMarker.transform.localPosition = new Vector3(0, -500, 0);
        DownMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        DownMarker.transform.localScale    = new Vector3(1, 1, 1);

        ForwardMarker = new GameObject("ForwardMarker");
        ForwardMarker.transform.SetParent(transform);
        ForwardMarker.transform.localPosition = new Vector3(0, 0, 500);
        ForwardMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        ForwardMarker.transform.localScale    = new Vector3(1, 1, 1);

        RightMarker = new GameObject("RightMarker");
        RightMarker.transform.SetParent(transform);
        RightMarker.transform.localPosition = new Vector3(500, 0, 0);
        RightMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        RightMarker.transform.localScale    = new Vector3(1, 1, 1);

        // Objects to detect the ground with
        float gcConst = 0.75f / 2;

        if (_collider)
        {
            _groundCheckers = new GameObject("GroundCheckers");
            _groundCheckers.transform.SetParent(transform);
            _groundCheckers.transform.localPosition = new Vector3(0, -1 * _collider.size.y / 2, 0);
            _groundCheckers.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _groundCheckers.transform.localScale    = new Vector3(1, 1, 1);

            _gcCenter = new GameObject("Center");
            _gcCenter.transform.SetParent(_groundCheckers.transform);
            _gcCenter.transform.localPosition = new Vector3(0, 0, 0);
            _gcCenter.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcCenter.transform.localScale    = new Vector3(1, 1, 1);

            _gcTopRight = new GameObject("TopRight");
            _gcTopRight.transform.SetParent(_groundCheckers.transform);
            _gcTopRight.transform.localPosition = new Vector3(_collider.size.x * gcConst, 0, _collider.size.z * gcConst);
            _gcTopRight.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcTopRight.transform.localScale    = new Vector3(1, 1, 1);

            _gcTopLeft = new GameObject("TopLeft");
            _gcTopLeft.transform.SetParent(_groundCheckers.transform);
            _gcTopLeft.transform.localPosition = new Vector3(-1 * _collider.size.x * gcConst, 0, _collider.size.z * gcConst);
            _gcTopLeft.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcTopLeft.transform.localScale    = new Vector3(1, 1, 1);

            _gcBottomRight = new GameObject("BottomRight");
            _gcBottomRight.transform.SetParent(_groundCheckers.transform);
            _gcBottomRight.transform.localPosition = new Vector3(_collider.size.x * gcConst, 0, -1 * _collider.size.z * gcConst);
            _gcBottomRight.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcBottomRight.transform.localScale    = new Vector3(1, 1, 1);

            _gcBottomLeft = new GameObject("BottomLeft");
            _gcBottomLeft.transform.SetParent(_groundCheckers.transform);
            _gcBottomLeft.transform.localPosition = new Vector3(-1 * _collider.size.x * gcConst, 0, -1 * _collider.size.z * gcConst);
            _gcBottomLeft.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcBottomLeft.transform.localScale    = new Vector3(1, 1, 1);
        }
        _initialized = true;
    }
    public void Initialize()
    {
        _io = GetComponent<InterplanetaryObject>();
        _collider = GetComponent<BoxCollider>();

        _orientationMarker = new GameObject("OrientationMarker");
        _orientationMarker.transform.SetParent(transform);
        _orientationMarker.transform.position = Vector3.zero;
        _orientationMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        _orientationMarker.transform.localScale = new Vector3(1, 1, 1);

        PlanetMarker = new GameObject("PlanetMarker");
        PlanetMarker.transform.SetParent(transform);
        PlanetMarker.transform.position = Vector3.zero;
        PlanetMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        PlanetMarker.transform.localScale = new Vector3(1, 1, 1);

        DownMarker = new GameObject("DownMarker");
        DownMarker.transform.SetParent(transform);
        DownMarker.transform.localPosition = new Vector3(0, -500, 0);
        DownMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        DownMarker.transform.localScale = new Vector3(1, 1, 1);

        ForwardMarker = new GameObject("ForwardMarker");
        ForwardMarker.transform.SetParent(transform);
        ForwardMarker.transform.localPosition = new Vector3(0, 0, 500);
        ForwardMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        ForwardMarker.transform.localScale = new Vector3(1, 1, 1);

        RightMarker = new GameObject("RightMarker");
        RightMarker.transform.SetParent(transform);
        RightMarker.transform.localPosition = new Vector3(500, 0, 0);
        RightMarker.transform.localRotation = new Quaternion(0, 0, 0, 0);
        RightMarker.transform.localScale = new Vector3(1, 1, 1);

        // Objects to detect the ground with
        float gcConst = 0.75f / 2;

        if (_collider) {
            _groundCheckers = new GameObject("GroundCheckers");
            _groundCheckers.transform.SetParent(transform);
            _groundCheckers.transform.localPosition = new Vector3(0, -1 * _collider.size.y / 2, 0);
            _groundCheckers.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _groundCheckers.transform.localScale = new Vector3(1, 1, 1);

            _gcCenter = new GameObject("Center");
            _gcCenter.transform.SetParent(_groundCheckers.transform);
            _gcCenter.transform.localPosition = new Vector3(0, 0, 0);
            _gcCenter.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcCenter.transform.localScale = new Vector3(1, 1, 1);

            _gcTopRight = new GameObject("TopRight");
            _gcTopRight.transform.SetParent(_groundCheckers.transform);
            _gcTopRight.transform.localPosition = new Vector3(_collider.size.x * gcConst, 0, _collider.size.z * gcConst);
            _gcTopRight.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcTopRight.transform.localScale = new Vector3(1, 1, 1);

            _gcTopLeft = new GameObject("TopLeft");
            _gcTopLeft.transform.SetParent(_groundCheckers.transform);
            _gcTopLeft.transform.localPosition = new Vector3(-1 * _collider.size.x * gcConst, 0, _collider.size.z * gcConst);
            _gcTopLeft.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcTopLeft.transform.localScale = new Vector3(1, 1, 1);

            _gcBottomRight = new GameObject("BottomRight");
            _gcBottomRight.transform.SetParent(_groundCheckers.transform);
            _gcBottomRight.transform.localPosition = new Vector3(_collider.size.x * gcConst, 0, -1 * _collider.size.z * gcConst);
            _gcBottomRight.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcBottomRight.transform.localScale = new Vector3(1, 1, 1);

            _gcBottomLeft = new GameObject("BottomLeft");
            _gcBottomLeft.transform.SetParent(_groundCheckers.transform);
            _gcBottomLeft.transform.localPosition = new Vector3(-1 * _collider.size.x * gcConst, 0, -1 * _collider.size.z * gcConst);
            _gcBottomLeft.transform.localRotation = new Quaternion(0, 0, 0, 0);
            _gcBottomLeft.transform.localScale = new Vector3(1, 1, 1);
        }
        _initialized = true;
    }
        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;
        }