/// <summary> /// 初始化方块信息 /// </summary> /// <param name="listData"></param> public void InitBlockInfo(List <BlockInfoBean> listData) { for (int i = 0; i < listData.Count; i++) { BlockInfoBean itemInfo = listData[i]; arrayBlockInfo[itemInfo.id] = itemInfo; } }
/// <summary> /// 获取游戏数据 /// </summary> /// <returns></returns> public BlockInfoBean GetBlockInfoData() { BlockInfoBean data = serviceBlockInfo.QueryData(); if (data == null) { data = new BlockInfoBean(); } return(data); }
/// <summary> /// 更新数据 /// </summary> /// <param name="data"></param> /// <returns></returns> public bool UpdateData(BlockInfoBean data) { bool deleteState = BaseDeleteDataById(data.id); if (deleteState) { bool insertSuccess = BaseInsertData(tableNameForMain, data); return(insertSuccess); } return(false); }
/// <summary> /// 获取数据 /// </summary> /// <param name="action"></param> /// <returns></returns> public BlockInfoBean GetBlockInfoData(Action <BlockInfoBean> action) { BlockInfoBean data = GetModel().GetBlockInfoData(); if (data == null) { GetView().GetBlockInfoFail("没有数据", null); return(null); } GetView().GetBlockInfoSuccess(data, action); return(data); }
/// <summary> /// 方块列表UI /// </summary> /// <param name="listData"></param> protected void UIForBlockList(List <BlockInfoBean> listData) { if (listData.IsNull()) { return; } for (int i = 0; i < listData.Count; i++) { GUILayout.Space(50); BlockInfoBean itemBlockInfo = listData[i]; UIForBlockItem(false, itemBlockInfo); } }
public override void RefreshCrop(Chunk chunk, Vector3Int localPosition, BlockInfoBean blockInfo) { base.RefreshCrop(chunk, localPosition, blockInfo); BlockBean blockData = chunk.GetBlockData(localPosition); //获取成长周期 BlockMetaCrop blockCropData = FromMetaData <BlockMetaCrop>(blockData.meta); //如果是等级大于0的子集 则不继续往上生长 if (blockCropData.uvIndex > 0) { return; } //判断是否已经是最大生长周期 int lifeCycle = GetCropLifeCycle(); if (blockCropData.growPro >= lifeCycle - 1) { //在玉米的上2格再生成同样的方格 //更新方块并 添加更新区块 GetCloseBlockByDirection(chunk, localPosition, DirectionEnum.UP, out Block blockUp, out Chunk chunkUp, out Vector3Int localPositionUp); GetCloseBlockByDirection(chunk, localPosition + Vector3Int.up, DirectionEnum.UP, out Block blockUpUp, out Chunk chunkUpUp, out Vector3Int localPositionUpUp); if (chunkUp != null && blockUp != null && blockUp.blockType != BlockTypeEnum.None) { return; } BlockMetaCrop blockCropDataUp = FromMetaData <BlockMetaCrop>(blockData.meta); if (chunkUpUp != null && blockUpUp != null && blockUpUp.blockType != BlockTypeEnum.None) { blockCropDataUp.uvIndex = 2; } else { blockCropDataUp.uvIndex = 1; } chunk.SetBlockForLocal(localPosition + Vector3Int.up, BlockTypeEnum.CropCorn, BlockDirectionEnum.UpForward, ToMetaData(blockCropDataUp), false); //继续往上 if (chunkUpUp != null && blockUpUp != null && blockUpUp.blockType != BlockTypeEnum.None) { return; } blockCropDataUp.uvIndex = 2; chunk.SetBlockForLocal(localPosition + Vector3Int.up * 2, BlockTypeEnum.CropCorn, BlockDirectionEnum.UpForward, ToMetaData(blockCropDataUp), false); } }
/// <summary> /// 刷新植物 /// </summary> /// <typeparam name="T"></typeparam> public virtual void RefreshCrop(Chunk chunk, Vector3Int localPosition, BlockInfoBean blockInfo) { chunk.chunkData.GetBlockForLocal(localPosition, out Block block, out BlockDirectionEnum direction); BlockBean blockData = chunk.GetBlockData(localPosition); BlockMetaCrop blockCrop; if (blockData == null) { blockCrop = new BlockMetaCrop(); blockCrop.growPro = 0; blockCrop.isStartGrow = false; string meta = ToMetaData(blockCrop); blockData = new BlockBean(localPosition, blockInfo.GetBlockType(), direction, meta); chunk.SetBlockData(blockData, false); } //获取成长周期 blockCrop = FromMetaData <BlockMetaCrop>(blockData.meta); //成长周期+1 if (blockCrop.isStartGrow) { //是否开始生长 blockCrop.growPro++; } else { blockCrop.isStartGrow = true; } //设置新数据 string newMeta = ToMetaData(blockCrop); blockData.meta = newMeta; chunk.SetBlockData(blockData); //刷新 WorldCreateHandler.Instance.manager.AddUpdateChunk(chunk, 1); //判断是否已经是最大生长周期 int lifeCycle = GetCropLifeCycle(); if (blockCrop.growPro >= lifeCycle - 1) { chunk.UnRegisterEventUpdate(localPosition, TimeUpdateEventTypeEnum.Min); } }
/// <summary> /// 注册所有方块 /// </summary> public void RegisterBlock() { List <BlockTypeEnum> listBlockType = EnumExtension.GetEnumValue <BlockTypeEnum>(); for (int i = 0; i < listBlockType.Count; i++) { BlockTypeEnum blockType = listBlockType[i]; //获取方块数据 BlockInfoBean blockInfo = GetBlockInfo(blockType); string blockTypeName = EnumExtension.GetEnumName(blockType); //通过反射获取类 Block block = ReflexUtil.CreateInstance <Block>($"BlockType{blockTypeName}"); if (block == null) { block = new Block(); } block.SetData(blockType); block.blockInfo = blockInfo; arrayBlockRegister[(int)blockType] = block; } }
/// <summary> /// 获取UVAdd /// </summary> public virtual Vector2[] GetUVsAddForCrop(Chunk chunk, Vector3Int localPosition, BlockInfoBean blockInfo) { BlockBean blockData = chunk.GetBlockData(localPosition); BlockMetaCrop blockCropData = BlockBaseCrop.FromMetaData <BlockMetaCrop>(blockData.meta); Vector2 uvStartPosition = GetUVStartPosition(blockInfo, blockCropData); Vector2[] uvsAdd = new Vector2[] { new Vector2(uvStartPosition.x, uvStartPosition.y), new Vector2(uvStartPosition.x, uvStartPosition.y + uvWidth), new Vector2(uvStartPosition.x + uvWidth, uvStartPosition.y + uvWidth), new Vector2(uvStartPosition.x + uvWidth, uvStartPosition.y), new Vector2(uvStartPosition.x, uvStartPosition.y), new Vector2(uvStartPosition.x, uvStartPosition.y + uvWidth), new Vector2(uvStartPosition.x + uvWidth, uvStartPosition.y + uvWidth), new Vector2(uvStartPosition.x + uvWidth, uvStartPosition.y) }; return(uvsAdd); }
public override void RefreshCrop(Chunk chunk, Vector3Int localPosition, BlockInfoBean blockInfo) { base.RefreshCrop(chunk, localPosition, blockInfo); BlockBean blockData = chunk.GetBlockData(localPosition); //获取成长周期 BlockMetaCrop blockCropData = FromMetaData <BlockMetaCrop>(blockData.meta); //如果是等级大于0的子集 则不继续往上生长 if (blockCropData.uvIndex > 0) { return; } //判断是否已经是最大生长周期 int lifeCycle = GetCropLifeCycle(); if (blockCropData.growPro >= lifeCycle - 1) { chunk.SetBlockForLocal(localPosition, BlockTypeEnum.CropWatermelon); } }
/// <summary> /// 获取生长UV /// </summary> public virtual Vector2 GetUVStartPosition(BlockInfoBean blockInfo, BlockMetaCrop blockCropData) { BlockBaseCrop blockCrop = block as BlockBaseCrop; List <Vector2Int[]> listUVData = blockCrop.GetListGrowUV(); Vector2 uvStartPosition; if (listUVData.IsNull()) { uvStartPosition = Vector2.zero; } else if (blockCropData.growPro >= blockCrop.GetCropLifeCycle()) { //如果生长周期大于UV长度 则取最后一个 Vector2Int[] itemUVData = listUVData[listUVData.Count - 1]; uvStartPosition = new Vector2(uvWidth * itemUVData[blockCropData.uvIndex].y, uvWidth * itemUVData[blockCropData.uvIndex].x); } else { Vector2Int[] itemUVData = listUVData[blockCropData.growPro]; //按生长周期取UV uvStartPosition = new Vector2(uvWidth * itemUVData[blockCropData.uvIndex].y, uvWidth * itemUVData[blockCropData.uvIndex].x); } return(uvStartPosition); }
/// <summary> /// 方块展示UI /// </summary> /// <param name="isCreate"></param> /// <param name="blockInfo"></param> protected void UIForBlockItem(bool isCreate, BlockInfoBean blockInfo) { if (blockInfo == null) { return; } GUILayout.BeginHorizontal(); if (isCreate) { if (EditorUI.GUIButton("创建方块", 150)) { blockInfo.link_id = blockInfo.id; blockInfo.valid = 1; bool isSuccess = serviceForBlockInfo.UpdateData(blockInfo); if (!isSuccess) { LogUtil.LogError("创建失败"); } } } else { if (EditorUI.GUIButton("更新方块", 150)) { blockInfo.link_id = blockInfo.id; bool isSuccess = serviceForBlockInfo.UpdateData(blockInfo); if (!isSuccess) { LogUtil.LogError("更新失败"); } } if (EditorUI.GUIButton("删除方块", 150)) { bool isSuccess = serviceForBlockInfo.DeleteData(blockInfo.id); if (isSuccess) { listQueryData.Remove(blockInfo); } else { LogUtil.LogError("删除失败"); } } } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); EditorUI.GUIText("方块Id", 50); blockInfo.id = EditorUI.GUIEditorText(blockInfo.id); EditorUI.GUIText("名字", 50); blockInfo.name_cn = EditorUI.GUIEditorText(blockInfo.name_cn); blockInfo.name_en = EditorUI.GUIEditorText(blockInfo.name_en); blockInfo.shape = (int)EditorUI.GUIEnum <BlockShapeEnum>("方块形状:", blockInfo.shape); EditorUI.GUIText("重量", 50); blockInfo.weight = EditorUI.GUIEditorText(blockInfo.weight); EditorUI.GUIText("旋转状态(0不能旋转 1可以旋转 2只能正面朝上旋转)", 200); EditorUI.GUIText("图片", 50); blockInfo.uv_position = EditorUI.GUIEditorText(blockInfo.uv_position); string[] uvStr = blockInfo.uv_position.SplitForArrayStr('|'); for (int i = 0; i < uvStr.Length; i++) { string itemUV = uvStr[i]; string itemUVPosition = itemUV.Replace(",", "_"); for (int f = 0; f < filesNameForTexture.Length; f++) { string fileName = filesNameForTexture[f]; if (fileName.Contains(".meta")) { continue; } if (fileName.Contains(itemUVPosition)) { EditorUI.GUIPic(fileName, 16, 16); break; } } } EditorUI.GUIText("模型名称", 50); blockInfo.model_name = EditorUI.GUIEditorText(blockInfo.model_name, 100); GUILayout.EndHorizontal(); }
/// <summary> /// 保存游戏数据 /// </summary> /// <param name="data"></param> public void SetBlockInfoData(BlockInfoBean data) { serviceBlockInfo.UpdateData(data); }
protected override void UseForPlayer(Player player, ItemsBean itemData, ItemUseTypeEnum useType) { //检测玩家前方是否有方块 if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition)) { ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>(); if (chunkForHit != null) { //获取位置和方向 player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction); //如果上手没有物品 或者是左键点击 则挖掘 if (itemData == null || itemData.itemId == 0 || useType == ItemUseTypeEnum.Left) { TargetBreak(itemData, targetPosition); } //如果手上有物品 则使用 else { //获取目标方块 WorldCreateHandler.Instance.manager.GetBlockForWorldPosition(targetPosition, out Block targetBlock, out BlockDirectionEnum targetBlockDirection, out Chunk taragetChunk); //首先获取靠近方块 WorldCreateHandler.Instance.manager.GetBlockForWorldPosition(closePosition, out Block closeBlock, out BlockDirectionEnum closeBlockDirection, out Chunk closeChunk); //如果靠近得方块有区块 if (closeChunk != null) { //如果不是空方块 则不放置(液体则覆盖放置) if (closeBlock != null && closeBlock.blockType != BlockTypeEnum.None && closeBlock.blockInfo.GetBlockShape() != BlockShapeEnum.Liquid) { return; } //如果重量为1 说明是草之类太轻的物体 则也不能放置 if (closeBlock != null && closeBlock.blockInfo.weight == 1) { return; } //获取物品信息 ItemsInfoBean itemsInfo = ItemsHandler.Instance.manager.GetItemsInfoById(itemData.itemId); //获取方块信息 Block useBlock = BlockHandler.Instance.manager.GetRegisterBlock(itemsInfo.type_id); BlockInfoBean blockInfo = useBlock.blockInfo; BlockTypeEnum changeBlockType = blockInfo.GetBlockType(); //获取meta数据 string metaData = useBlock.ItemUseMetaData(closePosition, changeBlockType, direction, itemData.meta); //使用方块 useBlock.ItemUse( targetPosition, targetBlockDirection, targetBlock, taragetChunk, closePosition, closeBlockDirection, closeBlock, closeChunk, direction, metaData); //扣除道具 UserDataBean userData = GameDataHandler.Instance.manager.GetUserData(); userData.AddItems(itemData, -1); //刷新UI UIHandler.Instance.RefreshUI(); } } } } }