public void Update(ref Intruder[] intruder, ref ShotSystem shotSystem) { if (target == -1) getTarget(intruder); if (target != -1) { if ((pos - intruder[target].pos).LengthSquared() > 20000 || !intruder[target].alive) { target = -1; getTarget(intruder); } else { if (reloadTimer == 0) { shotSystem.createShot(pos - 70 * Vector2.UnitY, intruder[target].pos, target); reloadTimer = 20; } } } if (reloadTimer > 0) reloadTimer--; }
private void getTarget(Intruder[] intruder) { float minDist = 40000; int minIdx = -1; for (int i = 0; i < 50; i++) { if (intruder[i].alive) { float dist = (pos - intruder[i].pos).LengthSquared(); if (dist < minDist) { minDist = dist; minIdx = i; } } } target = minIdx; }
public void Update(ref Intruder[] intruder) { foreach (Shot shot in shots) { if (!shot.dead) { shot.Update(); if (shot.dead) intruder[shot.target].health -= 20; } } //for (LinkedListNode<Shot> iterator = shots.First; true; iterator = iterator.Next) //{ // iterator.Value.Update(); // if (iterator.Value.dead) // { // intruder[iterator.Value.target].health -= 10; // shots.Remove(iterator); // } // if (iterator == shots.Last) break; //} }
public void Update(MouseState mouse, MouseState oldMouse, ref Intruder[] intruder, ref ShotSystem shotSystem) { // X = 1366 * 0.5f - (widthOfRow - 0.5f) * 0.5f * cellWidth + col * cellWidth, // Y = 768 * 0.5f - (height - 1) * 0.5f * cellHeight + row * cellHeight); Vector2 mouseWorldPos; mouseWorldPos.X = (mouse.X - Camera.centreOfScreen.X) / Camera.zoom + Camera.pos.X; mouseWorldPos.Y = (mouse.Y - Camera.centreOfScreen.Y) / Camera.zoom + Camera.pos.Y; int targetCell = 0, counter = 0; int row = (int)(mouseWorldPos.Y + 0.5f * cellHeight + (height - 1) * 0.5f * cellHeight - 768 * 0.5f) / (int)cellHeight; int widthOfRow = width - Math.Abs(row - height / 2); int col = (int)(mouseWorldPos.X + 0.5f * cellWidth + (widthOfRow - 0.5f) * 0.5f * cellWidth - 1366 * 0.5f) / (int)cellWidth; if (row >= 0 && row < height && col >= 0 && col < width) indexAtMouse = startIndexOfRow[row] + col; else indexAtMouse = -1; if (mouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released && indexAtMouse != -1 && !cells[indexAtMouse].passable && !cells[indexAtMouse].hasTower) { while (counter < NO_OF_TOWERS && tower[counter % NO_OF_TOWERS].alive) counter++; if (!tower[counter % NO_OF_TOWERS].alive) { tower[counter % NO_OF_TOWERS].Init(cells[indexAtMouse].centre, indexAtMouse); cells[indexAtMouse].SetHazardLevel(4); cells[indexAtMouse].hasTower = true; for (int d = 0; d < 6; d++) { targetCell = cells[indexAtMouse].connections[d].targetCell; if (targetCell != -1) if (cells[targetCell].indexInGrid != indexOfEntry && cells[targetCell].indexInGrid != indexOfExit) cells[targetCell].SetHazardLevel(2); } CalcSafestPath(); counter++; } } if (mouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released && indexAtMouse != -1 && cells[indexAtMouse].passable && !cells[indexAtMouse].occupied) { SetCellUnpassable(indexAtMouse); CalcShortestPath(); if (cells[indexOfEntry].shortestDist == Dir.None) { SetCellPassable(indexAtMouse); CalcShortestPath(); } else foreach (Intruder i in intruder) { if (i.alive && cells[i.nextCell].shortestWay == Dir.None) { SetCellPassable(indexAtMouse); CalcShortestPath(); break; } } if (!cells[indexAtMouse].passable) CalcSafestPath(); } if (mouse.RightButton == ButtonState.Pressed && oldMouse.RightButton == ButtonState.Released && indexAtMouse != -1 && !cells[indexAtMouse].passable) { for (int n = 0; n < NO_OF_TOWERS; n++) if (tower[n].alive && tower[n].pos == cells[indexAtMouse].centre) { tower[n].alive = false; cells[indexAtMouse].hasTower = false; if (cells[indexAtMouse].hazardLevel >= 5) cells[indexAtMouse].SetHazardLevel(-4); else cells[indexAtMouse].hazardLevel = 1; for (int d = 0; d < 6; d++) { targetCell = cells[indexAtMouse].connections[d].targetCell; if (targetCell != -1) if (cells[targetCell].hazardLevel >= 3) cells[targetCell].SetHazardLevel(-2); } CalcSafestPath(); counter = n; return; } SetCellPassable(indexAtMouse); CalcSafestPath(); CalcShortestPath(); } if (!Game1.paused) for (int n = 0; n < NO_OF_TOWERS; n++) if (tower[n].alive) tower[n].Update(ref intruder, ref shotSystem); }
public void Update(ref Grid grid, Intruder[] intruder, ref ShotSystem shotSystem) { if (target == -1) target = GetNewTarget(ref grid, intruder); if (target != -1 && !intruder[target].alive) target = GetNewTarget(ref grid, intruder); if (progress % 20 == 0 && target != -1 && intruder[target].alive && (pos - intruder[target].pos).LengthSquared() < 10000) shotSystem.createShot(pos - Vector2.One * 20, intruder[target].pos, target); pos = (progress * grid.cells[nextCell].centre + (STEPS_PER_CELL - progress) * grid.cells[prevCell].centre) / (float)STEPS_PER_CELL; if (progress >= STEPS_PER_CELL) if (target != -1 && intruder[target].alive) { if (pathing == GuardPathfinding.Dumb) { float dirToTarget = (float)((Math.Atan2(grid.cells[intruder[target].nextCell].centre.Y - pos.Y, pos.X - grid.cells[intruder[target].nextCell].centre.X) / Math.PI + 1) * 3); int dirToMove = Dir.None; float diff; float minDiff = 7; for (int d = 0; d < 6; d++) { if (grid.cells[nextCell].connections[d].passable) { diff = Math.Abs(d - dirToTarget); if (diff > 3) diff = 6 - diff; if (diff < minDiff) { minDiff = diff; dirToMove = d; } } } if (dirToMove != -1) { int newNextCell = grid.cells[nextCell].connections[dirToMove].targetCell; prevCell = nextCell; nextCell = newNextCell; progress = 0; } } else { grid.CalcGuardPath(nextCell, intruder[target].nextCell); int dir = grid.cells[nextCell].guardWay; if (dir != -1) { prevCell = nextCell; nextCell = grid.cells[nextCell].connections[dir].targetCell; progress = 0; } else { prevCell = nextCell; progress = 10; } } } if (progress < STEPS_PER_CELL) progress++; }
private int GetNewTarget(ref Grid grid, Intruder[] intruder) { int newTarget = -1; float minDist = 10000000; float dist; switch (strategy) { case GuardStrategy.GoForClosest: if (targetLookAhead) ; else for (int n = 0; n < 50; n++) { if (intruder[n].alive) { dist = (intruder[n].pos - pos).LengthSquared(); if (dist < minDist) { newTarget = n; minDist = dist; } } } break; case GuardStrategy.GoForMostCritical: if (targetLookAhead) ; else for (int n = 0; n < 50; n++) { if (intruder[n].alive) { dist = grid.cells[intruder[n].nextCell].shortestDist; if (dist < minDist) { newTarget = n; minDist = dist; } } } break; case GuardStrategy.MaximizeEfficiency: break; } return newTarget; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { grid = new Grid(13, 11); //grid = new Grid(47, 54); intruder = new Intruder[NO_OF_INTRUDERS]; shotSystem = new ShotSystem(); for (int n = 0; n < NO_OF_INTRUDERS; n++) intruder[n] = new Intruder(); guard = new Guard(); guard.Init(ref grid); graphics.PreferredBackBufferHeight = 768; graphics.PreferredBackBufferWidth = 1366; graphics.PreferMultiSampling = true; graphics.IsFullScreen = false; graphics.ApplyChanges(); Camera.centreOfScreen = new Vector2(1366 * 0.5f, 768 * 0.5f); Camera.pos = Camera.centreOfScreen; keyboard = Keyboard.GetState(); base.Initialize(); }