|
- using System;
- using CIG3.ExtensionMethods;
- using CIGEnums;
- using SUISS.Core;
- using SUISSEngine;
- using UnityEngine;
-
- public class CIGBuilder : Builder
- {
- public static Color AvailableElementColor
- {
- get
- {
- return CIGBuilder.availableElementColor;
- }
- }
-
- public static Color OccupiedElementColor
- {
- get
- {
- return CIGBuilder.occupiedElementColor;
- }
- }
-
- protected override void OnGridDeserialized()
- {
- base.OnGridDeserialized();
- this.ResetAllColors();
- base.OnTileBuild += SingletonMonobehaviour<CIGGameStats>.Instance.OnTileBuilt;
- base.OnTileDestroyed += SingletonMonobehaviour<CIGGameStats>.Instance.OnTileDestroyed;
- }
-
- protected virtual void OnDestroy()
- {
- if (SingletonMonobehaviour<CIGGameStats>.IsAvailable)
- {
- base.OnTileBuild -= SingletonMonobehaviour<CIGGameStats>.Instance.OnTileBuilt;
- base.OnTileDestroyed -= SingletonMonobehaviour<CIGGameStats>.Instance.OnTileDestroyed;
- }
- }
-
- public override bool TilesHidden
- {
- get
- {
- return base.TilesHidden;
- }
- set
- {
- bool tilesHidden = base.TilesHidden;
- base.TilesHidden = value;
- if (base.TilesHidden != tilesHidden)
- {
- this.ResetAllColors();
- }
- }
- }
-
- public void ExpansionBlockDidUnlock(CIGExpansions expansions, CIGExpansions.ExpansionBlock block)
- {
- for (int i = block.Origin.v; i < block.Origin.v + block.Size.v; i++)
- {
- for (int j = block.Origin.u; j < block.Origin.u + block.Size.u; j++)
- {
- GridElement gridElement = this.grid[j, i];
- gridElement.Color = this.GetColorForElement(true, gridElement);
- gridElement.Unlocked = true;
- expansions.DidUnlockTileWithType(gridElement.Type);
- }
- }
- }
-
- protected override bool IsElementTypeCompatible(int type, int required)
- {
- return base.IsElementTypeCompatible(type, required) || (required == 10 && ((SurfaceType)type).IsLand());
- }
-
- protected override void DidBuildTile(GridTile tile, GridIndex index, GridSize size, bool mirrored)
- {
- base.DidBuildTile(tile, index, size, mirrored);
- for (int i = index.v; i > index.v - size.v; i--)
- {
- for (int j = index.u; j > index.u - size.u; j--)
- {
- GridElement gridElement = this.grid[j, i];
- CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(new GridIndex(j, i));
- if (blockForIndex != null)
- {
- gridElement.Color = this.GetColorForElement(blockForIndex.Unlocked, gridElement);
- }
- }
- }
- }
-
- protected override void DidDestroyTile(GridTile tile, GridIndex index, GridSize size)
- {
- base.DidDestroyTile(tile, index, size);
- if (index.isInvalid)
- {
- return;
- }
- for (int i = index.v; i > index.v - size.v; i--)
- {
- for (int j = index.u; j > index.u - size.u; j--)
- {
- GridElement gridElement = this.grid[j, i];
- CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(new GridIndex(j, i));
- if (blockForIndex != null)
- {
- gridElement.Color = this.GetColorForElement(blockForIndex.Unlocked, gridElement);
- }
- }
- }
- }
-
- protected override bool CanBuildTileOnElement(GridTile tile, IsometricGrid grid, GridElement element)
- {
- bool flag = base.CanBuildTileOnElement(tile, grid, element);
- if (flag)
- {
- CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(element.Index);
- if (blockForIndex == null || !blockForIndex.Unlocked)
- {
- flag = false;
- }
- }
- return flag;
- }
-
- protected override bool CanBuildTile(GridTile tile, IsometricGrid grid, GridIndex index, bool mirrored, bool forced)
- {
- if (index.isInvalid || index.u >= grid.Size.u || index.v >= grid.Size.v)
- {
- return false;
- }
- if (mirrored && !tile.canMirror)
- {
- return false;
- }
- GridSize size = tile.size;
- if (mirrored)
- {
- size = new GridSize(size.v, size.u);
- }
- if (size.u > index.u + 1 || size.v > index.v + 1)
- {
- return false;
- }
- int num = 0;
- for (int i = index.v; i > index.v - size.v; i--)
- {
- for (int j = index.u; j > index.u - size.u; j--)
- {
- GridElement gridElement = grid[j, i];
- if (gridElement.Tile != null)
- {
- if (!forced || !(tile.GetComponent<Scenery>() == null))
- {
- return false;
- }
- if (gridElement.Tile.GetComponent<Scenery>() == null)
- {
- UnityEngine.Debug.LogWarning(string.Format("In order to build {0} at {1}, we need to remove {2} at ({3},{4})", new object[]
- {
- tile.name,
- index,
- gridElement.Tile.name,
- j,
- i
- }));
- }
- base.DestroyTile(gridElement.Tile);
- }
- if (!forced)
- {
- if (this.IsElementTypeCompatible(gridElement.Type, tile.requiredGridType))
- {
- num++;
- }
- if (!this.CanBuildTileOnElement(tile, grid, gridElement))
- {
- return false;
- }
- }
- }
- }
- return forced || num == tile.size.u * tile.size.v;
- }
-
- private void ResetAllColors()
- {
- GridSize size = this.grid.Size;
- for (int i = 0; i < size.v; i++)
- {
- for (int j = 0; j < size.u; j++)
- {
- GridElement gridElement = this.grid[j, i];
- if (gridElement != null)
- {
- CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(new GridIndex(j, i));
- if (blockForIndex != null)
- {
- gridElement.Color = this.GetColorForElement(blockForIndex.Unlocked, gridElement);
- }
- }
- }
- }
- }
-
- private Color GetColorForElement(bool unlocked, GridElement element)
- {
- if (!unlocked || element.Type < 0)
- {
- return GridOverlay.TransparentColor;
- }
- GridTile tile = element.Tile;
- if (tile == null)
- {
- Color a = new Color(1f, 1f, 1f, 1f);
- if (this.showOverlayColors)
- {
- if (element.Type >= 0 && element.Type < CIGBuilder.elementTypeColors.Length)
- {
- a = CIGBuilder.elementTypeColors[element.Type];
- }
- }
- else if (element.Type <= 0)
- {
- return GridOverlay.TransparentColor;
- }
- return a * CIGBuilder.availableElementColor;
- }
- if ((this.TilesHidden && tile.GetComponent<Road>() == null) || (this.roadsHidden && tile.GetComponent<Road>() != null))
- {
- return CIGBuilder.occupiedElementColor;
- }
- return GridOverlay.TransparentColor;
- }
-
- private static Color availableElementColor = new Color(1f, 1f, 1f, 0.15f);
-
- private static Color occupiedElementColor = new Color(0.51f, 0.428f, 1f, 1f);
-
- private static Color[] elementTypeColors = new Color[]
- {
- new Color(1f, 1f, 1f, 0f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f),
- new Color(1f, 1f, 1f, 1f)
- };
-
- [SelfReference]
- public CIGExpansions expansions;
-
- public bool showOverlayColors = true;
- }
|