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

📄 env.cs

📁 手机软件开发..手机软件开发..手机软件开发..手机软件开发..
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections.Generic;

namespace Skyiv.Ben.PushBox.Common
{
  /// <summary>
  /// 工作环境
  /// </summary>
  sealed class Env : IDisposable
  {
    DataFile db;       // 数据文件
    ConfigFile cfg;    // 配置文件
    string errorMsg;   // 错误信息
    string debugMsg;   // 调试信息
    bool isReplay;     // 是否正在回放
    Action active;     // 模式: 正常 新建 编辑 删除
    byte pen;          // 设计时的笔
    Bitmap img;        // 图形单元, 横向被均匀分为八份
    Stack<Step> stack; // 历史路线, 用于后退功能
    Size clientSize;   // 工作区域尺寸(以像素为单位)
    Size boxSize;      // 图形元素尺寸(以像素为单位)
    Point toPixel;     // 将要到达的位置(以像素为单位)
    Point worker;      // 当前工人位置(以单元格为单位)
    int pushSteps;     // 推动着箱子走的步数
    int levelOem;      // 原来的关数,仅用于“菜单 -> 数据 -> 设计 -> 新建”放弃后恢复现场

    public string ErrorMsg { get { return errorMsg; } }
    public string DebugMsg { get { return debugMsg; } }
    public string[] Groups { get { return cfg.Groups; } }
    public int Group { get { return cfg.Group; } set { cfg.Group = value; } }
    public int Level { get { return cfg.Levels[Group]; } set { cfg.Levels[Group] = value; } }
    public int LeveLOem { get { return levelOem; } }
    public int MaxLevel { get { return db.MaxLevel; } }
    public string Steps { get { return cfg.Steps; } }
    public Size LevelSize { get { return db.LevelSize; } }
    public Size ClientSize { set { clientSize = value; } }
    public Point ToPixel { set { toPixel = value; } }
    public int MaxLevelSize { get { return cfg.MaxLevelSize; } set { cfg.MaxLevelSize = value; } }
    public int StepDelay { get { return cfg.StepDelay; } set { cfg.StepDelay = value; } }
    public int ReplayDelay { get { return cfg.ReplayDelay; } set { cfg.ReplayDelay = value; } }
    public Action Active { get { return active; } set { active = value; } }
    public byte Pen { get { return pen; } set { pen = value; } }
    public bool HasError { get { return !string.IsNullOrEmpty(errorMsg); } }
    public bool HasWorker { get { return db.HasWorker; } }
    public bool CanUndo { get { return stack.Count != 0; } }
    public bool CanReplay { get { return db.IsFinished && !CanUndo; } }
    public bool IsSave { get { return cfg.IsSave; } set { cfg.IsSave = value; } }
    public bool IsFinish { get { return db.Tasks == db.Boths; } }
    public bool IsReplay { get { return isReplay; } set { isReplay = value; } }
    public bool IsDesign { get { return active != Action.None; } }

    public Env()
    {
      stack = new Stack<Step>();
      cfg = new ConfigFile();
      db = new DataFile();
      Init();
    }

    /// <summary>
    /// 状态栏信息
    /// </summary>
    public string StatusMessage
    {
      get
      {
        return HasError ? "请点击“菜单 -> 帮助 -> 错误信息”" : string.Format(
          "{0} {1}/{2} {3} {4} {5} [{6}] {7}",
          (active == Action.Create) ? '+' : (active == Action.Edit) ? '=' : isReplay ? "|/-\\"[stack.Count % 4] : '>',
          Level + 1, MaxLevel, Pub.ToString(LevelSize),
          IsDesign ? string.Format("{0}={1}", db.Boxs, db.Slots) : string.Format("{0}/{1}", db.Boths, db.Tasks),
          IsDesign ? Block.GetPenName(pen) : string.Format("{0}({1})", stack.Count, pushSteps),
          IsDesign ? (active == Action.Create ? "新建" : "编辑") : db.IsFinished ? 
          string.Format("{0}({1})", db.MovedSteps, db.PushedSteps) : string.Empty,
          db.GroupName);
      }
    }

    public void Dispose()
    {
      db.Dispose();
    }

    public void Init()
    {
      active = Action.None;
      pen = Block.Land;
      stack.Clear();
      SetExceptionMessage(null);
    }

