void Update() { if (!isLocalPlayer) { return; } if (radius <= 0) { return; } if (m_OriginPosition != transform.position) { m_OriginPosition = transform.position; var pos = FogOfWarEffect.WorldPositionToFOW(transform.position); if (m_FowMapPos.x != pos.x || m_FowMapPos.y != pos.y || !m_IsInitialized) { m_FowMapPos = pos; m_IsInitialized = true; m_FieldData.position = transform.position; m_FieldData.radius = radius; //FogOfWarEffect.SetVisibleAtPosition(m_FieldData); FogOfWarEffect.UpdateFOWFieldData(m_FieldData); } } }
private void RaycastLine(FOWMap map, FOWMapPos pos, int centX, int centZ, float px, float pz, FOWFieldData field) { Vector2 dir = new Vector2(px, pz) * 10; int x = centX + (int)dir.x; int y = centZ + (int)dir.y; SetInvisibleLine(map, pos.x, pos.y, x, y, centX, centZ, field.radiusSquare); }
protected override void RayCast(FOWMap map, FOWMapPos pos, int centX, int centZ, FOWFieldData field) { float radiusSq = field.radiusSquare; int x = pos.x - centX; int z = pos.y - centZ; m_SortAngle[0] = Mathf.Atan2((z * map.deltaZ + map.deltaZ / 2), (x * map.deltaX - map.deltaX / 2)) * Mathf.Rad2Deg; m_SortAngle[1] = Mathf.Atan2((z * map.deltaZ - map.deltaZ / 2), (x * map.deltaX - map.deltaX / 2)) * Mathf.Rad2Deg; m_SortAngle[2] = Mathf.Atan2((z * map.deltaZ + map.deltaZ / 2), (x * map.deltaX + map.deltaX / 2)) * Mathf.Rad2Deg; m_SortAngle[3] = Mathf.Atan2((z * map.deltaZ - map.deltaZ / 2), (x * map.deltaX + map.deltaX / 2)) * Mathf.Rad2Deg; float curAngle = Mathf.Atan2((z * map.deltaZ), (x * map.deltaX)) * Mathf.Rad2Deg; SortAngle(); m_RayCastQueue.Clear(); m_RayCastQueue.Enqueue(pos); int index = pos.y * map.texWidth + pos.x; m_Arrives.Add(index); while (m_RayCastQueue.Count > 0) { FOWMapPos root = m_RayCastQueue.Dequeue(); if (root.x - 1 >= 0 && (curAngle >= 90 || curAngle < -90)) { SetInvisibleAtPosition(map, root.x - 1, root.y, centX, centZ, radiusSq); } if (root.x - 1 >= 0 && root.y - 1 >= 0 && curAngle <= -90 && curAngle >= -180) { SetInvisibleAtPosition(map, root.x - 1, root.y - 1, centX, centZ, radiusSq); } if (root.y - 1 >= 0 && curAngle <= 0 && curAngle >= -180) { SetInvisibleAtPosition(map, root.x, root.y - 1, centX, centZ, radiusSq); } if (root.x + 1 < map.texWidth && root.y - 1 >= 0 && curAngle <= 0 && curAngle >= -90) { SetInvisibleAtPosition(map, root.x + 1, root.y - 1, centX, centZ, radiusSq); } if (root.x + 1 < map.texWidth && curAngle >= -90 && curAngle <= 90) { SetInvisibleAtPosition(map, root.x + 1, root.y, centX, centZ, radiusSq); } if (root.x + 1 < map.texWidth && root.y + 1 < map.texHeight && curAngle >= 0 && curAngle <= 90) { SetInvisibleAtPosition(map, root.x + 1, root.y + 1, centX, centZ, radiusSq); } if (root.y + 1 < map.texHeight && curAngle >= 0 && curAngle <= 180) { SetInvisibleAtPosition(map, root.x, root.y + 1, centX, centZ, radiusSq); } if (root.x - 1 >= 0 && root.y + 1 < map.texHeight && curAngle >= 90 && curAngle <= 180) { SetInvisibleAtPosition(map, root.x - 1, root.y + 1, centX, centZ, radiusSq); } } }
/// <summary> /// 射线检测,将障碍物后全部设为不可见 /// TODO:待优化,实际上不必全部设置 /// </summary> /// <param name="pos"></param> /// <param name="centX"></param> /// <param name="centZ"></param> /// <param name="radiusSq"></param> private void RayCast(FOWMapPos pos, int centX, int centZ, float radiusSq) { int x = pos.x - centX; int z = pos.y - centZ; m_SortAngle[0] = Mathf.Atan2((z * m_DeltaZ + m_DeltaZ / 2), (x * m_DeltaX - m_DeltaX / 2)) * Mathf.Rad2Deg; m_SortAngle[1] = Mathf.Atan2((z * m_DeltaZ - m_DeltaZ / 2), (x * m_DeltaX - m_DeltaX / 2)) * Mathf.Rad2Deg; m_SortAngle[2] = Mathf.Atan2((z * m_DeltaZ + m_DeltaZ / 2), (x * m_DeltaX + m_DeltaX / 2)) * Mathf.Rad2Deg; m_SortAngle[3] = Mathf.Atan2((z * m_DeltaZ - m_DeltaZ / 2), (x * m_DeltaX + m_DeltaX / 2)) * Mathf.Rad2Deg; float curAngle = Mathf.Atan2((z * m_DeltaZ), (x * m_DeltaX)) * Mathf.Rad2Deg; SortAngle(); m_RayCastQueue.Clear(); m_RayCastQueue.Enqueue(pos); int index = pos.y * m_TexWidth + pos.x; m_Arrives.Add(index); while (m_RayCastQueue.Count > 0) { FOWMapPos root = m_RayCastQueue.Dequeue(); if (root.x - 1 >= 0 && (curAngle >= 90 || curAngle < -90)) { SetInvisibleAtPosition(root.x - 1, root.y, centX, centZ, radiusSq); } if (root.x - 1 >= 0 && root.y - 1 >= 0 && curAngle <= -90 && curAngle >= -180) { SetInvisibleAtPosition(root.x - 1, root.y - 1, centX, centZ, radiusSq); } if (root.y - 1 >= 0 && curAngle <= 0 && curAngle >= -180) { SetInvisibleAtPosition(root.x, root.y - 1, centX, centZ, radiusSq); } if (root.x + 1 < m_TexWidth && root.y - 1 >= 0 && curAngle <= 0 && curAngle >= -90) { SetInvisibleAtPosition(root.x + 1, root.y - 1, centX, centZ, radiusSq); } if (root.x + 1 < m_TexWidth && curAngle >= -90 && curAngle <= 90) { SetInvisibleAtPosition(root.x + 1, root.y, centX, centZ, radiusSq); } if (root.x + 1 < m_TexWidth && root.y + 1 < m_TexHdight && curAngle >= 0 && curAngle <= 90) { SetInvisibleAtPosition(root.x + 1, root.y + 1, centX, centZ, radiusSq); } if (root.y + 1 < m_TexHdight && curAngle >= 0 && curAngle <= 180) { SetInvisibleAtPosition(root.x, root.y + 1, centX, centZ, radiusSq); } if (root.x - 1 >= 0 && root.y + 1 < m_TexHdight && curAngle >= 90 && curAngle <= 180) { SetInvisibleAtPosition(root.x - 1, root.y + 1, centX, centZ, radiusSq); } } }
protected override void RayCast(FOWMap map, FOWMapPos pos, int centX, int centZ, FOWFieldData field) { int x = pos.x - centX; int z = pos.y - centZ; float corner1x, corner1z, corner2x, corner2z; GetCornersPos(x, z, out corner1x, out corner1z, out corner2x, out corner2z); RaycastLine(map, pos, centX, centZ, corner1x, corner1z, field); RaycastLine(map, pos, centX, centZ, corner2x, corner2z, field); }
protected override void RayCast(FOWMap map, FOWMapPos pos, int centX, int centZ, FOWFieldData field) { float r = field.radius * map.invDeltaX; Vector2 dir = new Vector2(pos.x - centX, pos.y - centZ); float l = dir.magnitude; if (r - l <= 0) { return; } dir = dir.normalized * (r - l); int x = pos.x + (int)dir.x; int y = pos.y + (int)dir.y; SetInvisibleLine(map, pos.x, pos.y, x, y, centX, centZ, field.radiusSquare); }
void Update() { if (radius <= 0) { return; } var pos = FogOfWarEffect.WorldPositionToFOW(transform.position); if (m_FowMapPos.x != pos.x || m_FowMapPos.y != pos.y || first_time) { m_FowMapPos = pos; first_time = true; m_FieldData.position = transform.position; m_FieldData.radius = radius; //FogOfWarEffect.SetVisibleAtPosition(m_FieldData); FogOfWarEffect.UpdateFOWFieldData(m_FieldData); } }
private bool PreRayCast(FOWMap map, FOWMapPos pos, int centX, int centZ) { float k = ((float)(pos.y - centZ)) / (pos.x - centX); if (k < -0.414f && k >= -2.414f) { return(!IsVisible(map, pos.x + 1, pos.y + 1) && !IsVisible(map, pos.x - 1, pos.y - 1)); } else if (k < -2.414f || k >= 2.414f) { return(!IsVisible(map, pos.x + 1, pos.y) && !IsVisible(map, pos.x - 1, pos.y)); } else if (k < 2.414f && k >= 0.414f) { return(!IsVisible(map, pos.x + 1, pos.y - 1) && !IsVisible(map, pos.x - 1, pos.y + 1)); } else { return(!IsVisible(map, pos.x, pos.y + 1) && !IsVisible(map, pos.x, pos.y - 1)); } }
protected abstract void RayCast(FOWMap map, FOWMapPos pos, int centX, int centZ, FOWFieldData field);