Beispiel #1
0
        // Private methods
        void ProcessMessage()
        {
            var ict = (IControlTank)TankAGame.ThisGame.Services.GetService(typeof(IControlTank));
            var map = (IMap)TankAGame.ThisGame.Services.GetService(typeof(IMap));
            var netStat = (INetStat)TankAGame.ThisGame.Services.GetService(typeof(INetStat));
            MessageType type = (MessageType)readBuffer.ReadByte();

            byte tankIndex;
            Point pos = new Point();
            Direction direction;
            byte num;
            byte team;
            byte cIndex;
            byte itemIndex;
            int health;
            int freezeTime;
            List<byte> survivalIndexes = new List<byte>();

            switch (type)
            {
                case MessageType.FireAck:
                    if (!ready)
                        break;
                    tankIndex = readBuffer.ReadByte();
                    pos.X = readBuffer.ReadInt16();
                    pos.Y = readBuffer.ReadInt16();
                    ict.Fire(tankIndex, pos);
                    break;
                case MessageType.StateUpdate:
                    if (!ready)
                        break;
                    num = readBuffer.ReadByte();
                    for (int i = 0; i < num; ++i)
                    {
                        tankIndex = readBuffer.ReadByte();
                        pos.X = readBuffer.ReadInt16();
                        pos.Y = readBuffer.ReadInt16();
                        direction = (Direction)readBuffer.ReadByte();
                        ict.ChangeTankInMapState(tankIndex, new Vector2(pos.X, pos.Y), direction);

                        survivalIndexes.Add(tankIndex);
                    }
                    ict.FilterSurvival(survivalIndexes);
                    break;
                case MessageType.CreateTank:
                    if (!ready)
                        break;
                    bool newCSRound = readBuffer.ReadBoolean();
                    if (newCSRound)
                    {
                        map.NewCSRound();
                        Point teamScore;
                        teamScore.X = readBuffer.ReadInt16();
                        teamScore.Y = readBuffer.ReadInt16();
                        netStat.ChangeTeamStat(teamScore);
                    }
                    num = readBuffer.ReadByte();
                    for (int i = 0; i < num; ++i)
                    {
                        TankState state = UserTank.DefaultUserTankState;
                        cIndex = readBuffer.ReadByte();
                        tankIndex = readBuffer.ReadByte();
                        pos.X = readBuffer.ReadInt16();
                        pos.Y = readBuffer.ReadInt16();
                        team = readBuffer.ReadByte();

                        state.tankIndex = tankIndex;
                        state.position = new Vector2(pos.X, pos.Y);
                        state.team = team;
                        if (cIndex != clientIndex)
                            state.playerIndex = 0xfe;
                        Factory.CreateTank(state, ict.AllTanksInMap());
                    }
                    break;
                case MessageType.TankFreezeAnn:
                    if (!ready)
                        break;
                    tankIndex = readBuffer.ReadByte();
                    freezeTime = readBuffer.ReadInt16();
                    ict.AckTankFreeze(tankIndex, freezeTime);
                    break;
                case MessageType.CreateItem:
                    if (!ready)
                        break;
                    {
                        ItemType itemType = (ItemType)readBuffer.ReadByte();
                        ItemState state = Item.DefaultItemState(itemType);
                        state.itemIndex = readBuffer.ReadByte();
                        state.position.X = readBuffer.ReadInt16();
                        state.position.Y = readBuffer.ReadInt16();
                        // Add waiting time time to client's item to avoid
                        // network latency.
                        state.waitingTime += 200;

                        Factory.CreateItem(state, map.AllItemsInMap());
                    }
                    break;
                case MessageType.ActiveItem:
                    if (!ready)
                        break;
                    itemIndex = readBuffer.ReadByte();
                    tankIndex = readBuffer.ReadByte();
                    map.AckItemActivation(itemIndex, tankIndex);
                    break;
                case MessageType.HealthAnn:
                    if (!ready)
                        break;
                    // Tank's health.
                    num = readBuffer.ReadByte();
                    for (int i = 0; i < num; ++i)
                    {
                        tankIndex = readBuffer.ReadByte();
                        health = readBuffer.ReadByte();
                        ict.AckTankHealthChanged(tankIndex, health);
                    }
                    // BlockSprite's health.
                    num = readBuffer.ReadByte();
                    for (int i = 0; i < num; ++i)
                    {
                        pos.X = readBuffer.ReadByte();
                        pos.Y = readBuffer.ReadByte();
                        health = readBuffer.ReadInt16();
                        map.AckBlockSpriteHealthChanged(pos, health);
                    }
                    break;
                case MessageType.StatAnn:
                    if (!ready)
                        break;
                    AckStat(netStat, true);
                    AckStat(netStat, false);
                    break;
                case MessageType.ClientInfoAnn:
                    clientIndex = readBuffer.ReadByte();
                    hostMode = (GameLogicMode)readBuffer.ReadByte();
                    string mapName = readBuffer.ReadString();
                    LoadingScreen.Load(TankAGame.ScreenManager, true, PlayerIndex.One, new GameplayScreen(GameLogicMode.Client, mapName));
                    TankAGame.ThisGame.Services.RemoveService(typeof(IClientMenu));
                    break;
                case MessageType.MapInfoAnn:
                    // Tank info.
                    num = readBuffer.ReadByte();
                    for (int i = 0; i < num; ++i)
                    {
                        tankIndex = readBuffer.ReadByte();
                        pos.X = readBuffer.ReadInt16();
                        pos.Y = readBuffer.ReadInt16();
                        team = readBuffer.ReadByte();
                        direction = (Direction)readBuffer.ReadByte();
                        Direction gunDirection = (Direction)readBuffer.ReadByte();
                        byte speed = readBuffer.ReadByte();
                        byte ammoSpeed = readBuffer.ReadByte();
                        byte armor = readBuffer.ReadByte();
                        int tbf = readBuffer.ReadInt16();
                        health = readBuffer.ReadByte();
                        int invulTime = readBuffer.ReadInt16();
                        AmmoType ammoType = (AmmoType)readBuffer.ReadByte();

                        TankState state = UserTank.DefaultUserTankState;
                        state.tankIndex = tankIndex;
                        state.position = new Vector2(pos.X, pos.Y);
                        state.tankInfo.gunDirection = gunDirection;
                        state.team = team;
                        state.playerIndex = 0xfe;
                        state.tankInfo.speed = speed;

                        state.tankInfo.armor = armor;
                        state.tankInfo.timeBetweenFires = tbf;
                        state.tankInfo.health = health;
                        state.tankInfo.invulnerableTime = invulTime;
                        state.tankInfo.ammoInfo = Ammo.DefaultAmmoInfo(ammoType);
                        state.tankInfo.ammoInfo.speed = ammoSpeed;

                        Factory.CreateTank(state, ict.AllTanksInMap());
                        ict.ChangeTankInMapState(tankIndex, state.position, direction);
                    }
                    // Item info.
                    num = readBuffer.ReadByte();
                    for (int i = 0; i < num; ++i)
                    {
                        ItemState state = new ItemState();
                        state.itemIndex = readBuffer.ReadByte();
                        state.type = (ItemType)readBuffer.ReadByte();
                        state.position.X = readBuffer.ReadInt16();
                        state.position.Y = readBuffer.ReadInt16();
                        state.waitingTime = readBuffer.ReadInt16();
                        state.activeTime = readBuffer.ReadInt16();
                        state.affectedTankIndex = readBuffer.ReadByte();
                        state.oldValue = readBuffer.ReadInt32();

                        Factory.CreateItem(state, map.AllItemsInMap());
                    }
                    // Stat info.
                    num = readBuffer.ReadByte();
                    for (int i = 0; i < num; ++i)
                    {
                        byte[] b = readBuffer.ReadBytes(4);
                        IPAddress ip = new IPAddress(b);
                        string name = readBuffer.ReadString();
                        team = readBuffer.ReadByte();
                        short kill = readBuffer.ReadInt16();
                        short dealth = readBuffer.ReadInt16();
                        netStat.NewClient(ip, name, team);
                        netStat.ChangeStat(ip, kill, true);
                        netStat.ChangeStat(ip, dealth, false);
                    }
                    RequestBlockSpriteInfo();
                    break;
                case MessageType.BlockSpriteInfoAnn:
                    num = readBuffer.ReadByte();
                    var remainingBlockSprite = new List<Point>();
                    for (int i = 0; i < num; ++i)
                    {
                        health = readBuffer.ReadInt16();
                        int j = readBuffer.ReadInt16();
                        for (int k = 0; k < j; ++k)
                        {
                            pos.X = readBuffer.ReadByte();
                            pos.Y = readBuffer.ReadByte();
                            remainingBlockSprite.Add(pos);
                            map.AckBlockSpriteHealthChanged(pos, health);
                        }
                    }
                    map.FilterRemainingBlockSprite(remainingBlockSprite);
                    ready = true;
                    break;
            }
        }
