86 lines
2.1 KiB
C#
86 lines
2.1 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
|
|||
|
|
public class BuildMode : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public static BuildMode Instance { get; private set; }
|
|||
|
|
|
|||
|
|
private int previewItemId = -1; //this is the item that the user is placing
|
|||
|
|
private GameObject previewObject;
|
|||
|
|
|
|||
|
|
public void Awake()
|
|||
|
|
{
|
|||
|
|
Instance = this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void preview(int itemId)
|
|||
|
|
{
|
|||
|
|
if (itemId != -1)
|
|||
|
|
{
|
|||
|
|
GameObject clone = OnDemandLoader.Load<GameObject>("items/" + itemId);
|
|||
|
|
previewObject = Instantiate(clone);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//Destroy(previewObject);
|
|||
|
|
}
|
|||
|
|
previewItemId = itemId;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Update()
|
|||
|
|
{
|
|||
|
|
if (previewItemId != -1)
|
|||
|
|
{
|
|||
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|||
|
|
|
|||
|
|
//do a raycast on the terrain
|
|||
|
|
RaycastHit hitInfo;
|
|||
|
|
if (Physics.Raycast(ray, out hitInfo, 1000.0f))
|
|||
|
|
{
|
|||
|
|
previewObject.SetActive(true);
|
|||
|
|
previewObject.transform.position = hitInfo.point;
|
|||
|
|
previewObject.transform.position = new Vector3(Mathf.Floor(hitInfo.point.x) + 0.5f,
|
|||
|
|
World.Instance.getHeight(hitInfo.point),
|
|||
|
|
Mathf.Floor(hitInfo.point.z) + 0.5f);
|
|||
|
|
previewObject.transform.localScale = new Vector3(8.0f, 8.0f, 8.0f);
|
|||
|
|
|
|||
|
|
//make preview object transparent
|
|||
|
|
Renderer[] renderers = previewObject.GetComponentsInChildren<Renderer>();
|
|||
|
|
for (int i = 0; i < renderers.Length; i++)
|
|||
|
|
{
|
|||
|
|
renderers[i].material.color = new Color(1.0f, 1.0f, 1.0f, 0.5f);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//previewObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
previewObject.SetActive(false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//check for click
|
|||
|
|
if (Input.GetMouseButtonDown(0))
|
|||
|
|
{
|
|||
|
|
PacketManager.sendPlaceItem(previewItemId, previewObject.transform.position.x, previewObject.transform.position.y, previewObject.transform.position.z, previewObject.transform.rotation.y);
|
|||
|
|
preview(-1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//if the mouse wheen has scrolled up, rotate the previewObject
|
|||
|
|
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
|
|||
|
|
{
|
|||
|
|
previewObject.transform.Rotate(Vector3.up, 10.0f);
|
|||
|
|
}
|
|||
|
|
else if (Input.GetAxis("Mouse ScrollWheel") < 0f)
|
|||
|
|
{
|
|||
|
|
previewObject.transform.Rotate(Vector3.up, -10.0f);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|