コード例 #1
0
        /// <summary>
        /// Get ammo packs attributes
        /// </summary>
        /// <param name="xmlDoc">XML document that contains the attributes</param>
        public static void LoadAmmoPacksAttributes(XmlDocument xmlDoc, ref GameEntityList sceneObjects)
        {
            Debug.Assert(xmlDoc != null);
            Debug.Assert(sceneObjects != null);

            //try to get ammopack list
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("ammopacks");

            if (nodeList.Count > 0) //if XML has ammopacks...
            {
                //get list of ammopacks in scene
                XmlElement nodeElement = (XmlElement)nodeList.Item(0);

                //List available gun types in project
                List<String> guntypeNames = new List<String>(Enum.GetNames(typeof(GunType)));
                Int32[] guntypeValues = (Int32[])Enum.GetValues(typeof(GunType));

                //get all ammopacks definitions from XML file
                foreach (XmlElement xmle in nodeElement.ChildNodes)
                {
                    AmmoPack ap = new AmmoPack();
                    String assetName = null;

                    try
                    {
                        assetName = Convert.ToString(xmle.GetAttribute("name"));
                        Int32 position = guntypeNames.IndexOf(xmle.GetAttribute("guntype"));

                        ap.Quantity = Convert.ToInt32(xmle.GetAttribute("quantity"));
                        ap.GunType = (GunType)guntypeValues[position];
                    }
                    catch (FormatException fe)
                    {
                        Log.Write("BuildObjectsList: FormatExpcetion raised");
                        Log.Write(fe.Message);
                    }
                    catch (IndexOutOfRangeException ioore)
                    {
                        Log.Write("BuildObjectsList: IndexOutOfRangeException raised");
                        Log.Write(ioore.Message);
                    }
                    catch (InvalidCastException ice)
                    {
                        Log.Write("BuildObjectsList: InvalidCastExpcetion raised (did you forget some ammopack setting?)");
                        Log.Write(ice.Message);
                    }

                    sceneObjects.Add(assetName, ap);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Get med packs attributes
        /// </summary>
        /// <param name="xmlDoc">XML document that contains the attributes</param>
        public static void LoadMedPacksAttributes(XmlDocument xmlDoc, ref GameEntityList sceneObjects)
        {
            Debug.Assert(xmlDoc != null);
            Debug.Assert(sceneObjects != null);

            //try to get medpack list
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("medpacks");

            if (nodeList.Count > 0)
            {
                XmlElement nodeElement = (XmlElement)nodeList.Item(0);

                foreach (XmlElement xmle in nodeElement.ChildNodes)
                {
                    MedPack mp = new MedPack();
                    String assetName = null;

                    try
                    {
                        assetName = Convert.ToString(xmle.GetAttribute("name"));
                        mp.Quantity = Convert.ToInt32(xmle.GetAttribute("quantity"));
                    }
                    catch (InvalidCastException e)
                    {
                        Log.Write("BuildObjectsList: InvalidCastExpcetion raised (did you forget some medpack setting?)");
                        Log.Write(e.Message);
                    }

                    sceneObjects.Add(assetName, mp);
                }
            }
        }
コード例 #3
0
ファイル: Scene.cs プロジェクト: Oliverreason/xnafpsframework
        public override void Initialize()
        {
            float aspectRatio = this.device.Viewport.Width /
                                 this.device.Viewport.Height;

            this.collisionMesh = new CollisionMesh(sceneModel, 1);
            this.sceneObjects = new GameEntityList();

            XmlLoaderHelper.LoadPlayerAttributes(xmlDoc, aspectRatio, ref this.camera, ref this.player);
            XmlLoaderHelper.LoadGunsAttributes(xmlDoc, ref this.sceneObjects);
            XmlLoaderHelper.LoadAmmoPacksAttributes(xmlDoc, ref this.sceneObjects);
            XmlLoaderHelper.LoadMedPacksAttributes(xmlDoc, ref this.sceneObjects);
            XmlLoaderHelper.LoadEnemiesAttributes(this.Game, xmlDoc, ref this.enemies);

            //Create collisionbox for each dynamic entity
            if (this.sceneObjects != null)
            {
                foreach (String meshName in this.sceneObjects.Keys)
                {
                    ModelMesh mesh = null;

                    this.sceneModel.Meshes.TryGetValue(meshName, out mesh);

                    if (mesh == null)
                        continue;

                    GameEntity geOut = null;

                    this.sceneObjects.TryGetValue(meshName, out geOut);

                    if (geOut != null)
                    {
                        geOut.ModelMesh = mesh;
                        mesh.Tag = new object(); //only marks it as a dynamic object
                    }
                }

                foreach (String meshName in this.enemies.Keys)
                {
                    ModelMesh mesh = null;

                    this.sceneModel.Meshes.TryGetValue(meshName, out mesh);

                    if (mesh == null)
                        continue;

                    Enemy eOut = null;

                    this.enemies.TryGetValue(meshName, out eOut);

                    if (eOut != null)
                    {
                        eOut.ModelMesh = mesh;
                        Vector3 pos = mesh.BoundingSphere.Center;
                        eOut.Position = pos;
                        mesh.Tag = null; //don't render mesh marker
                    }
                }
            }

            base.Initialize();
        }
コード例 #4
0
        /// <summary>
        /// Get ammo packs attributes
        /// </summary>
        /// <param name="xmlDoc">XML document that contains the attributes</param>
        public static void LoadGunsAttributes(XmlDocument xmlDoc, ref GameEntityList sceneObjects)
        {
            Debug.Assert(xmlDoc != null);
            Debug.Assert(sceneObjects != null);

            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("guns");

            if (nodeList.Count > 0) //if XML has ammopacks...
            {
                //get list of guns in scene
                XmlElement nodeElement = (XmlElement)nodeList.Item(0);

                //List available gun types in project
                List<String> guntypeNames = new List<String>(Enum.GetNames(typeof(GunType)));
                Int32[] guntypeValues = (Int32[])Enum.GetValues(typeof(GunType));

                //get all guns definitions from XML file
                foreach (XmlElement xmle in nodeElement.ChildNodes)
                {
                    String assetName = null;
                    Gun g = new Gun();

                    try
                    {
                        assetName = Convert.ToString(xmle.GetAttribute("name"));
                        g.NumberOfBullets = Convert.ToInt32(xmle.GetAttribute("bullets"));

                        //traduz o string em um tipo enumerado
                        Int32 position = guntypeNames.IndexOf(xmle.GetAttribute("guntype"));
                        g.GunType = (GunType)guntypeValues[position];
                        g.IsRotatable = Convert.ToBoolean(xmle.GetAttribute("isRotatable"));

                        if (g.IsRotatable == true)
                            g.AngleOffset = Convert.ToSingle(xmle.GetAttribute("angleOffset"));

                        String spriteName = Convert.ToString(xmle.GetAttribute("spriteAssetName"));
                        String bulletAssetName = Convert.ToString(xmle.GetAttribute("bulletAssetName"));
                        Model bulletModel = SystemResources.Content.Load<Model>(@bulletAssetName);
                        int bulletLifeTime = Convert.ToInt32(xmle.GetAttribute("bulletLifeTime"));

                        g.Sprite = SystemResources.Content.Load<Texture2D>(@spriteName);
                        g.Bullet = new Bullet(bulletLifeTime, bulletModel.Meshes[0]);
                        g.Bullet.Damage = Convert.ToInt32(xmle.GetAttribute("bulletDamage"));
                        g.Bullet.BulletModel = bulletModel;
                    }
                    catch (InvalidCastException e)
                    {
                        Log.Write("BuildObjectsList: InvalidCastExpcetion raised (did you forget some ammopack setting?)");
                        Log.Write(e.Message);
                    }

                    sceneObjects.Add(assetName, g);
                }
            }
        }
コード例 #5
0
ファイル: Scene.cs プロジェクト: Oliverreason/xnafpsframework
        /// <summary>
        /// Check collisions for dynamic objects
        /// </summary>
        public virtual void CheckCollisions(GameTime gameTime)
        {
            GameEntityList deadObjects = new GameEntityList();

            CollisionBox cameraBox = new CollisionBox(this.camera.box);

            cameraBox.min += this.camera.world.Translation;
            cameraBox.max += this.camera.world.Translation;
            cameraBox.min.Y -= this.camera.head_height + this.camera.step_height;
            cameraBox.max.Y += this.camera.head_height + this.camera.step_height;

            ///Dynamic objects with player
            foreach (GameEntity ge in this.sceneObjects.Values)
            {
                if (cameraBox.BoxIntersect(ge.Box))
                {
                    if (ge is ICatchable)
                    {
                        ((ICatchable)ge).Attach(this.player);

                        deadObjects.Add(ge.ModelMesh.Name, ge);
                        ge.ModelMesh.Tag = null;
                    }
                    else if (ge is IAttachable)
                    {
                        if (ge is GameObjectEntity)
                            this.player.Add((GameObjectEntity)ge);
                    }
                }

                ge.Update(gameTime, this.collisionMesh);
            }

            foreach (GameEntity ge in deadObjects.Values)
            {
                this.sceneObjects.Remove(ge.ModelMesh.Name);
            }

            deadObjects.Clear();

            ///Enemies with player
            foreach (Enemy e in this.enemies.Values)
            {
                if (e.ActualAnimationState != GameEntityAnimationState.Die)
                {
                    if (cameraBox.BoxIntersect(e.Box))
                    {
                        this.SendMessage(player, e, GameEntityMessageType.Hit, gameTime);
                    }
                }
                e.Update(gameTime, this.collisionMesh);
            }

            ///Bullets with enemies, static objects...

            foreach (Bullet b in this.bullets)
            {
                Vector3 position = b.Position;
                Vector3 speed = b.Speed;

                Vector3 new_pos = position + speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                this.collisionMesh.PointMove(position, new_pos, 1.0f, 1.0f, 3, out position);

                //these are discartable, just to pass to the PointIntersect method as out parameters
                float intersect_distance;
                Vector3 intersect_position = new Vector3();
                Vector3 intersection_normal = new Vector3();

                //collision of bullets with static scenario (walls, etc.)
                if (this.collisionMesh.PointIntersect(position, new_pos, out intersect_distance, out intersect_position, out intersection_normal))
                {
                    b.Dead = true;
                }

                //collision of bullets with enemies
                foreach (Enemy e in this.enemies.Values)
                {
                    if (e.Box.PointInside(b.Position))
                    {
                        b.Dead = true;
                        SendMessage(b, e, GameEntityMessageType.Damage, gameTime);
                        if (e.Health >= 10)
                            SendMessage(b, e, GameEntityMessageType.Hit, gameTime);
                    }
                }

                b.Position = position;
                b.Speed = speed;
            }
        }