Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ slides can adjust length dynamically #27

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions logic/Client/GameStatusBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ namespace Client;

public partial class GameStatusBar : ContentView
{
private readonly int lengthOfHp1Slide;
private readonly int lengthOfHp2Slide;
private readonly int lengthOfHp3Slide;
private bool haveSetSlideLength = false;
double lengthOfWormHole1HpSlide = 80;
double lengthOfWormHole2HpSlide = 80;
double lengthOfWormHole3HpSlide = 80;

private readonly int WormHoleFullHp = 18000;
public GameStatusBar(Grid parent, int Row, int Column)
{
Expand All @@ -25,13 +27,13 @@ public void SetWormHoleValue(MessageOfBuilding wormholeMsg)
switch (wormholeMsg.BuildingId)
{
case 0:
WormHole1HpSlide.WidthRequest = wormholeMsg.Hp / WormHoleFullHp * lengthOfHp1Slide;
WormHole1HpSlide.WidthRequest = wormholeMsg.Hp / WormHoleFullHp * lengthOfWormHole1HpSlide;
break;
case 1:
WormHole2HpSlide.WidthRequest = wormholeMsg.Hp / WormHoleFullHp * lengthOfHp2Slide;
WormHole2HpSlide.WidthRequest = wormholeMsg.Hp / WormHoleFullHp * lengthOfWormHole2HpSlide;
break;
case 2:
WormHole3HpSlide.WidthRequest = wormholeMsg.Hp / WormHoleFullHp * lengthOfHp3Slide;
WormHole3HpSlide.WidthRequest = wormholeMsg.Hp / WormHoleFullHp * lengthOfWormHole3HpSlide;
break;
}
}
Expand All @@ -55,4 +57,11 @@ public void SetGameTimeValue(MessageOfAll obj)
GameTime.Text += sec.ToString();
}

public void SlideLengthSet()
{
UtilFunctions.SlideLengthSet(WormHole1HpSlide, ref haveSetSlideLength, ref lengthOfWormHole1HpSlide, GameStatusGrid.Width);
UtilFunctions.SlideLengthSet(WormHole2HpSlide, ref haveSetSlideLength, ref lengthOfWormHole2HpSlide, GameStatusGrid.Width);
UtilFunctions.SlideLengthSet(WormHole3HpSlide, ref haveSetSlideLength, ref lengthOfWormHole3HpSlide, GameStatusGrid.Width);
haveSetSlideLength = true;
}
}
1 change: 1 addition & 0 deletions logic/Client/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ TargetType="Label">
<ColumnDefinition Width="530"/>
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="0.5*" />
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
Expand Down
15 changes: 12 additions & 3 deletions logic/Client/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
public partial class MainPage : ContentPage
{

private bool UIinitiated = false;
public MainPage()
{
Console.WriteLine("Hello World");
Expand All @@ -27,6 +27,7 @@
SetStatusBars();
InitiateObjects();
Map map = new Map(MainGrid, 0, 2, 2);
UIinitiated = true;
}

/* Set the StatusBars */
Expand All @@ -36,7 +37,6 @@
bluePlayer = new PlayerStatusBar(MainGrid, 1, 1, 1);
gameStatusBar = new GameStatusBar(MainGrid, 0, 4);
}

/* initiate the Lists of Objects and CountList */
private void InitiateObjects()
{
Expand Down Expand Up @@ -371,7 +371,7 @@
}
}
}
catch (Exception ex)

