init: version 1
This commit is contained in:
21
Assets/Prefabs/Spawnpoint/Scripts/RespawnZone.cs
Normal file
21
Assets/Prefabs/Spawnpoint/Scripts/RespawnZone.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// RespawnPlane is a trigger zone that respawns the player or cube
|
||||
/// when they enter the zone.
|
||||
/// </summary>
|
||||
public class RespawnPlane : MonoBehaviour
|
||||
{
|
||||
[Header("Configuration")]
|
||||
public Spawnpoint spawnpoint;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Player")) {
|
||||
spawnpoint.Respawn();
|
||||
}
|
||||
|
||||
if (other.gameObject.CompareTag("Cube")) {
|
||||
other.GetComponent<CubeController>().Respawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Prefabs/Spawnpoint/Scripts/RespawnZone.cs.meta
Normal file
2
Assets/Prefabs/Spawnpoint/Scripts/RespawnZone.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7375580a7f6e8451d89987abc46838b3
|
||||
44
Assets/Prefabs/Spawnpoint/Scripts/Spawnpoint.cs
Normal file
44
Assets/Prefabs/Spawnpoint/Scripts/Spawnpoint.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Spawnpoint marks the start location of the player and
|
||||
/// is responsible for respawning the player when needed.
|
||||
/// </summary>
|
||||
public class Spawnpoint : MonoBehaviour
|
||||
{
|
||||
[Header("Configuration")]
|
||||
public Vector2 offset = new (0f, 0.5f);
|
||||
public int mapIdx;
|
||||
|
||||
private GameObject _player;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_player = GameObject.FindWithTag("Player");
|
||||
Respawn();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!Input.GetKeyDown(KeyCode.R)) return;
|
||||
Respawn();
|
||||
}
|
||||
|
||||
public void Respawn()
|
||||
{
|
||||
_player.transform.position = new Vector3(
|
||||
transform.position.x + offset.x,
|
||||
transform.position.y + offset.y,
|
||||
_player.transform.position.z
|
||||
);
|
||||
_player.GetComponent<Rigidbody2D>().linearVelocity = Vector2.zero;
|
||||
|
||||
GameManager gameManager = GameManager.Instance;
|
||||
if (gameManager.currentMapIdx != mapIdx)
|
||||
{
|
||||
gameManager.currentMapIdx = mapIdx;
|
||||
gameManager.SwitchMap(mapIdx);
|
||||
}
|
||||
gameManager.onRespawn.Invoke();
|
||||
}
|
||||
}
|
||||
2
Assets/Prefabs/Spawnpoint/Scripts/Spawnpoint.cs.meta
Normal file
2
Assets/Prefabs/Spawnpoint/Scripts/Spawnpoint.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b02938f59b4be2cc0a4f04d24007bb2a
|
||||
Reference in New Issue
Block a user