private void Spawn(object sender, ElapsedEventArgs e) { var vehicle = new VehicleObject(_vehicle, new Vector2(_spawnPoint, _laneLevel), _despawnPoint); vehicle.GoalReached += RemoveVehicle; VehicleObjects.Add(vehicle); _timer.Interval = _random.NextDouble() * (_spawnWindow - _minTimeDelay) + _minTimeDelay; _timer.Enabled = true; }
public void Update(GameTime gameTime, KeyboardState keyboardState) { var collided = false; Player.Update(gameTime, keyboardState); foreach (var vehicleLane in VehicleLanes) { vehicleLane.Update(gameTime); } if (!_disableCollisions) // Collisions { if (Player.Position.X + Player.Hitbox.Right > PlayZone.Width || Player.Position.X + Player.Hitbox.Left < 0 || Player.Position.Y + Player.Hitbox.Bottom > PlayZone.Height || Player.Position.Y + Player.Hitbox.Top < 0) { collided = true; OnCollision(_lastValidPlayerPosition); } else if (EnvironmentObjects != null) { foreach (var environmentObject in EnvironmentObjects) { if (environmentObject.CollisionEnabled && new Rectangle((int)(environmentObject.Position.X + environmentObject.Hitbox.X), (int)(environmentObject.Position.Y + environmentObject.Hitbox.Y), environmentObject.Hitbox.Width, environmentObject.Hitbox.Height).Intersects( new Rectangle((int)(Player.Position.X + Player.Hitbox.X), (int)(Player.Position.Y + Player.Hitbox.Y), Player.Hitbox.Width, Player.Hitbox.Height))) { collided = true; OnCollision(_lastValidPlayerPosition); break; } } } foreach (var litter in LitterList.ToList()) { if (new Rectangle((int)(litter.Position.X + litter.Hitbox.X), (int)(litter.Position.Y + litter.Hitbox.Y), litter.Hitbox.Width, litter.Hitbox.Height) .Intersects(new Rectangle((int)(Player.Position.X + Player.Hitbox.X), (int)(Player.Position.Y + Player.Hitbox.Y), Player.Hitbox.Width, Player.Hitbox.Height))) { litter.PickUp(); OnLitterPickedUp(litter); LitterList.Remove(litter); } } if (!InvincibilityFrames) { foreach (var vehicleLane in VehicleLanes) { if (vehicleLane.VehicleObjects != null) { foreach (var vehicleObject in vehicleLane.VehicleObjects.ToList()) { if (new Rectangle((int)(vehicleObject.Position.X + vehicleObject.Hitbox.X), (int)(vehicleObject.Position.Y + vehicleObject.Hitbox.Y), vehicleObject.Hitbox.Width, vehicleObject.Hitbox.Height).Intersects(new Rectangle( (int)(Player.Position.X + Player.Hitbox.X), (int)(Player.Position.Y + Player.Hitbox.Y), Player.Hitbox.Width, Player.Hitbox.Height))) { Vector2 direction; var pureDirection = (Player.Position + new Vector2(Player.Hitbox.Center.X, Player.Hitbox.Center.Y)) - (vehicleObject.Position + new Vector2(vehicleObject.Hitbox.Center.X, vehicleObject.Hitbox.Center.Y)); if (Math.Abs(pureDirection.X) > Math.Abs(pureDirection.Y)) { direction = Vector2.UnitX * (pureDirection.X > 0 ? 1 : -1); } else { direction = Vector2.UnitY * (pureDirection.Y > 0 ? 1 : -1); } OnVehicleHit(direction, vehicleObject.HitType); InvincibilityFrames = true; _disableCollisions = true; _invincibilityStopwatch.Reset(); _invincibilityStopwatch.Start(); Camera.CameraState = CameraState.Chase; Camera.ChaseFactor = 2f; Camera.SmoothFactor = 5f; Player.StopChase += StopChaseCamera; Player.Respawned += EnabledCollisions; if (vehicleObject.HitType == HitType.Stick) { _stick = true; _stickVehicle = vehicleObject; _stickPosition = Player.Position - _stickVehicle.Position; Player.Respawned += Unstick; } break; } } } } } } if (_stick) { OnCollision(_stickVehicle.Position + _stickPosition); } if (InvincibilityTime > _invincibilityTime) { _invincibilityStopwatch.Stop(); InvincibilityFrames = false; } if (!collided) { _lastValidPlayerPosition = Player.Position; } Camera.Update(gameTime); }
private void Unstick(object sender, EventArgs e) { _stick = false; _stickVehicle = null; }