123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System.Collections;
- using System.Collections.Generic;
- using CrossgateToolkit;
- using UnityEngine;
- using UnityEngine.Tilemaps;
- public class Scene_Map : MonoBehaviour
- {
- [SerializeField,Header("地图渲染用Camera")]
- private Camera MapCamera;
- [SerializeField,Header("地图地面TileMap")]
- private Tilemap TileMap_Ground;
- [SerializeField,Header("地图物件TileMap")]
- private Tilemap TileMap_Object;
-
- [SerializeField,Header("地图Grid")]
- private Grid MapGrid;
- [SerializeField, Header("地图背景音乐")]
- private AudioSource MapBGM;
-
-
- [SerializeField,Header("小地图TileMap")]
- private Tilemap TileMap_MiniMap;
- [SerializeField,Header("小地图TileSprite")]
- private Sprite MiniMapSprite;
-
-
- private MapInfo mapInfo;
-
- private uint mapBGMID;
-
- private Dictionary<uint,GraphicDetail> GroundGraphicDetails;
- private Dictionary<uint,GraphicDetail> ObjectGraphicDetails;
-
-
- void Start()
- {
-
- MapCamera.transparencySortMode = TransparencySortMode.CustomAxis;
- MapCamera.transparencySortAxis = new Vector3(0, 1, -0.1f);
-
- LoadMap();
- }
- private void Awake()
- {
- Util.Init();
- }
- private void LoadMap()
- {
-
- mapInfo = Map.GetMap(1000);
-
-
-
-
- BakeMapGraphics();
-
-
-
- DrawMapGround();
- DrawMapObject();
-
-
- int x = mapInfo.Width / 2 * 32 - mapInfo.Height / 2 * 32;
- int y = -mapInfo.Height / 2 * 24 - mapInfo.Width / 2 * 24;
- MapGrid.GetComponent<RectTransform>().localPosition = new Vector3(x, y, 0);
-
-
- DrawMiniMap();
- }
-
- private void BakeMapGraphics()
- {
-
-
-
-
- List<GraphicInfoData> graphicInfoDatas;
-
- graphicInfoDatas = new List<GraphicInfoData>();
- for (var i = 0; i < mapInfo.GroundDatas.Count; i++)
- {
- MapBlockData mapBlockData = mapInfo.GroundDatas[i];
- if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
- graphicInfoDatas.Add(mapBlockData.GraphicInfo);
- }
- GroundGraphicDetails = GraphicData.BakeGraphics(graphicInfoDatas, true, 0, -1, false, 2048);
-
-
- graphicInfoDatas = new List<GraphicInfoData>();
- for (var i = 0; i < mapInfo.ObjectDatas.Count; i++)
- {
- MapBlockData mapBlockData = mapInfo.ObjectDatas[i];
- if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
- graphicInfoDatas.Add(mapBlockData.GraphicInfo);
- }
- ObjectGraphicDetails = GraphicData.BakeGraphics(graphicInfoDatas, true, 0, -1, false, 2048);
- }
-
- private void DrawMapGround()
- {
- int width = mapInfo.Width;
- int height = mapInfo.Height;
- List<Vector3Int> drawPositions = new List<Vector3Int>();
- List<Tile> drawTiles = new List<Tile>();
- for(int x = 0; x < width; x++)
- {
- for(int y = 0; y < height; y++)
- {
- MapBlockData mapBlockData = mapInfo.GroundDatas[y * width + x];
- if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
- GraphicDetail graphicDetail = GroundGraphicDetails[mapBlockData.MapSerial];
- Tile groundTile = Tile.CreateInstance(typeof(Tile)) as Tile;
- groundTile.sprite = graphicDetail.Sprite;
- drawPositions.Add(new Vector3Int(x, y, 0));
- drawTiles.Add(groundTile);
- }
- }
-
- TileMap_Ground.SetTiles(drawPositions.ToArray(), drawTiles.ToArray());
- }
-
-
- private void DrawMapObject()
- {
- int width = mapInfo.Width;
- int height = mapInfo.Height;
- List<Vector3Int> drawPositions = new List<Vector3Int>();
- List<Tile> drawTiles = new List<Tile>();
- for(int x = 0; x < width; x++)
- {
- for(int y = 0; y < height; y++)
- {
- MapBlockData mapBlockData = mapInfo.ObjectDatas[y * width + x];
- if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
- if (mapBlockData.GraphicInfo.Serial < 1000)
- {
-
- if (mapBGMID == 0)
- {
- mapBGMID = mapBlockData.MapSerial;
-
- Audio.Play(MapBGM, Audio.Type.BGM, (int)mapBGMID);
- }
- }
- GraphicDetail graphicDetail = ObjectGraphicDetails[mapBlockData.MapSerial];
- Tile objectTile = Tile.CreateInstance(typeof(Tile)) as Tile;
- objectTile.sprite = graphicDetail.Sprite;
-
- drawPositions.Add(new Vector3Int(x, y, 0));
- drawTiles.Add(objectTile);
- }
- }
-
- TileMap_Object.SetTiles(drawPositions.ToArray(), drawTiles.ToArray());
- }
-
-
- private void DrawMiniMap()
- {
-
-
-
- List<Vector3Int> drawPositions = new List<Vector3Int>();
- List<Tile> drawTiles = new List<Tile>();
-
- for(int x = 0; x < mapInfo.Width; x++)
- {
- for(int y = 0; y < mapInfo.Height; y++)
- {
- MapBlockData mapBlockData = mapInfo.GroundDatas[y * mapInfo.Width + x];
- MapBlockData mapObjectData = mapInfo.ObjectDatas[y * mapInfo.Width + x];
- if(mapBlockData==null && mapObjectData==null) continue;
- Tile tile = Tile.CreateInstance(typeof(Tile)) as Tile;
- tile.sprite = MiniMapSprite;
-
-
- if (mapObjectData == null)
- {
-
- tile.color = GroundGraphicDetails[mapBlockData.MapSerial].PrimaryColor;
- }
- else
- {
-
- tile.color = ObjectGraphicDetails[mapObjectData.MapSerial].PrimaryColor;
- }
-
- drawPositions.Add(new Vector3Int(x, y, 0));
- drawTiles.Add(tile);
- }
- }
-
- TileMap_MiniMap.SetTiles(drawPositions.ToArray(), drawTiles.ToArray());
- }
- }
|