/// <summary> /// Collisions the specified entity. /// </summary> /// <param name="player">The player.</param> /// <param name="npc">The NPC.</param> /// <returns></returns> public static bool collision(Player player, NPC npc) { if (npc.visible) { if (npc.attacking) //Waarom controleren op collision als ie toch stilstaat... { TimeSpan hitTimePassed = DateTime.Now - npc.lastHit; if (hitTimePassed.TotalMilliseconds >= npc.definition.attackSpeed) { if (player.electricWallTimer > 0) { npc.hit(new Hit(npc, player, player.Damage)); } player.hit(new Hit(player, npc, GameWorld.random.Next(1, npc.Damage))); npc.lastHit = DateTime.Now; } return true; } GifAnimation mainTexture = npc.definition.mainTexture; Rectangle npcRectangle = new Rectangle(npc.getX(), npc.getY(), mainTexture.Width, mainTexture.Height); Rectangle wallRectangle = new Rectangle(player.Wall.getX(), player.Wall.getY(), player.Wall.definition.mainTexture.Width, player.Wall.definition.mainTexture.Height); if (intersects(npcRectangle, wallRectangle) && (npc.getX() >= (player.Wall.getX() + player.Wall.definition.mainTexture.Width / 2) - npc.definition.mainTexture.Width)) { Matrix npcMatrix = Matrix.CreateTranslation(npc.getX(), npc.getY(), 0); Matrix wallMatrix = Matrix.CreateTranslation(player.Wall.getX(), player.Wall.getY(), 0); Vector2 collision = texturesCollide(player.Wall.definition.pixels, wallMatrix, npc.definition.pixels, npcMatrix); if (collision.X != -1 && collision.Y != -1) { TimeSpan hitTimePassed = DateTime.Now - npc.lastHit; if (hitTimePassed.TotalMilliseconds >= npc.definition.attackSpeed) { if (player.electricWallTimer > 0) { npc.hit(new Hit(npc, player, player.Damage)); } player.hit(new Hit(player, npc, GameWorld.random.Next(1, npc.Damage))); npc.attacking = true; npc.lastHit = DateTime.Now; } return true; } } } return false; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { try { //animation.Update(gameTime.ElapsedGameTime.Ticks); try { foreach (NPC npc in new LinkedList<NPC>(npcs)) { npc.Update(gameTime); } } catch (Exception e) { return; } switch (gameState) { case GameState.Begin: intro.StopVideo(); if (Keyboard.GetState().IsKeyDown(Keys.End)) { intro.SkipVideo(); } break; case GameState.GameOver: case GameState.Highscore: case GameState.Select: case GameState.Paused: case GameState.Upgrade: case GameState.Menu: IManager.Update(); break; case GameState.InGame: IManager.Update(); gameFrame.UpdateGifGameFrame(gameTime); arduino.sendMessage(); if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.Paused; break; } else if (Keyboard.GetState().IsKeyDown(Keys.PrintScreen)) { ScreenShot(DateTime.Now.ToString("yyyyMMdd_HHmmss")); break; } else if (Keyboard.GetState().IsKeyDown(Keys.Space)) { if (player.Wall.getLevel() == 1) { if (player.electricWallTimer == -1) { if (player.evilWall) { if (player.oil - 50 > 0) { player.Wall.definition.ChangeTextureToElectric(); player.electricWallTimer = 2000; player.oil -= 50; gameFrame.UpdateOil(player.oil); break; } } else { player.Wall.definition.ChangeTextureToElectric(); player.electricWallTimer = 2000; break; } } } } lastWave += gameTime.ElapsedGameTime; if (lastWave.TotalMilliseconds >= LevelInformation.forValue(player.currentLevel).waveDelay) { LevelInformation wave = LevelInformation.forValue(player.currentLevel); if (!wave.bossSpawned) { bool spawnBoss = wave.currentSpawnedEnemiesAmount >= wave.amountOfEnemies && !wave.bossSpawned; if (spawnBoss) { wave.bossSpawned = spawnBoss; if (player.currentLevel == 1) { EnterFistBoss.PlaySound(); } else if (player.currentLevel == 2) { enterSecondBoss.PlaySound(); } else if (player.currentLevel == 3) { enterThirdBoss.PlaySound(); } } int typeToSpawn = spawnBoss ? wave.bossType : wave.npcTypes[random.Next(wave.npcTypes.Length)]; NPC npc = new NPC(typeToSpawn); float maxY = GameFrame.Height - npc.definition.mainTexture.Height - 50; float minY = GameFrame.Height / 2 + 75; float y = random.Next((int)minY, (int)maxY); if (y < minY) y = minY; else if (y > maxY) y = maxY; npc.setLocation(new Vector2(-npc.definition.mainTexture.Width, y)); npcs.AddLast(npc); wave.currentSpawnedEnemiesAmount++; } lastWave = TimeSpan.Zero; } break; } info.Update(gameTime); base.Update(gameTime); } catch (Exception e) { return; } }
private void RemoveNPC(NPC npc) { lock (Program.INSTANCE.npcs) { Program.INSTANCE.npcs.Remove(npc); Program.INSTANCE.player.Score += 100; Program.INSTANCE.gameFrame.UpdateScore(); } }