public void Update(ref Grid grid) { if (health <= 0) { alive = false; grid.cells[nextCell].occupied = false; if (Game1.learning) { if (prevCell != grid.indexOfEntry) grid.cells[prevCell].SetHazardLevel(1); if (nextCell != grid.indexOfExit) grid.cells[nextCell].SetHazardLevel(1); grid.CalcSafestPath(); } return; } pos = (progress * grid.cells[nextCell].centre + (40 - progress) * grid.cells[prevCell].centre) / 40f; if (progress >= 40) { int newNextCell; if (nextCell == grid.indexOfExit) { alive = false; grid.cells[nextCell].occupied = false; progress = 0; return; } else { if (grid.cells[nextCell].safestWay == Dir.None) newNextCell = nextCell; else { if (safe) newNextCell = grid.cells[nextCell].connections[grid.cells[nextCell].safestWay].targetCell; else newNextCell = grid.cells[nextCell].connections[grid.cells[nextCell].shortestWay].targetCell; } if (!grid.cells[newNextCell].occupied) { prevCell = nextCell; if (prevCell != grid.indexOfExit) nextCell = newNextCell; grid.cells[prevCell].occupied = false; grid.cells[nextCell].occupied = true; progress = 0; } } } if (progress < 40) progress++; }
public void Init(ref Grid grid) { target = -1; alive = false; health = 100; pos = grid.cells[grid.indexOfExit].centre; safe = Rnd.r.NextDouble() > 0.5; progress = 0; prevCell = grid.indexOfExit; nextCell = prevCell; strategy = GuardStrategy.GoForMostCritical; pathing = GuardPathfinding.Dumb; targetLookAhead = false; }
public void Init(ref Grid grid, bool isSmart) { alive = true; health = 100; pos = grid.cells[grid.indexOfEntry].centre; safe = isSmart; progress = 0; prevCell = grid.indexOfEntry; if (grid.cells[prevCell].safestWay == Dir.None) nextCell = prevCell; else { if (safe) nextCell = grid.cells[prevCell].connections[grid.cells[prevCell].safestWay].targetCell; else nextCell = grid.cells[prevCell].connections[grid.cells[prevCell].shortestWay].targetCell; } }
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(); }