public PlanetController TryCreateRandomPlanet(Transform parent) { float scale = Random.Range(planetScaleRange.x, planetScaleRange.y); PlanetController planet = CreatePlanet(); PlanetSettings settings = planet.GetSettings(); settings.Scale = scale; settings.ShipsAmount = (int)Random.Range(planeEscadrilleAmountRange.x, planeEscadrilleAmountRange.y); settings.OwnerID = 0; planet.Setup(settings); Vector3 point; if (TryGetSpawnPoint(planet, out point)) { planet.transform.position = point; planet.transform.parent = parent; return(planet); } else { DeletePlanet(planet); return(null); } }
//Brute method of searching spawn points by rule: private bool TryGetSpawnPoint(PlanetController planet, out Vector3 point) { Bounds planetBounds = planet.GetBounds; Vector3 minSpawnPoint = minPoint + planetBounds.extents; Vector3 maxSpawnPoint = maxPoint - planetBounds.extents; float x = Random.Range(minSpawnPoint.x, maxSpawnPoint.x); float y = Random.Range(minSpawnPoint.y, maxSpawnPoint.y); point = new Vector3(x, y, planet.transform.position.z); //Debug.Log(point); int i = 0; const int max = 100;//looping protection while (!SpawnPointBruteCheck(point, planet) && i < max) { x = Random.Range(minSpawnPoint.x, maxSpawnPoint.x); y = Random.Range(minSpawnPoint.y, maxSpawnPoint.y); point = new Vector3(x, y, planet.transform.position.z); i++; } if (i < max) { return(true); } //Debug.LogError("Couldn't find free place"); return(false); }
public void Init(int owner_id, PlanetController target, int damage) { OwnerID = owner_id; Target = target; Damage = damage; initialized = true; }
private IEnumerator SendShips2Target(int totalDamage, PlanetController target) { #if DEBUG int j = 0; #endif int damagePerShip = 1; ShipController ship; if (totalDamage > maxShipsSending) { damagePerShip = totalDamage / maxShipsSending; int undividableDmg = totalDamage % maxShipsSending; if (undividableDmg != 0) { ship = CreateShip(target, undividableDmg); } } for (int i = 0; i < (totalDamage > maxShipsSending ? maxShipsSending : totalDamage); i++) { ship = CreateShip(target, damagePerShip); #if DEBUG j++; ship.gameObject.name = "Ship " + j; #endif if (i % shipsSendingPerFrame == 0) { yield return(new WaitForFixedUpdate()); } } }
public void Attack(PlanetController other) { //Debug.Log(other.gameObject.name); int totalDamage = ShipsAmount / 2; ShipsAmount -= totalDamage; StartCoroutine(SendShips2Target(totalDamage, other)); }
private void OnCollisionEnter2D(Collision2D collision) { PlanetController planetOnTheWay = collision.gameObject.GetComponentInParent <PlanetController>(); if (planetOnTheWay != null && planetOnTheWay == Target) { Explode(); } }
public PlanetController CreatePlanet() { PlanetController planet = Instantiate(_planetPrefab); createdPlanets.Add(planet); planet.gameObject.name = "Planet " + createdPlanets.Count; return(planet); }
private void PlayerOwnedPlanet(PlanetController planet) { PlanetSettings settings = planet.GetSettings(); settings._Color = playerColor; settings.ShipsProductionRate = playerShipsProductionRate; settings.OwnerID = 1; planet.Setup(settings); playersPlanets.Add(planet); }
private void GenerateGame() { GenerateMap(); PlanetController rndPlanet = planetFactory.GetRandomPlanet(); playersPlanets = new List <PlanetController>(); PlayerOwnedPlanet(rndPlanet); PlanetSettings settings = rndPlanet.GetSettings(); settings.ShipsAmount = playerBaseShips; rndPlanet.Setup(settings); }
// the new planet must be located on distance of 2 radius or more from another planet //not ideal but fast develop private bool SpawnPointBruteCheck(Vector3 spawnPoint, PlanetController newPlanet) { foreach (PlanetController planet in createdPlanets.Where(other => other != newPlanet)) { float left = (spawnPoint.x - planet.transform.position.x) * (spawnPoint.x - planet.transform.position.x) + (spawnPoint.y - planet.transform.position.y) * (spawnPoint.y - planet.transform.position.y); float right = 4 * (planet.GetBounds.extents.x + newPlanet.GetBounds.extents.x) * (planet.GetBounds.extents.y + newPlanet.GetBounds.extents.y); if (left <= right) { return(false); } } return(true); }
private void OwnerOfPlanetChanged(PlanetController planet, int owner_id) { if (playersPlanets[0].OwnerID == owner_id) { PlayerOwnedPlanet(planet); } else { if (playersPlanets.Contains(planet)) { playersPlanets.Remove(planet); } } }
private ShipController CreateShip(PlanetController target, int damage) { Vector3 targetPos; Vector3 instantiatePoint; targetPos = target.transform.position; instantiatePoint = planetCollider.ClosestPoint(targetPos); ShipController newShip; newShip = Instantiate(shipPrefab); newShip.transform.position = instantiatePoint; newShip.Init(OwnerID, target, damage); return(newShip); }
public void OnPointerClick(PointerEventData eventData) { Vector3 currentPosition = cam.ScreenToWorldPoint(eventData.position); RaycastHit2D currentHit = Physics2D.Raycast(currentPosition, zero); Collider2D hittedCollider = currentHit.collider; if (hittedCollider != null) { PlanetController clickedPlanet = hittedCollider.GetComponentInParent <PlanetController>(); OnClick?.Invoke(clickedPlanet); } else { OnClick?.Invoke(null); } }
private void InteractWithPlanet(PlanetController clicked) { if (clicked != null) { if (playersPlanets.Contains(clicked)) { clicked.SetMarked(true); } else { foreach (var planet in playersPlanets.Where(planet => planet.IsSelected)) { planet.Attack(clicked); } } } foreach (var planet in playersPlanets.Where(other => other != clicked)) { planet.SetMarked(false); } }
private void DeletePlanet(PlanetController planet) { createdPlanets.Remove(planet); Destroy(planet.gameObject); }