    void SetExceptionMessage(Exception ex)
    {
      errorMsg = Pub.GetMessage(ex, false);
      debugMsg = Pub.GetMessage(ex, true);
    }

    /// <summary>
    /// 计算当使用标准箱子尺寸时主窗体客户区的尺寸
    /// </summary>
    /// <param name="statusBarHeight">状态条的高度</param>
    /// <returns>客户区的尺寸</returns>
    public Size GetClientSize(int statusBarHeight)
    {
      int width = (Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox24.Width / 8) * LevelSize.Width;
      int height = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox24.Height * LevelSize.Height + statusBarHeight;
      if (width < 240) width = 240;
      if (height < 48) height = 48;
      if (width > 1008) width = 1008;
      if (height > 672) height = 672;
      return new Size(width, height);
    }

    /// <summary>
    /// 根据客户区尺寸,计算箱子的尺寸,并相应设定要显示的图形单元
    /// </summary>
    public void SetBoxInfo()
    {
      if (HasError) return;
      if (LevelSize.IsEmpty) return;
      int rX = clientSize.Width / LevelSize.Width;
      int rY = clientSize.Height / LevelSize.Height;
      int r = Math.Min(rX, rY);
      if (r >= 24) img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox24;
      else if (r >= 20) img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox20;
      else if (r >= 16) img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox16;
      else img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox12;
      boxSize = new Size(img.Height, img.Width / 8);
    }

    /// <summary>
    /// 装入配置文件
    /// </summary>
    public void LoadConfig()
    {
      if (HasError) return;
      try
      {
        cfg.LoadConfig();
      }
      catch (Exception ex)
      {
        SetExceptionMessage(ex);
      }
    }

    /// <summary>
    /// 保存组信息到配置文件
    /// </summary>
    /// <param name="groups">组信息</param>
    public void SaveConfig(string[] groups)
    {
      if (HasError) return;
      try
      {
        cfg.SaveConfig(groups);
      }
      catch (Exception ex)
      {
        SetExceptionMessage(ex);
      }
    }

    /// <summary>
    /// 保存当前选项及当前走法到配置文件
    /// </summary>
    public void SaveConfig()
    {
      if (HasError) return;
      try
      {
        cfg.SaveConfig(stack.ToArray());
      }
      catch (Exception ex)
      {
        SetExceptionMessage(ex);
      }
    }

    /// <summary>
    /// 装入当前组信息
    /// </summary>
    public void LoadGroup()
    {
      if (HasError) return;
      try
      {
        db.LoadGroup(Groups[Group]);
      }
      catch (Exception ex)
      {
        SetExceptionMessage(ex);
      }
    }

    /// <summary>
    /// 装入当前关信息
    /// </summary>
    public void LoadLevel()
    {
      active = Action.None;
      if (HasError) return;
      try
      {
        db.LoadLevel(Level);
        worker = db.Worker;
        stack.Clear();
        pushSteps = 0;
        isReplay = false;
      }
      catch (Exception ex)
      {
        SetExceptionMessage(ex);
      }
    }

    /// <summary>
    /// 新建一关
    /// </summary>
    /// <param name="isCopy">是否复制当前关</param>
    /// <param name="size">新建关的尺寸</param>
    public void NewLevel(bool isCopy, Size size)
    {
      if (HasError) return;
      try
      {
        levelOem = Level;
        Level = MaxLevel;
        db.NewLevel(isCopy, size);
      }
      catch (Exception ex)
      {
        SetExceptionMessage(ex);
      }
    }

    /// <summary>
    /// 给出通关步骤
    /// </summary>
    /// <returns>通关步骤</returns>
    public string GetSteps()
    {
      string steps = "";
      if (!HasError)
      {
        try
        {
          steps = db.GetSteps(Level);
        }
        catch (Exception ex)
        {
          SetExceptionMessage(ex);
        }
      }
      return steps;
    }

    /// <summary>
    /// 记录通关步骤
    /// </summary>
    public void Record()
    {
      if (HasError) return;
      try
      {
        db.SaveLevel(Level, stack.ToArray(), pushSteps);
      }
      catch (Exception ex)
      {
        SetExceptionMessage(ex);
      }
    }

    /// <summary>

⌨️ 快捷键说明

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