Beispiel #1
0
        public void UpdateEntityPosition(long entityId, PlayerLocation position, bool relative = false, bool updateLook = false, bool updatePitch = false)
        {
            if (EntityManager.TryGet(entityId, out IEntity entity))
            {
                entity.KnownPosition.OnGround = position.OnGround;
                if (!relative)
                {
                    entity.KnownPosition = position;
                }
                else
                {
                    entity.KnownPosition.X += position.X;
                    entity.KnownPosition.Y += position.Y;
                    entity.KnownPosition.Z += position.Z;
                    //entity.KnownPosition.Move(position);
                }

                if (updateLook)
                {
                    //entity.KnownPosition.Yaw = position.Yaw;
                    if (updatePitch)
                    {
                        entity.KnownPosition.Pitch = position.Pitch;
                    }

                    entity.KnownPosition.HeadYaw = position.HeadYaw;
                    //	entity.UpdateHeadYaw(position.HeadYaw);
                }
            }
        }
Beispiel #2
0
        public void UpdateEntityPosition(long entityId, PlayerLocation position, bool relative = false, bool updateLook = false, bool updatePitch = false, bool teleport = false)
        {
            if (EntityManager.TryGet(entityId, out Entity entity))
            {
                entity.KnownPosition.OnGround = position.OnGround;
                if (!relative)
                {
                    entity.Movement.MoveTo(position, false);
                }
                else
                {
                    entity.Movement.Move(position);
                }

                entity.Velocity = Vector3.Zero;

                if (updateLook)
                {
                    if (updatePitch)
                    {
                        entity.KnownPosition.Pitch = position.Pitch;
                    }

                    entity.KnownPosition.Yaw     = position.Yaw;
                    entity.KnownPosition.HeadYaw = position.HeadYaw;
                    //	entity.UpdateHeadYaw(position.HeadYaw);
                }
            }
        }
Beispiel #3
0
        public static string GetCardinalDirection(PlayerLocation cam)
        {
            double rotation = (cam.HeadYaw) % 360;

            if (rotation < 0)
            {
                rotation += 360.0;
            }

            return(GetDirection(rotation));
        }
Beispiel #4
0
        public void UpdatePlayerPosition(PlayerLocation location)
        {
            var oldPosition = Player.KnownPosition;

            if (!ChunkManager.TryGetChunk(new ChunkCoordinates(location), out _))
            {
                Player.WaitingOnChunk = true;
            }

            Player.KnownPosition = location;

            Player.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, location));
        }
Beispiel #5
0
        public void UpdateEntityPosition(long entityId, PlayerLocation position, bool relative = false, bool updateLook = false, bool updatePitch = false)
        {
            if (EntityManager.TryGet(entityId, out Entity entity))
            {
                entity.KnownPosition.OnGround = position.OnGround;
                if (!relative)
                {
                    var oldPosition = entity.KnownPosition;
                    entity.KnownPosition = position;
                    //if (entity is PlayerMob p)
                    {
                        entity.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, position));
                    }
                }
                else
                {
                    var   oldPosition = entity.KnownPosition;
                    float offset      = 0f;

                    /*	if (this.ServerType == ServerType.Bedrock)
                     *      {
                     *              offset = (float) entity.PositionOffset;
                     *      }*/

                    //TODO: Fix position offset

                    entity.KnownPosition.X += position.X;
                    entity.KnownPosition.Y += (position.Y - offset);
                    entity.KnownPosition.Z += position.Z;

                    //if (entity is PlayerMob p)
                    {
                        entity.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, entity.KnownPosition));
                    }
                    //entity.KnownPosition.Move(position);
                }

                if (updateLook)
                {
                    //entity.KnownPosition.Yaw = position.Yaw;
                    if (updatePitch)
                    {
                        entity.KnownPosition.Pitch = position.Pitch;
                    }

                    entity.KnownPosition.HeadYaw = position.HeadYaw;
                    //	entity.UpdateHeadYaw(position.HeadYaw);
                }
            }
        }
