コード例 #1
0
ファイル: MasterMap.cs プロジェクト: mp1011/Simple2DGame
        private IEnumerable <Rectangle> FindRegions()
        {
            Vector2 pointInMap = Vector2.Zero;

            List <Rectangle>  ret = new List <Rectangle>();
            Stack <Rectangle> regionsToProcess = new Stack <Rectangle>();

            var firstRegion = GetRegionAtLocation(Vector2.Zero);

            ret.Add(firstRegion);
            regionsToProcess.Push(firstRegion);

            while (regionsToProcess.Any())
            {
                var regionToProcess = regionsToProcess.Pop();

                var regionToTheRight = GetRegionAtLocation(regionToProcess.UpperRight.Translate(2, 1));
                var regionBelow      = GetRegionAtLocation(regionToProcess.BottomRight.Translate(-1, 2));
                var regionToTheLeft  = GetRegionAtLocation(regionToProcess.BottomLeft.Translate(-2, -1));
                var regionAbove      = GetRegionAtLocation(regionToProcess.UpperLeft.Translate(2, -1));

                var adjacentRegions = new Rectangle[] { regionToTheRight, regionBelow, regionToTheLeft, regionAbove }
                .Where(p => p.Width > 0 && !ret.Any(q => q.Equals(p))).ToArray();

                ret.AddRange(adjacentRegions);

                foreach (var adjacentRegion in adjacentRegions)
                {
                    regionsToProcess.Push(adjacentRegion);
                }
            }

            return(ret);
        }
コード例 #2
0
ファイル: MasterMap.cs プロジェクト: mp1011/Simple2DGame
        public MapTemplate Extract(int regionIndex)
        {
            var mapRegion      = MapRegions[regionIndex];
            var cellRegion     = new Rectangle(mapRegion.Left / CellSize.X, mapRegion.Top / CellSize.Y, mapRegion.Width / CellSize.X, mapRegion.Height / CellSize.Y);
            var extractedRange = Cells.ExtractBlock(cellRegion);

            return(new MapTemplate(extractedRange));
        }
コード例 #3
0
        public bool Intersects(Rectangle b)
        {
            if (Left <= b.Right
            && b.Left <= Right
            && Top <= b.Bottom
            && b.Top <= Bottom)
            return true;

              return false;
        }
コード例 #4
0
ファイル: GameOverText.cs プロジェクト: mp1011/Simple2DGame
        public GameOverText(QuickGameScene scene)
        {
            layer  = scene.InterfaceLayer;
            motion = new MotionManager(this);

            Text = new GameText(Fonts.BigFont, "GAME OVER", scene.InterfaceLayer);
            Text.StayRelativeTo(this, 0, 0, this);

            Position = new GameEngine.Rectangle();

            var screen = Engine.GetScreenSize();

            new PathMover(this, screen.BottomCenter, screen.Center);
        }
コード例 #5
0
ファイル: BaseState.cs プロジェクト: shusso/Strategy-Game
        public void ClickAction(Click action)
        {
            Point coordinatesInArea = _cameraSystem.GetAreaCoordinates(action.X, action.Y);

            // Area should be 2 x 2
            Rectangle rec = new Rectangle(coordinatesInArea.X - 1, coordinatesInArea.Y - 1, 2, 2);
            List<GameObject> selected = _objectSystem.Query(rec);

            // Only move the camera
            if (selected.Count == 0)
                _cameraSystem.SetTargetLocation((int)coordinatesInArea.X, (int)coordinatesInArea.Y);
            // TODO: Now choose first
            else if (selected.Count > 0)
                _cameraSystem.Target = selected[0];
        }
コード例 #6
0
ファイル: MathHelper.cs プロジェクト: shusso/Strategy-Game
        public static bool IntersectsWith(this Rectangle rec1, Rectangle rec2)
        {
            // Totally inside
            if (rec2.Contains(rec1))
                return true;

            // Any of the corners inside
            if (rec1.Contains(rec2.UpLeft))
                return true;
            if (rec1.Contains(rec2.UpRight))
                return true;
            if (rec1.Contains(rec2.DownLeft))
                return true;
            if (rec1.Contains(rec2.DownRight))
                return true;

            // Crosses vertically
            if (rec1.UpLeft.X <= rec2.UpRight.X && rec1.UpRight.X >= rec2.UpRight.X ||
                rec1.UpLeft.X <= rec2.UpLeft.X && rec1.UpRight.X >= rec2.UpLeft.X)
            {
                if (rec1.UpLeft.Y >= rec2.UpLeft.Y && rec1.DownLeft.Y <= rec2.DownLeft.Y)
                    return true;
            }

            // Crosses horizontally
            if (rec1.UpLeft.Y <= rec2.DownLeft.Y && rec1.DownLeft.Y >= rec2.DownLeft.Y ||
                rec1.UpLeft.Y <= rec2.UpLeft.Y && rec1.DownLeft.Y >= rec2.UpLeft.Y)
            {
                if (rec1.UpLeft.X >= rec2.UpLeft.X && rec1.UpRight.X <= rec2.UpRight.X)
                    return true;
            }

            return false;
        }
