Beispiel #1
0
        public void UpdateEntities()
        {
            if (CurrentChunk == null)
            {
                return;
            }

            List <int> tempelist = new List <int>();

            int sx = CurrentChunk.x - p.viewdistance; //StartX
            int ex = CurrentChunk.x + p.viewdistance; //EndX
            int sz = CurrentChunk.z - p.viewdistance; //StartZ
            int ez = CurrentChunk.z + p.viewdistance; //EndZ

            for (int x = sx; x <= ex; x++)
            {
                for (int z = sz; z <= ez; z++)
                {
                    if (!level.chunkData.ContainsKey(new Point(x, z)))
                    {
                        continue;
                    }

                    foreach (Entity e in p.level.chunkData[new Point(x, z)].GetEntities().ToArray())
                    {
                        tempelist.Add(e.id);
                        if (p.VisibleEntities.Contains(e.id))
                        {
                            continue;                             //Continue if the player already has this entity
                        }
                        if (e.isPlayer)
                        {
                            if (e.p == p)
                            {
                                continue;
                            }
                            p.VisibleEntities.Add(e.id);
                            p.SendNamedEntitySpawn(e.p);
                            if (!e.p.VisibleEntities.Contains(id))
                            {
                                e.p.VisibleEntities.Add(id);
                                e.p.SendNamedEntitySpawn(p);
                            }
                            continue;
                        }
                        else if (e.isItem)
                        {
                            p.VisibleEntities.Add(e.id);
                            p.SendPickupSpawn(e);
                            continue;
                        }
                        else if (e.isAI)
                        {
                            p.VisibleEntities.Add(e.id);
                            p.SendMobSpawn(e);
                            continue;
                        }
                        else if (e.isObject)
                        {
                            p.VisibleEntities.Add(e.id);
                            p.SendObjectSpawn(e);
                            continue;
                        }
                    }
                }
            }
            foreach (int i in p.VisibleEntities.ToArray())
            {
                if (!Entities.ContainsKey(i))
                {
                    p.VisibleEntities.Remove(i);
                    continue;
                }

                Entity e = Entities[i];
                if (e.isItem)
                {
                    Point3 diff = pos - e.pos;

                    //Console.WriteLine(diff.x + " " + diff.z);
                    if (Math.Abs(diff.x) <= 1.5 && Math.Ceiling(diff.y) <= 0 && Math.Ceiling(diff.y) >= -1 && Math.Abs(diff.z) <= 1.5)
                    {
                        if (e.age < 10)
                        {
                            continue;
                        }

                        if (p.inventory.Add(e.I))
                        {
                            p.SendPickupAnimation(e.id);
                            Player.players.ForEach(delegate(Player pl)
                            {
                                if (pl != p && pl.level == p.level && pl.VisibleEntities.Contains(p.id))
                                {
                                    pl.SendPickupAnimation(e.id, p.id);
                                }
                            });

                            RemoveEntity(e);
                        }
                    }
                }

                /*if (e.isAI)
                 * {
                 *      Point3 sendme = e.pos * 32;
                 *      byte[] bytes = new byte[18];
                 *      util.EndianBitConverter.Big.GetBytes(e.id).CopyTo(bytes, 0);
                 *      util.EndianBitConverter.Big.GetBytes((int)sendme.x).CopyTo(bytes, 4);
                 *      util.EndianBitConverter.Big.GetBytes((int)sendme.y).CopyTo(bytes, 8);
                 *      util.EndianBitConverter.Big.GetBytes((int)sendme.z).CopyTo(bytes, 12);
                 *      bytes[16] = (byte)(e.ai.rot[0] / 1.40625);
                 *      bytes[17] = (byte)(e.ai.rot[1] / 1.40625);
                 *
                 *      if (!p.VisibleEntities.Contains(i)) continue;
                 *      if (!p.MapLoaded) continue;
                 *      p.SendRaw(0x22, bytes);
                 * }*/
            }
            foreach (int i in p.VisibleEntities.ToArray())
            {
                if (!tempelist.Contains(i))
                {
                    p.VisibleEntities.Remove(i);
                    p.SendDespawn(i);
                }
            }
        }