Beispiel #6
0
        private void GameTick(object state)
        {
            if (World == null)
            {
                return;
            }

            if (_initiated)
            {
                var p = World.Player;
                if (p != null && p is Player player && Client.HasSpawned)
                {
                    //	player.IsSpawned = Spawned;

                    if (player.IsFlying != _flying)
                    {
                        _flying = player.IsFlying;

                        McpeAdventureSettings settings = McpeAdventureSettings.CreateObject();
                        settings.flags = GetAdventureFlags();
                        Client.SendPacket(settings);
                        //SendPlayerAbilities(player);
                    }

                    var pos = (PlayerLocation)player.KnownPosition.Clone();

                    if (pos.DistanceTo(_lastSentLocation) > 0.0f)
                    {
                        Client.SendMcpeMovePlayer(new MiNET.Utils.PlayerLocation(pos.X,
                                                                                 pos.Y + Player.EyeLevel, pos.Z, pos.HeadYaw,
                                                                                 pos.Yaw, -pos.Pitch), player.KnownPosition.OnGround);

                        _lastSentLocation = pos;
                    }

                    if (pos.DistanceTo(_lastLocation) > 16f && _stopwatch.ElapsedMilliseconds > 500)
                    {
                        World.ChunkManager.FlagPrioritization();

                        _stopwatch.Stop();
                        _stopwatch.Reset();
                        _lastLocation = pos;
                        UnloadChunks(new ChunkCoordinates(pos), Client.ChunkRadius + 3);
                        _stopwatch.Restart();
                    }
                }
            }
        }
Beispiel #7
0
        public Entity(int entityTypeId, World level, INetworkProvider network)
        {
            Network = network;

            EntityId      = -1;
            Level         = level;
            EntityTypeId  = entityTypeId;
            KnownPosition = new PlayerLocation();
            Inventory     = new Inventory(46);
            //	HealthManager = new HealthManager(this);

            Inventory.SlotChanged += OnInventorySlotChanged;
            Inventory.SelectedHotbarSlotChanged += InventoryOnSelectedHotbarSlotChanged;

            HideNameTag  = true;
            ServerEntity = true;
        }
Beispiel #8
0
        public Entity(int entityTypeId, World level, NetworkProvider network)
        {
            Network = network;

            EntityId      = -1;
            Level         = level;
            EntityTypeId  = entityTypeId;
            KnownPosition = new PlayerLocation();
            Inventory     = new Inventory(46);
            //	HealthManager = new HealthManager(this);

            Inventory.SlotChanged += OnInventorySlotChanged;
            Inventory.SelectedHotbarSlotChanged += InventoryOnSelectedHotbarSlotChanged;

            HideNameTag         = true;
            ServerEntity        = true;
            IsAffectedByGravity = true;

            HealthManager = new HealthManager(this);
            UUID          = new UUID(Guid.NewGuid().ToByteArray());
        }
Beispiel #9
0
        public static bool TryLoadEntity(NbtCompound nbt, long entityId, out Entity entity)
        {
            var id  = nbt["id"].StringValue;
            var pos = nbt["Pos"];
            var rot = nbt["Rotation"];

            if (id != null && pos != null && EntityType.TryParse(id.Replace("minecraft:", ""), true, out EntityType entityType))
            {
                var uuidLeast = nbt["UUIDLeast"].LongValue;
                var uuidMost  = nbt["UUIDMost"].LongValue;

                Guid uuid = Extensions.GuidFromBits(uuidLeast, uuidMost);

                var renderer = GetEntityRenderer(id, null);
                if (renderer != null)
                {
                    entity = entityType.Create(null);
                    if (entity == null)
                    {
                        return(false);
                    }

                    entity.EntityId = entityId;
                    entity.UUID     = new UUID(uuid.ToByteArray());

                    PlayerLocation position = new PlayerLocation(Convert.ToSingle(pos[0].DoubleValue), Convert.ToSingle(pos[1].DoubleValue),
                                                                 Convert.ToSingle(pos[2].DoubleValue), rot[0].FloatValue, rot[0].FloatValue, rot[1].FloatValue);

                    entity.KnownPosition = position;

                    entity.ModelRenderer = renderer;

                    return(true);
                }
            }

            entity = null;
            return(false);
        }
Beispiel #10
0
 public double Distance(PlayerLocation other)
 {
     return(Square(other.X - X) + Square(other.Y - Y) + Square(other.Z - Z));
 }
Beispiel #11
0
 public double DistanceTo(PlayerLocation other)
 {
     return(Math.Sqrt(Square(other.X - X) +
                      Square(other.Y - Y) +
                      Square(other.Z - Z)));
 }
Beispiel #12
0
 public void UpdatePlayerPosition(PlayerLocation location)
 {
     Player.KnownPosition = location;
 }
