⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 level.cs

📁 说明如何使用托管 Direct3D Mobile 创建一个简单的二维游戏。
💻 CS
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using GraphicsLibrary;
using InputLibrary;

namespace GameApp
{
    /// <summary>
    /// Defines a level in the game and all of its contents.
    /// </summary>
    public class Level : IDisposable
    {
        /// <summary>
        /// Gets the list of AI definitions for the level.
        /// </summary>
        public ArrayList AIList { get { return ai; } }
        private ArrayList ai = new ArrayList();

        /// <summary>
        /// Gets the list of world objects in the level.
        /// </summary>
        public ArrayList WorldObjects { get { return worldObjectsValue; } }
        private ArrayList worldObjectsValue = new ArrayList();

        /// <summary>
        /// List of layers in the level.
        /// </summary>
        private ArrayList layers = new ArrayList();

        /// <summary>
        /// List of images used by layers in the level.
        /// </summary>
        private ArrayList images = new ArrayList();

        /// <summary>
        /// Gets the list of animation definitions used in the level.
        /// </summary>
        public ArrayList AnimationList { get { return animations; } }
        private ArrayList animations = new ArrayList();

        /// <summary>
        /// Graphics instance used by the game.
        /// </summary>
        private IGraphics graphics = null;

        /// <summary>
        /// Width of the screen portion into which the level is drawn.
        /// </summary>
        public int ViewWidth { get { return graphics.ScreenWidth; } }

        /// <summary>
        /// Gets the buffer area on the left and right screen edges where the
        /// player is not allowed to travel.
        /// </summary>
        public float ScreenEdgeBuffer { get { return screenEdgeBufferValue; } }
        private float screenEdgeBufferValue = 0;

        /// <summary>
        /// Gets the player from the world object list.
        /// </summary>
        public Player Player { get { return (Player)worldObjectsValue[0]; } }

        /// <summary>
        /// Gets the foreground layer.  The foreground is actually the closer
        /// of the backgrounds but is still behind the player and world
        /// objects.
        /// </summary>
        public Layer Foreground { get { return ((Layer)layers[1]); } }

        /// <summary>
        /// Gets the background layer.
        /// </summary>
        public Layer Background { get { return ((Layer)layers[0]); } }

        /// <summary>
        /// Rate at which the layer of the world that the player is on is
        /// scrolling (pixels/second).
        /// </summary>
        public float ScrollRate { get { return Foreground.ScrollRate; } }

        /// <summary>
        /// Maximum Y world location. 
        /// </summary>
        public float MaxWorldY
        {
            get
            {
                return (Foreground.ScreenY - Background.ScreenY) +
                    Foreground.Height;
            }
        }

        /// <summary>
        /// X location of the screen in the world.
        /// </summary>
        public float WorldX { get { return Foreground.WorldX; } }

        /// <summary>
        /// Y location of the screen in the world.
        /// </summary>
        public float WorldY { get { return 0.0F; } }

        /// <summary>
        /// X screen location of the world
        /// </summary>
        public float DrawX { get { return 0.0F; } }

        /// <summary>
        /// Y screen location of the world.
        /// </summary>
        public float DrawY { get { return (float)Background.ScreenY; } }

        /// <summary>
        /// Rate of gravity in pixels/second.
        /// </summary>
        public float Gravity { get { return gravityValue; } }
        private float gravityValue;

        /// <summary>
        /// Rate, in seconds, to check the framerate.
        /// </summary>
        public float FrameRateCheckRate
        {
            get { return FrameRateCheckRateValue; }
        }

        private float FrameRateCheckRateValue = 1.0F;

        /// <summary>
        /// Gets if the level is done and waiting for a reset.
        /// </summary>
        public bool Done { get { return Player.Dead && Player.Animation.Done; } }

        /// <summary>
        /// Initialize the level.
        /// </summary>
        /// <param name="ds">DataSet containing level data</param>
        /// <param name="graphics">Valid Graphics object</param>
        public Level(DataSet ds, IGraphics graphics)
        {
            // Store graphics
            Debug.Assert(graphics != null,
                "Level.Level: Invalid Graphics object");

            this.graphics = graphics;

            // Validate the DataSet
            Debug.Assert(ds != null && ds.Tables != null,
                "Level.Level: Invalid DataSet");

            // General
            DataTable dt = ds.Tables["General"];
            DataRow drGen = dt.Rows[0];

            screenEdgeBufferValue = float.Parse(
                (string)(drGen["ScreenEdgeBuffer"]), 
                CultureInfo.InvariantCulture);
            FrameRateCheckRateValue = float.Parse(
                (string)(drGen["FrameRateCheckRate"]), 
                CultureInfo.InvariantCulture);
            gravityValue = float.Parse((string)(drGen["Gravity"]), 
                CultureInfo.InvariantCulture);

            // Images
            Debug.Assert(ds.Tables["Image"] != null &&
                ds.Tables["Image"].Rows != null,
                "Level.Level: No images specified in level data");

            dt = ds.Tables["Image"];
            foreach (DataRow dr in dt.Rows)
            {
                IBitmap bmp = graphics.CreateBitmap(GameMain.GetFullPath(
                    @"Data\Level\" + (string)dr["FileName"]),
                    bool.Parse((string)dr["Transparency"]));
                Debug.Assert(bmp != null,
                    string.Format(CultureInfo.InvariantCulture,
                    "Failed to initialize bitmap {0}",
                    @"Data\Level\" + (string)dr["FileName"]));

                images.Add(bmp);
            }

            // Layers
            dt = ds.Tables["Layer"];
            foreach (DataRow dr in dt.Rows)
            {
                layers.Add(new Layer(dr, images));
            }

            Debug.Assert(layers.Count >= 1,
                "Level does not contain 2 or more layers");

            ds = null;

            // AI
            DataSet dsAI = new DataSet();
            Debug.Assert(dsAI != null && dsAI.Tables != null,
                "Level.Level: Failed to initialize AI DataSet");

            dsAI.Locale = CultureInfo.InvariantCulture;
            dsAI.ReadXml(GameMain.GetFullPath(@"Data\AI\ai.xml"));
            dt = dsAI.Tables["Definition"];
            Debug.Assert(dt != null && dt.Rows != null,
                "Level.Level: Failed to load AI DataTable");

            foreach (DataRow dr in dt.Rows)
            {
                AI ai = AIHandler.Create(dr);
                Debug.Assert(ai != null,
                    "Level.Level: Failed to initialize AI");
                this.ai.Add(ai);
            }
            dsAI = null;

            DataSet dsAnimations = new DataSet();
            Debug.Assert(dsAnimations != null && dsAnimations.Tables != null,
                "Level.Level: Failed to initialize animation DataSet");

            dsAnimations.Locale = CultureInfo.InvariantCulture;

            // Animations
            dsAnimations.ReadXml(GameMain.GetFullPath(@"Data\Animations\animations.xml"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -