// Use this for initialization protected override void OnStart() { myFogState = FogState.Following_players; Messenger.RegisterListener(new Listener("creaturesarenear", gameObject, "FreezeOn")); Messenger.RegisterListener(new Listener("MergeNewCreature1", gameObject, "FollowingPlayers")); Messenger.RegisterListener(new Listener("MergeNewCreature2", gameObject, "FollowingPlayers")); Messenger.RegisterListener(new Listener("GoingToMerge", gameObject, "FollowingCreatures")); Messenger.RegisterListener(new Listener("FogFollowingCreatures", gameObject, "FollowingCreatures")); Messenger.RegisterListener(new Listener("FogFollowingPlayers", gameObject, "FollowingPlayers")); freeze = false; //fogParticle.layer = 11; // Save memory space for vectors of important data fogParticles = new GameObject[numberParticles]; fogParticlesOriginalPosition = new Vector3[numberParticles]; // Get players transforms GameObject tempPlayer1GameObject = GameObject.FindGameObjectWithTag("Player"); player1Position = tempPlayer1GameObject.transform; GameObject tempPlayer2GameObject = GameObject.FindGameObjectWithTag("Player2"); player2Position = tempPlayer2GameObject.transform; // Instantiate particles int fogParticleCreated = 0; float offset = (Mathf.Sqrt(numberParticles) * particleDistance) / 2; float randomOffset = particleDistance * percentageOfRandom; for (int x = 0; x < (int)Mathf.Sqrt(numberParticles); x++) { for (int y = 0; y < (int)Mathf.Sqrt(numberParticles); y++) { Quaternion rotationTemp = Quaternion.identity; rotationTemp.eulerAngles = new Vector3(0, Random.Range(-percentageOfRandom * 100, percentageOfRandom * 100), 0); GameObject tempGameObject = Instantiate(fogParticle, new Vector3(x * particleDistance - offset + Random.Range(-randomOffset, randomOffset), floorDistance + Random.Range(-randomOffset, randomOffset), y * particleDistance - offset + Random.Range(-randomOffset, randomOffset)), rotationTemp) as GameObject; fogParticlesOriginalPosition[fogParticleCreated] = tempGameObject.transform.position; fogParticles[fogParticleCreated] = tempGameObject; fogParticleCreated++; } } }
public List <FogState> GetFogState() { List <FogState> flist = new List <FogState>(); foreach (Transform child in transform) { FogData fg = child.GetComponent <FogData>(); if (fg != null) { FogState fs = new FogState() { globalPosition = fg.transform.position, chapterName = fg.chapterName }; flist.Add(fs); } } return(flist); }
//单独更新某格雾 public void UpdateFogMask(int x, int y, FogState state, float fadeTime = 0.3f) { if (FogMaskMap == null) { return;//无效的mask图 } if (!Created) { return;//没有正确生成mask图 } if (x >= m_FogMaskWidth || y >= m_FogMaskHeight) { return;//越界 } byte colorTo = (byte)state; //TODO if (FogMaskMap[x, y] == colorTo) { return;//状态相同则跳过 } if (fadeTime > 0) { //byte colorFrom = FogMaskMap[x, y]; byte colorFrom = GetFogMask(x, y); DOTween.To(delegate(float v) { //FogMaskMap[x, y] = (byte)v; SetFogMask(x, y, (byte)v); UpdatePixel(x, y, v); ApplyTextureAndSendToGlobal(); }, colorFrom, colorTo, fadeTime).SetEase(Ease.Linear); } else { //FogMaskMap[x, y] = colorTo; SetFogMask(x, y, colorTo); UpdatePixel(x, y, colorTo); ApplyTextureAndSendToGlobal(); } }
// code from // https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C.23 public static void DrawLine(this FogState[,] array, int x0, int y0, int x1, int y1, FogState fulfillValue) { int dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1; int dy = Mathf.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1; int err = (dx > dy ? dx : -dy) / 2, e2; for (; ;) { array[x0, y0] = fulfillValue; if (x0 == x1 && y0 == y1) { break; } e2 = err; if (e2 > -dx) { err -= dy; x0 += sx; } if (e2 < dy) { err += dx; y0 += sy; } } }
// code from // https://rosettacode.org/wiki/Bitmap/Midpoint_circle_algorithm#C.23k // public static void DrawCircleInside(this FogState[,] array, int centerX, int centerY, int radius, FogState fulfillValue) { int d = (5 - radius * 4) / 4; int x = 0; int y = radius; int lengthOne = array.GetLength(0); int lengthTwo = array.GetLength(1); do { // ensure index is in range before setting (depends on your image implementation) // in this case we check if the pixel location is within the bounds of the image before setting the pixel if (centerX + x >= 0 && centerX + x <= lengthOne - 1 && centerY + y >= 0 && centerY + y <= lengthTwo - 1 && centerY - y >= 0 && centerY - y <= lengthTwo - 1) { DrawLine(array, centerX + x, centerY + y, centerX + x, centerY - y, fulfillValue); } if (centerX - x >= 0 && centerX - x <= lengthOne - 1 && centerY + y >= 0 && centerY + y <= lengthTwo - 1 && centerY - y >= 0 && centerY - y <= lengthTwo - 1) { DrawLine(array, centerX - x, centerY - y, centerX - x, centerY + y, fulfillValue); } if (centerX + y >= 0 && centerX + y <= lengthOne - 1 && centerY + x >= 0 && centerY + x <= lengthTwo - 1 && centerY - x >= 0 && centerY - x <= lengthTwo - 1) { DrawLine(array, centerX + y, centerY + x, centerX + y, centerY - x, fulfillValue); } if (centerX - y >= 0 && centerX - y <= lengthOne - 1 && centerY + x >= 0 && centerY + x <= lengthTwo - 1 && centerY - x >= 0 && centerY - x <= lengthTwo - 1) { DrawLine(array, centerX - y, centerY + x, centerX - y, centerY - x, fulfillValue); } if (d < 0) { d += 2 * x + 1; } else { d += 2 * (x - y) + 1; y--; } x++; } while (x <= y); }
/// <summary> /// Toggles fog to near, far and none. /// </summary> public void ToggleFog() { switch (this.State) { case FogState.None: this.State = FogState.Near; break; case FogState.Near: this.State = FogState.Far; break; case FogState.Far: this.State = FogState.None; break; } }
public static FogState ConvertFogState(int i) { return(FogState.GetFogState(i)); }
// Use this for initialization void Start() { fogState = FogState.Undiscovered; }
void FollowingPlayers() { myFogState = FogState.Following_players; }
void FollowingCreatures() { myFogState = FogState.Following_creatures; }
void FreezeOn() { myFogState = FogState.Freeze; }