C#笔记12//建造游戏8/阻止重复建造

做得有些复杂,但好在还没卡。目前只实现了基本的网格建造功能,像一些道路、城墙这种能拼接的建筑还不能做,到时候每个建筑自身写个类试一下。

关于建造类的代码就先到这,用PlayerPrefs做的这个类只是实现了基本思路,后面要做的话我觉得可能还是会用JSON

遗留下来的问题大致有三个,一是这个Update有些占资源,但是又没法删掉(因为后续要做的功能可能会很大,希望在合适的时间打开这个功能)。第二是没能做到区分两次点击,而是暂时用左右键来区分(毕竟玩家习惯于点击左键操作,而且最好将右键作为取消键)。第三是铲除建筑的时候鼠标必须点到建筑上,而点到建筑所在的地面块时并不会让建筑消失,只能让存档中的该建筑消失(如果这样的话,就会要求美工为建筑加一个薄薄的底座以覆盖地面网格,需要花时间调整尺寸,还占用资源,而且不美观,显然很麻烦)。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///-----------------------------------------------------------------------------------------          
///   Founction:	
///   InvolvingKnowledge:      	
///   Author:               	AttouchAusturo
///   LatestReviseTime:	
///-----------------------------------------------------------------------------------------

public class PreventBuildFunction : MonoBehaviour
{
    //建筑的代号 /唯一标识 可表示建筑本身 建筑尺寸 /最多达到99 /编码中该位置为0表示杂草,整串编码只有一个0表示空地 /99表示被占用的土地
    public int buildingID;
    //建筑预制件 /提前指定
    public GameObject followTestPrefab; //建造预览物体预制件 /暂定:小圆球
    public GameObject preventPrefab; //阻止建造物体预制件 /暂定:大方块
    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, positionZ, newBuildingID, newPositionX, newPositionZ;

    //建筑码 /编码为建筑属性
    public int allCode;
    //建筑码存储地址 /编码后转字符串 /地址为网格属性
    public int theAddress, theNewAddress;
    public string strTheAddress;

    //射线获取坐标
    public Vector3 target;//鼠标点击位置的世界坐标
    public Ray ray; 
    public RaycastHit hit; //一个类 用于存储发射射线后产生的碰撞信息 常用的成员变量collider distance normal point 

    //坐标转换
    public int offset; //偏移
    public Vector3 groundArea;
    public Vector3 recGroundArea;

    //判断 /是否仍被选中 /未点击建造
    public int beSelected = 0;
    //判断 /是否有障碍
    public int beOccupied = 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(); //清除存档内容为xxxx00
        TryingMyRebuildMethodWithPlayerPrefs(); //重现存档中建筑
    }

    private void Update()
    {
        CreateBuilding(); //建造功能函数 /目前包含功能 射线检测 坐标取整 生成建筑 生成编码并保存 建造中实时预览 多网格建造 阻止重复建造
        UpRoot(); //拆除 /目前
    }

    //建造功能
    private void CreateBuilding()
    {
        //如何区分两个因果关系的点击为两次点击 /暂时方案:区分左右键
        if ((buildingID != 99) && (buildingID != 0))
        {
            //如果未决定建造 /预览
            if (beSelected != 0)
            {
                //射线 /得到格子信息hit和target
                RayCheckGroundArea();

                //区分建筑尺寸 /根据建筑ID发生偏移 /由ID计算groundArea
                DistinguishDifferentSizeBuildings();

                //如果groundArea发生改变 
                if (groundArea != recGroundArea)
                {
                    //摧毁上一个位置的对象
                    Destroy(virtualBuilding, 0);

                    //恢复 重新检测
                    beOccupied = 0;
                    //编写地址
                    theAddress = 10000 + ((int)target.x / 10 + 1) * 100 + ((int)target.z / 10 + 1);

                    //以所持有建筑ID为依据 决定要检测的格数 /每格单独检测
                    switch (buildingID)
                    {
                        case 1:
                        case 2:
                            strTheAddress = theAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++; break;
                        case 3:
                            strTheAddress = theAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 1; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 2; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 100; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 101; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 102; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 200; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 201; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 202; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++; break; //3*3
                        case 4:
                            strTheAddress = theAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 1; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 100; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++;
                            theNewAddress = theAddress + 101; strTheAddress = theNewAddress.ToString(); allCode = PlayerPrefs.GetInt(strTheAddress); if (allCode != 0) beOccupied++; break; //2*2

                    }

                    //生成圆球
                    if (beOccupied == 0) virtualBuilding = Instantiate(followTestPrefab, groundArea, Quaternion.identity) as GameObject;
                    //生成方块
                    else virtualBuilding = Instantiate(preventPrefab, groundArea, Quaternion.identity) as GameObject;

                }

                //记录鼠标位置变化
                recGroundArea = groundArea;

            }//预览

            //当点击鼠标右键时 /决定建造
            if (Input.GetMouseButtonDown(1))
            {
                //如果无障碍
                if (beOccupied == 0)
                {
                    //删除最后残留的预览物体
                    Destroy(virtualBuilding, 0);

                    //射线
                    RayCheckGroundArea();

                    //取消预览
                    beSelected = 0;

                    //区分建筑尺寸
                    DistinguishDifferentSizeBuildings();

                    //在格子中央生成该物体
                    buildings = Instantiate(objectToBuild, groundArea, Quaternion.identity) as GameObject;

                    //将新建筑编码并保存
                    CodingNewBuildings();

                    buildingID = 99;

                } 

            }//决定建造

        }

    }//建造功能

    //射线检测坐标
    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()
    {
        //手动铲除建筑 /难点:由建筑获取其地址
        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;

            }

        }

    }//铲除

    //区分建筑尺寸 /在决定建造前被调用 /根据建筑ID发生偏移
    private void DistinguishDifferentSizeBuildings()
    {
        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 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()
    {
        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();//解码
            }
        }

    }//重现存档中建筑

    //解码 并生成建筑 /根据建筑ID发生偏移
    private void AnalyzeCode()
    {
        if (allCode != 0) //若为0则什么也不做
        {

            positionX = allCode / 10000 - 1;
            positionZ = allCode % 10000 / 100 - 1;
            newBuildingID = allCode % 100;

            switch (newBuildingID)
            {
                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;

        }

    }//解码 并生成建筑

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值