public float FindAngleToCitySquare(Transform sourceTransform, CitySquare targetCitySquare) { Vector3 dir = targetCitySquare.transform.position - sourceTransform.position; float ang = Vector3.SignedAngle(Vector3.up, dir, Vector3.forward); return(ang); }
private void CmdRequestTurretUpgrade(int index, Vector3 position, int iff, GameObject sacrificialHouse) { int cost = turretOptionCosts[index]; if (mh.CheckForSufficientFunds(cost) == false) { return; } //Server check for sufficient funds if (sacrificialHouse.GetComponent <IFF>().GetIFFAllegiance() != iff) { return; } // Server check that the house is allied mh.ModifyMoney(-1 * turretOptionCosts[currentSelectionIndex]); GameObject newTurret = Instantiate(turretOptionPrefabs[index], position, Quaternion.identity) as GameObject; newTurret.GetComponent <IFF>().SetIFFAllegiance(iff); CitySquare newCS = sacrificialHouse.GetComponent <Building>().GetOwningCitySquare(); Building newTurretBuilding = newTurret.GetComponent <Building>(); newTurretBuilding.SetOwningCity(newCS); newCS.AddTurretToList(newTurretBuilding); NetworkServer.Spawn(newTurret); //RpcUpgradeHouseIntoTurret(index, position, iff); sacrificialHouse.GetComponent <Building>().DyingActions(); //The server doesn't have a working allegiance manager, I guess? This line makes the server sad. NetworkServer.Destroy(sacrificialHouse); nearestHouse = null; ClearCurrentSelection(); }
public CitySquareDist(int _distance, CitySquare _tile, bool _roadAccess) { distance = _distance; tile = _tile; roadAccess = _roadAccess; driveDistance = _distance; }
private void OrientCompassTowardsNearestCity() { nearestCS = cm.FindNearestCitySquare(player.transform); cityNameTextBar.text = nearestCS.cityName; float ang = cm.FindAngleToCitySquare(player.transform, nearestCS); Quaternion rot = Quaternion.Euler(0, 0, ang); transform.rotation = rot; }
private void OnTriggerExit2D(Collider2D collision) { if (!cityToCapture) { return; } cityToCapture = null; PushUpdateToUIDriver(cityToCapture); }
void setDestination() { locationPointer = 0; destination = workplace; routeTo = Location.Routes[destination.CitySquare]; nextLocation = routeTo.Squares[locationPointer]; rail = nextLocation.Offset - Location.Offset; rail.y = (nextLocation.Height - Location.Height); distTraveled = 0; }
private void OnTriggerEnter2D(Collider2D collision) { if (cs.IsRunningOnServer) { int collisionIFF = collision.GetComponent <IFF>().GetIFFAllegiance(); int ownIFF = iff.GetIFFAllegiance(); if (collisionIFF == ownIFF) { return; } cityToCapture = collision.GetComponent <CitySquare>(); } }
public void InitializePlayerInArena() { int playerIFF = am.GetPlayerIFF(); am.AddFactionLeaderToList(playerIFF, player.GetComponent <FactionLeader>()); player.GetComponent <PlayerInput>().ReinitializePlayer(); player.GetComponent <IFF>().SetIFFAllegiance(playerIFF); CitySquare playerCity = cm.FindNearestCitySquare(transform, playerIFF); playerCity.GetComponentInChildren <IFF>().SetIFFAllegiance(playerIFF); player.transform.position = playerCity.transform.position; Camera.main.GetComponentInChildren <CinemachineVirtualCamera>().Follow = player.transform; }
public static Mesh GenerateMesh(float scale, float vScale, DrawMode drawMode, CitySquare[,] cityTiles) { List <Vector2> uvs = new List <Vector2>(); List <Color32> color32s = new List <Color32>(); List <Vector3> vertices = new List <Vector3>(); List <int> triangles = new List <int>(); float uUnit = 1f / cityTiles.GetLength(0); float vUnit = 1f / cityTiles.GetLength(1); for (int x = 0; x < cityTiles.GetLength(0); x++) { for (int y = 0; y < cityTiles.GetLength(1); y++) { CitySquare cityTile = cityTiles[x, y]; Vector3[] tileVerts = cityTile.vertices; Vector2 uvTL = new Vector2(uUnit * x, vUnit * y); Vector3[] mVertices = new Vector3[] { tileVerts[0], tileVerts[1], tileVerts[2], tileVerts[1], tileVerts[3], tileVerts[2], }; Vector2[] mUVs = new Vector2[] { uvTL, uvTL, uvTL, uvTL, uvTL, uvTL }; int currVerts = vertices.Count; List <int> trianglesToAdd = new List <int>(); List <Color32> colors = new List <Color32>(6); float alphaValue = cityTile.getRequestedValue(drawMode); byte alpha = (byte)(alphaValue * 255f); for (int g = 0; g < 6; g++) { colors.Add(new Color32(0, 0, 0, alpha)); trianglesToAdd.Add(currVerts + g); } vertices.AddRange(mVertices); triangles.AddRange(trianglesToAdd); color32s.AddRange(colors.ToArray()); uvs.AddRange(mUVs); } } Mesh mesh = new Mesh(); //GetComponent<MeshFilter>().mesh = mesh; mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.colors32 = color32s.ToArray(); mesh.uv = uvs.ToArray(); /*for (int x = 1; x < numMeshes; x++) * { * mesh.SetTriangles(triangles[x], x, true, (x * trianglesPerSubMesh)); * }*/ mesh.RecalculateNormals(); return(mesh); }
private void BuildProperty(GameObject newConstruction, CitySquare tile, EconomicUnit owner) { DemolishProperty(tile); if (tile.RealEstate == null) { Vector2 index = offsetToIndex(tile.Offset); GameObject newRE = Instantiate(newConstruction); RealEstate newREVal = newRE.GetComponent <RealEstate>(); int mapIndex = (int)newREVal.zone + 3; map[mapIndex][(int)index.x, (int)index.y] = 1; Properties.Add(newREVal); newRE.transform.parent = this.transform; tile.AddPropertyCentral(newREVal, owner); } }
public CitySquare FindNearestCitySquare(Transform sourceTransform) { citySquares = FindObjectsOfType <CitySquare>(); CitySquare closestCitySquare = null; float distance = Mathf.Infinity; foreach (CitySquare currentCS in citySquares) { float diff = (currentCS.transform.position - sourceTransform.position).magnitude; if (diff < distance) { closestCitySquare = currentCS; distance = diff; } } return(closestCitySquare); }
private void Update() { if (hasAuthority) { cityNameTMP.text = "1"; if (cm == null) { cm = FindObjectOfType <CityManager>(); } cityNameTMP.text = cm.name; nearestCS = cm.FindNearestCitySquare(gameObject.transform); OrientCompassTowardsNearestCity(); ScaleCompassWithDistanceToNearestCity(); ChangeCompassBGWithIFFStatusOfNearestCity(); UpdateNameBarWithClosestCity(); } }
void Update() { if ( rail != null && nextLocation != null && Location != destination.CitySquare && workplace != home ) { dist = (this.transform.localPosition - (nextLocation.Offset + new Vector3(0, nextLocation.Height + .5f, 0))).magnitude; if (dist < .01 || distTraveled > rail.magnitude) { Location = nextLocation; this.transform.localPosition = nextLocation.Offset + new Vector3(0, nextLocation.Height + .5f); distTraveled = 0; if (Location == destination.CitySquare) { if (destination == workplace) { destination = home; } else { destination = workplace; } locationPointer = 0; if (nextLocation.Routes.ContainsKey(destination.CitySquare)) { routeTo = nextLocation.Routes[destination.CitySquare]; getNextLocation(); } return; } locationPointer++; getNextLocation(); } else { Vector3 translation = rail * Time.deltaTime * speed; distTraveled += translation.magnitude; this.transform.Translate(translation); } } }
// Property value is based on both distance to jobs and level of pollution, gets recalculated every certain number of ticks public float CalculatePropertyValues(int commuteDist, float pollutionExp) { Routes = new Dictionary <CitySquare, Route>(); float propertyValue = 0; this.HousingValue = 0; this.ProductivityValue = 0; this.ShopValue = 0; if (!this.hasRoad) { foreach (var tile in Neighbors) { this.ShopValue += tile.trafficValue; } this.ShopValue += this.trafficValue; } if (!this.HasRoad) { HashSet <CitySquareDist> nearbyTiles = this.NearbyTiles(commuteDist); propertyValue = -Mathf.Pow(this.Pollution, pollutionExp); int productiveCount = 0; foreach (CitySquareDist tile in nearbyTiles) { CitySquare square = tile.tile; float multiplier = 5 / tile.driveDistance; // This is complicated but boils down to the 80th percentile times number of jobs times .1 divided by distance float productivityAdd = tile.roadAccess ? (square.EightyIncome * square.AvailableJobs * .1f) : 0; float housingAdd = tile.roadAccess && !tile.tile.hasRoad ? (square.Housing / tile.driveDistance) : 0; productiveCount += tile.roadAccess ? 1 : 0; float pollution = Mathf.Pow(square.Pollution / (tile.distance + 1), pollutionExp); propertyValue += productivityAdd - pollution + 1; float accessValue = tile.roadAccess ? 200 / tile.driveDistance : 0; this.HousingValue += productivityAdd + accessValue; this.HousingValue -= housingAdd - pollution; this.ProductivityValue += housingAdd + accessValue * 2; } propertyValue /= Mathf.Sqrt(productiveCount); } this.RealEstateValue = propertyValue; return(propertyValue); }
private void InitializeAIsInArena() { int numberOfFactions = am.GetNumberOfFactionsIncludingFeral(); GameObject feralFactionLeader = Instantiate(dummyFactionLeaderPrefab, Vector3.zero, Quaternion.identity) as GameObject; am.AddFactionLeaderToList(IFF.feralIFF, feralFactionLeader.GetComponent <FactionLeader>()); for (int i = 1; i < numberOfFactions; i++) { int newIFFAllegiance = i; if (am.GetPlayerIFF() == newIFFAllegiance) { continue; } CitySquare startingCS = cm.FindNearestCitySquare(transform, newIFFAllegiance); Vector3 startingPos = startingCS.transform.position; GameObject newFactionLeader = Instantiate(actualFactionLeaderPrefab, startingPos, Quaternion.identity) as GameObject; newFactionLeader.GetComponent <IFF>().SetIFFAllegiance(newIFFAllegiance); am.AddFactionLeaderToList(newIFFAllegiance, newFactionLeader.GetComponent <FactionLeader>()); startingCS.SetAllegianceForBuildingsInCity(newIFFAllegiance); } }
private void OnTriggerStay2D(Collider2D collision) { if (cs.IsRunningOnServer) { if (!cityToCapture) { return; } cityToCapture.BuildCaptureTime(Time.deltaTime); PushUpdateToUIDriver(cityToCapture); if (cityToCapture.GetTimeSpentCapturing() >= cityToCapture.GetTimeRequiredToCapture()) { int newAllegiance = iff.GetIFFAllegiance(); cityToCapture.GetComponent <IFF>().SetIFFAllegiance(newAllegiance); // change allegiance of square itself cityToCapture.SetAllegianceForBuildingsInCity(newAllegiance); // change allegiance for all city objects cityToCapture.ResetCaptureStatus(); cityToCapture = null; avcuid.UpdateTimes(0, 0); } } }
public CitySquare FindNearestCitySquare(Transform sourceTransform, int allegianceToLookFor) { citySquares = FindObjectsOfType <CitySquare>(); CitySquare closestCitySquare = null; float distance = Mathf.Infinity; foreach (CitySquare currentCS in citySquares) { if (currentCS.GetComponentInChildren <IFF>().GetIFFAllegiance() != allegianceToLookFor) { continue; } float diff = (currentCS.transform.position - sourceTransform.position).magnitude; if (diff < distance) { closestCitySquare = currentCS; distance = diff; } } //Debug.Log($"Closest CS for {allegianceToLookFor} is at {closestCitySquare.transform.position}"); return(closestCitySquare); }
protected override void Scan() { float scanRange = ss.GetComponent <CircleCollider2D>().radius; if (ut.TryGetClosestUnitWithinRange(gameObject, scanRange, "Turret", ownAllegiance, out closestEnemyTurret) == true) { return; } if (ut.TryGetClosestAttackerWithinRange(gameObject, scanRange, ownAllegiance, out closestEnemyUnit) == true) { return; } float distToFactionLeader = (transform.position - factionLeader.transform.position).magnitude; if (distToFactionLeader <= scanRange && factionLeaderCS.GetFollowMeStatus()) { playerToFollow = factionLeader; return; } FindClosestHomeCity(); if (homeCity) { float distToHome = (homeCity.transform.position - transform.position).magnitude; CitySquare possibleTargetCity = cm.FindNearestCitySquare_IgnoreIFF(transform, ownAllegiance); float distToInvade = (possibleTargetCity.transform.position - transform.position).magnitude; if (distToHome > distToInvade) { targetCity = possibleTargetCity; } else { targetCity = null; } } playerToFollow = null; closestEnemyTurret = null; closestEnemyTurret = null; }
void MakeTiles(CityPoint[,] cityPoints, int width, int height, float scale, float vScale, CityManager city) { float halfWidth = width / 2f - .5f; float halfHeight = height / 2f - .5f; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { //CitySquareMeshes[x, y] = Instantiate(CitySquareBase, transform.position + , Quaternion.identity); List <CityPoint> neighbors = new List <CityPoint>(); for (int v = 0; v < 2; v++) { for (int u = 0; u < 2; u++) { neighbors.Add(cityPoints[u + x, v + y]); } } cityTiles[x, y] = new CitySquare( neighbors.ToArray(), scale, vScale, drawMode, new Vector3((x - halfWidth), 0, (halfHeight - y)), city ); if (x > 0) { cityTiles[x - 1, y].addNeighbor(cityTiles[x, y]); } if (y > 0) { cityTiles[x, y - 1].addNeighbor(cityTiles[x, y]); } } } }
private void FindClosestHomeCity() { homeCity = cm.FindNearestCitySquare(transform, ownAllegiance); }
private void Update() { closestCS = FindNearestCitySquare(player.transform); DisplayCityName(); BoldCityNameIfWithinRange(); }
public bool TryGetCitySquareWithinRange(Transform sourceTransform, float searchRange, int iffToIgnore, out CitySquare outCS) { outCS = null; bool foundSomething = false; foreach (CitySquare cs in citySquares) { float dist = (cs.transform.position - sourceTransform.position).magnitude; if (dist >= searchRange) { continue; } if (cs.transform.root.GetComponentInChildren <IFF>().GetIFFAllegiance() == iffToIgnore) { continue; } else { foundSomething = true; outCS = cs; } } return(foundSomething); }
public bool TryFindNearestCitySquare(Transform sourceTransform, int allegianceToIgnore, out CitySquare closestCitySquare) { citySquares = FindObjectsOfType <CitySquare>(); bool foundSomething = false; closestCitySquare = null; float distance = Mathf.Infinity; foreach (CitySquare currentCS in citySquares) { if (currentCS.GetComponentInChildren <IFF>().GetIFFAllegiance() == allegianceToIgnore) { foundSomething = false; continue; } float diff = (currentCS.transform.position - sourceTransform.position).magnitude; if (diff < distance) { closestCitySquare = currentCS; distance = diff; } } if (closestCitySquare) { foundSomething = true; } return(foundSomething); }
public Route(List <CitySquare> _squares, CitySquare _square) { Squares = new List <CitySquare>(_squares); Squares.Add(_square); UsedSquares = new HashSet <CitySquare>(Squares); }
private void DemolishProperty(CitySquare tile) { tile.RemoveProperty(); }
private void ScheduleBuilds() { occupations = new HashSet <Occupation>(); foreach (var prop in Properties) { occupations.UnionWith(prop.OccupationsList); prop.CalculateTaxes(); } foreach (var occ in occupations) { if (occ.Employee == null) { var options = new List <Route>(); // Find available unoccupied real estate slots and add them to the options for a given occupation foreach (var commute in occ.Location.CitySquare.Commutes) { var dest = commute.Squares[commute.Squares.Count - 1]; if (dest.RealEstate != null && dest.RealEstate.OpenUnits && dest.RealEstate.Price < occ.Income * HousingIncomeMultiplier) { options.Add(commute); } } // If there's available housing for jobs, fill it with a worker if (options.Count >= 1) { float best = float.MaxValue; int index = 0; for (int x = 0; x < options.Count; x++) { var commute = options[x]; var dest = commute.Squares[commute.Squares.Count - 1]; // Shorter commutes with lower REValues are better(assumed to be renters and not owners for now) var reValue = dest.RealEstateValue + commute.Length * 100; if (reValue < best) { index = x; best = reValue; } } // Applicant pool is currently assumed to be unlimited so a worker will be found immediately var optimalHome = options[index].Squares[options[index].Squares.Count - 1]; GameObject newHuman = Instantiate(Human); Human human = newHuman.GetComponent <Human>(); humans.Add(human); human.init(this, optimalHome.RealEstate, occ.Requirements, this.Economy); occ.interview(human); optimalHome.RealEstate.addOccupant(human); } } } for (int x = 0; x < cityTiles.GetLength(0); x++) { for (int y = 0; y < cityTiles.GetLength(1); y++) { List <GameObject> Buildables = new List <GameObject>(); CitySquare tile = cityTiles[x, y]; if ( tile.HousingValue > Home.GetComponent <RealEstate>().maxOccupants *costOfLiving *HousingMultiplier&& tile.RealEstate == null && !tile.HasRoad && tile.HousingValue > tile.RealEstateValue && tile.ZonedFor.Contains(Zoning.RZone) ) { Buildables.Add(Home); } if (tile.ProductivityValue > 20000 * FactoryMultiplier && tile.RealEstate == null && !tile.HasRoad && tile.ProductivityValue > tile.RealEstateValue && tile.ZonedFor.Contains(Zoning.IZone) ) { Buildables.Add(Factory); } // If the land is valuable enough there's a chance something will get built if (Buildables.Count > 0 && UnityEngine.Random.value > .8f) { BuildProperty(Buildables[(int)(UnityEngine.Random.value * Buildables.Count)], tile, this.Economy); } } } }
public void addNeighbor(CitySquare _neighbor) { _neighbor.Neighbors.Add(this); this.Neighbors.Add(_neighbor); }
public void init(CityManager _cityManager, CitySquare _CitySquare, EconomicUnit _owner) { CityManager = _cityManager; CitySquare = _CitySquare; Owner = _owner; }
private void addRoute(CitySquare tile, CitySquare newRouteTo) { Route route = new Route(Routes[tile].Squares, newRouteTo); Routes.Add(newRouteTo, route); }
public Route(CitySquare square) { Squares = new List <CitySquare>(); Squares.Add(square); UsedSquares = new HashSet <CitySquare>(Squares); }