CRIME!!! + C# Generics
I added Arson and added a Police manager that goes out and solves these crimes. Lots more to come, let me know of any ideas you have!
But before that let me teach you ALL something.
C# Generics.
I had a bunch of scripts that all did ABOUT the same thing but with different.
This was FindClosestHospital, FindClosestFireStation and the thing I was adding which was FindClosestPoliceStation.
It felt quire redundant to copy the same lines but instead of Hopsital it was replaced with FireStation or PoliceStation.
//Generic functions
private T FindClosestReachableService<T>(Vector2Int targetPos, Func<T, bool> hasAvailableVehicles, out List<Vector2Int> bestRoute) where T : Building
{
T closestBuilding = null;
bestRoute = null;
int shortestRouteLength = int.MaxValue;
bool foundBuildingOfThisType = false;
foreach (var kvp in gridManager.GetMapGrid())
{
GridManager.GridTile tile = kvp.Value;
if (tile.buildingScript != null && tile.buildingScript is T serviceBuilding)
{
foundBuildingOfThisType = true;
if (!hasAvailableVehicles(serviceBuilding)) continue;
List<Vector2Int> testRoute = CalculateRoadPath(tile.buildingScript.gridPos, targetPos);
if (testRoute == null || testRoute.Count == 0) continue;
if (testRoute.Count < shortestRouteLength)
{
shortestRouteLength = testRoute.Count;
bestRoute = testRoute;
closestBuilding = serviceBuilding;
}
}
}
if (!foundBuildingOfThisType)
{
bestRoute = null;
return null;
}
return closestBuilding;
}
Above is an example of the C# generic.
We have “T” type, and I think this is a good place to learn about them.
Instead of defining the function as returning something like Building, we make T a type. and you can see it passed in a little <T> after the function name. We can also pass in entire functions like Func<T, bool> which is utilised as shown below:
Building bestStation = FindClosestReachableService<PoliceStation>(building.gridPos, ps => ps.HasPolice(), out route);
you can see ps => ps.HasPolice() calls that out. And finally, to define what T is meant to be, I’ve added where T : Building.
You can do this many times, such as by having both a T as well as a U! and you’d have a repeated where T : Building where U: PotatoChips or something idk what U is meant to be bro.
This was my first time using it but it’s pretty cool and if you’re using C# I’d recommend learning it as it saved me like 200 lines of code and changing something 3 times to change the same thing!
And yes, I added a third thing which is Arson! which is a crime based thing, and you have a visual that shows up as a timer. if a police doesn’t solve the crime it sets on fire. It’s not too bad but it adds a use for the police, with much more to come for the police to deal with in the future!
Also added asteroid BOMBARDMENT as a debuff. Yeah it’s brutal. 3 strikes… thats up to 108 tiles DESTROYED and set on fire.
Comments 9
What platforms do you plan to support? Like will it be a desktop thing or do you think it could run on a website?
@Niamh im planning to release on steam as a desktop game. It’s a bit heavy to run on a web browser and/or unsustainable.
asteroid strikes, tries to find nearest fire station, but it was destroyed by the asteroid. cooked.
it look really cool do you have github ?
A R S O N ! ! !
four
@Miyako yep, check the project source code
@FrogDude sick innit
@SpaceEvee four
Wow super cool!
@soomit thank you bro
Sign in to join the conversation.