Beispiel #2
0
        // Constructors.
        static Item()
        {
            ItemState state = new ItemState()
            {
                type = ItemType.MaxTankSpeed,
                waitingTime = 10000,
                activeTime = 10000,
                affectedTankIndex = 0xff,
                itemIndex = 0xff,
            };
            defaultItemState.Add(state.type, state);
            state.type = ItemType.IncreaseTankSpeed;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MinTankSpeed;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MinimizeEnemiesTanksSpeed;
            defaultItemState.Add(state.type, state);

            state.type = ItemType.MaxAmmoSpeed;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.IncreaseAmmoSpeed;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MinAmmoSpeed;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MinimizeEnemiesTanksAmmoSpeed;
            defaultItemState.Add(state.type, state);

            state.type = ItemType.MaxArmor;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.IncreaseArmor;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MinArmor;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MinimizeEnemiesTanksArmor;
            defaultItemState.Add(state.type, state);

            state.type = ItemType.MinTBF;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.DecreaseTBF;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MaxTBF;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MaximizeEnemiesTBF;
            defaultItemState.Add(state.type, state);

            state.type = ItemType.Add50Health;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.MakeTankInvulnerable;
            state.activeTime = 5000;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.ChangeAmmoToRocket;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.ChangeAmmoToFireBall;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.ChangeAmmoToPhiTieu;
            defaultItemState.Add(state.type, state);
            state.type = ItemType.FreezeAllEnemiesTanks;
            defaultItemState.Add(state.type, state);
        }
