private void TakeShot() { m_shotCandidates.Clear(); foreach (var entity in m_owner.EntityPerception.Enemies) { Vector2 direction = (Vector2)entity.transform.position - (Vector2)m_owner.transform.position; if (ShotInRegion(direction)) { m_shotCandidates.Add(entity); } } if (m_shotCandidates.Count == 0 && !AlwaysFiring) { return; } Vector2 shotDirection = Vector2.zero; int target = -1; if (!AlwaysFiring) { target = Random.Range(0, m_shotCandidates.Count); shotDirection = (m_shotCandidates[target].transform.position - m_owner.transform.position); } if (Random.Range(0, 5) != 0 || AlwaysFiring) { shotDirection = Vector3.Lerp(m_shotRegionStartDirection, m_shotRegionEndDirection, Random.value) * 10.0f; LevelGrid grid = Level.Instance.GetGrid(); int hitMask = 1 << (int)GridCellContentsType.Wall; LevelGridRaycastHit hit = new LevelGridRaycastHit(); if (grid.Raycast(m_owner.transform.position, m_owner.transform.position + (Vector3)shotDirection, hitMask, ref hit)) { Vector2 hitPosition; grid.GetCellLocation(hit.x, hit.y, out hitPosition); hit.cell.ApplyDamage(ShotDamage, DamageType.Bullet, shotDirection); Debug.DrawLine((Vector3)hitPosition + new Vector3(0.0f, 0.0f, -1.2f), (Vector3)hitPosition + new Vector3(0.2f, 0.2f, -1.2f), Color.green, 2.0f); } } else { m_shotCandidates[target].EntityHealth.ApplyDamage(ShotDamage, DamageType.Bullet); } Debug.DrawLine(m_owner.transform.position, m_owner.transform.position + (Vector3)shotDirection, Color.white, 0.2f); ScreenShake.Instance.AddShake(0.2f, m_owner.transform.position); }
public void Start() { s_instance = this; m_raycastHit = new LevelGridRaycastHit(); m_meshRenderer = GetComponent <MeshRenderer>(); LevelLayout layout = LevelLayout.Deserialise(@"C:\Unity\Squad\Assets\Resources\Levels\newlevel.xml"); LoadFromLayout(layout); }
// Bounds checking on input rays should be done outside of this method to avoid s******g it up further public bool Raycast(Vector2 rayStart, Vector2 rayEnd, int contentsMask, ref LevelGridRaycastHit outHit) { rayStart -= m_gridStart; rayEnd -= m_gridStart; float x1 = rayStart.x; float y1 = rayStart.y; float x2 = rayEnd.x; float y2 = rayEnd.y; int lowX = Mathf.Max((int)(x1 / m_cellSize), 0); int lowY = Mathf.Max((int)(y1 / m_cellSize), 0); int highX = Mathf.Min((int)(x2 / m_cellSize), m_numCellsX - 1); int highY = Mathf.Min((int)(y2 / m_cellSize), m_numCellsY - 1); highX = Mathf.Max(highX, 0); highY = Mathf.Max(highY, 0); int dx = ((x1 < x2) ? 1 : ((x1 > x2) ? -1 : 0)); int dy = ((y1 < y2) ? 1 : ((y1 > y2) ? -1 : 0)); float minx = m_cellSize * ((int)(x1 / m_cellSize)); float maxx = minx + m_cellSize; float tx = ((x1 > x2) ? (x1 - minx) : maxx - x1) / Mathf.Abs(x2 - x1); float miny = m_cellSize * ((int)(y1 / m_cellSize)); float maxy = miny + m_cellSize; float ty = ((y1 > y2) ? (y1 - miny) : maxy - y1) / Mathf.Abs(y2 - y1); float deltatx = m_cellSize / Mathf.Abs(x2 - x1); float deltaty = m_cellSize / Mathf.Abs(y2 - y1); const int maxIterations = 200; int currentIterations = 0; while (currentIterations < maxIterations) { currentIterations++; if (tx <= ty) { if (lowX == highX) { return(false); } tx += deltatx; lowX += dx; if ((m_cells[lowX, lowY].m_contentsMask & contentsMask) != 0) { outHit.x = lowX; outHit.y = lowY; outHit.cell = m_cells[lowX, lowY]; return(true); } } else if (ty <= tx) { if (lowY == highY) { return(false); } ty += deltaty; lowY += dy; if ((m_cells[lowX, lowY].m_contentsMask & contentsMask) != 0) { outHit.x = lowX; outHit.y = lowY; outHit.cell = m_cells[lowX, lowY]; return(true); } } } return(false); }