public bool canSpawnObject(float distance, ObjectType thisObjectType, int targetObjectIndex, ObjectType targetObjectType) { // return true if the parameters do not apply to the current distance if (!probability.withinDistance(distance)) { return(true); } // The target object doesn't matter if we are using objects of the same object type float totalDistance = infiniteObjectHistory.getTotalDistance(thisObjectType == ObjectType.Scene); if (minDistanceSameObjectType) { // lastSpawnDistance: the distance of the last object spawned of the inputted object type float lastSpawnDistance = infiniteObjectHistory.getLastObjectTypeSpawnDistance(thisObjectType); if (totalDistance - lastSpawnDistance <= minDistance) { return(false); } } // The rest of the tests need the target object, so if there is no target object then we are done early if (targetObjectIndex == -1) { return(true); } // objectSpawnIndex: spawn index of the last object of the same type (for example, the last duck obstacle spawn index) int objectSpawnIndex = infiniteObjectHistory.getObjectSpawnIndex(targetObjectIndex); // can always spawn if the object hasn't been spawned before and it is within the probabilities if (objectSpawnIndex == -1) { return(true); } // latestSpawnIndex: spawn index of the latest object type int latestSpawnIndex = infiniteObjectHistory.getObjectTypeSpawnIndex(targetObjectType); // can't spawn if there isn't enough object separation if (latestSpawnIndex - objectSpawnIndex <= minObjectSeparation) { return(false); } // objectLastDistance: distance of the last spawned object of the same type float objectLastDistance = infiniteObjectHistory.getSpawnDistance(targetObjectIndex); // can't spawn if we are too close to another object if (totalDistance - objectLastDistance <= minDistance) { return(false); } // looks like we can spawn return(true); }