Example #1
0
        public virtual void Update(GameTime gameTime)
        {
            // follow the target
            Vector2 dir       = target.position - position;
            float   magnitude = dir.Length();

            if (magnitude < attackRange)
            {
                dir.Normalize();
                if (magnitude > targetRange)
                {
                    Vector2 direction = dir;

                    if (direction != Vector2.Zero)
                    {
                        move = direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }
                }
                else
                {
                    move = Vector2.Zero;
                }

                Vector2 rotationDirection = Game1.AngleToVector(rotation);

                Vector2 newDir = Vector2.Lerp(rotationDirection, dir, turnSpeed);

                float newrot = Game1.VectorToAngle(newDir);

                rotation = newrot;

                lookDirection = newDir;
            }
            else
            {
                move = Vector2.Zero;
            }

            CellUpdate();

            ray = new Raycast(position, target.position);

            if (ray.Intersecting(out Collider[] colinfo, out Vector2 point))
            {
                foreach (var info in colinfo)
                {
                    if (info.owner is SceneObject col)
                    {
                        OnTargetBlocked(info, point);

                        break;
                    }
                    else
                    {
                        targetPosition = target.position;
                    }
                }
            }
            else
            {
                targetPosition = target.position;
            }

            NearbyCells = Cell.GetAreaOfCells(Cell.GetCell(position), 4, 4);

            Rectangle enemyRect = new Rectangle((int)position.X - sprite.Width / 2, (int)position.Y - sprite.Height / 2, sprite.Width, sprite.Height);

            Rectangle[] colliders = Collision.CollidingRectangle(position, NearbyCells, sprite.Width, sprite.Height, out Collider[] colInfo, "enemy", out Collider[] nearEnemies);

            if (nearEnemies != null)
            {
                Repel(nearEnemies);
            }

            if (colliders != null)
            {
                foreach (var col in colliders)
                {
                    Collision.RestrictPosition(enemyRect, col, ref move);
                }
                foreach (var info in colInfo)
                {
                    OnCollisionEnter(info);
                }
            }
            position += move;

            healthBar.position = position + healthBarOffset;
            collider.position  = position;
        }