using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
///
/// Проектирование помещения
/// не используется
///
public class LocationController : MonoBehaviour
{
public List rooms;
public GameObject Location;
// Start is called before the first frame update
void Start()
{
// location_22
//var rooms_0 = rooms.ElementAt(0);
//Cube(rooms_0, new Vector3(6, 8, 1)); //x=6 y=8 h1
//var rooms_1 = rooms.ElementAt(1);
//Cube(rooms_1, new Vector3(2, 10, 1), new Vector2(6, 0));
//var rooms_2 = rooms.ElementAt(2);
//Cube(rooms_2, new Vector3(5, 4, 1), new Vector2(8, 5));
// location_25
var rooms_0 = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject)) as GameObject);
rooms_0.transform.SetParent(Location.transform);
Cube(rooms_0, new Vector3(3.57f, 6.31f, 1)); //x=6 y=8 h1
rooms.Add(rooms_0);
var rooms_1 = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject)) as GameObject);
rooms_1.transform.SetParent(Location.transform);
Cube(rooms_1, new Vector3(4, 3.7f, 1), new Vector2(3.57f, 0)); //x=6 y=8 h1
rooms.Add(rooms_1);
var rooms_2 = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject)) as GameObject);
rooms_2.transform.SetParent(Location.transform);
Cube(rooms_2, new Vector3(3, 4.46f, 1), new Vector2(3.57f, 1.85f)); //x=6 y=8 h1
rooms.Add(rooms_2);
//var rooms_1 = rooms.ElementAt(1);
//Cube(rooms_1, new Vector3(2, 10, 1), new Vector2(6, 0));
//var rooms_2 = rooms.ElementAt(2);
//Cube(rooms_2, new Vector3(5, 4, 1), new Vector2(8, 5));
//Room("r0", new Vector3(6, 8, 1)); //x=6 y=8 h1
//Room("r1", new Vector3(2, 10, 1), new Vector2(6, 0));
//Room("r2", new Vector3(5, 4, 1), new Vector2(8, 5));
}
// Update is called once per frame
void Update()
{
}
public static void Cube(GameObject cube, Vector3 cube_size, Vector2 position = new Vector2())
{
//GameObject cube = GameObject.Find("Cube");
cube_size = new Vector3(cube_size.x, cube_size.y, cube_size.z);
Vector3 pos_cube = new Vector3(cube_size.x / 2f, cube_size.z / 2f, cube_size.y / 2f);
cube.transform.position = new Vector3(pos_cube.x + position.x, pos_cube.y, pos_cube.z + position.y);
// above
GameObject plane_a = cube.transform.GetChild(0).gameObject;
//plane_above.transform.localScale = cube_size;
plane_a.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.y);
plane_a.transform.localPosition = new Vector3(0, pos_cube.y, 0);//y x=0
// front
GameObject plane_b = cube.transform.GetChild(1).gameObject;
plane_b.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.z);
plane_b.transform.localPosition = new Vector3(0, 0, -pos_cube.z);
// side
GameObject plane_c = cube.transform.GetChild(2).gameObject;
plane_c.transform.localScale = new Vector3(cube_size.y, 0.01f, cube_size.z);
plane_c.transform.localPosition = new Vector3(-pos_cube.x, 0, 0);
// низ
GameObject plane_d = cube.transform.GetChild(3).gameObject;
plane_d.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.y);
plane_d.transform.localPosition = new Vector3(0, -pos_cube.y, 0);
// зад
GameObject plane_e = cube.transform.GetChild(4).gameObject;
plane_e.transform.localScale = new Vector3(cube_size.y, 0.01f, cube_size.z);
plane_e.transform.localPosition = new Vector3(pos_cube.x, 0, 0);
// front 2
GameObject plane_f = cube.transform.GetChild(5).gameObject;
plane_f.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.z);
plane_f.transform.localPosition = new Vector3(0, 0, pos_cube.z);
}
public static void Room(string room_name, Vector3 cube_size, Vector2 position = new Vector2())
{
//GameObject cube = GameObject.Find("Cube");
var room = Instantiate(Resources.Load("GameObjects/RoomTest", typeof(GameObject)) as GameObject);
room.name = room_name;
cube_size = new Vector3(cube_size.x, cube_size.y, cube_size.z);
Vector3 pos_cube = new Vector3(cube_size.x / 2f, cube_size.z / 2f, cube_size.y / 2f);
room.transform.position = new Vector3(pos_cube.x + position.x, pos_cube.y, pos_cube.z + position.y);
// above
var SideA = room.transform.GetChild(0).gameObject;
var panel_a = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject)) as GameObject);
panel_a.name = "Plane_a_" + 0;
panel_a.transform.SetParent(SideA.transform);
//GameObject plane_a = cube.transform.GetChild(0).gameObject;
//plane_above.transform.localScale = cube_size;
panel_a.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.y);
panel_a.transform.localPosition = new Vector3(0, pos_cube.y, 0);//y x=0
panel_a.transform.rotation = SideA.transform.rotation;
// front
var SideB = room.transform.GetChild(1).gameObject;
//GameObject plane_b = cube.transform.GetChild(1).gameObject;
//plane_b.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.z);
for (int i = 0; i < cube_size.x; i++)
{
var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
panel.name = "Plane_b_" + i;
panel.transform.SetParent(SideB.transform);
panel.transform.localPosition = new Vector3(i, 0, -pos_cube.z);
panel.transform.rotation = SideB.transform.rotation;
}
// side
var SideC = room.transform.GetChild(2).gameObject;
//GameObject plane_c = cube.transform.GetChild(2).gameObject;
for (int i = 0; i > -cube_size.x; i--)
{
var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
panel.name = "Plane_c_" + i;
panel.transform.SetParent(SideC.transform);
panel.transform.position = new Vector3(i, 0, 0);
panel.transform.rotation = SideC.transform.rotation;
}
// низ
var SideD = room.transform.GetChild(3).gameObject;
var panel_d = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
//GameObject plane_d = cube.transform.GetChild(3).gameObject;
panel_d.name = "Plane_d_" + 0;
panel_d.transform.SetParent(SideD.transform);
panel_d.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.y);
panel_d.transform.position = new Vector3(0, -pos_cube.y, 0);
panel_d.transform.rotation = SideD.transform.rotation;
// зад
var SideE = room.transform.GetChild(4).gameObject;
for (int i = 0; i > -cube_size.x; i--)
{
var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
//GameObject plane_e = cube.transform.GetChild(4).gameObject;
panel.name = "Plane_e_" + i;
panel.transform.SetParent(SideE.transform);
panel.transform.position = new Vector3(pos_cube.x, 0, 0);
panel.transform.rotation = SideE.transform.rotation;
}
// front 2
var SideF = room.transform.GetChild(5).gameObject;
for (int i = 0; i > -cube_size.x; i--)
{
var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
//GameObject plane_f = cube.transform.GetChild(5).gameObject;
panel.name = "Plane_f_" + i;
panel.transform.SetParent(SideF.transform);
panel.transform.position = new Vector3(0, 0, pos_cube.z);
panel.transform.rotation = SideF.transform.rotation;
}
}
//public GameObject currentTank;
//private GameObject Tank;
private NavMeshAgent agent;
public GameObject SpotSpawn;
public Vector3 target;
private bool b_target = true;
public void TakePosition()
{
var Tank = Instantiate(Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject);
Tank.transform.position = SpotSpawn.transform.position;
if (Tank != null)
{
Ray ray;
RaycastHit hit;
ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 10000.0f))
{
// Tank.transform.position = hit.point;
target = hit.point;
if (b_target)
{
if (Vector3.Distance(target, transform.position) > 2f)
{
agent.SetDestination(target);
b_target = false;
Tank = null;
}
}
}
}
}
//public GameObject tankTemplatePrefab; //префаб для прозрачного танка
//public GameObject tankPrefab; //префаб оригинального танка
public LayerMask terrainMask; //слой который назначет террейну
// этот метод назначить на клик кнопки "создать танк"
public void makeTankOnClick()
{
//запускаем корутину
StartCoroutine("setTankPosition");
}
///
/// http://unity3d.ru/distribution/viewtopic.php?f=18&t=36275
///
///
IEnumerator setTankPosition()
{
// создаём прозрачный танк
GameObject tankTemplate = Instantiate(Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject);
// переменные для обнаружения столкновений мышки с террейном
Ray ray;
RaycastHit hit;
// бесконечный цикл до тех пор, пока не кликнем по террейну
while (true)
{
// пускаем луч из мышки
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// если луч попал в террейн
if (Physics.Raycast(ray, out hit, terrainMask))
{
// перемещаем прозрачный танк в позицию столкновения луча и террейна
tankTemplate.transform.position = hit.point;
//tankPrefab.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
// если нажали левую кнопку мышки
if (Input.GetMouseButtonDown(0))
{
// удаляем прозрачный танк
Destroy(tankTemplate);
// и создаём оригинальный танк на его месте
GameObject tank = Instantiate(Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject);
tank.transform.position = hit.point;
//пустить tank в плаванье через навигейтАгент
// останавливаем корутину
yield break;
}
}
yield return null;
}
}
}