Added autospawning
This commit is contained in:
@@ -220,6 +220,7 @@ MonoBehaviour:
|
|||||||
Body2D: {fileID: 5645363082347469150}
|
Body2D: {fileID: 5645363082347469150}
|
||||||
Button: {fileID: 1572770211658593958}
|
Button: {fileID: 1572770211658593958}
|
||||||
AttachedCanvas: {fileID: 0}
|
AttachedCanvas: {fileID: 0}
|
||||||
|
Spawner: {fileID: 0}
|
||||||
EnteredCanvas: 0
|
EnteredCanvas: 0
|
||||||
--- !u!50 &5645363082347469150
|
--- !u!50 &5645363082347469150
|
||||||
Rigidbody2D:
|
Rigidbody2D:
|
||||||
|
|||||||
@@ -641,10 +641,14 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 1fc36d0808cf971459d9e430faaeadd9, type: 3}
|
m_Script: {fileID: 11500000, guid: 1fc36d0808cf971459d9e430faaeadd9, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
|
MaxSpawned: 60
|
||||||
|
MinSpawnTime: 0
|
||||||
|
MaxSpawnTime: 0.2
|
||||||
FruitSpawnParent: {fileID: 1944344878}
|
FruitSpawnParent: {fileID: 1944344878}
|
||||||
FruitPrefab: {fileID: 8732739964968511634, guid: 958453a8750b46642babdd2f3a92952e,
|
FruitPrefab: {fileID: 8732739964968511634, guid: 958453a8750b46642babdd2f3a92952e,
|
||||||
type: 3}
|
type: 3}
|
||||||
GameCanvas: {fileID: 1165634413}
|
GameCanvas: {fileID: 1165634413}
|
||||||
|
ActiveFruits: []
|
||||||
--- !u!1 &1944344877
|
--- !u!1 &1944344877
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -6,9 +6,8 @@ namespace Fruitomation
|
|||||||
{
|
{
|
||||||
public class FruitBehaviour : MonoBehaviour
|
public class FruitBehaviour : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("References")] [SerializeField]
|
[Header("References")]
|
||||||
private RectTransform RectTransform;
|
[SerializeField] private RectTransform RectTransform;
|
||||||
|
|
||||||
[SerializeField] private Rigidbody2D Body2D;
|
[SerializeField] private Rigidbody2D Body2D;
|
||||||
[SerializeField] private Button Button;
|
[SerializeField] private Button Button;
|
||||||
|
|
||||||
@@ -43,6 +42,7 @@ namespace Fruitomation
|
|||||||
|
|
||||||
private void TriggerDestruction()
|
private void TriggerDestruction()
|
||||||
{
|
{
|
||||||
|
Spawner.RemoveFruit(this);
|
||||||
Destroy(gameObject);
|
Destroy(gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using PashaBibko.Pacore.Attributes;
|
using PashaBibko.Pacore.Attributes;
|
||||||
|
using System.Collections.Generic;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@@ -6,22 +7,48 @@ namespace Fruitomation
|
|||||||
{
|
{
|
||||||
public class FruitSpawner : MonoBehaviour
|
public class FruitSpawner : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
[Header("Settings")]
|
||||||
|
[SerializeField] private int MaxSpawned;
|
||||||
|
[SerializeField] private float MinSpawnTime;
|
||||||
|
[SerializeField] private float MaxSpawnTime;
|
||||||
|
|
||||||
[Header("References")]
|
[Header("References")]
|
||||||
[SerializeField] private Transform FruitSpawnParent;
|
[SerializeField] private Transform FruitSpawnParent;
|
||||||
[SerializeField] private GameObject FruitPrefab;
|
[SerializeField] private GameObject FruitPrefab;
|
||||||
[SerializeField] private Canvas GameCanvas;
|
[SerializeField] private Canvas GameCanvas;
|
||||||
|
|
||||||
|
[Header("Read only")]
|
||||||
|
[SerializeField, InspectorReadOnly] private List<FruitBehaviour> ActiveFruits;
|
||||||
|
|
||||||
|
private float TimeUntilNextSpawn;
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
if (ActiveFruits.Count <= MaxSpawned)
|
||||||
|
{
|
||||||
|
TimeUntilNextSpawn -= Time.deltaTime;
|
||||||
|
|
||||||
|
if (TimeUntilNextSpawn <= 0f)
|
||||||
|
{
|
||||||
|
TimeUntilNextSpawn = Random.Range(MinSpawnTime, MaxSpawnTime);
|
||||||
|
SpawnFruit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[UsedImplicitly, InspectorCallable("Spawn Fruit")]
|
|
||||||
private void SpawnFruit()
|
private void SpawnFruit()
|
||||||
{
|
{
|
||||||
GameObject go = Instantiate(FruitPrefab, FruitSpawnParent);
|
GameObject go = Instantiate(FruitPrefab, FruitSpawnParent);
|
||||||
FruitBehaviour behaviour = go.GetComponent<FruitBehaviour>();
|
FruitBehaviour behaviour = go.GetComponent<FruitBehaviour>();
|
||||||
Debug.Assert(behaviour != null, "Could not find FruitBehaviour");
|
Debug.Assert(behaviour != null, "Could not find FruitBehaviour");
|
||||||
|
|
||||||
|
ActiveFruits.Add(behaviour);
|
||||||
behaviour.InitFruitBehaviour
|
behaviour.InitFruitBehaviour
|
||||||
(
|
(
|
||||||
GameCanvas, this
|
GameCanvas, this
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RemoveFruit(FruitBehaviour fruit) => ActiveFruits.Remove(fruit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user