WXMC/.svn/pristine/26/26604cf8c860772ad7623c80b2a13cf8ec600fd8.svn-base
2024-12-04 16:18:46 +08:00

99 lines
2.4 KiB
Plaintext

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PuzzleDefine;
public class PuzzleReplaceBlockDisplayer : MonoBehaviour
{
public int BlockID;
public GameObject GemPrefab;
public List<GameObject> Gems = new List<GameObject>();
public Dragable Dragable;
public bool IsPredictBlock
{
get
{
return BlockID < 0;
}
}
public float PredictBlockOpacity;
public void SetBlockID(int newID)
{
BlockID = newID;
ReplaceBlock block;
if(!PuzzleGameMode.main.GamePlayer.ReplaceBlocks.TryGetValue(BlockID, out block))
{
return;
}
foreach (var item in block.GemsToReplace)
{
ITEM_TYPE newType = PuzzleGameMode.GetItemType(item.Gem.Type);
int color;
GameObject prefab;
Item.GetColorAndPrefab(newType, out color, out prefab);
GameObject newGemDisplayer = Instantiate(GemPrefab, transform);
SpriteRenderer renderer = newGemDisplayer.GetComponent<SpriteRenderer>();
if (IsPredictBlock)
{
Color old = renderer.color;
old.a = PredictBlockOpacity;
renderer.color = old;
}
renderer.sprite = prefab.GetComponent<SpriteRenderer>().sprite;
Gems.Add(newGemDisplayer);
}
if (IsPredictBlock)
{
Dragable.CanDrag = false;
}
}
public Position GetPlaceOnPosition()
{
Item destItem = null;
float minDist = float.MaxValue;
foreach(var board in PuzzleGameMode.main.Boards){
foreach (var item in board.nodes)
{
if (item.item == null)
{
continue;
}
float dist = Vector2.Distance(transform.position, item.item.transform.position);
if (dist < minDist)
{
destItem = item.item;
minDist = dist;
}
}
}
return new Position()
{
X = destItem.node.j,
Y = destItem.node.i,
};
}
public void TryPlaceHere()
{
if (IsPredictBlock)
{
return;
}
PuzzleGameMode.main.PlayerPlaceBlock(BlockID, GetPlaceOnPosition());
}
}