public override void Update(GameTime gameTime, Level level) { base.Update(gameTime, level); if (Parent == null) { return; } var lookDirection = VectorHelper.AngleToV2(Angle + (float)(Math.PI / 2), 1); Position = Parent.CenterPosition + lookDirection * new Vector2(15); foreach (var ent in GetCollisionRects().SelectMany(e => level.CollidesWith(e, true)).Distinct()) { if (ent is Arrow && ((Arrow)ent).HitEntity == null) { ent.CurrentDirection = CurrentDirection.Rotate(-_swingedAngle / 4).Normalized(); ent.Parent = Parent; } else if (ent != this && ent != Parent && (ent.Health != null || ent is Boomerang)) { var direction = VectorHelper.AngleToV2(Angle, 5); direction = new Vector2(-direction.Y, direction.X); ent.Hit(this, gameTime, level, direction); } } if (!_rotationCompleted || _keepRotating > 0) { var oldAngle = _swingedAngle; _swingedAngle += (float)(_swingDirection * _swingSpeed * gameTime.ElapsedGameTime.TotalMilliseconds / 1000); if (!_rotationCompleted && (_swingDirection > 0 == _swingedAngle > 0)) { _rotationCompleted = true; if (!_removeOnComplete) { _swingedAngle = 0; } } if (_rotationCompleted) { _keepRotating--; if (_removeOnComplete && _keepRotating <= 0) { level.RemoveEntity(this); _removeOnComplete = false; } } } Angle = _desiredAngle + _swingedAngle; Parent.Look(lookDirection, updateDirection: false); }
public override void Update(GameTime gameTime, Levels.Level level) { _timeFromCreation += gameTime.ElapsedGameTime; if (level.Map.Collides(CollisionRect, false, true)) { level.RemoveEntity(this); return; } if (HitEntity != null) { SoundManager.PlaySound("onfire"); if (gameTime.TotalGameTime > _entHitTime + _maxHitTime) { level.RemoveEntity(this); } return; } var timeFactor = gameTime.ElapsedGameTime.TotalMilliseconds / 1000; Position += CurrentDirection * (float)timeFactor * Speed; foreach (var ent in level.CollidesWith(CollisionRect).Distinct()) { if (ent != this && ent != Parent && ent.Health != null) { _entHitTime = gameTime.TotalGameTime; HitEntity = ent; var direction = VectorHelper.AngleToV2(Angle, 5); direction = new Vector2(-direction.Y, direction.X); ent.Hit(this, gameTime, level, direction); if (HitEntity != null) { OverlapEntities = false; Position = ent.CenterPosition; Angle = 0; return; } else { _timeFromCreation = TimeSpan.Zero; } } } if (_timeFromCreation > MaxFlyTime) { level.RemoveEntity(this); } }
private void Drop(Levels.Level level, Base.Entity dropEntity) { var enemyPosition = Entity.CenterPosition; var dropDistance = VectorHelper.AngleToV2((float)(Random.NextDouble() * Math.PI - Math.PI / 2), 32); var dropPosition = enemyPosition + dropDistance; dropEntity.Parent = null; dropEntity.OverlapEntities = true; dropEntity.Position = dropPosition; if (level.Map.Collides(dropEntity.CollisionRect)) { dropEntity.Position = enemyPosition; } level.AddEntity(dropEntity); }
private IEnumerable <Rectangle> GetCollisionRects() { const int rectSize = 5; var direction = VectorHelper.AngleToV2(Angle, rectSize); direction = new Vector2(-direction.Y, direction.X); var location = new Vector2(Position.X, Position.Y); for (int i = 0; i < 35; i += rectSize) { location += direction; yield return(new Rectangle((int)(location.X - rectSize / 2), (int)(location.Y - rectSize / 2), rectSize, rectSize)); } }
public override void Update(GameTime gameTime, Levels.Level level) { if (CurrentDirection != Vector2.Zero) { Angle = (float)(Math.Atan2(-CurrentDirection.X, CurrentDirection.Y) + _spriteAngle); } _timeFromCreation += gameTime.ElapsedGameTime; if (level.Map.Collides(CollisionRect, false, true) && CurrentDirection != Vector2.Zero) { if (level.Map.IsOutsideBorders(CollisionRect)) { level.RemoveEntity(this); } else { Dettach(level, removeFromLevel: Parent == null || Parent.Category != "Player"); } return; } if (HitEntity != null) { if (gameTime.TotalGameTime > _entHitTime + _maxHitTime || !level.ContainsEntity(HitEntity)) { Dettach(level, HitEntity.Arrows == null || HitEntity.Health <= 0); } else { Position = HitEntity.CenterPosition - _hitLocation; } return; } if (Parent != null) { var timeFactor = gameTime.ElapsedGameTime.TotalMilliseconds / 1000; Position += CurrentDirection * (float)timeFactor * Speed; } foreach (var ent in level.CollidesWith(CollisionRect).Distinct()) { if (Parent == null) { if (ent.Arrows != null && ent.Arrows.Quantity < ent.Arrows.Maximum) { ent.Arrows.Quantity += PickupCount; level.RemoveEntity(this); } return; } if (ent != this && ent != Parent && ent.Health != null) { _entHitTime = gameTime.TotalGameTime; HitEntity = ent; var direction = VectorHelper.AngleToV2(Angle, 5); direction = new Vector2(-direction.Y, direction.X); ent.Hit(this, gameTime, level, direction); _hitLocation = ent.CenterPosition - Position - CurrentDirection; return; } } }