栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

C#笔记10//建造游戏6/多网格建造

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#笔记10//建造游戏6/多网格建造

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

//需增加功能 阻止重复建造 铲除建筑

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

public class MultyGridBuild1 : MonoBehaviour
{
    //建筑的代号 /理论上最多达到99 
    public int buildingID;
    //建筑预制件 /提前指定
    public GameObject followTestPrefab; //建造预览物体预制件 /暂定:小圆球
    public GameObject resistBuildPrefab; //阻止建造物体预制件 /暂定:大方块
    public GameObject tentPrefab; //帐篷
    public GameObject materialsPrefab; //物资
    public GameObject manorPrefab; //庄园
    public GameObject warehousePrefab; //仓库
    //要建造的建筑和建筑实例 /非提前指定
    public GameObject objectToBuild; //衔接选择建筑与建造
    public GameObject objectToReBuild; //衔接选择建筑与重建
    public GameObject buildings; //建造物实例化
    public GameObject virtualBuilding; //预览建造物

    //编码内容
    public int positionX = 0, positionZ, reBuildBuildingID;

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

    //射线获取坐标
    public Vector3 target;//鼠标点击位置的世界坐标
    private Ray ray;
    private RaycastHit hit;

    //坐标转换
    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()
    {
        //ClearAllBuildings();//清除存档内容为0
        TryingMyRebuildMethodWithPlayerPrefs();//重现存档中建筑
    }

    private void Update()
    {
        buildingID = CreateBuilding(buildingID);//建造功能函数 /目前包含功能 射线检测 坐标取整 生成建筑 生成编码并保存 建造中实时预览
    }

    //区分并选择不同尺寸的建筑
    private Vector3 DistinguishDifferentSizeBuildings(int buildingID)
    {

        Vector3 groundArea;
        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);

        return groundArea;

    }

    //将新建筑编码 /仅在决定建造时被调用
    private void CodingNewBuildings()
    {

        allCode = (int)target.x / 10 * 10000 + (int)target.z / 10 * 100 + buildingID;

        TryingMyNewSavingMethodWithPlayerPrefs();

        switch (buildingID)
        {
            case 1: break;
            case 2: break;
            case 3:
                allCode = (int)target.x / 10 * 10000 + ((int)target.z / 10 + 1) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 1) * 10000 + ((int)target.z / 10 + 1) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 2) * 10000 + ((int)target.z / 10 + 1) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = (int)target.x / 10 * 10000 + ((int)target.z / 10 + 2) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 1) * 10000 + ((int)target.z / 10 + 2) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 2) * 10000 + ((int)target.z / 10 + 2) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 1) * 10000 + (int)target.z / 10 * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 2) * 10000 + (int)target.z / 10 * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs(); break;
            case 4:
                allCode = (int)target.x / 10 * 10000 + ((int)target.z / 10 + 1) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 1) * 10000 + ((int)target.z / 10 + 1) * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs();
                allCode = ((int)target.x / 10 + 1) * 10000 + (int)target.z / 10 * 100 + 99; TryingMyNewSavingMethodWithPlayerPrefs(); break;
        }

    }

    //建造功能
    private int CreateBuilding(int buildingID)
    {
        //衔接选择建筑与建造
        //if (buildingID == 1) objectToBuild = tentPrefab;
        //if (buildingID == 2) objectToBuild = materialsPrefab;
        //if (buildingID == 3) objectToBuild = manorPrefab;
        //if (buildingID == 4) objectToBuild = warehousePrefab; 

        //如何区分两个因果关系的点击为两次点击 /暂时方案:区分左右键
        if (buildingID != 0)
        {
            //如果未决定建造 /预览
            if (beSelected != 0)
            {
                //鼠标在屏幕的位置
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit))
                {
                    //绘制出一条从相机射出的红色射线
                    Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
                }

                //获取点击位置的世界坐标
                target = hit.point;

                //区分建筑尺寸
                groundArea = 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);

                //鼠标在屏幕的位置
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit))
                {
                    //绘制出一条从相机射出的红色射线
                    Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
                }
                target = hit.point;//获取点击位置的世界坐标

                //判断建造的位置在存档中是否有障碍物

                //取消预览
                beSelected = 0;

                //区分建筑尺寸
                groundArea = DistinguishDifferentSizeBuildings(buildingID);

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

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

                return buildingID = 0;

            }//决定建造

        }

        return buildingID;

    }//建造功能

    //清除所有建筑
    private void ClearAllBuildings()
    {

        for (int i = 1; i < 21; i++)
        {
            for (int j = 1; j < 21; j++)
            {
                theAddress = 10000 + (i * 100) + j;
                strTheAddress = theAddress.ToString();
                PlayerPrefs.SetInt(strTheAddress, 0);
            }
        }

    }//清除所有建筑

    //保存建筑 /将Code解码来确定将其放入哪个地址中
    private void TryingMyNewSavingMethodWithPlayerPrefs()
    {

        for (int i = 1; i < 21; i++) //i和j代表网格数 /超出后不保存
        {
            for (int j = 1; j < 21; j++)
            {
                if (allCode / 10000 == i - 1 && allCode % 10000 / 100 == j - 1)
                {
                    theAddress = 10000 + (i * 100) + j;
                    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)
        {
            positionX = allCode / 10000;
            positionZ = allCode % 10000 / 100;
            reBuildBuildingID = allCode % 100;

            switch (reBuildBuildingID)
            {
                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; break;
                case 99: return;
            }

            buildings = Instantiate(objectToReBuild, new Vector3(positionX * 10 + offset, 0, positionZ * 10 + offset), Quaternion.identity) as GameObject;

        }

    }//解码 并生成建筑

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/902219.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号