Ejemplo n.º 1
0
        private static void HandleMouse(Camera camera, Town town)
        {
            Rectangle mapRect = new Rectangle(0, 0, TileMap.Width * Engine.TileWidth, TileMap.Height * Engine.TileHeight);

            Point tilePos = (InputHelper.MousePosition + camera.Position).ConvertPositionToCell();
            Tile  selectedTile;

            if (tilePos.X >= 0 && tilePos.X < TileMap.Width && tilePos.Y >= 0 && tilePos.Y < TileMap.Height)
            {
                selectedTile = TileMap.Tiles[tilePos.X, tilePos.Y];

                bool leftClicked  = InputHelper.LeftMouseButtonClicked() && InputHelper.IsMouseOn(mapRect);
                bool rightClicked = InputHelper.RightMouseButtonClicked() && InputHelper.IsMouseOn(mapRect);
                bool leftDown     = InputHelper.LeftMouseButtonDown() && InputHelper.IsMouseOn(mapRect);

                _tilesToDraw = new Dictionary <Tile, Color>();


                //Handle selection
                if (rightClicked)
                {
                    _selectedUnit = selectedTile.GetFirstObject <Villager>(true);
                }


                //Handle mouse click on map
                switch (_state)
                {
                case ClickState.Tree:
                {
                    _tilesToDraw.Add(selectedTile, selectedTile.IsSolid ? Color.Red : Color.White);
                    if (leftDown && !selectedTile.IsSolid)
                    {
                        selectedTile.AddObject(new TownSimulator.Scenery.Tree());
                        foreach (KeyValuePair <int, Villagers.Villager> v in town.Villagers)
                        {
                            v.Value.Warn(EnvironmentEvent.TreeGrowed);
                        }
                    }

                    break;
                }

                case ClickState.Woodcutter:
                {
                    _tilesToDraw.Add(selectedTile, selectedTile.IsSolid ? Color.Red : Color.White);
                    if (leftClicked && !selectedTile.IsSolid)
                    {
                        selectedTile.AddObject(new TownSimulator.Villagers.Woodcutter(town));
                    }

                    break;
                }

                case ClickState.Rock:
                {
                    _tilesToDraw.Add(selectedTile, selectedTile.IsSolid ? Color.Red : Color.White);
                    if (leftDown && !selectedTile.IsSolid)
                    {
                        selectedTile.AddObject(new GameObject()
                            {
                                ObjectSprite = new Sprite(6), IsSolid = true
                            });
                    }

                    break;
                }

                case ClickState.House:
                {
                    //TODO: Problem because the house is added in the town in the constructor
                    House house = new House(town);

                    Size size = house.GetTileSize();
                    //if (DrawingUtils.DrawingRectangle)
                    //{
                    List <Tile> tiles = TileMap.GetTileArea(selectedTile.Position, size);
                    if (tiles != null)
                    {
                        foreach (Tile t in tiles)
                        {
                            _tilesToDraw.Add(t, t.IsSolid ? Color.Red : Color.White);
                        }
                    }
                    //}

                    if (leftClicked && !selectedTile.IsSolid)
                    {
                        selectedTile.AddObject(house);

                        foreach (KeyValuePair <int, Villagers.Villager> v in town.Villagers)
                        {
                            v.Value.Warn(EnvironmentEvent.BuildingAdded);
                        }
                    }
                    break;
                }
                    //case ClickState.LumberMill:
                    //    {
                    //        //Problem
                    //        LumberMill lm = new LumberMill(town);

                    //        Size size = lm.GetTileSize();
                    //        List<Tile> tiles = TileMap.GetTileArea(selectedTile.Position, size.Width, size.Height);
                    //        if (tiles != null)
                    //        {
                    //            foreach (Tile t in tiles)
                    //            {
                    //                _tilesToDraw.Add(t, t.IsSolid ? Color.Red : Color.White);
                    //            }

                    //            if (leftClicked && !selectedTile.IsSolid)
                    //            {
                    //                selectedTile.AddObject(lm);
                    //            }
                    //        }

                    //        break;
                    //    }
                }
            }
        }
Ejemplo n.º 2
0
 public static void Update(GameTime gameTime, Camera camera, Town town)
 {
     HandleKeyboard(town);
     HandleMouse(camera, town);
 }