Beispiel #13
0
 public BlockCoordinates(PlayerLocation location)
 {
     X = (int)Math.Floor(location.X);
     Y = (int)Math.Floor(location.Y);
     Z = (int)Math.Floor(location.Z);
 }
        private void GameTick(object state)
        {
            var isRealTick = _isRealTick;

            _isRealTick = !isRealTick;

            if (World == null)
            {
                return;
            }

            if (_initiated)
            {
                if (_isRealTick)
                {
                    _tickTime++;
                }

                var p = World.Player;
                if (p != null && p is Player player && Client.HasSpawned)
                {
                    //	player.IsSpawned = Spawned;

                    if (player.IsFlying != _flying && _isRealTick)
                    {
                        _flying = player.IsFlying;

                        McpeAdventureSettings settings = McpeAdventureSettings.CreateObject();
                        settings.flags = GetAdventureFlags();
                        Client.SendPacket(settings);
                        //SendPlayerAbilities(player);
                    }

                    var pos = (PlayerLocation)player.KnownPosition.Clone();

                    if (pos.DistanceTo(_lastSentLocation) > 0.0f)
                    {
                        Client.SendMcpeMovePlayer(new MiNET.Utils.PlayerLocation(pos.X,
                                                                                 pos.Y + Player.EyeLevel, pos.Z, pos.HeadYaw,
                                                                                 pos.Yaw, -pos.Pitch), player.KnownPosition.OnGround);

                        _lastSentLocation = pos;
                    }

                    if ((pos.DistanceTo(_lastLocation) > 16f || MathF.Abs(pos.HeadYaw - _lastLocation.HeadYaw) >= 10f) && (_tickTime - _lastPrioritization >= 10 && _isRealTick))
                    {
                        World.ChunkManager.FlagPrioritization();

                        _lastLocation = pos;
                        UnloadChunks(new ChunkCoordinates(pos), Client.ChunkRadius + 3);

                        _lastPrioritization = _tickTime;
                    }
                }

                if (_isRealTick && _tickTime % 20 == 0 && CustomConnectedPong.CanPing)
                {
                    Client.SendPing();
                }


                if (_isRealTick)
                {
                    World.Player.OnTick();
                    World.EntityManager.Tick();
                }
            }
        }
Beispiel #15
0
        public void SpawnMob(long entityId, Guid uuid, EntityType type, PlayerLocation position, Microsoft.Xna.Framework.Vector3 velocity)
        {
            Entity entity = null;

            if (EntityFactory.ModelByType(type, out var renderer, out EntityData knownData))
            {
                //if (Enum.TryParse(knownData.Name, out type))
                //{
                //	entity = type.Create(null);
                //}
                entity = type.Create(null);

                if (entity == null)
                {
                    entity = new Entity((int)type, null, BaseClient);
                }

                //if (knownData.Height)
                {
                    entity.Height = knownData.Height;
                }

                //if (knownData.Width.HasValue)
                entity.Width = knownData.Width;

                if (string.IsNullOrWhiteSpace(entity.NameTag) && !string.IsNullOrWhiteSpace(knownData.Name))
                {
                    entity.NameTag = knownData.Name;
                }
            }

            if (entity == null)
            {
                Log.Warn($"Could not create entity of type: {(int) type}:{type.ToString()}");
                return;
            }

            if (renderer == null)
            {
                var def = AlexInstance.Resources.BedrockResourcePack.EntityDefinitions.FirstOrDefault(x =>
                                                                                                      x.Value.Filename.Replace("_", "").Equals(type.ToString().ToLowerInvariant()));
                if (!string.IsNullOrWhiteSpace(def.Key))
                {
                    EntityModel model;
                    if (ModelFactory.TryGetModel(def.Value.Geometry["default"],
                                                 out model) && model != null)
                    {
                        var    textures = def.Value.Textures;
                        string texture;
                        if (!textures.TryGetValue("default", out texture))
                        {
                            texture = textures.FirstOrDefault().Value;
                        }

                        if (AlexInstance.Resources.BedrockResourcePack.Textures.TryGetValue(texture,
                                                                                            out Bitmap bmp))
                        {
                            Texture2D t = TextureUtils.BitmapToTexture2D(AlexInstance.GraphicsDevice, bmp);

                            renderer = new EntityModelRenderer(model, t);
                        }
                    }
                }
            }

            if (renderer == null)
            {
                Log.Debug($"Missing renderer for entity: {type.ToString()} ({(int) type})");
                return;
            }

            if (renderer.Texture == null)
            {
                Log.Debug($"Missing texture for entity: {type.ToString()} ({(int) type})");
                return;
            }

            entity.ModelRenderer = renderer;

            entity.KnownPosition = position;
            entity.Velocity      = velocity;
            entity.EntityId      = entityId;
            entity.UUID          = new UUID(uuid.ToByteArray());


            BaseClient.WorldProvider.SpawnEntity(entityId, entity);
        }
Beispiel #16
0
 public ChunkCoordinates(PlayerLocation location)
 {
     X = ((int)Math.Floor(location.X)) >> 4;
     Z = ((int)Math.Floor(location.Z)) >> 4;
 }