public abstract void Update(long gameTime, Collision collisionEngine);
public override void Update(long gameTime, Collision collisionEngine) { tankDirection = collisionEngine.MakeDirection(dx, dy); if (moving) { bool isSomethingInFrontOfPlayer = collisionEngine.RayCast(x + spriteWidth / 2, y + spriteHeight / 2, tankDirection, 20, 30) .Where(obj => obj.id != this.id) .Count() > 0; if (!isSomethingInFrontOfPlayer) { // Move player (speed * direction) x += velocity * dx; y += velocity * dy; } } // Keep the player on the screen if (x + velocity * dx <= 0) { x = 0; } if (x + velocity * dx >= 745) { x = 745; } if (y + velocity * dy <= 0) { y = 0; } if (y + velocity * dy >= 525) { y = 525; } if (missiles.Count > 0) { bool isBulletDestroyed = missiles[0].IsDestroyed(); bool shouldDestroyBullet = missiles[0].ShouldBeDestroyed(); if (isBulletDestroyed || shouldDestroyBullet) { missiles.Clear(); } else { missiles[0].Update(Environment.TickCount, collisionEngine); } } // Update collision pos rectangle.X = x; rectangle.Y = y; }
private void InitCollisionEngine() { collisionEngine = new Collision(); try { allGameObjects.Clear(); UpdateIronWallsCollisions(); UpdateDestroyableWallsCollisions(); UpdateEnemyCollisions(); if (player != null && !player.isDestroyed) { allGameObjects.Add(player as GameObject); } collisionEngine.Update(allGameObjects); } catch (Exception e) { } }
public override void Update(long gameTime, Collision collisionEngine) { if (missiles.Count == 0 && gameTime - lastShot >= SHOOT_PERIOD) { lastShot = gameTime; Shoot(); } if (missiles.Count > 0) { bool isBulletDestroyed = missiles[0].IsDestroyed(); bool shouldDestroyBullet = missiles[0].ShouldBeDestroyed(); if (isBulletDestroyed || shouldDestroyBullet) { missiles.Clear(); } else { missiles[0].Update(Environment.TickCount, collisionEngine); } } tankDirection = collisionEngine.MakeDirection(dx, dy); if (moving) { List<GameObject> rayCastHitObjects = collisionEngine.RayCast(x + spriteWidth / 2, y + spriteHeight / 2, tankDirection, 20, 30) .Where(obj => obj.id != this.id).ToList(); bool isSomethingInFrontOfMe = rayCastHitObjects.Count() > 0; if (!isSomethingInFrontOfMe) { x += velocity * dx; y += velocity * dy; } else { if( rayCastHitObjects.Where(obj => !obj.isDestroyable).Count() > 0 ) { ChangeDirection(); } else { bool timeLimit = stopWatch.Elapsed.Seconds > 1f; if (timeLimit) { stopWatch.Restart(); ChangeDirection(); } } } } if (x <= 0) { x = 0 + velocity; ChangeDirection(); } if (x >= 745) { x = 745 - velocity; ChangeDirection(); } if (y <= 0) { y = 0 + velocity; ChangeDirection(); } if (y >= 525) { y = 525 - velocity; ChangeDirection(); } rectangle.X = x; rectangle.Y = y; if (moving == true) { // Play moving animation if (gameTime - frameTicker > framePeriod) { frameTicker = gameTime; if (currentFrame % 2 == 0) { currentFrame++; } else { currentFrame--; } } } }
public void Update(long gameTime, Collision collisionEngine) { if (isDestroyed) { // Play destroying animation if (gameTime - frameTicker > framePeriod) { frameTicker = gameTime; this.sourceRect.X += spriteWidth; } return; } // Move missile x += velocity * dx; y += velocity * dy; // Collision rectangle.X = x; rectangle.Y = y; Directions moveDirection = collisionEngine.MakeDirection(dx, dy); // Get direction var hitObjects = collisionEngine.RayCast(x + spriteWidth / 2, y + spriteHeight / 2, moveDirection, velocity, 8) .Where(gameObj => gameObj.id != this.id) .Cast<GameObject>() .ToList(); if (hitObjects.Count() > 0) { // Destroy hit objects and the bullet foreach (var item in hitObjects) { if (item.isDestroyable && item.GetType() != shooter) { item.isDestroyed = true; if (item.GetType() == typeof(Enemy)) { GameWindow.game.IncreasePointsBy(100); } else if (item.GetType() == typeof(Player)) { GameWindow.game.DecreaseLives(); } } } Destroy(); } }