Example #1
0
    private void CreatePlanet(Vector2 position, Player owner, int currentType, int currentUpgradeLevel, int population)
    {
        Planet planet = Instantiate <Planet>("Planet", position);

        planet.Owner               = owner;
        planet.CurrentType         = currentType;
        planet.CurrentUpgradeLevel = currentUpgradeLevel;
        planet.Population          = population;
        planet.GameController      = this;

        // Add SpriteRenderer component
        var sr = planet.AddComponent <SpriteRenderer>();

        sr.Sprite = planet.CurrentUpgrade.Sprite;

        // Add TextRenderer component
        var tr = planet.AddComponent <TextRenderer>();

        tr.Font   = PlanetPopulationFont;
        tr.Offset = new Vector2(0, -58);
        tr.Size   = 0.75f;

        // Add AudioSource component
        var au = planet.AddComponent <AudioSource>();

        // Add upgrade menu
        var upgradeMenu = planet.AddComponent <UpgradeMenu>();

        upgradeMenu.UIFont       = PlanetPopulationFont;
        upgradeMenu.UpgradeText  = Instantiate <TextRenderer>("Text", Vector2.Zero);
        upgradeMenu.Convert1Text = Instantiate <TextRenderer>("Text", Vector2.Zero);
        upgradeMenu.Convert2Text = Instantiate <TextRenderer>("Text", Vector2.Zero);
        upgradeMenu.Hide();

        // Bind callbacks
        planet.LaunchShipHandler  += LaunchShip;
        planet.OnPopulationChange += UpdateTotalPopulation;

        // Add to list
        Planets.Add(planet);

        // Invoke callback
        OnPlanetCreated?.Invoke(planet);
    }