Check warning on line 374 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The variable 'ex' is declared but never used
{
/*
#TODO
Expand Down Expand Up @@ -410,9 +410,18 @@
{
lock (drawPicLock)
{
if (UIinitiated)
{
redPlayer.SlideLengthSet();
bluePlayer.SlideLengthSet();
gameStatusBar.SlideLengthSet();
}
if (!isClientStocked)
{
MapGrid.Children.Clear();
if (MapGrid.Children.Count() > 0)
{
MapGrid.Children.Clear();
}
foreach (var data in listOfAll)
{
gameStatusBar.SetGameTimeValue(data);
Expand Down Expand Up @@ -585,16 +594,16 @@
private readonly IDispatcherTimer timer;
private long counter;

AsyncServerStreamingCall<MessageToClient>? responseStream;

Check warning on line 597 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 597 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Field 'MainPage.responseStream' is never assigned to, and will always have its default value null
private bool isClientStocked;

Check warning on line 598 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Field 'MainPage.isClientStocked' is never assigned to, and will always have its default value false

private PlayerStatusBar redPlayer;
private PlayerStatusBar bluePlayer;
private GameStatusBar gameStatusBar;

private bool isPlaybackMode;

Check warning on line 604 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The field 'MainPage.isPlaybackMode' is never used
private long playerID;

Check warning on line 605 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The field 'MainPage.playerID' is never used
private double unit;

Check warning on line 606 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The field 'MainPage.unit' is never used
private double unitFontSize = 10;
private double unitHeight = 10.6;
private double unitWidth = 10.6;
Expand All @@ -611,7 +620,7 @@
private List<MessageOfBullet> listOfBullet;
private List<MessageOfResource> listOfResource;
private List<MessageOfHome> listOfHome;
private int gameTime;

Check warning on line 623 in logic/Client/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The field 'MainPage.gameTime' is never used

private Label[] resourceArray;
private (int x, int y)[] resourcePositionIndex;
Expand Down
4 changes: 2 additions & 2 deletions logic/Client/PlayerStatusBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Grid x:Name="PlayerRoleInfoGrid" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>

Expand All @@ -29,7 +29,7 @@
<BoxView x:Name="MyColor" Grid.Row="0" Grid.Column="0" HeightRequest="15" WidthRequest="15" HorizontalOptions="Center" VerticalOptions="Center" />
<Label x:Name="MyName" Text="Name" Grid.Row="0" Grid.Column="1" FontSize="15" HorizontalOptions="Start" HorizontalTextAlignment="Start" WidthRequest="300" VerticalOptions="Center" />
<Label x:Name="MyHpData" Text="100/100" Grid.Row="0" Grid.Column="2" FontSize="15" HorizontalOptions="End" HorizontalTextAlignment="Start" VerticalOptions="Center" />
<BoxView x:Name="MyHpSlide" Grid.Row="1" Grid.ColumnSpan="3" Grid.Column="0" HorizontalOptions="Start" WidthRequest="240"/>
<BoxView x:Name="MyHpSlide" Grid.Row="1" Grid.ColumnSpan="3" Grid.Column="0" HorizontalOptions="Start" WidthRequest="260"/>
</Grid>
<Label x:Name="MyMoney" Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="3" Text=" 💰总经济:" Style="{StaticResource littleAttributes}" />
</Grid>
Expand Down
37 changes: 25 additions & 12 deletions logic/Client/PlayerStatusBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ namespace Client
{
public partial class PlayerStatusBar : ContentView
{
private bool haveSetSlideLength = false;
enum PlayerRole
{
Red, //the down player
Blue //the up player
};
PlayerRole myRole;
private readonly int lengthOfHpSlide = 240;

private double lengthOfHpSlide = 240;
List<ShipLabel> shipLabels = new List<ShipLabel>();
public PlayerStatusBar(Grid parent, int Row, int Column, int role)
{
InitializeComponent();
MyHpSlide.WidthRequest = lengthOfHpSlide;
if (role == 0)
{
myRole = PlayerRole.Red;
Expand All @@ -37,6 +38,7 @@ public PlayerStatusBar(Grid parent, int Row, int Column, int role)

private void DrawSelfInfo()
{
MyHpSlide.WidthRequest = lengthOfHpSlide;
if (myRole == PlayerRole.Red)
{
MyName.Text = "Red Player";
Expand All @@ -63,21 +65,20 @@ private void DrawShipTable()
{
shipLabels[shipCounter].hpSlide.Color = Colors.Blue;
}
Grid shipStatusGrid = new Grid();
shipStatusGrid.RowDefinitions.Add(new RowDefinition());
shipStatusGrid.RowDefinitions.Add(new RowDefinition(10));
shipStatusGrid.Add(shipLabels[shipCounter].status);
shipStatusGrid.Add(shipLabels[shipCounter].hpSlide);
shipStatusGrid.SetRow(shipLabels[shipCounter].status, 0);
shipStatusGrid.SetRow(shipLabels[shipCounter].hpSlide, 1);
shipLabels[shipCounter].shipStatusGrid.RowDefinitions.Add(new RowDefinition());
shipLabels[shipCounter].shipStatusGrid.RowDefinitions.Add(new RowDefinition(10));
shipLabels[shipCounter].shipStatusGrid.Add(shipLabels[shipCounter].status);
shipLabels[shipCounter].shipStatusGrid.Add(shipLabels[shipCounter].hpSlide);
shipLabels[shipCounter].shipStatusGrid.SetRow(shipLabels[shipCounter].status, 0);
shipLabels[shipCounter].shipStatusGrid.SetRow(shipLabels[shipCounter].hpSlide, 1);

ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].name);
ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].producer);
ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].constructor);
ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].armor);
ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].shield);
ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].weapon);
ShipAllAttributesGrid.Children.Add(shipStatusGrid);
ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].shipStatusGrid);
//ShipAllAttributesGrid.Children.Add(shipLabels[shipCounter].status);

ShipAllAttributesGrid.SetRow(shipLabels[shipCounter].name, shipCounter);
Expand All @@ -86,7 +87,7 @@ private void DrawShipTable()
ShipAllAttributesGrid.SetRow(shipLabels[shipCounter].armor, shipCounter);
ShipAllAttributesGrid.SetRow(shipLabels[shipCounter].shield, shipCounter);
ShipAllAttributesGrid.SetRow(shipLabels[shipCounter].weapon, shipCounter);
ShipAllAttributesGrid.SetRow(shipStatusGrid, shipCounter);
ShipAllAttributesGrid.SetRow(shipLabels[shipCounter].shipStatusGrid, shipCounter);
//ShipAllAttributesGrid.SetRow(shipLabels[shipCounter].status, shipCounter);

ShipAllAttributesGrid.SetColumn(shipLabels[shipCounter].name, 0);
Expand All @@ -95,7 +96,7 @@ private void DrawShipTable()
ShipAllAttributesGrid.SetColumn(shipLabels[shipCounter].armor, 3);
ShipAllAttributesGrid.SetColumn(shipLabels[shipCounter].shield, 4);
ShipAllAttributesGrid.SetColumn(shipLabels[shipCounter].weapon, 5);
ShipAllAttributesGrid.SetColumn(shipStatusGrid, 6);
ShipAllAttributesGrid.SetColumn(shipLabels[shipCounter].shipStatusGrid, 6);
//ShipAllAttributesGrid.SetColumn(shipLabels[shipCounter].status, 6);
}
}
Expand Down Expand Up @@ -125,6 +126,16 @@ public void SetShipValue(MessageOfShip ship)
}
//TODO: Dynamic change the ships
}

public void SlideLengthSet()
{
UtilFunctions.SlideLengthSet(MyHpSlide, ref haveSetSlideLength, ref lengthOfHpSlide, PlayerRoleInfoGrid.Width);
foreach (ShipLabel shiplabel in shipLabels)
{
UtilFunctions.SlideLengthSet(shiplabel.hpSlide, ref haveSetSlideLength, ref shiplabel.lengthOfShipHpSlide, shiplabel.shipStatusGrid.Width);
}
haveSetSlideLength = true;
}
}
public class ShipLabel
{
Expand All @@ -135,6 +146,8 @@ public class ShipLabel
public Label shield = new Label() { Text = "shield" };
public Label weapon = new Label() { Text = "weapon" };
public Label status = new Label() { Text = "IDLE" };
public double lengthOfShipHpSlide = 80;
public BoxView hpSlide = new BoxView() { Color = Colors.Red, WidthRequest = 80, HeightRequest = 3, HorizontalOptions = LayoutOptions.Start, VerticalOptions = LayoutOptions.End };
public Grid shipStatusGrid = new Grid();
};
}
29 changes: 29 additions & 0 deletions logic/Client/UtilFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client
{
public class UtilFunctions
{
public static void SlideLengthSet(BoxView slide, ref bool haveSetSlideLength, ref double lengthOfSlide, double parentGridWidth)
{
if (parentGridWidth < 0)
{
return;
}
if (haveSetSlideLength == false)
{
lengthOfSlide = parentGridWidth;
slide.WidthRequest = lengthOfSlide;
}
else
{
slide.WidthRequest = slide.WidthRequest / lengthOfSlide * parentGridWidth;
lengthOfSlide = parentGridWidth;
}
}
}
}
Loading