void OnPlayerEntityDeath(Entity source, Dictionary <Entity, int> culprits) { // Find current player entity in playerVehiclePrefabs. // Obtain the name of the current player, minus any potential post affix "(Clone)", to find its prefab index. string oldName = playerEntity.gameObject.name; oldName = oldName.Replace("(Clone)", ""); // Obtain reference to the current player's prefab to respawn. int prefabIndex = playerVehiclePrefabs.GetPrefabNameIndex(oldName); playerPrefab = playerVehiclePrefabs.GetPrefabAtIndex(prefabIndex); // Start the respawn timer if one set. if (timer) { timer.gameObject.SetActive(true); timer.StartTimer(respawnDelay); } // Otherwise, just respawn player now. else if (playerPrefab) { playerEntity = Instantiate(playerPrefab, originalPosition + Vector3.up * .5f, originalRotation) as Entity; } }
// Cycle vehicle demonstration... void CycleVehicle() { // Obtain reference to old vehicle. Vehicle oldVehicle = playerDriver.vehicle; // Obtain the name of the old vehicle, minus any potential post affix "(Clone)", to find its prefab index. string oldVehicleName = oldVehicle.gameObject.name; oldVehicleName = oldVehicleName.Replace("(Clone)", ""); // Obtain the prefab index of the next vehicle to instantiate (+1 of the old index). int nextVehiclePrefabIndex = playerVehiclePrefabs.GetPrefabNameIndex(oldVehicleName) + 1; // Save position and velocity of the old vehicle. Vector3 position = oldVehicle.transform.position + Vector3.up * .5f; Rigidbody oldRb = oldVehicle.GetComponentInChildren <Rigidbody>(); Vector3 velocity = (oldRb != null ? (oldRb.velocity.sqrMagnitude < .01f ? Vector3.forward : oldRb.velocity) : Vector3.forward); // Get the old vehicle entity if one exists so we can save Entity health, shield, etc. Entity oldEntity = oldVehicle.GetComponent <Entity>(); // Disable old vehicle to avoid interaction with new one. oldVehicle.gameObject.SetActive(false); // Instantiate next vehicle Vehicle newVehicle = Instantiate(playerVehiclePrefabs.GetPrefabAtIndex(nextVehiclePrefabIndex), position, Quaternion.LookRotation(velocity)) as Vehicle; // Copy rigidbody velocity if the new vehicle has one Rigidbody newRb = newVehicle.GetComponentInChildren <Rigidbody>(); if (newRb) { newRb.velocity = velocity; } // Obtain Entity on new vehicle Entity newEntity = newVehicle.GetComponent <Entity>(); // If old and new vehicles are Entities (typically the case), copy status. if (oldEntity && newEntity) { newEntity.health = oldEntity.health; newEntity.shield = oldEntity.shield; newEntity.GainExperience(null, oldEntity.experience); } // Send event for new vehicle to set it as the player vehicle. Events.setPlayerVehicle(newVehicle, false); // Destroy old vehicle. Destroy(oldVehicle.gameObject); }
void Update() { // While we have spawns left and have reached the next spawn time, spawn an Entity prefab. if (vehiclePrefabs && spawnsLeft > 0 && Time.time >= nextSpawnTime && vehiclePrefabs.count > 0) { // Decrement spawns left and reset next spawn time. spawnsLeft--; nextSpawnTime = Time.time + spawnDelay; // Get random waypoint index and spawn a prefab near it. int waypointIndex = Random.Range(0, path.count); Vector3 placeVector = path.WaypointAtIndex(waypointIndex) - path.WaypointAtIndex(waypointIndex - 1); Vector3 position = path.WaypointAtIndex(waypointIndex) - placeVector.normalized + Vector3.up; Quaternion rotation = Quaternion.LookRotation(placeVector); // Instantiate random vehicle from aiVehiclePrefabs list. Vehicle aiVehicle = Instantiate(vehiclePrefabs.GetPrefabAtIndex(Random.Range(0, vehiclePrefabs.count)), position, rotation) as Vehicle; // Attempt to find a WaypointPathFollower component and if found, set it on it's path. WaypointPathFollower follower = null; if (aiVehicle) { follower = aiVehicle.GetComponentInChildren <WaypointPathFollower>(); } if (follower) { follower.path = path; } // Subscribe to entity death event Entity entity = aiVehicle.GetComponent <Entity>(); if (entity) { entity.death += OnEntityDeath; } } }