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

📄 env.cs

📁 windows mobile上开发的推箱子小游戏,用C#写的,虽然只是DEMO版但大致功能已经实现
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
  2using System.Drawing;
  3using System.Collections.Generic;
  4
  5namespace Skyiv.Ben.PushBox.Common
  6{
  7  /**//// <summary>
  8  /// 工作环境
  9  /// </summary>
 10  sealed class Env : IDisposable
 11  {
 12    DataFile db;       // 数据文件
 13    ConfigFile cfg;    // 配置文件
 14    string errorMsg;   // 错误信息
 15    string debugMsg;   // 调试信息
 16    bool isReplay;     // 是否正在回放
 17    Action active;     // 模式: 正常 新建 编辑 删除
 18    byte pen;          // 设计时的笔
 19    Bitmap img;        // 图形单元, 横向被均匀分为八份
 20    Stack<Step> stack; // 历史路线, 用于后退功能
 21    Size clientSize;   // 工作区域尺寸(以像素为单位)
 22    Size boxSize;      // 图形元素尺寸(以像素为单位)
 23    Point toPixel;     // 将要到达的位置(以像素为单位)
 24    Point worker;      // 当前工人位置(以单元格为单位)
 25    int pushSteps;     // 推动着箱子走的步数
 26    int levelOem;      // 原来的关数,仅用于“菜单 -> 数据 -> 设计 -> 新建”放弃后恢复现场
 27
 28    public string ErrorMsg { get { return errorMsg; } }
 29    public string DebugMsg { get { return debugMsg; } }
 30    public string[] Groups { get { return cfg.Groups; } }
 31    public int Group { get { return cfg.Group; } set { cfg.Group = value; } }
 32    public int Level { get { return cfg.Levels[Group]; } set { cfg.Levels[Group] = value; } }
 33    public int LeveLOem { get { return levelOem; } }
 34    public int MaxLevel { get { return db.MaxLevel; } }
 35    public string Steps { get { return cfg.Steps; } }
 36    public Size LevelSize { get { return db.LevelSize; } }
 37    public Size ClientSize { set { clientSize = value; } }
 38    public Point ToPixel { set { toPixel = value; } }
 39    public int MaxLevelSize { get { return cfg.MaxLevelSize; } set { cfg.MaxLevelSize = value; } }
 40    public int StepDelay { get { return cfg.StepDelay; } set { cfg.StepDelay = value; } }
 41    public int ReplayDelay { get { return cfg.ReplayDelay; } set { cfg.ReplayDelay = value; } }
 42    public Action Active { get { return active; } set { active = value; } }
 43    public byte Pen { get { return pen; } set { pen = value; } }
 44    public bool HasError { get { return !string.IsNullOrEmpty(errorMsg); } }
 45    public bool HasWorker { get { return db.HasWorker; } }
 46    public bool CanUndo { get { return stack.Count != 0; } }
 47    public bool CanReplay { get { return db.IsFinished && !CanUndo; } }
 48    public bool IsSave { get { return cfg.IsSave; } set { cfg.IsSave = value; } }
 49    public bool IsFinish { get { return db.Tasks == db.Boths; } }
 50    public bool IsReplay { get { return isReplay; } set { isReplay = value; } }
 51    public bool IsDesign { get { return active != Action.None; } }
 52
 53    public Env()
 54    {
 55      stack = new Stack<Step>();
 56      cfg = new ConfigFile();
 57      db = new DataFile();
 58      Init();
 59    }
 60
 61    /**//// <summary>
 62    /// 状态栏信息
 63    /// </summary>
 64    public string StatusMessage
 65    {
 66      get
 67      {
 68        return HasError ? "请点击“菜单 -> 帮助 -> 错误信息”" : string.Format(
 69          "{0} {1}/{2} {3} {4} {5} [{6}] {7}",
 70          (active == Action.Create) ? '+' : (active == Action.Edit) ? '=' : isReplay ? "|/-\\"[stack.Count % 4] : '>',
 71          Level + 1, MaxLevel, Pub.ToString(LevelSize),
 72          IsDesign ? string.Format("{0}={1}", db.Boxs, db.Slots) : string.Format("{0}/{1}", db.Boths, db.Tasks),
 73          IsDesign ? Block.GetPenName(pen) : string.Format("{0}({1})", stack.Count, pushSteps),
 74          IsDesign ? (active == Action.Create ? "新建" : "编辑") : db.IsFinished ? 
 75          string.Format("{0}({1})", db.MovedSteps, db.PushedSteps) : string.Empty,
 76          db.GroupName);
 77      }
 78    }
 79
 80    public void Dispose()
 81    {
 82      db.Dispose();
 83    }
 84
 85    public void Init()
 86    {
 87      active = Action.None;
 88      pen = Block.Land;
 89      stack.Clear();
 90      SetExceptionMessage(null);
 91    }
 92
 93    void SetExceptionMessage(Exception ex)
 94    {
 95      errorMsg = Pub.GetMessage(ex, false);
 96      debugMsg = Pub.GetMessage(ex, true);
 97    }
 98
 99    /**//// <summary>
100    /// 计算当使用标准箱子尺寸时主窗体客户区的尺寸
101    /// </summary>
102    /// <param name="statusBarHeight">状态条的高度</param>
103    /// <returns>客户区的尺寸</returns>
104    public Size GetClientSize(int statusBarHeight)
105    {
106      int width = (Properties.Resources.PushBox24.Width / 8) * LevelSize.Width;
107      int height = Properties.Resources.PushBox24.Height * LevelSize.Height + statusBarHeight;
108      if (width < 240) width = 240;
109      if (height < 48) height = 48;
110      if (width > 1008) width = 1008;
111      if (height > 672) height = 672;
112      return new Size(width, height);
113    }
114
115    /**//// <summary>
116    /// 根据客户区尺寸,计算箱子的尺寸,并相应设定要显示的图形单元
117    /// </summary>
118    public void SetBoxInfo()
119    {
120      if (HasError) return;
121      if (LevelSize.IsEmpty) return;
122      int rX = clientSize.Width / LevelSize.Width;
123      int rY = clientSize.Height / LevelSize.Height;
124      int r = Math.Min(rX, rY);
125      if (r >= 24) img = Properties.Resources.PushBox24;
126      else if (r >= 20) img = Properties.Resources.PushBox20;
127      else if (r >= 16) img = Properties.Resources.PushBox16;
128      else img = Properties.Resources.PushBox12;
129      boxSize = new Size(img.Height, img.Width / 8);
130    }
131
132    /**//// <summary>
133    /// 装入配置文件
134    /// </summary>
135    public void LoadConfig()
136    {
137      if (HasError) return;
138      try
139      {
140        cfg.LoadConfig();
141      }
142      catch (Exception ex)
143      {
144        SetExceptionMessage(ex);
145      }
146    }
147
148    /**//// <summary>
149    /// 保存组信息到配置文件
150    /// </summary>
151    /// <param name="groups">组信息</param>
152    public void SaveConfig(string[] groups)
153    {
154      if (HasError) return;
155      try
156      {
157        cfg.SaveConfig(groups);
158      }
159      catch (Exception ex)
160      {
161        SetExceptionMessage(ex);
162      }
163    }
164
165    /**//// <summary>
166    /// 保存当前选项及当前走法到配置文件
167    /// </summary>
168    public void SaveConfig()
169    {
170      if (HasError) return;
171      try
172      {
173        cfg.SaveConfig(stack.ToArray());
174      }
175      catch (Exception ex)
176      {
177        SetExceptionMessage(ex);
178      }
179    }
180
181    /**//// <summary>
182    /// 装入当前组信息
183    /// </summary>
184    public void LoadGroup()
185    {
186      if (HasError) return;
187      try
188      {
189        db.LoadGroup(Groups[Group]);
190      }
191      catch (Exception ex)
192      {
193        SetExceptionMessage(ex);
194      }
195    }
196
197    /**//// <summary>
198    /// 装入当前关信息
199    /// </summary>
200    public void LoadLevel()
201    {
202      active = Action.None;
203      if (HasError) return;
204      try
205      {
206        db.LoadLevel(Level);
207        worker = db.Worker;
208        stack.Clear();
209        pushSteps = 0;
210        isReplay = false;
211      }
212      catch (Exception ex)
213      {
214        SetExceptionMessage(ex);
215      }
216    }
217
218    /**//// <summary>
219    /// 新建一关
220    /// </summary>
221    /// <param name="isCopy">是否复制当前关</param>
222    /// <param name="size">新建关的尺寸</param>
223    public void NewLevel(bool isCopy, Size size)
224    {
225      if (HasError) return;
226      try
227      {
228        levelOem = Level;
229        Level = MaxLevel;
230        db.NewLevel(isCopy, size);
231      }
232      catch (Exception ex)
233      {
234        SetExceptionMessage(ex);
235      }
236    }
237
238    /**//// <summary>
239    /// 给出通关步骤
240    /// </summary>
241    /// <returns>通关步骤</returns>
242    public string GetSteps()
243    {
244      string steps = "";
245      if (!HasError)
246      {
247        try
248        {
249          steps = db.GetSteps(Level);
250        }
251        catch (Exception ex)
252        {
253          SetExceptionMessage(ex);
254        }
255      }
256      return steps;
257    }
258
259    /**//// <summary>
260    /// 记录通关步骤
261    /// </summary>
262    public void Record()
263    {
264      if (HasError) return;
265      try
266      {
267        db.SaveLevel(Level, stack.ToArray(), pushSteps);
268      }
269      catch (Exception ex)
270      {
271        SetExceptionMessage(ex);
272      }
273    }
274
275    /**//// <summary>

⌨️ 快捷键说明

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