using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//新编码所需的信息 xz坐标 建筑ID 所占格数 /区分建筑码和地址码 地址码只记录xz坐标和是否被占用 /所占格数可以用建筑ID
//需增加功能 阻止重复建造
///-----------------------------------------------------------------------------------------
/// Founction:
/// InvolvingKnowledge:
/// Author: AttouchAusturo
/// LatestReviseTime:
///-----------------------------------------------------------------------------------------
public class UpRootFunction : MonoBehaviour
{
//建筑的代号 /理论上最多达到99 /用于编码 /编码中该位置为0可以表示杂草,若整串编码只有一个0则表示空地 /99可以表示任何被建筑占用的土地
public int buildingID;
//建筑预制件 /提前指定
public GameObject followTestPrefab; //建造预览物体预制件 /暂定:小圆球
public GameObject resistBuildPrefab; //阻止建造物体预制件 /暂定:大方块
public GameObject tentPrefab; //帐篷
public GameObject materialsPrefab; //物资
public GameObject manorPrefab; //庄园
public GameObject warehousePrefab; //仓库
//要建造的建筑和建筑实例 /非提前指定
private GameObject objectToBuild; //衔接选择建筑与建造
private GameObject objectToReBuild; //衔接选择建筑与重建
private GameObject buildings; //建造物实例化
private GameObject virtualBuilding; //预览建造物
//编码内容
public int positionX = 0, positionZ, reBuildBuildingID;
//建筑码 /编码为建筑属性
public int allCode;
//建筑码存储地址 /编码后转字符串 /地址为网格属性
public int theAddress;
public string strTheAddress;
//射线获取坐标
public Vector3 target;//鼠标点击位置的世界坐标
public Ray ray; //似乎不必要 /待验证
public RaycastHit hit; //一个类 用于存储发射射线后产生的碰撞信息 常用的成员变量collider distance normal point
//坐标转换
private Vector3 groundArea;
private Vector3 recGroundArea;
//是否仍被选中 /未点击建造
public int beSelected = 0;
private void OnGUI()
{
GUILayout.BeginHorizontal("box");
GUILayout.Label("选择建筑");
if (GUILayout.Button("小帐篷")) { buildingID = 1; beSelected = 1; } //边长1
if (GUILayout.Button("物资")) { buildingID = 2; beSelected = 1; } //边长1
if (GUILayout.Button("庄园")) { buildingID = 3; beSelected = 1; } //边长3
if (GUILayout.Button("仓库")) { buildingID = 4; beSelected = 1; } //边长2
if (GUILayout.Button("铲除")) { buildingID = 0; } //清除不需要预览
GUILayout.EndHorizontal();
}
private void Start()
{
buildingID = 99;
//ClearAllBuildings();//清除存档内容为0
TryingMyRebuildMethodWithPlayerPrefs();//重现存档中建筑
}
private void Update()
{
CreateBuilding();//建造功能函数 /目前包含功能 射线检测 坐标取整 生成建筑 生成编码并保存 建造中实时预览 多网格建造
UpRoot();
}
//射线检测坐标
private void RayCheckGroundArea()
{
//鼠标在屏幕的位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) //Physics.Raycast(ray, out hit) 该方法中hit变量会携带检测物体的相关信息 如transform
{
//绘制出一条从相机射出的红色射线
Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
}
//获取点击位置的世界坐标
target = hit.point;
}//射线检测坐标
//铲除
private void UpRoot()
{
int newPositionX, newPositionZ;
//手动铲除建筑 /难点:由建筑获取其地址
if (buildingID == 0)
{
//确定铲除对象
if (Input.GetMouseButtonDown(1))
{
//射线
RayCheckGroundArea();
//取出该地址存储的code
theAddress = 10000 + ((int)target.x / 10 + 1) * 100 + ((int)target.z / 10 + 1);
strTheAddress = theAddress.ToString();
allCode = PlayerPrefs.GetInt(strTheAddress);
//解码 取地址
positionX = allCode / 10000 - 1;
positionZ = allCode % 10000 / 100 - 1;
//在存档中将所有相同地址的网格改为0 /替代原数据
for (int i = 1; i < 21; i++)
{
for (int j = 1; j < 21; j++)
{
theAddress = 10000 + (i * 100) + j;
strTheAddress = theAddress.ToString();
allCode = PlayerPrefs.GetInt(strTheAddress);
newPositionX = allCode / 10000 - 1;
newPositionZ = allCode % 10000 / 100 - 1;
if ((positionX == newPositionX) && (positionZ == newPositionZ))
{
PlayerPrefs.SetInt(strTheAddress, 0);
}
}
}
//暂时替代方案:移走这个模型 /这样既可以实现消失的效果 又不用担心点到地面
hit.transform.position = new Vector3(-200, -5, -200);
//结束
buildingID = 99;
}
}
}//铲除
//区分并选择不同尺寸的建筑 /在决定建造前被调用
private void DistinguishDifferentSizeBuildings(int buildingID)
{
int offset = 0;
switch (buildingID)
{
case 1: offset = 5; objectToBuild = tentPrefab; break;
case 2: offset = 5; objectToBuild = materialsPrefab; break;
case 3: offset = 15; objectToBuild = manorPrefab; break;
case 4: offset = 10; objectToBuild = warehousePrefab; break;
}
groundArea = new Vector3((int)target.x / 10 * 10 + offset, 0, (int)target.z / 10 * 10 + offset);
}//区分并选择不同尺寸的建筑
//将新建筑编码 /仅在决定建造时被调用
private void CodingNewBuildings()
{
//编码
allCode = ((int)target.x / 10 + 1) * 10000 + (((int)target.z / 10 + 1) * 100) + buildingID;
//保存
TryingMyNewSavingMethodWithPlayerPrefs();
//在存档中将多余网格数据改为xxxx99 /99即占用 /99前表示其占有者的位置 /如果包含99的code中存储的是自身位置 将没有意义 /只有一个位置将生成物体时 位置才有意义
switch (buildingID)
{
case 1: break;
case 2: break;
case 3:
allCode = ((int)target.x / 10 + 1) * 10000 + (((int)target.z / 10 + 1) * 100) + 99;
theAddress = 10000 + (((int)target.x / 10 + 1) * 100) + ((int)target.z / 10 + 2); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 1) * 100) + ((int)target.z / 10 + 3); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 2) * 100) + ((int)target.z / 10 + 1); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 2) * 100) + ((int)target.z / 10 + 2); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 2) * 100) + ((int)target.z / 10 + 3); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 3) * 100) + ((int)target.z / 10 + 1); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 3) * 100) + ((int)target.z / 10 + 2); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 3) * 100) + ((int)target.z / 10 + 3); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
break; //3*3
case 4:
allCode = ((int)target.x / 10 + 1) * 10000 + (((int)target.z / 10 + 1) * 100) + 99;
theAddress = 10000 + (((int)target.x / 10 + 1) * 100) + ((int)target.z / 10 + 2); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 2) * 100) + ((int)target.z / 10 + 2); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
theAddress = 10000 + (((int)target.x / 10 + 2) * 100) + ((int)target.z / 10 + 1); strTheAddress = theAddress.ToString(); PlayerPrefs.SetInt(strTheAddress, allCode);
break; //2*2
}
}//将新建筑编码
//建造功能
private void CreateBuilding()
{
//如何区分两个因果关系的点击为两次点击 /暂时方案:区分左右键
if ((buildingID != 99)&& (buildingID != 0))
{
//如果未决定建造 /预览
if (beSelected != 0)
{
//射线
RayCheckGroundArea();
//区分建筑尺寸
DistinguishDifferentSizeBuildings(buildingID);
//如果groundArea发生改变
if (groundArea != recGroundArea)
{
//摧毁上一个位置的对象
Destroy(virtualBuilding, 0);
//在格子中央生成该物体
virtualBuilding = Instantiate(followTestPrefab, groundArea, Quaternion.identity) as GameObject;
}
//记录鼠标位置变化
recGroundArea = groundArea;
}//预览
//当点击鼠标右键时 /决定建造
if (Input.GetMouseButtonDown(1))
{
//删除最后残留的预览物体
Destroy(virtualBuilding, 0);
//射线
RayCheckGroundArea();
//判断建造的位置在存档中是否有障碍物
//取消预览
beSelected = 0;
//区分建筑尺寸
DistinguishDifferentSizeBuildings(buildingID);
//在格子中央生成该物体
buildings = Instantiate(objectToBuild, groundArea, Quaternion.identity) as GameObject;
//将新建筑编码并保存
CodingNewBuildings();
buildingID = 99;
}//决定建造
}
}//建造功能
//清除所有建筑
private void ClearAllBuildings()
{
for (int i = 1; i < 21; i++)
{
for (int j = 1; j < 21; j++)
{
allCode = i * 10000 + (j * 100); //xxxx00
theAddress = 10000 + (i * 100) + j;
strTheAddress = theAddress.ToString();
PlayerPrefs.SetInt(strTheAddress, allCode);
}
}
}//清除所有建筑
//保存建筑
private void TryingMyNewSavingMethodWithPlayerPrefs()
{
positionX = allCode / 10000 - 1;
positionZ = allCode % 10000 / 100 - 1;
if ((positionX > -1 && positionX < 20) && (positionZ > -1 && positionZ < 20)) //范围限制:世界中心正20*20格
{
theAddress = 10000 + ((positionX + 1) * 100) + (positionZ + 1);
strTheAddress = theAddress.ToString();
PlayerPrefs.SetInt(strTheAddress, allCode);
}
}//保存建筑
//重现存档中建筑 /取出所有地址中的值,赋给Code并进行解码
private void TryingMyRebuildMethodWithPlayerPrefs()
{
int allCode;
for (int i = 1; i < 21; i++)
{
for (int j = 1; j < 21; j++)
{
theAddress = 10000 + (i * 100) + j;
strTheAddress = theAddress.ToString();
allCode = PlayerPrefs.GetInt(strTheAddress);
AnalyzeCode(allCode);//解码
}
}
}//重现存档中建筑
//解码 并生成建筑
private void AnalyzeCode(int allCode)
{
int offset = 0;
if (allCode != 0) //若为0则什么也不做
{
positionX = allCode / 10000 - 1;
positionZ = allCode % 10000 / 100 - 1;
reBuildBuildingID = allCode % 100;
switch (reBuildBuildingID)
{
case 0: offset = 5; objectToReBuild = tentPrefab; break; //改为杂草
case 1: offset = 5; objectToReBuild = tentPrefab; break;
case 2: offset = 5; objectToReBuild = materialsPrefab; break;
case 3: offset = 15; objectToReBuild = manorPrefab; break;
case 4: offset = 10; objectToReBuild = warehousePrefab; break;
case 99: offset = 0; break;
}
if(offset != 0) buildings = Instantiate(objectToReBuild, new Vector3(positionX * 10 + offset, 0, positionZ * 10 + offset), Quaternion.identity) as GameObject;
}
}//解码 并生成建筑
}