mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-27 20:04:35 +00:00
150 lines
5.5 KiB
C#
150 lines
5.5 KiB
C#
using HPJ.Simulation.Enums;
|
|
using HPJ.Simulation.Map;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace HPJ.Presentation
|
|
{
|
|
/// <summary>
|
|
/// The Map Sprite Renderer is a set of sprite renderers that render the tile state of a desired map.
|
|
/// </summary>
|
|
[RequireComponent(typeof(SpriteRenderer))]
|
|
public class MapSpriteRenderer : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
protected float UpdatePeriod = 1; // How often the sprite updates
|
|
protected float UpdateTimer = 0;
|
|
[SerializeField]
|
|
protected int DesiredMapIndex; // The desired map you want
|
|
[SerializeField]
|
|
protected List<TileColorData> ColorData = new List<TileColorData>(); // Colors you want to display for each tile type
|
|
protected Dictionary<TileTypes, TileColorData> SortedData = new Dictionary<TileTypes, TileColorData>();
|
|
protected SpriteRenderer SpriteRen; // The Base Sprite renderer for the tile colors
|
|
[SerializeField]
|
|
protected SpriteRenderer GridSpriteRen; // The grid sprite renderer
|
|
|
|
private void Awake()
|
|
{
|
|
FirstUpdate = true;
|
|
SpriteRen = GetComponent<SpriteRenderer>();
|
|
UpdateTimer = UpdatePeriod;
|
|
|
|
foreach(TileTypes TileType in System.Enum.GetValues(typeof(TileTypes)))
|
|
{
|
|
foreach(TileColorData Data in ColorData)
|
|
{
|
|
if (Data.TileType == TileType)
|
|
{
|
|
SortedData.Add(TileType, Data);
|
|
continue;
|
|
}
|
|
}
|
|
if (SortedData.ContainsKey(TileType))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
SortedData.Add(TileType, new TileColorData(Color.clear, TileType));
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (NavigationManager.Instance == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!NavigationManager.Instance)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (DesiredMapIndex < 0 || DesiredMapIndex >= NavigationManager.Instance.MapSets.Count)
|
|
{
|
|
return;
|
|
}
|
|
|
|
UpdateTimer += Time.deltaTime;
|
|
if (UpdateTimer >= UpdatePeriod)
|
|
{
|
|
UpdateTimer = 0;
|
|
UpdateSprite();
|
|
}
|
|
}
|
|
|
|
protected bool FirstUpdate = true;
|
|
protected Texture2D tex;
|
|
protected Color[] colors;
|
|
/// <summary>
|
|
/// Updates the sprite to match the current state of the desired map
|
|
/// </summary>
|
|
public void UpdateSprite()
|
|
{
|
|
MapSet Map = NavigationManager.Instance.MapSets[DesiredMapIndex];
|
|
|
|
if (FirstUpdate)
|
|
{
|
|
//Debug.Log("Set Position");
|
|
tex = new Texture2D(Map.SetSettings.MapSettings.MapWidth, Map.SetSettings.MapSettings.MapHeight);
|
|
colors = new Color[Map.SetSettings.MapSettings.MapWidth * Map.SetSettings.MapSettings.MapHeight];
|
|
|
|
transform.localScale = new Vector3((Map.SetSettings.MapSettings.MapHeight + 1) * Map.SetSettings.MapSettings.TileSize / 100f, (Map.SetSettings.MapSettings.MapHeight + 1) * Map.SetSettings.MapSettings.TileSize / 100f, 1);
|
|
|
|
Vector3 Position = new Vector3();
|
|
Position.x = Map.SetSettings.MapSettings.MapWidth * Map.SetSettings.MapSettings.TileSize / 200f + Map.SetSettings.MapSettings.Offset.x / 100f;
|
|
Position.y = Map.SetSettings.MapSettings.Offset.y / 100f + 0.005f;
|
|
Position.z = Map.SetSettings.MapSettings.MapHeight * Map.SetSettings.MapSettings.TileSize / 200f + Map.SetSettings.MapSettings.Offset.z / 100f;
|
|
|
|
transform.position = Position;
|
|
|
|
GridSpriteRen.transform.SetParent(null);
|
|
|
|
GridSpriteRen.transform.localScale = new Vector3(Map.SetSettings.MapSettings.TileSize / 100f, Map.SetSettings.MapSettings.TileSize / 100f, 1);
|
|
GridSpriteRen.size = new Vector2(Map.SetSettings.MapSettings.MapWidth * Map.SetSettings.MapSettings.TileSize / 100f, Map.SetSettings.MapSettings.MapHeight * Map.SetSettings.MapSettings.TileSize / 100f) * 100f / Map.SetSettings.MapSettings.TileSize;
|
|
GridSpriteRen.transform.position += new Vector3(0, 0.01f, 0);
|
|
|
|
GridSpriteRen.transform.SetParent(transform);
|
|
|
|
FirstUpdate = false;
|
|
}
|
|
|
|
|
|
for(int x = 0; x < Map.SetSettings.MapSettings.MapWidth; x++)
|
|
{
|
|
for (int y = 0; y < Map.SetSettings.MapSettings.MapHeight; y++)
|
|
{
|
|
int index = y * Map.SetSettings.MapSettings.MapWidth + x;
|
|
|
|
colors[index] = SortedData[Map.GetTileType(x, y)].TileColor;
|
|
}
|
|
}
|
|
|
|
tex.SetPixels(colors);
|
|
tex.filterMode = FilterMode.Point;
|
|
tex.Apply();
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
|
SpriteRen.sprite = sprite;
|
|
//Debug.Log("Update SpriteRenderer");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The Color data for a given tile type
|
|
/// </summary>
|
|
[System.Serializable]
|
|
public struct TileColorData
|
|
{
|
|
public Color TileColor;
|
|
public TileTypes TileType;
|
|
|
|
public TileColorData(Color color, TileTypes Type)
|
|
{
|
|
TileColor = color;
|
|
TileType = Type;
|
|
}
|
|
}
|
|
}
|