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

📄 board.cs

📁 一个由.NET WindowsForm上开发的华容道游戏
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace HuaRongDao
{
    class Board: Panel
    {
        private Size _logicSize = Size.Empty;
        private List<Character> _characters = new List<Character>();
        private Rectangle _logicExit = Rectangle.Empty;
        private Rectangle _logicBounds = Rectangle.Empty;
        private Character _focusedCharacter = null;
        private int _sizeRate = 50;

        public Board()
        {
            _logicSize = new Size(4, 5);
            _logicExit = new Rectangle(2, 5, 2, 1);

            _logicBounds = new Rectangle(new Point(1, 1), LogicSize);

            Size = new Size(LogicSize.Width * SizeRate, LogicSize.Height * SizeRate);
        }

        public Board(Size logicSize, Rectangle logicExit)
        {
            _logicSize = logicSize;
            _logicExit = logicExit;

            Size = new Size(LogicSize.Width * SizeRate, LogicSize.Width * SizeRate);
        }

        public int SizeRate
        {
            get { return _sizeRate; }
        }

        public Size LogicSize 
        {
            get { return _logicSize; }
        }

        public Rectangle LogicExit
        {
            get { return _logicExit; }
        }

        public Rectangle LogicBounds
        {
            get { return _logicBounds; } 
        }

        public Character FocusedCharacter
        {
            get { return _focusedCharacter; }
        }

        public Character GetCharacter(string name)
        {
            foreach (Character c in _characters)
            {
                if (c.LogicName == name)
                {
                    return c;
                }
            }

            return null;
        }

        public Character GetCharacter(Point point)
        {
            foreach (Character c in _characters)
            {
                if (c.LogicBounds.Contains(point))
                {
                    return c;
                }
            }

            return null;
        }

        public bool SomeoneIsHere(Rectangle rectangle)
        {
            return OthersIsHere(rectangle, null);
        }

        public bool OthersIsHere(Rectangle rectangle, Character character)
        {
            foreach (Character c in _characters)
            {
                if (c != character)
                {
                    if (rectangle.IntersectsWith(c.LogicBounds))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public Point GetLogicalPoint(Point point)
        {
            return new Point(point.X / this.SizeRate + 1, point.Y / this.SizeRate + 1);
        }

        public void AddCharacter(Character character)
        {
            if (this.GetCharacter(character.LogicName) != null)
            {
                throw new Exception("LogicName is duplicated");
            }
            if (this.SomeoneIsHere(new Rectangle(character.LogicLocation, character.LogicSize)))
            {
                throw new Exception("Someone is here already");
            }
            _characters.Add(character);
            character.LogicLocationChanged += new EventHandler(character_LogicLocationChanged);
            character.Click += new EventHandler(character_Click);
            ResetCharacterBounds(character);
            this.Controls.Add(character);
        }

        void character_Click(object sender, EventArgs e)
        {
            if (_focusedCharacter != null)
            {
                _focusedCharacter.IsFocused = false;
            }
            _focusedCharacter = (Character)sender;
            _focusedCharacter.IsFocused = true;

        }

        private void ResetCharacterBounds(Character character)
        {
            character.Size = new Size(character.LogicSize.Width * this.SizeRate, character.LogicSize.Height * this.SizeRate);
            character.Location = new Point((character.LogicLocation.X - 1) * this.SizeRate, (character.LogicLocation.Y - 1) * this.SizeRate);
        }
        private void character_LogicLocationChanged(object sender, EventArgs e)
        {
            Character character = (Character)sender;
            character.Location = new Point((character.LogicLocation.X - 1) * this.SizeRate, (character.LogicLocation.Y - 1) * this.SizeRate);
        }

    }
}

⌨️ 快捷键说明

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