コード例 #7
0
ファイル: MathHelper.cs プロジェクト: shusso/Strategy-Game
        public static bool Contains(this Rectangle rec1, Rectangle rec2)
        {
            if (rec2.Location.X >= rec1.Location.X && rec2.Location.Y >= rec1.Location.Y &&
                rec2.DownRight.X <= rec1.DownRight.X && rec2.DownRight.Y <= rec1.DownRight.Y)
                return true;

            return false;
        }
コード例 #8
0
ファイル: ObjectSystem.cs プロジェクト: shusso/Strategy-Game
 // Return all gameobjects from QuadTree that are in rectangles area
 public List<GameObject> Query(Rectangle rec)
 {
     return _quadTree.Query(rec);
 }
コード例 #9
0
ファイル: ObjectSystem.cs プロジェクト: shusso/Strategy-Game
        public void Initialize(Size gameAreaSize)
        {
            _gameAreaRectangle = new Rectangle(new Point(0, 0), gameAreaSize);
            _quadTree = new QuadTree<GameObject>(new Size(1, 1), _gameAreaRectangle);
            GameObject.QuadTree = _quadTree;

            Size test = _gameAreaRectangle.Size;
            _gameAreaSize = gameAreaSize;

            CharacterGroup b1 = new CharacterGroup();
            CharacterGroup b2 = new CharacterGroup();

            int count1 = 3;

            for (int i = 0; i < count1; i++)
            {
                Character c1 = ObjectFactory.GetKnight(0, BehaviorType.NoAI);
                c1.ID = i;
                c1.Initialize(b1, b2);
                //c1.Location = new Point(100, 100);
                c1.Location = new Point((_gameAreaSize.Width / (count1 + 1)) * (i + 1),  c1.Size.Height * 2 + 100);
                //c1.SetBehavior(BehaviorType.NoAI);
                objects.Add(c1);
                _quadTree.Insert(c1);
            }

            int count = 10 ;

            for (int i = 0; i < count; i++)
            {
                Character c2 = ObjectFactory.GetPeasant(1, BehaviorType.SmartAttack);
                c2.Initialize(b2, b1);
                //c2.Location = new Point(200,100);
                c2.Location = new Point(1000 + (i + 1) * ((_gameAreaSize.Width - 1000) / (count +1)), _gameAreaSize.Height - c2.Size.Height * 2);
                objects.Add(c2);
                //c2.SetBehavior(BehaviorType.SmartAttack);
                _quadTree.Insert(c2);
            }

            GameObject house = ObjectFactory.House();
            house.Location = new Point(400, _gameAreaSize.Height - 400);
            _quadTree.Insert(house);

            GameObject f1 = ObjectFactory.FenceHorizontal();
            f1.Location = new Point(400, _gameAreaSize.Height - 700);
            _quadTree.Insert(f1);

            GameObject f2 = ObjectFactory.FenceVertical();
            f2.Location = new Point(800, _gameAreaSize.Height - 400);
            _quadTree.Insert(f2);

            init(b1, b2);
        }
コード例 #10
0
ファイル: BaseState.cs プロジェクト: shusso/Strategy-Game
        public void MouseUpAction(MouseUpAction action)
        {
            if (_objectSystem.DraggingObject == null)
            {
                return;
            }

            Point coordinatesInArea = _cameraSystem.GetAreaCoordinates(action.X, action.Y);

            // If Releas point is very close to original point do nothing
            if (MathHelper.Length(_objectSystem.DraggingObject.Location, coordinatesInArea) > _objectSystem.DraggingObject.Radius)
            {
                Character move = _objectSystem.DraggingObject as Character;

                // TODO: Check if enemies nearby
                // Area should be 5 x 5
                Rectangle rec = new Rectangle(coordinatesInArea.X - 1, coordinatesInArea.Y - 1, 5, 5);
                List<GameObject> selected = _objectSystem.Query(rec);

                // There should only be one
                if (selected.Count > 0 && move.Enemies.Contains(selected[0]))
                {
                    move.Target = selected[0];
                    move.TargetSetByUser = true;
                }
                else
                {
                    move.Target = null;
                    move.TargetSetByUser = false;
                    move.MoveTo = coordinatesInArea;
                }
            }

            _objectSystem.DraggingObject = null;
            _objectSystem.DraggingObjectDrawObject = null;
        }
コード例 #11
0
ファイル: BaseState.cs プロジェクト: shusso/Strategy-Game
        public void MouseDownAction(MouseDownAction action)
        {
            Point coordinatesInArea = _cameraSystem.GetAreaCoordinates(action.X, action.Y);

            // Area should be 2 x 2
            Rectangle rec = new Rectangle(coordinatesInArea.X - 1, coordinatesInArea.Y - 1, 2, 2);
            List<GameObject> selected = _objectSystem.Query(rec);

            if (selected.Count > 0)
                _objectSystem.DraggingObject = selected[0];
        }