示例#1
0
文件: Server.cs 项目: antonijn/7dfps
        public void SendDropItemMessage(TcpClient client, InventoryItem item, float x, float z, float xDir, float zDir)
        {
            List <byte> bytes  = new List <byte>();
            ItemID      itemID = ItemID.Nothing;

            if (item is InventoryStack)
            {
                itemID = ItemID.Grenade;
            }
            else if (item != null)
            {
                itemID = (ItemID)Enum.Parse(typeof(ItemID), item.GetType().Name);
            }
            bytes.AddRange(BitConverter.GetBytes((ushort)itemID));
            switch (itemID)
            {
            case ItemID.DefaultPistol:
            case ItemID.Rifle:
            case ItemID.SemiAuto:
                bytes.AddRange(BitConverter.GetBytes((item as Gun).Ammo));
                bytes.AddRange(BitConverter.GetBytes((item as Gun).MagazinesLeft));
                break;

            case ItemID.Grenade:
                InventoryStack stack = item as InventoryStack;
                if (stack != null)
                {
                    bytes.AddRange(BitConverter.GetBytes(stack.Amount));
                }
                else
                {
                    bytes.Add(1);
                }
                break;
            }
            byte[] xBytes    = BitConverter.GetBytes(x);
            byte[] zBytes    = BitConverter.GetBytes(z);
            byte[] xDirBytes = BitConverter.GetBytes(xDir);
            byte[] zDirBytes = BitConverter.GetBytes(zDir);
            bytes.AddRange(xBytes);
            bytes.AddRange(zBytes);
            bytes.AddRange(xDirBytes);
            bytes.AddRange(zDirBytes);
            SendMessage(client, Message.DropItem, bytes.ToArray());
        }
示例#2
0
文件: Server.cs 项目: antonijn/7dfps
        public void SendSetInventoryItemMessage(TcpClient client, int slot, InventoryItem item)
        {
            // TODO: grenade inventory stacks
            List <byte> bytes = new List <byte>();

            bytes.Add((byte)slot);
            ItemID itemID = ItemID.Nothing;

            if (item is InventoryStack)
            {
                itemID = ItemID.Grenade;
            }
            else if (item != null)
            {
                itemID = (ItemID)Enum.Parse(typeof(ItemID), item.GetType().Name);
            }
            bytes.AddRange(BitConverter.GetBytes((ushort)itemID));
            switch (itemID)
            {
            case ItemID.DefaultPistol:
            case ItemID.Rifle:
            case ItemID.SemiAuto:
                bytes.AddRange(BitConverter.GetBytes((item as Gun).Ammo));
                bytes.AddRange(BitConverter.GetBytes((item as Gun).MagazinesLeft));
                break;

            case ItemID.Grenade:
                InventoryStack stack = item as InventoryStack;
                if (stack != null)
                {
                    bytes.AddRange(BitConverter.GetBytes(stack.Amount));
                }
                else
                {
                    bytes.AddRange(BitConverter.GetBytes((int)1));
                }
                break;
            }
            SendMessage(client, Message.SetInventoryItem, bytes.ToArray());
        }