Beispiel #3
0
        public Item(SpriteInfo si, ItemState state, SpriteDestroyedHandler handler)
            : base(si)
        {
            this.position = state.position;
            this.activeTime = state.activeTime;
            this.waitingTime = state.waitingTime;
            this.type = state.type;
            this.hasBeenDestroyed = handler;
            this.affectedTankIndex = state.affectedTankIndex;
            this.oldValue = state.oldValue;
            this.itemIndex = state.itemIndex;

            if (waitingTime <= 0)
                activated = true;

            occupiedArea.X = (int)position.X;
            occupiedArea.Y = (int)position.Y;
            occupiedArea.Width = FrameWidth;
            occupiedArea.Height = FrameHeight;
        }
Beispiel #4
0
        void ActivateUponEnemiesTanks(Dictionary<byte, Tank> tanks, byte myTeam, ItemType itType)
        {
            var map = (IMap)TankAGame.ThisGame.Services.GetService(typeof(IMap));
            foreach (var tank in tanks.Values)
                if (tank.Team != myTeam)
                {
                    ItemState state = new ItemState()
                    {
                        type = itType,
                        activeTime = activeTime,
                        waitingTime = 1,
                    };

                    Factory.CreateItem(state, map.AllItemsInMap()).ForceActive(tanks, tank.TankIndex);
                }
        }
Beispiel #5
0
        public static Item CreateItem(ItemState state, List<Item> items)
        {
            if (state.itemIndex == 0xff)
            {
                // Need generate item index.
                state.itemIndex = (byte)TankAGame.Random.Next(200);
                bool duplicated = false;
                do
                {
                    ++state.itemIndex;
                    if (state.itemIndex > 200)
                        state.itemIndex = 0;
                    duplicated = false;
                    foreach (var item in items)
                        if (item.ItemIndex == state.itemIndex)
                        {
                            duplicated = true;
                            break;
                        }
                } while (duplicated);
            }
            SpriteInfo si;
            if (state.itemIndex % 5 == 0)
            {
                si = itemSpriteInfo[ItemType.Random];
            }
            else
            {
                si = itemSpriteInfo[state.type];
            }
            Item it = new Item(si, state, destroyed);

            created(it);
            return it;
        }