C#笔记9//建造游戏5/建造预览

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

//新编码所需的信息 xz坐标 建筑ID 所占格数 /区分建筑码和地址码 地址码只记录xz坐标和是否被占用
//需增加功能 阻止重复建造 铲除建筑 建造时跟随鼠标吸附网格并虚化

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

public class MultyGridBuild : MonoBehaviour
{
    //建筑的代号
    public int buildingID;
    //建筑预制件 /提前指定
    public GameObject followTestPrefab; //建造预览物体预制件 /暂定:小圆球
    public GameObject tentPrefab; //帐篷
    public GameObject warehousePrefab; //仓库
    public GameObject ovenPrefab; //土灶
    public GameObject towerPrefab; //塔
    public GameObject workbenchPrefab; //工作台
    public GameObject smelterPrefab; //熔炉
    public GameObject grainsPrefab; //粮堆
    //要建造的建筑和建筑实例 /非提前指定
    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;

    //射线获取坐标
    private Ray ray;
    private RaycastHit hit;
    private Vector3 target;//鼠标点击位置的世界坐标
    //坐标转换
    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; }//选择建筑
        if (GUILayout.Button("仓库")) { buildingID = 2; beSelected = 1; }
        if (GUILayout.Button("土灶")) { buildingID = 3; beSelected = 1; }
        if (GUILayout.Button("瞭望塔")) { buildingID = 4; beSelected = 1; }
        if (GUILayout.Button("工作台")) { buildingID = 5; beSelected = 1; }
        if (GUILayout.Button("熔炉")) { buildingID = 6; beSelected = 1; }
        if (GUILayout.Button("粮堆")) { buildingID = 7; beSelected = 1; }
        if (GUILayout.Button("铲除")) {; }
        GUILayout.EndHorizontal();
    }

    private void Start()
    {
        ClearAllBuildings();//清除存档内容为0
        TryingMyRebuildMethodWithPlayerPrefs();//再次运行时重建
    }

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

    //建造功能
    private int CreateBuilding(int buildingID)
    {

        //衔接选择建筑与建造
        if (buildingID == 1) objectToBuild = tentPrefab;
        if (buildingID == 2) objectToBuild = warehousePrefab;
        //if (buildingID == 3) ;
        //if (buildingID == 4) ;
        //if (buildingID == 5) ;
        //if (buildingID == 6) ;
        //if (buildingID == 7) ;

        //如何区分两个因果关系的点击为两次点击 /暂时方案:区分左右键
        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 = new Vector3((int)target.x / 10 * 10 + 5, 0, (int)target.z / 10 * 10 + 5);//坐标取整并移至格子中心 /取整:/10*10 /取中:+5

                //如果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 = new Vector3((int)target.x / 10 * 10 + 5, 0, (int)target.z / 10 * 10 + 5);//坐标取整并移至格子中心 /取整:/10*10 /取中:+5
                buildings = Instantiate(objectToBuild, groundArea, Quaternion.identity) as GameObject;//在格子中央生成该物体

                //将新建筑编码
                allCode = (int)target.x / 10 * 10000 + (int)target.z / 10 * 100 + buildingID;
                //保存
                TryingMyNewSavingMethodWithPlayerPrefs();

                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++)
        {
            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);//解码
            }
        }

    }//取出所有地址中的值,赋给Code并进行解码

    //解码 并生成建筑
    private void AnalyzeCode(int allCode)
    {

        if (allCode != 0)
        {
            positionX = allCode / 10000;
            positionZ = allCode % 10000 / 100;
            reBuildBuildingID = allCode % 100;
            if (reBuildBuildingID == 1) objectToReBuild = tentPrefab;
            if (reBuildBuildingID == 2) objectToReBuild = warehousePrefab;
            buildings = Instantiate(objectToReBuild, new Vector3(positionX * 10 + 5, 0, positionZ * 10 + 5), Quaternion.identity) as GameObject;
        }

    }//解码 并生成建筑
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值