【发布时间】:2020-04-30 21:25:44
【问题描述】:
我目前正在从事一项大学作业,我必须在其中创建一个 2D 游戏。我选择了“以撒的结合”风格的地牢爬行者。这个问题是从我的房间精灵中添加盒子碰撞器开始的。地牢生成是由每个游戏对象上的生成点创建的。有一个封闭的房间,当两个重生点在一个房间产生之前发生碰撞并摧毁时,就会产生一个封闭的房间。
在房间中添加碰撞器后,生成点会与房间发生碰撞并摧毁它们。由于驱逐舰位于入口房间,这种情况特别发生在入口房间,因为重生室通常有 4 个重生点在彼此之上生成 - 所以驱逐舰将这些重生点移除。
以下是我的地牢生成课程。
房间生成器:
using System.Collections.Generic;
using UnityEngine;
public class RoomSpawner : MonoBehaviour
{
public int openingDirection;
//1 = need bottom door
//2 = need top door
//3 = need left door
//4 = need right door
//So for a room with a door on the right, you will type 3, as a left door is needed in the next room to connect the two rooms.
private RoomTemplates templates;
private int rand;
private bool spawned = false;
private Vector3 entryPos;
void Start()
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
Invoke("Spawn", 0.1f);
}
void Spawn()
{
if (spawned == false)
{
rand = Random.Range(0, templates.bottomRooms.Length);
if (openingDirection == 1)
{
//Need to spawn room with BOTTOM door
Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
}
else if (openingDirection == 2)
{
//Need to spawn room with TOP door
Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
}
else if (openingDirection == 3)
{
//Need to spawn room with LEFT door
Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
}
else if (openingDirection == 4)
{
//Need to spawn room with RIGHT door
Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
}
spawned = true;
}
}
void OnTriggerEnter2D(Collider2D other)
{
entryPos = templates.entryRoom.transform.position;
if (other.CompareTag("Spawn Point"))
{
if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false && transform.position != entryPos)
{
Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
}
spawned = true;
}
}
}
房间模板:
using System.Collections.Generic;
using UnityEngine;
public class RoomTemplates : MonoBehaviour
{
public GameObject[] bottomRooms;
public GameObject[] topRooms;
public GameObject[] leftRooms;
public GameObject[] rightRooms;
public GameObject entryRoom;
public GameObject closedRoom;
public List<GameObject> rooms;
public float waitTime;
private bool spawnedBoss;
public GameObject boss;
private void Update()
{
if(waitTime <= 0 && spawnedBoss ==false) //if the wait time is 0 and boss isnt spawned a boss will spawn.
{
for (int i = 0; i < rooms.Count; i++)// will run as long as i is less than the no. of elements in list
{
if (i == rooms.Count - 1)//lists start with an index of zero
{
Instantiate(boss, rooms[i].transform.position, Quaternion.identity);//spawn boss at the room of index i's position
spawnedBoss = true; //stop bosses infinitly spawning
}
}
} else
{
waitTime -= Time.deltaTime; //slowly decrease wait time value
//we must wait before spawning boss to ensure all rooms have been spawned.
}
}
}
添加房间:
using System.Collections.Generic;
using UnityEngine;
public class AddRoom : MonoBehaviour
{
private RoomTemplates templates;
void Start()
{
templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
templates.rooms.Add(this.gameObject);
}
}
毁灭者:
using System.Collections.Generic;
using UnityEngine;
public class Destroyer : MonoBehaviour
{
private RoomTemplates templates;
void OnTriggerEnter2D(Collider2D other)
{
if(gameObject.tag == "Spawn Point")
{
Destroy(other.gameObject);
} else if(gameObject.tag != "Spawn Point")
{
Physics2D.IgnoreCollision(templates.entryRoom.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
}
}
【问题讨论】: