Exemple #1
0
 public override void OnEnd()
 {
     _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
     _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
     _entity.KnownPosition.Yaw     = (float)_entity.Direction;
     _entity.BroadcastMove(true);
 }
        public override void OnTick(Entity[] entities)
        {
            if (_currentPath.HavePath())
            {
                //// DEBUG
                //_pathfinder.PrintPath(_entity.Level, _currentPath);

                Tile next;
                if (!GetNextTile(out next))
                {
                    _currentPath = null;
                    return;
                }

                _entity.Controller.RotateTowards(new Vector3((float)next.X + 0.5f, _entity.KnownPosition.Y, (float)next.Y + 0.5f));
                _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
                _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
                _entity.KnownPosition.Yaw     = (float)_entity.Direction;

                _entity.Controller.MoveForward(_speedMultiplier, entities);
                if (_entity.Velocity.Length() < 0.01)
                {
                    _currentPath = null;
                }
            }
            else
            {
                _currentPath = null;
            }
        }
Exemple #3
0
        public override void OnTick(Entity[] entities)
        {
            if (_currentPath.HavePath())
            {
                if (!_currentPath.GetNextTile(_entity, out var next))
                {
                    return;
                }

                _entity.Controller.RotateTowards(new Vector3((float)next.X + 0.5f, _entity.KnownPosition.Y, (float)next.Y + 0.5f));
                _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
                _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
                _entity.KnownPosition.Yaw     = (float)_entity.Direction;

                _entity.Controller.MoveForward(_speedMultiplier, entities);
            }
        }
Exemple #4
0
        public void OnTick(Entity[] entities)
        {
            if (_player == null)
            {
                return;
            }
            var distanceToPlayer = _entity.DistanceTo(_player);

            if (distanceToPlayer < 1.75)
            {
                _entity.Velocity = Vector3.Zero;
                _entity.Controller.LookAt(_player);

                _entity.Controller.RotateTowards(_player.KnownPosition);
                _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
                _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
                _entity.KnownPosition.Yaw     = (float)_entity.Direction;
                _currentPath = null;

                return;
            }

            bool haveNoPath    = (_currentPath == null || _currentPath.Count == 0);
            var  deltaDistance = Vector3.Distance(_lastPlayerPos, _player.KnownPosition);

            if (haveNoPath || deltaDistance > 0)
            {
                Log.Debug($"Search new solution");
                _pathfinder  = new Pathfinder();
                _currentPath = _pathfinder.FindPath(_entity, _player, distanceToPlayer + 1);
                if (_currentPath.Count == 0)
                {
                    _currentPath = _pathfinder.FindPath(_entity, _player, _lookDistance);
                }
            }

            _lastPlayerPos = _player.KnownPosition;

            if (_currentPath.Count > 0)
            {
                // DEBUG
                //_pathfinder?.PrintPath(_entity.Level, _currentPath);

                ImprovedTile next;
                if (!GetNextTile(out next))
                {
                    _currentPath = null;
                    return;
                }

                _entity.Controller.RotateTowards(new Vector3((float)next.X + 0.5f, _entity.KnownPosition.Y, (float)next.Y + 0.5f));
                _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
                _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
                _entity.KnownPosition.Yaw     = (float)_entity.Direction;

                if (distanceToPlayer < 1.75)
                {
                    // if within x m stop following (walking)
                    _entity.Velocity = Vector3.Zero;
                    _currentPath     = null;
                }
                else
                {
                    // else find path to player

                    var m = 2 - distanceToPlayer;
                    if (m <= 0)
                    {
                        m = 1;
                    }
                    else
                    {
                        m = m / 2.0;
                    }
                    //double m = 1;
                    _entity.Controller.MoveForward(_speedMultiplier * m, entities);
                }
            }
            else
            {
                _entity.Velocity = Vector3.Zero;
                _currentPath     = null;
            }

            _entity.Controller.LookAt(_player, true);
        }
Exemple #5
0
        public virtual void OnTick(Entity[] entities)
        {
            float speedFactor = (float)(_speed * _speedMultiplier * 0.7f * (_entity.IsInWater ? 0.3 : 1.0));             // 0.7 is a general mob base factor
            var   level       = _entity.Level;
            var   coordinates = _entity.KnownPosition;
            var   direction   = _entity.GetHorizDir() * new Vector3(1, 0, 1);

            var blockDown = level.GetBlock(coordinates + BlockCoordinates.Down);

            if (_entity.Velocity.Y < 0 && blockDown is Air)
            {
                _timeLeft = 0;
                return;
            }

            BlockCoordinates coord = (BlockCoordinates)(coordinates + (direction * speedFactor) + (direction * (float)_entity.Length / 2));

            var  players       = level.GetSpawnedPlayers();
            bool entityCollide = false;
            var  boundingBox   = _entity.GetBoundingBox().OffsetBy((direction * speedFactor) + (direction * (float)_entity.Length / 2));

            foreach (var player in players)
            {
                if (player.GetBoundingBox().Intersects(boundingBox))
                {
                    entityCollide = true;
                    break;
                }
            }

            if (!entityCollide)
            {
                var bbox = boundingBox;
                foreach (var ent in entities)
                {
                    if (ent == _entity)
                    {
                        continue;
                    }

                    if (ent.EntityId > _entity.EntityId && _entity.IsColliding(bbox, ent))
                    {
                        if (_entity.Velocity == Vector3.Zero && level.Random.Next(1000) == 0)
                        {
                            break;
                        }
                        entityCollide = true;
                        break;
                    }
                }
            }

            var block     = level.GetBlock(coord);
            var blockUp   = level.GetBlock(coord + BlockCoordinates.Up);
            var blockUpUp = level.GetBlock(coord + BlockCoordinates.Up + BlockCoordinates.Up);

            var colliding = block.IsSolid || (_entity.Height >= 1 && blockUp.IsSolid);

            if (!colliding && !entityCollide)
            {
                var velocity = direction * speedFactor;
                //Log.Debug($"Moving sheep: {velocity}");
                if ((_entity.Velocity * new Vector3(1, 0, 1)).Length() < velocity.Length())
                {
                    _entity.Velocity += velocity - _entity.Velocity;
                }
                else
                {
                    _entity.Velocity = velocity;
                }
            }
            else
            {
                if (!entityCollide && !blockUp.IsSolid && !(_entity.Height > 1 && blockUpUp.IsSolid) && level.Random.Next(4) != 0)
                {
                    //Log.Debug($"Block ahead: {block}, jumping");
                    _entity.Velocity = new Vector3(0, 0.42f, 0);
                }
                else
                {
                    //Log.Debug($"Block ahead: {block}, turning");
                    int rot = level.Random.Next(2) == 0 ? level.Random.Next(45, 180) : level.Random.Next(-180, -45);
                    _entity.Direction            += rot;
                    _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
                    _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
                    _entity.KnownPosition.Yaw     = (float)_entity.Direction;
                    _entity.Velocity *= new Vector3(0, 1, 0);
                }
            }
        }
        public override void OnTick(Entity[] entities)
        {
            if (_temptingPlayer == null)
            {
                return;
            }
            var distanceToPlayer = _entity.DistanceTo(_temptingPlayer);

            if (distanceToPlayer < 1.75)
            {
                _entity.Velocity = Vector3.Zero;
                _entity.Controller.LookAt(_temptingPlayer);

                _entity.Controller.RotateTowards(_temptingPlayer.KnownPosition);
                _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
                _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
                _entity.KnownPosition.Yaw     = (float)_entity.Direction;
                _currentPath = null;

                return;
            }

            bool haveNoPath    = (_currentPath == null || _currentPath.NoPath());
            var  deltaDistance = Vector3.Distance(_lastPlayerPos, _temptingPlayer.KnownPosition);

            if (haveNoPath || deltaDistance > 1)
            {
                _pathfinder    = new Pathfinder();
                _currentPath   = _pathfinder.FindPath(_entity, _temptingPlayer, _lookDistance);
                _lastPlayerPos = _temptingPlayer.KnownPosition;
            }


            if (_currentPath.HavePath())
            {
                if (!_currentPath.GetNextTile(_entity, out var next, true))
                {
                    _currentPath = null;
                    return;
                }

                _entity.Controller.RotateTowards(new Vector3((float)next.X + 0.5f, _entity.KnownPosition.Y, (float)next.Y + 0.5f));
                _entity.Direction             = Mob.ClampDegrees(_entity.Direction);
                _entity.KnownPosition.HeadYaw = (float)_entity.Direction;
                _entity.KnownPosition.Yaw     = (float)_entity.Direction;

                if (distanceToPlayer < 1.75)
                {
                    // if within x m stop following (walking)
                    _entity.Velocity = Vector3.Zero;
                    _currentPath     = null;
                }
                else
                {
                    // else find path to player

                    var m = 2 - distanceToPlayer;
                    if (m <= 0)
                    {
                        m = 1;
                    }
                    else
                    {
                        m = m / 2.0;
                    }
                    //double m = 1;
                    _entity.Controller.MoveForward(_speedMultiplier * m, entities);
                }
            }
            else
            {
                _entity.Velocity = Vector3.Zero;
                _currentPath     = null;
            }

            _entity.Controller.LookAt(_temptingPlayer);
        }