Пример #1
0
        public void Update()
        {
            if (Session != null)
            {
                try
                {
                    Session.Update();
                }
                catch (Exception e)
                {
                    e.ToString();
                }
                foreach (LocalNetworkGamer gamer in Session.LocalGamers)
                {
                    #region Send network data
                    //write YOUR player data
                    gamer.Tag = game1.Player;
                    packetWriter.Write((Int16)0);
                    packetWriter.Write(game1.Player.Pos);

                    //write HOST data
                    if (Session.IsHost)
                    {
                        packetWriter.Write((Int16)1);   //1 = enemy updates
                        for (int i = 0; i < Game1.MAX_ENEMIES; ++i)
                        {
                            packetWriter.Write(game1.enemy[i].Pos);
                            packetWriter.Write(game1.enemy[i].CurrentAnimation);
                            packetWriter.Write(game1.enemy[i].CurrentFrame);
                            packetWriter.Write((Int16)game1.enemy[i].Flag);
                        }
                    }
                    foreach (NetworkGamer remoteGamer in Session.RemoteGamers)
                        gamer.SendData(packetWriter, SendDataOptions.InOrder, remoteGamer);
                    packetWriter.Flush();
                    packetWriter = new PacketWriter();
                    #endregion
                    #region Get network data
                    while (gamer.IsDataAvailable)
                    {
                        NetworkGamer sender;
                        gamer.ReceiveData(packetReader, out sender);
                        if (!sender.IsLocal)
                        {
                            LivingEntity remotePlayer = sender.Tag as LivingEntity;
                            if (remotePlayer == null)
                            {
                                Vector2 Pos = new Vector2(Map.MapSize / 2, Map.MapSize - 8);
                                remotePlayer = new LivingEntity(Pos);
                            }
                            while (packetReader.Position < packetReader.Length)
                                switch (packetReader.ReadInt16())   //get message type
                                {
                                    case 0: //remote player pos update
                                        {
                                            remotePlayer.Pos = packetReader.ReadVector2();
                                            break;
                                        }

                                    case 1:
                                        {
                                            for (int i = 0; i < Game1.MAX_ENEMIES; ++i)
                                            {
                                                game1.enemy[i].Pos = packetReader.ReadVector2();
                                                game1.enemy[i].CurrentAnimation = packetReader.ReadInt16();
                                                game1.enemy[i].CurrentFrame = packetReader.ReadInt16();
                                                game1.enemy[i].Flag = packetReader.ReadInt16();
                                            }
                                            break;
                                        }
                                    case 2:
                                        {   //receive map!
                                            for (int i = 0; i < Map.MapSize; ++i)
                                                for (int j = 0; j < Map.MapSize; ++j)
                                                    game1.GameMap.MapArray[i, j] = packetReader.ReadInt16();
                                            break;
                                        }
                                    case 3: //only host responds to attacks
                                        {
                                            if (Session.IsHost)
                                            {
                                                int e = packetReader.ReadInt16();
                                                remotePlayer.Attack(game1.enemy[e]);
                                                if (game1.enemy[e].CurrentHealth < 1)    //death
                                                {
                                                    packetWriter.Write((Int16)4);
                                                    Int16 XPToAdd = (Int16)(game1.enemy[e].Level * (game1.enemy[e].Dexterity +
                                                        game1.enemy[e].Speed + game1.enemy[e].Strength) / 3);
                                                    packetWriter.Write((Int16)XPToAdd);
                                                    game1.Player.XP += XPToAdd;
                                                    game1.enemy[e].Pos.X = 236 + game1.rand.Next() % 40;
                                                    game1.enemy[e].Pos.Y = 484 + game1.rand.Next() % 20;
                                                }
                                            }
                                            break;
                                        }
                                    case 4: //xp additions
                                        {
                                            game1.Player.XP += packetReader.ReadInt16();
                                            break;
                                        }
                                    case 5:
                                        {
                                            if(gamer.Gamertag.GetHashCode() == packetReader.ReadInt32())
                                                game1.Player.CurrentHealth -= packetReader.ReadInt16();
                                            else
                                                packetReader.ReadInt16();
                                            break;
                                        }
                                    case 6:
                                        {
                                            remotePlayer.Level = packetReader.ReadInt16();
                                            remotePlayer.Dexterity = packetReader.ReadInt16();
                                            remotePlayer.Speed = packetReader.ReadInt16();
                                            remotePlayer.Strength = packetReader.ReadInt16();
                                            remotePlayer.CurrentAnimation = packetReader.ReadInt16();
                                            remotePlayer.CurrentFrame = packetReader.ReadInt16();
                                            remotePlayer.isAttacking = packetReader.ReadBoolean();
                                            if (remotePlayer.isAttacking)
                                            {
                                                remotePlayer.CurrentAnimationAttack = packetReader.ReadInt16();
                                                remotePlayer.CurrentFrameAttack = packetReader.ReadInt16();
                                            }
                                            break;
                                        }
                                }
                            sender.Tag = remotePlayer;
                        }
                    }
                    #endregion
                }
            }
        }
