Exemplo n.º 1
0
        // draws unit name plates
        private void DrawUnitNamePlates(SpriteBatch sb)
        {
            // draw a name plate for every unit the client can see
            foreach (UnitInstance unit in client.CachedUnits.ToArray())
            {
                // darken the nameplate of the player's units if they have no moves/actions left
                Color color = Color.White;
                if (unit.PlayerID == client.Player.InstanceID && unit.Movement == 0)
                {
                    color = Color.Lerp(color, Color.Black, 0.8f);
                }

                // format the text on the name plate
                RichText label;
                if (unit.PlayerID == client.Player.InstanceID)
                {
                    label = $"$(flag) <[{Color.PaleGreen.ToRichFormat()}]{unit.Name}>".ToRichText();
                }
                else
                {
                    label = $"$(flag) <[{Color.IndianRed.ToRichFormat()}]{unit.Name}>".ToRichText();
                }
                Vector2 labelMeasure = label.Measure();

                // position the name plate
                Vector2   tileCentre = camera.ConvertWorldToScreen(boardRenderer.GetTileCentre(unit.Location));
                Rectangle dest       = new Rectangle((int)(tileCentre.X - labelMeasure.X / 2 - unitLabelPadding), (int)(tileCentre.Y - boardRenderer.TileHeight * camera.Zoom.Y), (int)labelMeasure.X + unitLabelPadding * 2, unitLabelHeight);

                // draw the name plate
                unitLabelSprite.DrawNineCut(sb, dest, null, color);
                label.Draw(sb, new Vector2(dest.X + (int)labelMeasure.X / 2 - labelMeasure.X / 2 + unitLabelPadding, dest.Y + labelMeasure.Y / 4));

                // draw the hp bar above the nameplate
                double hpPercent = unit.HP / (double)unit.BaseUnit.MaxHP;
                if (hpPercent < 1)
                {
                    // colour the bar based on current hp
                    Color hpBarColor = Color.Green;
                    if (hpPercent <= 0.5)
                    {
                        hpBarColor = Color.Yellow;
                    }
                    else if (hpPercent <= 0.25)
                    {
                        hpBarColor = Color.Red;
                    }
                    // calculate the bar pos/length based on current hp
                    Vector2 pos0 = new Vector2(dest.X, dest.Y);
                    Vector2 pos1 = new Vector2(dest.X + (int)(dest.Width * hpPercent), dest.Y);
                    // draw the hp bar
                    GraphicsHelper.DrawLine(sb, pos0, pos1, 5, hpBarColor);
                }
            }
        }
Exemplo n.º 2
0
        // draws every tile
        // tiles the client hasn't explored are covered by clouds
        // tiles the client has explored but cannot currently see are tinted gray
        private void DrawTiles(SpriteBatch sb)
        {
            // get the tile under the mouse
            Tile       tileUnderMouse = GetTileAtPoint(InputManager.Instance.MouseWorldPos(camera));
            List <int> myCityIDs      = client.GetMyCityIDs();

            for (int y = 0; y < client.Board.Tiles.Length; y++)
            {
                for (int x = 0; x < client.Board.Tiles[y].Length; x++)
                {
                    // get data
                    Tile      cachedTile   = client.GetCachedTile(x, y);
                    Tile      boardTile    = client.Board.GetTile(x, y);
                    Vector2[] localCorners = GetTileCorners(boardTile);

                    // check for cull
                    bool camContainsTile = false;
                    foreach (Vector2 corner in localCorners)
                    {
                        if (new Rectangle(0, 0, 1920, 1080).Contains(camera.ConvertWorldToScreen(corner)))
                        {
                            camContainsTile = true;
                            break;
                        }
                    }
                    if (!camContainsTile)
                    {
                        continue; // cull
                    }
                    // calc data
                    Rectangle rect = new Rectangle((int)localCorners[2].X, (int)localCorners[4].Y, TileWidth, TileHeight);

                    // check if explored
                    if (cachedTile == null)
                    {
                        TileAtlas.Draw(sb, "TILEUTIL_CLOUD", rect); // draw a cloud over the unexplored tile
                        continue;                                   // dont draw the underlying tile
                    }

                    // check if a unit or a city can see the tile
                    bool visible = false;
                    foreach (UnitInstance unit in client.GetMyUnits())
                    {
                        if (unit == null || unit.BaseUnit == null || cachedTile == null)
                        {
                            continue;
                        }
                        if (Board.HexDistance(unit.Location, cachedTile.Location) <= unit.BaseUnit.Sight)
                        {
                            visible = true;
                            break;
                        }
                    }
                    if (!visible)
                    {
                        foreach (Point nLoc in cachedTile.GetNeighbourTileLocations())
                        {
                            Tile n = client.GetCachedTile(nLoc);
                            if (n == null || n.CityID == -1)
                            {
                                continue;
                            }
                            if (myCityIDs.Contains(n.CityID))
                            {
                                visible = true;
                                break;
                            }
                        }
                    }

                    // visible tiles are white
                    Color color = Color.White;
                    if (!visible) // while explored but not currently visible tiles are gray
                    {
                        color = Color.Gray;
                    }

                    // draw the tile
                    TileAtlas.Draw(sb, $"TILEBASE_{cachedTile.TerrainBase}", rect, null, color);
                    if (cachedTile.TerrainFeature != TileTerrainFeature.OPEN)
                    {
                        TileAtlas.Draw(sb, $"TILEFEATURE_{cachedTile.TerrainFeature}", rect, null, color);
                    }
                    // draw the improvment if the tile has one
                    if (cachedTile.Improvement != TileImprovment.Null)
                    {
                        TileAtlas.Draw(sb, $"TILEIMPROVMENT_{cachedTile.Improvement}", rect, null, color);
                    }
                    // draw a resource icon if the tile has a resource
                    //if (cachedTile.Resource != TileResource.Null)
                    //{
                    //    tileAtlas.Draw(sb, $"TILERESOURCE_{cachedTile.Resource}", rect, null, color);
                    //}
                    // draw the city border is the tile belongs to a city
                    if (cachedTile.CityID != -1)
                    {
                        City city = client.Cities.Find(c => c.InstanceID == cachedTile.CityID);
                        if (city != null)
                        {
                            Color empColorPri = client.DataManager.Empire.GetEmpire(city.EmpireID).PrimaryColor;
                            Color empColorSec = client.DataManager.Empire.GetEmpire(city.EmpireID).SecondaryColor;
                            if (city.PlayerID == client.Player.InstanceID && tileUnderMouse != null && tileUnderMouse.Location == city.Location)
                            {
                                empColorPri = Color.Lerp(empColorPri, Color.White, 0.1f);
                            }
                            empColorPri.A = 255;
                            TileAtlas.Draw(sb, $"TILEUTIL_HIGHLIGHT", rect, null, empColorPri);
                            TileAtlas.Draw(sb, $"TILEUTIL_OUTLINE", rect, null, empColorSec);
                        }
                    }
                    // draw yield icons if enabled
                    if (DrawResourceIcons)
                    {
                        lock (_lock_tileIconUpdate)
                        {
                            tileIcons[y][x]?.Draw(sb);
                        }
                    }
                }
            }
        }