[TASK] Spawn collectable ducks on the map when in range

This commit is contained in:
2019-08-19 00:36:56 +02:00
parent 98219ecfef
commit 279c5fbbbe
2270 changed files with 6269477 additions and 182 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Mapbox.CheapRulerCs;
using Mapbox.Unity.Map;
using UnityEngine;
using UnityEngine.Serialization;
@@ -9,8 +10,18 @@ public class CollectableDuckManager : MonoBehaviour
{
[FormerlySerializedAs("CollectableDucks")] public List<CollectableDuckData> collectableDucks;
Dictionary<CollectableDuckData, CollectableDuck> spawnedDucks = new Dictionary<CollectableDuckData, CollectableDuck>();
public AbstractMap map;
public Transform player;
/// <summary>
/// Spawn distance in meters (i.e. how close should the player be to a duck before it spawns on the map
/// </summary>
public float spawnDistance = 50;
public CollectableDuck duckSpawnPrefab;
private static CollectableDuckManager _instance;
public static CollectableDuckManager Instance
{
@@ -25,10 +36,41 @@ public class CollectableDuckManager : MonoBehaviour
private void Update()
{
var ruler = new CheapRuler(map.CenterLatitudeLongitude.x, CheapRulerUnits.Meters);
var playerPosition = map.WorldToGeoPosition(player.position).ToArray();
for (int i = 0; i < collectableDucks.Count; i++)
{
var duck = collectableDucks[i];
if (duck.LatitudeLongitude)
var duckCoord = duck.LatitudeLongitude;
var dist = ruler.Distance(duckCoord.ToArray(), playerPosition);
// Debug.Log("Distance to " + duck.name + ": " + dist);
if (dist < spawnDistance)
{
if (!spawnedDucks.ContainsKey(duck))
{
// Spawn the duck
var duckSpawn = Instantiate(duckSpawnPrefab, map.GeoToWorldPosition(duckCoord), Quaternion.identity,
transform);
var duckModel = Instantiate(duck.ModelPrefab, duckSpawn.transform);
duckModel.transform.localPosition = Vector3.zero;
spawnedDucks[duck] = duckSpawn;
}
else
{
if (!spawnedDucks[duck].gameObject.activeSelf)
{
spawnedDucks[duck].gameObject.SetActive(true);
}
}
} else if (spawnedDucks.ContainsKey(duck))
{
if (spawnedDucks[duck].gameObject.activeSelf)
{
spawnedDucks[duck].gameObject.SetActive(false);
}
}
}
}
}
}