Пример #2
0
 public void PlayerStatsUpdate(LivingEntity Player)
 {
     packetWriter.Write((Int16)6);
     packetWriter.Write(Player.Level);
     packetWriter.Write(Player.Dexterity);
     packetWriter.Write(Player.Speed);
     packetWriter.Write(Player.Strength);
     packetWriter.Write(Player.CurrentAnimation);
     packetWriter.Write(Player.CurrentFrame);
     packetWriter.Write(Player.isAttacking);
     if (Player.isAttacking)
     {
         packetWriter.Write(Player.CurrentAnimationAttack);
         packetWriter.Write(Player.CurrentFrameAttack);
     }
 }
Пример #3
0
        public Int16 Attack(LivingEntity opponent)
        {
            if (Environment.TickCount - TimeOfLastAttack > 5000 / Speed)
            {
                TimeOfLastAttack = Environment.TickCount;
                Int16 Damage = 0;
                try
                {
                    Damage = (Int16)Math.Ceiling((double)Strength / (opponent.Dexterity / 3));
                }
                catch (Exception e)
                {
                    e.ToString();
                }
                opponent.CurrentHealth -= Damage;

                return Damage;
            }
            return 0;
        }
Пример #4
0
        public void DoSaveGame(LivingEntity Entity)
        {
            // Create the data to save.
            SaveGameData data = new SaveGameData();
            data.Level = Entity.Level;
            data.XP = Entity.XP;
            data.Pos = Entity.Pos;
            data.Strength = Entity.Strength;
            data.Speed = Entity.Speed;
            data.Dexterity = Entity.Dexterity;
            data.Health = Entity.Health;

            // Open a storage container.
            StorageContainer container = Device.OpenContainer("Savegames");

            // Get the path of the save game.
            string filename = Path.Combine(container.Path, "savegame.sav");

            // Open the file, creating it if necessary.
            FileStream stream = File.Open(filename, FileMode.OpenOrCreate);

            // Convert the object to XML data and put it in the stream.
            XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
            serializer.Serialize(stream, data);

            // Close the file.
            stream.Close();

            // Dispose the container, to commit changes.
            container.Dispose();
        }
Пример #5
0
        private void RenderEntityTexture(LivingEntity pLE)
        {
            //Description: Renders a texture from world coordinates relative to player (so player is always in middle)
            //the magic interpolator
            Vector2 RenderPos = RenderPosCalc(pLE.Pos);
    
            Rectangle RenderRect = new Rectangle((int)RenderPos.X - LivingEntity.LivingEntityRadius,
                (int)RenderPos.Y - LivingEntity.LivingEntityRadius,
                LivingEntity.LivingEntityRadiusT2,
                LivingEntity.LivingEntityRadiusT2);

            if (pLE.Velocity.X > .005f || pLE.Velocity.Y > .005f ||
                pLE.Velocity.X < -.005f || pLE.Velocity.Y < -.005f ||
                pLE.isAttacking)
                spriteBatch.Draw(pLE.texAnimation[pLE.CurrentAnimation][pLE.CurrentFrame], RenderRect, Color.White);
            else
                spriteBatch.Draw(pLE.texAnimation[pLE.CurrentAnimation][0], RenderRect, Color.White);
        }
Пример #6
0
        private Int16 IsCollision(LivingEntity Entity)
        {
            if (Entity.Pos.X < 7) Entity.Pos.X = 7;
            if (Entity.Pos.X > Map.MapSize - 6) Entity.Pos.X = Map.MapSize - 7;
            if (Entity.Pos.Y < 7) Entity.Pos.Y = 7;
            if (Entity.Pos.Y > Map.MapSize - 7) Entity.Pos.Y = Map.MapSize - 7;

            const float COLLISION_DISTANCE = 0.6f;
            Int16 MapInt;
            try
            {
                MapInt = GameMap.MapArray[(int)Math.Floor(Entity.Pos.X + 0.5f), (int)Math.Floor(Entity.Pos.Y + 0.5f)];
            }
            catch (Exception e)
            {
                //array out of bounds probably
                MapInt = 2;
                e.ToString();
            }
            if ((MapInt == 2) ||    //any other impass
                (MapInt == 5) ||
                (MapInt == 4) ||
                MapInt > 99)
                return MapInt;

            /*foreach (NetworkGamer gamer in Network.Session.RemoteGamers)
            {
                LivingEntity player = gamer.Tag as LivingEntity;
                if (player != null)
                    if (Fast2d(Entity.Pos, Player.Pos) < COLLISION_DISTANCE)
                        return true;
            }*/

            for (int i = 0; i < MAX_ENEMIES; ++i)
            {
                float Distance = Fast2d(enemy[i].Pos, Entity.Pos);
                if (Distance < COLLISION_DISTANCE && Distance != 0.0f)
                    return 1;
            }
            return 0;
        }