示例#3
0
        public override void Update(MainGameState game, float time)
        {
            float playerPosRelX = game.CurrentPlayer.X - X;
            float playerPosRelZ = game.CurrentPlayer.Z - Z;

            if (Altitude == 0f && playerPosRelX * playerPosRelX + playerPosRelZ * playerPosRelZ < .5f * .5f)
            {
                Weapon drop = GetDropped();
                if (drop.MaxStack <= 1)
                {
                    for (int i = 0; i < Player.InventorySize; ++i)
                    {
                        if (game.CurrentPlayer.GetInventoryItem(i) == null)
                        {
                            game.CurrentPlayer.SetInventoryItem(i, drop);
                            Sounds.PickupWeapon.Play();
                            ShouldBeRemoved = true;
                            return;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < Player.InventorySize; ++i)
                    {
                        InventoryStack iStack = game.CurrentPlayer.GetInventoryItem(i) as InventoryStack;
                        // thanks novynn for pointing out the silly != / == mistake!
                        if (iStack != null && iStack.GetItemType() == drop.GetType() && iStack.Amount != iStack.MaxValue)
                        {
                            ++iStack.Amount;
                            Sounds.PickupWeapon.Play();
                            ShouldBeRemoved = true;
                            return;
                        }
                    }
                    for (int i = 0; i < Player.InventorySize; ++i)
                    {
                        if (game.CurrentPlayer.GetInventoryItem(i) == null)
                        {
                            game.CurrentPlayer.SetInventoryItem(i, new DroppedItemInventoryStack(drop.GetType(), drop.MaxStack));
                            Sounds.PickupWeapon.Play();
                            ShouldBeRemoved = true;
                            return;
                        }
                    }
                }
            }

            UpdateGravity(time);
            if (Altitude != 0f)
            {
                Vector2 movement      = new Vector2(XDir * time * 3f, ZDir * time * 3f);
                Vector2 finalPosition = new Vector2(X, Z) + movement;
                Block   b             = game.BlockGrid[(int)finalPosition.X, (int)finalPosition.Y];
                if (b == null || !b.CollidesWithVector(finalPosition))
                {
                    X = finalPosition.X;
                    Z = finalPosition.Y;
                }
            }
        }
示例#4
0
        private void HandleMessage(TcpClient client, byte[] message, int bytesRead)
        {
            Message header = (Message)BitConverter.ToUInt16(message, 0);

            Console.WriteLine(header.ToString());
            switch (header)
            {
//			case Message.OtherPlayerInformation:
//				lock (PlayerMutex) {
//					game.OtherPlayers.Clear();
//					int playerAmount = BitConverter.ToInt32(message, 2);
//					//Console.WriteLine(playerAmount);
//					for (int i = 2 + sizeof(int), j = 0; j < playerAmount; ++j) {
//						int unameLength = BitConverter.ToInt32(message, i);
//						i += sizeof(int);
//						string uname = new ASCIIEncoding().GetString(message, i, unameLength);
//						//Console.WriteLine(unameLength);
//						i += unameLength;
//						float x = BitConverter.ToSingle(message, i);
//						i += sizeof(float);
//						float y = BitConverter.ToSingle(message, i);
//						i += sizeof(float);
//						//Console.WriteLine("X: {0}, Y: {1}", x, y);
//						game.OtherPlayers.Add(new Player(game, x, y, uname));
//					}
//				}
//				break;
            case Message.ConnectPlayer:
                string newPlayerUname = new ASCIIEncoding().GetString(message, 2, bytesRead - 2);
                lock (PlayerMutex) {
                    if (!game.OtherPlayers.Any(xOP => xOP.Name == newPlayerUname))
                    {
                        game.OtherPlayers.Add(new Player(game, game.Spawn.X, game.Spawn.Y, newPlayerUname));
                    }
                }
                break;

            case Message.OtherPlayerMove:
                string username = new ASCIIEncoding().GetString(message, 2, bytesRead - 2 - sizeof(float) * 2);
                Console.WriteLine(username);
                float xNewPos = BitConverter.ToSingle(message, bytesRead - sizeof(float) * 2);
                float zNewPos = BitConverter.ToSingle(message, bytesRead - sizeof(float) * 1);
                lock (PlayerMutex) {
                    Player pl = game.OtherPlayers.SingleOrDefault(x => x.Name == username);
                    if (pl == null)
                    {
                        break;
                    }
                    pl.PreviousPosition        = pl.ActualPosition;
                    pl.ActualPosition          = new Vector2(xNewPos, zNewPos);
                    pl.PositionCounter         = pl.PreviousPositionCounter;
                    pl.PreviousPositionCounter = 0f;
                    pl.InterpolationCounter    = 0f;
                }
                break;

            case Message.SetInventoryItem:
                int           slot   = message [2];
                ItemID        itemID = (ItemID)BitConverter.ToUInt16(message, 3);
                InventoryItem item   = null;
                if (itemID != ItemID.Nothing)
                {
                    Type invItemType = typeof(ItemID).Assembly.GetType("DFPS." + itemID.ToString());
                    item = Activator.CreateInstance(invItemType) as InventoryItem;

                    switch (itemID)
                    {
                    case ItemID.DefaultPistol:
                    case ItemID.Rifle:
                    case ItemID.SemiAuto:
                        ((Gun)item).Ammo          = BitConverter.ToInt32(message, 3 + sizeof(ushort));
                        ((Gun)item).MagazinesLeft = BitConverter.ToInt32(message, 3 + sizeof(ushort) + sizeof(int));
                        break;

                    case ItemID.Grenade:
                        item = new InventoryStack <Grenade>(BitConverter.ToInt32(message, 3 + sizeof(ushort)));
                        break;
                    }
                }
                game.CurrentPlayer.SetInventoryItem(slot, item);
                break;

            case Message.Move:
                float xPlayer = BitConverter.ToSingle(message, 2);
                float yPlayer = BitConverter.ToSingle(message, 2 + sizeof(float));
                game.CurrentPlayer.Position = new Vector2(xPlayer, yPlayer);
                break;

            case Message.UpdateWorld:
                lock (WorldMutex) {
                    game.BlockGrid = new Block[message [2], message [3]];
                    for (int i = 4; i < bytesRead - 1 - sizeof(int) * 2;)
                    {
                        byte wallType = message [i++];

                        switch (wallType)
                        {
                        case 0:
                        {
                            int x = BitConverter.ToInt32(message, i);
                            i += sizeof(int);
                            int z = BitConverter.ToInt32(message, i);
                            i += sizeof(int);
                            Block toAdd = new Block(x, z, TextureTools.TextureWall1);
                            game.Blocks.Add(toAdd);
                            game.BlockGrid [toAdd.X, toAdd.Z] = toAdd;
                        }
                        break;

                        case 1:
                        {
                            int x = BitConverter.ToInt32(message, i);
                            i += sizeof(int);
                            int z = BitConverter.ToInt32(message, i);
                            i += sizeof(int);
                            Block toAdd = new Block(x, z, TextureTools.TextureWall2);
                            game.Blocks.Add(toAdd);
                            game.BlockGrid [toAdd.X, toAdd.Z] = toAdd;
                        }
                        break;

                        case 2:
                        {
                            int x = BitConverter.ToInt32(message, i);
                            i += sizeof(int);
                            int z = BitConverter.ToInt32(message, i);
                            i += sizeof(int);
                            Door toAdd = new Door(x, z, TextureTools.TextureDoor1, TextureTools.TextureDoor1Upper, TextureTools.TextureDoor1Lower);
                            game.Blocks.Add(toAdd);
                            game.BlockGrid [toAdd.X, toAdd.Z] = toAdd;
                        }
                        break;

                        case 3:
                        {
                            float x = BitConverter.ToSingle(message, i);
                            i += sizeof(float);
                            float y = BitConverter.ToSingle(message, i);
                            i += sizeof(float);
                            HealthPack hp = new HealthPack(x, y);
                            game.HealthPacks.Add(hp);
                        }
                        break;

                        case 4:
                        {
                            float x = BitConverter.ToSingle(message, i);
                            i += sizeof(float);
                            float y = BitConverter.ToSingle(message, i);
                            i += sizeof(float);
                            AmmoCrate ac = new AmmoCrate(x, y);
                            game.Crates.Add(ac);
                        }
                        break;

                        default:
                            throw new Exception();
                        }
                    }
                }
                break;

            case Message.Shot1Confirmed:
                Sounds.Shoot1.Play();
                break;

            case Message.Shot2Confirmed:
                Sounds.Shoot2.Play();
                break;

            case Message.Shot3Confirmed:
                Sounds.Shoot3.Play();
                break;

            case Message.ReloadConfimed:
                Sounds.Reload1.Play();
                break;

            case Message.UpdateHealth:
                float health = BitConverter.ToSingle(message, 2);
                game.CurrentPlayer.Health = health;
                break;

            case Message.AddCrate:
                game.Crates.Add(new AmmoCrate(BitConverter.ToSingle(message, 2), BitConverter.ToSingle(message, 2 + sizeof(float))));
                break;

            case Message.AddHealthPack:
                game.HealthPacks.Add(new HealthPack(BitConverter.ToSingle(message, 2), BitConverter.ToSingle(message, 2 + sizeof(float))));
                break;

            case Message.RemoveCrate:
                float xCrate = BitConverter.ToSingle(message, 2);
                float zCrate = BitConverter.ToSingle(message, 2 + sizeof(float));
                game.Crates = game.Crates.Where(c => c.X != xCrate && c.Z != zCrate).ToList();
                break;

            case Message.RemoveHealthPack:
                float xHP = BitConverter.ToSingle(message, 2);
                float zHP = BitConverter.ToSingle(message, 2 + sizeof(float));
                game.HealthPacks = game.HealthPacks.Where(c => c.X != xHP && c.Z != zHP).ToList();
                break;

            case Message.ThrowGrenade:
                float xGren    = BitConverter.ToSingle(message, 2);
                float yGren    = BitConverter.ToSingle(message, 2 + sizeof(float) * 1);
                float xGrenDir = BitConverter.ToSingle(message, 2 + sizeof(float) * 2);
                float yGrenDir = BitConverter.ToSingle(message, 2 + sizeof(float) * 3);
                game.Projectiles.Add(new Grenade.GenadeProjectile(xGren, yGren, new Vector2(xGrenDir, yGrenDir)));
                break;

            case Message.DropItem:
                ItemID        toDrop   = (ItemID)BitConverter.ToUInt16(message, 2);
                int           position = sizeof(ItemID);
                InventoryItem iItem    = null;
                if (toDrop != ItemID.Nothing)
                {
                    Type invItemType = typeof(ItemID).Assembly.GetType("DFPS." + toDrop.ToString());
                    iItem = Activator.CreateInstance(invItemType) as InventoryItem;

                    switch (toDrop)
                    {
                    case ItemID.DefaultPistol:
                    case ItemID.Rifle:
                    case ItemID.SemiAuto:
                        ((Gun)iItem).Ammo          = BitConverter.ToInt32(message, 3 + sizeof(ushort));
                        ((Gun)iItem).MagazinesLeft = BitConverter.ToInt32(message, 3 + sizeof(ushort) + sizeof(int));
                        position += 2 * sizeof(int);
                        break;

                    case ItemID.Grenade:
                        iItem     = new InventoryStack <Grenade>(BitConverter.ToInt32(message, 3 + sizeof(ushort)));
                        position += sizeof(int);
                        break;
                    }
                }
                float xDrop    = BitConverter.ToSingle(message, 2 + position);
                float yDrop    = BitConverter.ToSingle(message, 2 + position + sizeof(float) * 1);
                float xDropDir = BitConverter.ToSingle(message, 2 + position + sizeof(float) * 2);
                float yDropDir = BitConverter.ToSingle(message, 2 + position + sizeof(float) * 3);
                game.Projectiles.Add(new InventoryDroppedWeapon((Weapon)iItem, xDrop, yDrop, xDropDir, yDropDir));
                break;

            case Message.DisconnectPlayer:
                string otherUsername = new ASCIIEncoding().GetString(message, 2, bytesRead - 2);
                lock (PlayerMutex) {
                    game.OtherPlayers = game.OtherPlayers.Where(op => op.Name != otherUsername).ToList();
                }
                break;

            case Message.Chat:
                string chatMessage = new ASCIIEncoding().GetString(message, 2, bytesRead - 2);
                lock (MainGameState.ChatMutex) {
                    game.Chat.Insert(0, new Tuple <DateTime, string>(DateTime.Now, chatMessage));
                }
                break;
            }
        }
示例#5
0
文件: Player.cs 项目: antonijn/7dfps
        public void Update(float time)
        {
            if (!MainGameState.IsServer)
            {
                if (Game.IsMultiplayer)
                {
                    sendRotationTimeCounter -= time;
                    if (sendRotationTimeCounter <= 0f)
                    {
                        Game.Client.SendUpdateWayFacingMessage(SinWorldRotation, CosWorldRotation);
                        Game.Client.SendMoveMessage(X, Z);
                        sendRotationTimeCounter = .1f;
                    }
                }

                int mouseWheel            = Glfw.GetMouseWheel();
                int scrollWheelDifference = mouseWheel - prevMouseWheel;
                CurrentInventorySlot -= scrollWheelDifference;
                CurrentInventorySlot  = Math.Min(InventorySize - 1, CurrentInventorySlot);
                CurrentInventorySlot  = Math.Max(0, CurrentInventorySlot);
                prevMouseWheel        = mouseWheel;
            }

            if (!Game.IsMultiplayer && Health <= 0f)
            {
                Game.Game.CurrentGameState = new DeathState(Game);
            }

            InventoryItem iItem = Inventory [CurrentInventorySlot];

            if (Game.Game.MouseClickCurrent)
            {
                if (iItem != null)
                {
                    if (Game.IsMultiplayer && !MainGameState.IsServer && !Game.Game.MouseClickPrevious)
                    {
                        Game.Client.SendLeftClickMessage(CurrentInventorySlot);
                    }
                    else
                    {
                        if (iItem.LeftClick(Game, time))
                        {
                            Inventory [CurrentInventorySlot] = null;
                        }
                    }
                }
            }
            else if (!MainGameState.IsServer && Game.IsMultiplayer && Game.Game.MouseClickPrevious)
            {
                Game.Client.SendLeftUnclickMessage();
            }

            if (!Game.IsMultiplayer)
            {
                Weapon w = iItem as Weapon;
                if (w != null && Game.Game.CurrentKS ['Q'] && !Game.Game.PreviousKS ['Q'])
                {
                    Inventory [CurrentInventorySlot] = null;
                    Game.DroppedWeapons.Add(new InventoryDroppedWeapon(w, X, Z, -SinWorldRotation, CosWorldRotation));
                }
                InventoryStack iStack = iItem as InventoryStack;
                if (iStack != null && Game.Game.CurrentKS ['Q'] && !Game.Game.PreviousKS ['Q'])
                {
                    --iStack.Amount;
                    if (iStack.Amount == 0)
                    {
                        Inventory [CurrentInventorySlot] = null;
                    }
                    Game.DroppedWeapons.Add(new InventoryDroppedWeapon(iStack.GetItem() as Weapon, X, Z, -SinWorldRotation, CosWorldRotation));
                }
            }

            if (!MainGameState.IsServer)
            {
                if (!Game.WritingChat)
                {
                    if (Game.Game.CurrentKS ['1'])
                    {
                        CurrentInventorySlot = 0;
                    }
                    else if (Game.Game.CurrentKS ['2'])
                    {
                        CurrentInventorySlot = 1;
                    }
                    else if (Game.Game.CurrentKS ['3'])
                    {
                        CurrentInventorySlot = 2;
                    }
                    else if (Game.Game.CurrentKS ['4'])
                    {
                        CurrentInventorySlot = 3;
                    }
                    else if (Game.Game.CurrentKS ['5'])
                    {
                        CurrentInventorySlot = 4;
                    }
                }
            }

            int x, y;

            Glfw.GetMousePos(out x, out y);
            WorldRotation = -x / 100f;

            if (!Game.WritingChat)
            {
                Vector2 movement = Vector2.Zero;

                if (Game.Game.CurrentKS ['W'])
                {
                    movement.Y += CosWorldRotation;
                    movement.X -= SinWorldRotation;
                }
                if (Game.Game.CurrentKS ['A'])
                {
                    movement.X -= CosWorldRotation;
                    movement.Y -= SinWorldRotation;
                }
                if (Game.Game.CurrentKS ['S'])
                {
                    movement.Y -= CosWorldRotation;
                    movement.X += SinWorldRotation;
                }
                if (Game.Game.CurrentKS ['D'])
                {
                    movement.X += CosWorldRotation;
                    movement.Y += SinWorldRotation;
                }

                if (movement != Vector2.Zero)
                {
                    movement.Normalize();
                    // thanks killraptor for the suggestion!
                    Vector2 movement1      = new Vector2(movement.X, 0f);
                    Vector2 movement2      = new Vector2(0f, movement.Y);
                    Vector2 finalPosition1 = Position + movement1 * time * MovementSpeed;
                    Vector2 finalPosition2 = Position + movement2 * time * MovementSpeed;
                    {
                        Block collidingWithPlayer = Game.BlockGrid [(int)finalPosition1.X, (int)finalPosition1.Y];
                        Door  d = collidingWithPlayer as Door;
                        if (collidingWithPlayer == null || (d != null && d.IsOpen))
                        {
                            X = finalPosition1.X;
                        }
                    }
                    {
                        Block collidingWithPlayer = Game.BlockGrid [(int)finalPosition2.X, (int)finalPosition2.Y];
                        Door  d = collidingWithPlayer as Door;
                        if (collidingWithPlayer == null || (d != null && d.IsOpen))
                        {
                            Z = finalPosition2.Y;
                        }
                    }
                }
            }

            foreach (InventoryItem item in Inventory)
            {
                if (item != null)
                {
                    item.Update(time);
                }
            }
        }
示例#6
0
        public static void Restore(MainGameState world)
        {
            if (last == null)
            {
                world.CurrentPlayer = null;
                world.LoadLevel(world.CurrentLevel);
            }
            else
            {
                world.Enemies.Clear();
                foreach (Enemy e in last.enemies)
                {
                    world.Enemies.Add(new Enemy(e.X, e.Z));
                }

                world.HealthPacks.Clear();
                // TODO: take healthpack timer into account
                foreach (HealthPack hp in last.healthPack)
                {
                    world.HealthPacks.Add(new HealthPack(hp.X, hp.Z));
                }

                world.Crates.Clear();
                // TODO: idem
                foreach (AmmoCrate ac in last.crates)
                {
                    world.Crates.Add(new AmmoCrate(ac.X, ac.Z));
                }

                world.Blocks.Clear();
                world.BlockGrid = new Block[world.WorldWidth, world.WorldHeight];
                for (int x = 0; x < world.WorldWidth; ++x)
                {
                    for (int z = 0; z < world.WorldWidth; ++z)
                    {
                        Block oldBlock = last.blockGrid [x, z];
                        Door  oldDoor  = oldBlock as Door;
                        if (oldBlock != null)
                        {
                            Block newBlock = null;
                            if (oldDoor != null)
                            {
                                // TODO: take full door texture into account
                                Door newDoor = new Door(oldDoor.X, oldDoor.Z, TextureTools.TextureDoor1, oldDoor.Upper, oldDoor.Lower);
                                if (oldDoor.IsOpen)
                                {
                                    newDoor.Open();
                                }
                                newBlock = newDoor;
                            }
                            else
                            {
                                newBlock = new Block(oldBlock.X, oldBlock.Z, oldBlock.Wall1.Texture);
                            }
                            world.BlockGrid [x, z] = newBlock;
                            world.Blocks.Add(newBlock);
                        }
                    }
                }

                world.DroppedWeapons.Clear();
                foreach (DroppedWeapon weapon in last.dropped)
                {
                    Weapon droppedWeapon = weapon.GetDropped();
                    world.DroppedWeapons.Add(new InventoryDroppedWeapon(droppedWeapon, weapon.X, weapon.Z, 0f, 0f));
                }

                for (int i = 0; i < world.CurrentPlayer.Inventory.Length; ++i)
                {
                    InventoryItem current = last.playerInventory [i];
                    if (current != null)
                    {
                        InventoryStack stack = current as InventoryStack;
                        if (stack != null)
                        {
                            DroppedItemInventoryStack newStack = new DroppedItemInventoryStack(stack.GetItemType(), stack.MaxValue);
                            newStack.Amount = stack.Amount;
                            world.CurrentPlayer.Inventory [i] = newStack;
                        }
                        else
                        {
                            world.CurrentPlayer.Inventory [i] = (InventoryItem)Activator.CreateInstance(current.GetType());
                            Gun g = current as Gun;
                            if (g != null)
                            {
                                ((Gun)world.CurrentPlayer.Inventory [i]).Ammo          = g.Ammo;
                                ((Gun)world.CurrentPlayer.Inventory [i]).MagazinesLeft = g.MagazinesLeft;
                            }
                        }
                    }
                }

                world.CurrentPlayer.X      = last.xPlayer;
                world.CurrentPlayer.Z      = last.zPlayer;
                world.CurrentPlayer.Health = last.healthPlayer;
            }
        }