From 3bc6ac4af63174a3610f4280a23ed015ca804422 Mon Sep 17 00:00:00 2001 From: Grange <2634070476@qq.com> Date: Sat, 9 Mar 2024 16:27:47 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E2=9C=A8=20tried=20to=20connect=20?= =?UTF-8?q?Server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/Client/ViewModel/GeneralViewModel.cs | 18 +++- logic/Client/ViewModel/MapViewModel.cs | 104 +-------------------- 2 files changed, 17 insertions(+), 105 deletions(-) diff --git a/logic/Client/ViewModel/GeneralViewModel.cs b/logic/Client/ViewModel/GeneralViewModel.cs index f7372f24..5bc89ea2 100644 --- a/logic/Client/ViewModel/GeneralViewModel.cs +++ b/logic/Client/ViewModel/GeneralViewModel.cs @@ -104,9 +104,9 @@ public void ConnectToServer(string[] comInfo) private void UpdateTest(object sender, EventArgs e) { counterViewModelTest++; - if (!hasDrawn) + if (!hasDrawn && getMapFlag) { - PureDrawMap(GameMap.GameMapArray); + PureDrawMap(defaultMap); hasDrawn = true; } if (testcounter < 30) @@ -147,8 +147,8 @@ private void UpdateTest(object sender, EventArgs e) Hp = 100 }); - ballX += 1; - ballY += 1; + //ballX += 1; + //ballY += 1; } public GeneralViewModel() @@ -186,12 +186,20 @@ public GeneralViewModel() } } - + ConnectToServer(new string[]{ + "127.0.0.1", + "8888", + "1", + "1", + "1" + }); timerViewModel = Dispatcher.CreateTimer(); timerViewModel.Interval = TimeSpan.FromMilliseconds(5); timerViewModel.Tick += new EventHandler(UpdateTest); timerViewModel.Start(); + + OnReceive(); } } } diff --git a/logic/Client/ViewModel/MapViewModel.cs b/logic/Client/ViewModel/MapViewModel.cs index 3c3beb50..30fa5837 100644 --- a/logic/Client/ViewModel/MapViewModel.cs +++ b/logic/Client/ViewModel/MapViewModel.cs @@ -301,11 +301,13 @@ private async void OnReceive() { try { - CancellationToken cts = new CancellationToken(); - while (responseStream != null && await responseStream.ResponseStream.MoveNext(cts)) + while (responseStream != null && await responseStream.ResponseStream.MoveNext()) { lock (drawPicLock) { + ballX += 20; + ballY += 20; + listOfAll.Clear(); listOfShip.Clear(); listOfBullet.Clear(); @@ -806,104 +808,6 @@ private void DrawShip(MessageOfShip data, ICanvas canvas) canvas.DrawString(Convert.ToString(hp), x - 5, y - 5, 10, 10, HorizontalAlignment.Left, VerticalAlignment.Top); } - //private void DrawBullet(MessageOfBullet data) - //{ - // //Ellipse iconOfBullet = new() - // //{ - // // WidthRequest = 2 * bulletRadiusTimes * unitWidth, - // // HeightRequest = 2 * bulletRadiusTimes * unitHeight, - // // HorizontalOptions = LayoutOptions.Start, - // // VerticalOptions = LayoutOptions.Start, - // // Margin = new Thickness(unitHeight * data.Y / 1000.0 - unitWidth * bulletRadiusTimes, unitWidth * data.X / 1000.0 - unitWidth * bulletRadiusTimes, 0, 0), - // // Fill = (data.TeamId == (long)PlayerTeam.Red) ? Colors.Red : Colors.Blue - // //}; - // //switch (data.Type) - // //{ - // // case BulletType.Plasma: - // // iconOfBullet.Fill = Colors.Yellow; - // // break; - // // case BulletType.Laser: - // // iconOfBullet.Fill = Colors.Orange; - // // break; - // // case BulletType.Missile: - // // iconOfBullet.Fill = Colors.Purple; - // // break; - // // case BulletType.Arc: - // // iconOfBullet.Fill = Colors.Green; - // // break; - // //} - // //MapGrid.Children.Add(iconOfBullet); - //} - - //private void DrawBombedBullet(MessageOfBombedBullet data) - //{ - // //Ellipse iconOfBombedBullet = new() - // //{ - // // WidthRequest = 2 * bulletRadiusTimes * unitWidth, - // // HeightRequest = 2 * bulletRadiusTimes * unitHeight, - // // HorizontalOptions = LayoutOptions.Start, - // // VerticalOptions = LayoutOptions.Start, - // // Margin = new Thickness(unitHeight * data.Y / 1000.0 - unitWidth * bulletRadiusTimes, unitWidth * data.X / 1000.0 - unitWidth * bulletRadiusTimes, 0, 0), - // // Fill = (data.MappingId == (long)PlayerTeam.Red) ? Colors.Red : Colors.Blue - // //}; - // //switch (data.Type) - // //{ - // // case BulletType.Plasma: - // // iconOfBombedBullet.Fill = Colors.Yellow; - // // break; - // // case BulletType.Laser: - // // iconOfBombedBullet.Fill = Colors.Orange; - // // break; - // // case BulletType.Missile: - // // iconOfBombedBullet.Fill = Colors.Purple; - // // break; - // // case BulletType.Arc: - // // iconOfBombedBullet.Fill = Colors.Green; - // // break; - // //} - // //MapGrid.Children.Add(iconOfBombedBullet); - //} - - //private void DrawResource(MessageOfResource data) - //{ - // int idx = FindIndexOfResource(data); - // //resourceArray[idx].FontSize = unitFontSize; - // //resourceArray[idx].WidthRequest = unitWidth; - // //resourceArray[idx].HeightRequest = unitHeight; - // //resourceArray[idx].Text = Convert.ToString(data.Progress); - // //resourceArray[idx].Margin = new Thickness(unitHeight * data.Y / 1000.0 - unitWidth * characterRadiusTimes, unitWidth * data.X / 1000.0 - unitWidth * characterRadiusTimes, 0, 0); - // //MapGrid.Children.Add(resourceArray[idx]); - //} - - //private void DrawShip(MessageOfShip data) - //{ - // //Ellipse iconOfShip = new() - // //{ - // // WidthRequest = 2 * characterRadiusTimes * unitWidth, - // // HeightRequest = 2 * characterRadiusTimes * unitHeight, - // // HorizontalOptions = LayoutOptions.Start, - // // VerticalOptions = LayoutOptions.Start, - // // Margin = new Thickness(unitHeight * data.Y / 1000.0 - unitWidth * characterRadiusTimes, unitWidth * data.X / 1000.0 - unitWidth * characterRadiusTimes, 0, 0), - // // Fill = (data.TeamId == (long)PlayerTeam.Red) ? Colors.Red : Colors.Blue - // //}; - // Label nameOfShip = new() - // { - // FontSize = unitFontSize, - // WidthRequest = unitWidth, - // HeightRequest = unitHeight, - // Text = data.ShipType.ToString()[0] + data.PlayerId.ToString(), - // HorizontalOptions = LayoutOptions.Start, - // VerticalOptions = LayoutOptions.Start, - // HorizontalTextAlignment = TextAlignment.Center, - // VerticalTextAlignment = TextAlignment.Center, - // BackgroundColor = Colors.Transparent, - // Margin = new Thickness(unitHeight * data.Y / 1000.0 - unitWidth * characterRadiusTimes, unitWidth * data.X / 1000.0 - unitWidth * characterRadiusTimes, 0, 0) - // }; - // //MapGrid.Children.Add(iconOfShip); - // //MapGrid.Children.Add(nameOfShip); - //} - - private bool isClientStocked = false; private bool hasDrawn = false; private bool getMapFlag = false; From 0a63d3bdc1d0eceae29d0e0692a49ff242aa3afc Mon Sep 17 00:00:00 2001 From: Grange <2634070476@qq.com> Date: Sat, 16 Mar 2024 19:08:12 +0800 Subject: [PATCH 2/4] =?UTF-8?q?chore:=20=E2=9C=A8=20upgraded=20draw=20logi?= =?UTF-8?q?c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/Client/Model/Player.cs | 16 +- logic/Client/Model/Ship.cs | 132 +++++---- logic/Client/Old/GameStatusBar.xaml.cs | 1 + logic/Client/Old/PlayerStatusBar.xaml.cs | 1 + logic/Client/{ => Util}/UtilFunctions.cs | 78 +++-- logic/Client/Util/UtilInfo.cs | 75 +++++ logic/Client/View/MainPage.xaml | 4 +- logic/Client/ViewModel/GeneralViewModel.cs | 324 +++++++++++++++++++++ logic/Client/ViewModel/MapViewModel.cs | 310 +++----------------- 9 files changed, 558 insertions(+), 383 deletions(-) rename logic/Client/{ => Util}/UtilFunctions.cs (94%) mode change 100755 => 100644 create mode 100644 logic/Client/Util/UtilInfo.cs diff --git a/logic/Client/Model/Player.cs b/logic/Client/Model/Player.cs index 85f107f6..38d6446c 100644 --- a/logic/Client/Model/Player.cs +++ b/logic/Client/Model/Player.cs @@ -9,8 +9,8 @@ namespace Client.Model { public class Player : BindableObject { - private bool team; - public bool Team + private long team; + public long Team { get => team; set @@ -39,6 +39,18 @@ public int Money OnPropertyChanged(); } } + + private int score; + public int Score + { + get => score; + set + { + score = value; + OnPropertyChanged(); + } + } + private ObservableCollection ships; public ObservableCollection Ships { diff --git a/logic/Client/Model/Ship.cs b/logic/Client/Model/Ship.cs index 739634cc..ef252220 100644 --- a/logic/Client/Model/Ship.cs +++ b/logic/Client/Model/Ship.cs @@ -1,4 +1,6 @@ -using System; +using Client.Util; +using Protobuf; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,84 +8,86 @@ namespace Client.Model { - public enum TeamEnum - { - Red, - Blue - } + //public enum TeamEnum + //{ + // Red, + // Blue + //} - public enum TypeEnum - { - CivilShip, - WarShip, - FlagShip - } + //public enum TypeEnum + //{ + // CivilShip, + // WarShip, + // FlagShip + //} - public enum StateEnum - { - Idle, - Producing, - Constructing, - Recovering, - Recycling, - Attacking, - Swinging, - Deceased - } + //public enum StateEnum + //{ + // Idle, + // Producing, + // Constructing, + // Recovering, + // Recycling, + // Attacking, + // Swinging, + // Deceased + //} - public enum ProducerModuleEnum - { + //public enum ProducerModuleEnum + //{ - } + //} - public enum ConstuctorModuleEnum - { + //public enum ConstuctorModuleEnum + //{ - } + //} - public enum ArmorModuleEnum - { + //public enum ArmorModuleEnum + //{ - } + //} - public enum ShieldModuleEnum - { + //public enum ShieldModuleEnum + //{ - } + //} - public enum AttackerModuleEnum - { + //public enum AttackerModuleEnum + //{ - } + //} public class Ship : BindableObject { - private TeamEnum team; - private TypeEnum type; - private StateEnum state; + private long teamID; + private ShipType type; + private ShipState state; private int Hp; - private ProducerModuleEnum producerModule; - private ConstuctorModuleEnum constuctorModule; - private ArmorModuleEnum armorModule; - private ShieldModuleEnum shieldModule; - private AttackerModuleEnum attackerModule; + private ProducerType producerModule; + private ConstructorType constuctorModule; + private ArmorType armorModule; + private ShieldType shieldModule; + private WeaponType weaponModule; private string type_s; private string state_s; private string producerModule_s; private string constuctorModule_s; private string armorModule_s; private string shieldModule_s; - private string attackerModule_s; - public TeamEnum Team + private string weaponModule_s; + + + public long TeamID { - get => team; + get => teamID; set { - team = value; + teamID = value; OnPropertyChanged(); } } - public TypeEnum Type + public ShipType Type { get => type; set @@ -92,7 +96,7 @@ public TypeEnum Type OnPropertyChanged(); } } - public StateEnum State + public ShipState State { get => state; set @@ -110,7 +114,7 @@ public int HP OnPropertyChanged(); } } - public ProducerModuleEnum ProducerModule + public ProducerType ProducerModule { get => producerModule; set @@ -119,7 +123,7 @@ public ProducerModuleEnum ProducerModule OnPropertyChanged(); } } - public ConstuctorModuleEnum ConstuctorModule + public ConstructorType ConstuctorModule { get => constuctorModule; set @@ -128,7 +132,7 @@ public ConstuctorModuleEnum ConstuctorModule OnPropertyChanged(); } } - public ArmorModuleEnum ArmorModule + public ArmorType ArmorModule { get => armorModule; set @@ -137,7 +141,7 @@ public ArmorModuleEnum ArmorModule OnPropertyChanged(); } } - public ShieldModuleEnum ShieldModule + public ShieldType ShieldModule { get => shieldModule; set @@ -146,18 +150,18 @@ public ShieldModuleEnum ShieldModule OnPropertyChanged(); } } - public AttackerModuleEnum AttackerModule + public WeaponType WeaponModule { - get => attackerModule; + get => weaponModule; set { - attackerModule = value; + weaponModule = value; OnPropertyChanged(); } } public string Type_s { - get => type_s; + get => UtilInfo.ShipTypeNameDict[Type]; set { type_s = value; @@ -166,7 +170,7 @@ public string Type_s } public string State_s { - get => state_s; + get => UtilInfo.ShipStateNameDict[State]; set { state_s = value; @@ -209,12 +213,12 @@ public string ShieldModule_s OnPropertyChanged(); } } - public string AttackerModule_s + public string WeaponModule_s { - get => attackerModule_s; + get => weaponModule_s; set { - attackerModule_s = value; + weaponModule_s = value; OnPropertyChanged(); } } diff --git a/logic/Client/Old/GameStatusBar.xaml.cs b/logic/Client/Old/GameStatusBar.xaml.cs index 32f24370..e3e775a0 100644 --- a/logic/Client/Old/GameStatusBar.xaml.cs +++ b/logic/Client/Old/GameStatusBar.xaml.cs @@ -1,3 +1,4 @@ +using Client.Util; using Protobuf; namespace Client; diff --git a/logic/Client/Old/PlayerStatusBar.xaml.cs b/logic/Client/Old/PlayerStatusBar.xaml.cs index aa5ffde2..69c03f78 100644 --- a/logic/Client/Old/PlayerStatusBar.xaml.cs +++ b/logic/Client/Old/PlayerStatusBar.xaml.cs @@ -1,4 +1,5 @@ using System; +using Client.Util; using Protobuf; namespace Client diff --git a/logic/Client/UtilFunctions.cs b/logic/Client/Util/UtilFunctions.cs old mode 100755 new mode 100644 similarity index 94% rename from logic/Client/UtilFunctions.cs rename to logic/Client/Util/UtilFunctions.cs index 0d7f3292..9a08b78f --- a/logic/Client/UtilFunctions.cs +++ b/logic/Client/Util/UtilFunctions.cs @@ -1,40 +1,38 @@ -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; - } - } - public static int getIndex(int i, int j) - { - return 50 * i + j; - } - - public static PointF getMapCenter(float i, float j) - { - return new PointF(10 * i + 5, 10 * j + 5); - } - } - - -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Util +{ + 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; + } + } + public static int getIndex(int i, int j) + { + return 50 * i + j; + } + + public static PointF getMapCenter(float i, float j) + { + return new PointF(10 * i + 5, 10 * j + 5); + } + } +} diff --git a/logic/Client/Util/UtilInfo.cs b/logic/Client/Util/UtilInfo.cs new file mode 100644 index 00000000..20d38249 --- /dev/null +++ b/logic/Client/Util/UtilInfo.cs @@ -0,0 +1,75 @@ +using Protobuf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Util +{ + public static class UtilInfo + { + public static Dictionary ShipTypeNameDict = new Dictionary{ + { ShipType.NullShipType, "NullShipType" }, + { ShipType.CivilianShip, "CivilianShip" }, + { ShipType.MilitaryShip, "MilitaryShip" }, + { ShipType.FlagShip, "FlagShip" } + }; + + public static Dictionary ShipStateNameDict = new Dictionary + { + { ShipState.Idle, "Idle" }, + { ShipState.Producing, "Producing" }, + { ShipState.Constructing, "Constructing" }, + { ShipState.Recovering, "Recovering" }, + { ShipState.Recycling, "Recycling" }, + { ShipState.Attacking, "Attacking" }, + { ShipState.Swinging, "Swinging" }, + { ShipState.Moving, "Moving" } + }; + + public static Dictionary ShipArmorTypeNameDict = new Dictionary + { + { ArmorType.NullArmorType, "NullArmorType" }, + { ArmorType.Armor1, "Armor1" }, + { ArmorType.Armor2, "Armor2" }, + { ArmorType.Armor3, "Armor3" } + }; + + public static Dictionary ShipShieldTypeNameDict = new Dictionary + { + { ShieldType.NullShieldType, "NullShieldType" }, + { ShieldType.Shield1, "Shield1" }, + { ShieldType.Shield2, "Shield2" }, + { ShieldType.Shield3, "Shield3" } + }; + + public static Dictionary ShipConstructorNameDict = new Dictionary + { + { ConstructorType.NullConstructorType, "NullConstructorType" }, + { ConstructorType.Constructor1, "Constructor1" }, + { ConstructorType.Constructor2, "Constructor2" }, + { ConstructorType.Constructor3, "Constructor3" } + }; + + public static Dictionary ShipProducerTypeNameDict = new Dictionary + { + { ProducerType.NullProducerType, "NullProducerType" }, + { ProducerType.Producer1, "Producer1" }, + { ProducerType.Producer2, "Producer2" }, + { ProducerType.Producer3, "Producer3" } + }; + + public static Dictionary ShipWeaponTypeNameDict = new Dictionary + { + { WeaponType.NullWeaponType, "NullWeaponType" }, + { WeaponType.Lasergun, "Lasergun" }, + { WeaponType.Arcgun, "Arcgun" }, + { WeaponType.Plasmagun, "Plasmagun" }, + { WeaponType.Missilegun, "Missilegun" }, + { WeaponType.Shellgun, "Shellgun" } + }; + } + + +} diff --git a/logic/Client/View/MainPage.xaml b/logic/Client/View/MainPage.xaml index 4b6b15bc..538cf895 100644 --- a/logic/Client/View/MainPage.xaml +++ b/logic/Client/View/MainPage.xaml @@ -121,7 +121,7 @@ - + @@ -200,7 +200,7 @@ - + diff --git a/logic/Client/ViewModel/GeneralViewModel.cs b/logic/Client/ViewModel/GeneralViewModel.cs index 5bc89ea2..d36e94b8 100644 --- a/logic/Client/ViewModel/GeneralViewModel.cs +++ b/logic/Client/ViewModel/GeneralViewModel.cs @@ -9,6 +9,7 @@ using Grpc.Core; using System.Diagnostics.Metrics; using CommunityToolkit.Mvvm.ComponentModel; +using Client.Util; namespace Client.ViewModel { @@ -101,6 +102,327 @@ public void ConnectToServer(string[] comInfo) } int testcounter = 0; + + private async void OnReceive() + { + try + { + while (responseStream != null && await responseStream.ResponseStream.MoveNext()) + { + lock (drawPicLock) + { + ballX += 20; + ballY += 20; + + listOfAll.Clear(); + listOfShip.Clear(); + listOfBullet.Clear(); + listOfBombedBullet.Clear(); + listOfFactory.Clear(); + listOfCommunity.Clear(); + listOfFort.Clear(); + listOfResource.Clear(); + listOfHome.Clear(); + listOfWormhole.Clear(); + MessageToClient content = responseStream.ResponseStream.Current; + MessageOfMap mapMassage = new(); + bool mapMessageExist = false; + switch (content.GameState) + { + case GameState.GameStart: + foreach (var obj in content.ObjMessage) + { + switch (obj.MessageOfObjCase) + { + case MessageOfObj.MessageOfObjOneofCase.ShipMessage: + listOfShip.Add(obj.ShipMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.BulletMessage: + listOfBullet.Add(obj.BulletMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.BombedBulletMessage: + listOfBombedBullet.Add(obj.BombedBulletMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.FactoryMessage: + listOfFactory.Add(obj.FactoryMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.CommunityMessage: + listOfCommunity.Add(obj.CommunityMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.FortMessage: + listOfFort.Add(obj.FortMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.ResourceMessage: + listOfResource.Add(obj.ResourceMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.HomeMessage: + listOfHome.Add(obj.HomeMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.MapMessage: + mapMassage = obj.MapMessage; + break; + } + } + listOfAll.Add(content.AllMessage); + countMap.Clear(); + countMap.Add((int)MapPatchType.Resource, listOfResource.Count); + countMap.Add((int)MapPatchType.Factory, listOfFactory.Count); + countMap.Add((int)MapPatchType.Community, listOfCommunity.Count); + countMap.Add((int)MapPatchType.Fort, listOfFort.Count); + GetMap(mapMassage); + break; + case GameState.GameRunning: + foreach (var obj in content.ObjMessage) + { + switch (obj.MessageOfObjCase) + { + case MessageOfObj.MessageOfObjOneofCase.ShipMessage: + listOfShip.Add(obj.ShipMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.FactoryMessage: + listOfFactory.Add(obj.FactoryMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.CommunityMessage: + listOfCommunity.Add(obj.CommunityMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.FortMessage: + listOfFort.Add(obj.FortMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.BulletMessage: + listOfBullet.Add(obj.BulletMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.BombedBulletMessage: + listOfBombedBullet.Add(obj.BombedBulletMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.ResourceMessage: + listOfResource.Add(obj.ResourceMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.HomeMessage: + listOfHome.Add(obj.HomeMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.MapMessage: + mapMassage = obj.MapMessage; + mapMessageExist = true; + break; + } + } + listOfAll.Add(content.AllMessage); + if (mapMessageExist) + { + countMap.Clear(); + countMap.Add((int)MapPatchType.Resource, listOfResource.Count); + countMap.Add((int)MapPatchType.Factory, listOfFactory.Count); + countMap.Add((int)MapPatchType.Community, listOfCommunity.Count); + countMap.Add((int)MapPatchType.Fort, listOfFort.Count); + GetMap(mapMassage); + mapMessageExist = false; + } + break; + + case GameState.GameEnd: + //DisplayAlert("Info", "Game End", "OK"); + foreach (var obj in content.ObjMessage) + { + switch (obj.MessageOfObjCase) + { + case MessageOfObj.MessageOfObjOneofCase.ShipMessage: + listOfShip.Add(obj.ShipMessage); + break; + + //case MessageOfObj.MessageOfObjOneofCase.BuildingMessage: + // listOfBuilding.Add(obj.BuildingMessage); + // break; + + case MessageOfObj.MessageOfObjOneofCase.FactoryMessage: + listOfFactory.Add(obj.FactoryMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.CommunityMessage: + listOfCommunity.Add(obj.CommunityMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.FortMessage: + listOfFort.Add(obj.FortMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.BulletMessage: + listOfBullet.Add(obj.BulletMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.BombedBulletMessage: + listOfBombedBullet.Add(obj.BombedBulletMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.ResourceMessage: + listOfResource.Add(obj.ResourceMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.HomeMessage: + listOfHome.Add(obj.HomeMessage); + break; + + case MessageOfObj.MessageOfObjOneofCase.MapMessage: + mapMassage = obj.MapMessage; + break; + } + } + listOfAll.Add(content.AllMessage); + break; + } + } + if (responseStream == null) + { + throw new Exception("Unconnected"); + } + } + } + catch (Exception ex) + { + /* + #TODO + Show the error message + */ + } + } + + private void Refresh(object sender, EventArgs e) + { + try + { + lock (drawPicLock) + { + //if (UIinitiated) + //{ + // redPlayer.SlideLengthSet(); + // bluePlayer.SlideLengthSet(); + // gameStatusBar.SlideLengthSet(); + //} + if (!isClientStocked) + { + if (!hasDrawn && getMapFlag) + { + //PureDrawMap(defaultMap); + hasDrawn = true; + } + + foreach (var data in listOfAll) + { + RedPlayer.Money = data.RedTeamMoney; + RedPlayer.Hp = data.RedHomeHp; + RedPlayer.Score = data.RedTeamScore; + BluePlayer.Money = data.BlueTeamMoney; + BluePlayer.Hp = data.BlueHomeHp; + BluePlayer.Score = data.BlueTeamScore; + } + + foreach (var data in listOfHome) + { + DrawHome(data); + if (data.TeamId == (long)PlayerTeam.Red) + { + RedPlayer.Team = data.TeamId; + } + else if (data.TeamId == (long)PlayerTeam.Blue) + { + BluePlayer.Team = data.TeamId; + } + } + + foreach (var data in listOfShip) + { + if (data.TeamId == (long)PlayerTeam.Red) + { + Ship ship = new Ship + { + Type = data.ShipType, + State = data.ShipState, + ArmorModule = data.ArmorType, + ShieldModule = data.ShieldType, + WeaponModule = data.WeaponType, + ProducerModule = data.ProducerType, + ConstuctorModule = data.ConstructorType, + Type_s = UtilInfo.ShipTypeNameDict[data.ShipType], + State_s = UtilInfo.ShipStateNameDict[data.ShipState], + ArmorModule_s = UtilInfo.ShipArmorTypeNameDict[data.ArmorType], + ShieldModule_s = UtilInfo.ShipShieldTypeNameDict[data.ShieldType], + WeaponModule_s = UtilInfo.ShipWeaponTypeNameDict[data.WeaponType], + ConstuctorModule_s = UtilInfo.ShipConstructorNameDict[data.ConstructorType], + ProducerModule_s = UtilInfo.ShipProducerTypeNameDict[data.ProducerType] + }; + RedPlayer.Ships.Add(ship); + } + else if (data.TeamId == (long)PlayerTeam.Blue) + { + Ship ship = new Ship + { + Type = data.ShipType, + State = data.ShipState, + ArmorModule = data.ArmorType, + ShieldModule = data.ShieldType, + WeaponModule = data.WeaponType, + ProducerModule = data.ProducerType, + ConstuctorModule = data.ConstructorType, + Type_s = UtilInfo.ShipTypeNameDict[data.ShipType], + State_s = UtilInfo.ShipStateNameDict[data.ShipState], + ArmorModule_s = UtilInfo.ShipArmorTypeNameDict[data.ArmorType], + ShieldModule_s = UtilInfo.ShipShieldTypeNameDict[data.ShieldType], + WeaponModule_s = UtilInfo.ShipWeaponTypeNameDict[data.WeaponType], + ConstuctorModule_s = UtilInfo.ShipConstructorNameDict[data.ConstructorType], + ProducerModule_s = UtilInfo.ShipProducerTypeNameDict[data.ProducerType] + }; + BluePlayer.Ships.Add(ship); + } + } + + foreach (var data in listOfCommunity) + { + DrawCommunity(data); + } + + foreach (var data in listOfFactory) + { + DrawFactory(data); + } + + foreach (var data in listOfWormhole) + { + DrawWormHole(data); + } + + foreach (var data in listOfFort) + { + DrawFort(data); + } + + foreach (var data in listOfResource) + { + DrawResource(data); + } + } + } + } + finally + { + + } + //counter++; + } private void UpdateTest(object sender, EventArgs e) { counterViewModelTest++; @@ -153,6 +475,7 @@ private void UpdateTest(object sender, EventArgs e) public GeneralViewModel() { + InitiateObjects(); Title = "THUAI7"; Links = [ new Link { Name = "天梯信息", Url = "" }, @@ -185,6 +508,7 @@ public GeneralViewModel() }); } } + PureDrawMap(GameMap.GameMapArray); ConnectToServer(new string[]{ "127.0.0.1", diff --git a/logic/Client/ViewModel/MapViewModel.cs b/logic/Client/ViewModel/MapViewModel.cs index 30fa5837..e7a0ec2c 100644 --- a/logic/Client/ViewModel/MapViewModel.cs +++ b/logic/Client/ViewModel/MapViewModel.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Microsoft.Maui.Dispatching; using Grpc.Core; +using Client.Util; namespace Client.ViewModel @@ -126,6 +127,38 @@ public void Draw(ICanvas canvas, RectF dirtyRect) Hp = 100, TeamId = 1 }, canvas); + + listOfBullet.Add(new MessageOfBullet + { + X = 20, + Y = 20, + Type = BulletType.NullBulletType, + BombRange = 5 + }); + + listOfShip.Add(new MessageOfShip + { + X = 10, + Y = 12, + Hp = 100, + TeamId = 1 + }); + + if (listOfBullet.Count > 0) + { + foreach (var data in listOfBullet) + { + DrawBullet(data, canvas); + } + } + + if (listOfBullet.Count > 0) + { + foreach (var data in listOfShip) + { + DrawShip(data, canvas); + } + } } private Dictionary PatchColorDict = new Dictionary @@ -297,203 +330,7 @@ private void PureDrawMap(int[,] Map) // } //} - private async void OnReceive() - { - try - { - while (responseStream != null && await responseStream.ResponseStream.MoveNext()) - { - lock (drawPicLock) - { - ballX += 20; - ballY += 20; - - listOfAll.Clear(); - listOfShip.Clear(); - listOfBullet.Clear(); - listOfBombedBullet.Clear(); - listOfFactory.Clear(); - listOfCommunity.Clear(); - listOfFort.Clear(); - listOfResource.Clear(); - listOfHome.Clear(); - listOfWormhole.Clear(); - MessageToClient content = responseStream.ResponseStream.Current; - MessageOfMap mapMassage = new(); - bool mapMessageExist = false; - switch (content.GameState) - { - case GameState.GameStart: - foreach (var obj in content.ObjMessage) - { - switch (obj.MessageOfObjCase) - { - case MessageOfObj.MessageOfObjOneofCase.ShipMessage: - listOfShip.Add(obj.ShipMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.BulletMessage: - listOfBullet.Add(obj.BulletMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.BombedBulletMessage: - listOfBombedBullet.Add(obj.BombedBulletMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.FactoryMessage: - listOfFactory.Add(obj.FactoryMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.CommunityMessage: - listOfCommunity.Add(obj.CommunityMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.FortMessage: - listOfFort.Add(obj.FortMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.ResourceMessage: - listOfResource.Add(obj.ResourceMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.HomeMessage: - listOfHome.Add(obj.HomeMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.MapMessage: - mapMassage = obj.MapMessage; - break; - } - } - listOfAll.Add(content.AllMessage); - countMap.Clear(); - countMap.Add((int)MapPatchType.Resource, listOfResource.Count); - countMap.Add((int)MapPatchType.Factory, listOfFactory.Count); - countMap.Add((int)MapPatchType.Community, listOfCommunity.Count); - countMap.Add((int)MapPatchType.Fort, listOfFort.Count); - GetMap(mapMassage); - break; - case GameState.GameRunning: - foreach (var obj in content.ObjMessage) - { - switch (obj.MessageOfObjCase) - { - case MessageOfObj.MessageOfObjOneofCase.ShipMessage: - listOfShip.Add(obj.ShipMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.FactoryMessage: - listOfFactory.Add(obj.FactoryMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.CommunityMessage: - listOfCommunity.Add(obj.CommunityMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.FortMessage: - listOfFort.Add(obj.FortMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.BulletMessage: - listOfBullet.Add(obj.BulletMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.BombedBulletMessage: - listOfBombedBullet.Add(obj.BombedBulletMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.ResourceMessage: - listOfResource.Add(obj.ResourceMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.HomeMessage: - listOfHome.Add(obj.HomeMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.MapMessage: - mapMassage = obj.MapMessage; - mapMessageExist = true; - break; - } - } - listOfAll.Add(content.AllMessage); - if (mapMessageExist) - { - countMap.Clear(); - countMap.Add((int)MapPatchType.Resource, listOfResource.Count); - countMap.Add((int)MapPatchType.Factory, listOfFactory.Count); - countMap.Add((int)MapPatchType.Community, listOfCommunity.Count); - countMap.Add((int)MapPatchType.Fort, listOfFort.Count); - GetMap(mapMassage); - mapMessageExist = false; - } - break; - - case GameState.GameEnd: - //DisplayAlert("Info", "Game End", "OK"); - foreach (var obj in content.ObjMessage) - { - switch (obj.MessageOfObjCase) - { - case MessageOfObj.MessageOfObjOneofCase.ShipMessage: - listOfShip.Add(obj.ShipMessage); - break; - - //case MessageOfObj.MessageOfObjOneofCase.BuildingMessage: - // listOfBuilding.Add(obj.BuildingMessage); - // break; - - case MessageOfObj.MessageOfObjOneofCase.FactoryMessage: - listOfFactory.Add(obj.FactoryMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.CommunityMessage: - listOfCommunity.Add(obj.CommunityMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.FortMessage: - listOfFort.Add(obj.FortMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.BulletMessage: - listOfBullet.Add(obj.BulletMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.BombedBulletMessage: - listOfBombedBullet.Add(obj.BombedBulletMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.ResourceMessage: - listOfResource.Add(obj.ResourceMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.HomeMessage: - listOfHome.Add(obj.HomeMessage); - break; - - case MessageOfObj.MessageOfObjOneofCase.MapMessage: - mapMassage = obj.MapMessage; - break; - } - } - listOfAll.Add(content.AllMessage); - break; - } - } - if (responseStream == null) - { - throw new Exception("Unconnected"); - } - } - } - catch (Exception ex) - { - /* - #TODO - Show the error message - */ - } - } + //private int FindIndexOfResource(MessageOfResource obj) //{ @@ -543,84 +380,7 @@ Show the error message // return -1; //} - //private void Refresh(object sender, EventArgs e) - //{ - // try - // { - // lock (drawPicLock) - // { - // //if (UIinitiated) - // //{ - // // redPlayer.SlideLengthSet(); - // // bluePlayer.SlideLengthSet(); - // // gameStatusBar.SlideLengthSet(); - // //} - // if (!isClientStocked) - // { - // /* For Debug */ - // //if (MapGrid.Children.Count > 0) - // //{ - // // MapGrid.Children.Clear(); - // //} - // //foreach (var data in listOfAll) - // //{ - // // gameStatusBar.SetGameTimeValue(data); - // //} - // /* For Debug */ - // //if (!hasDrawed && mapFlag) - // //{ - // // DrawMap(); - // //} - // if (!hasDrawed) - // { - // PureDrawMap(); - // } - // //foreach (var data in listOfHome) - // //{ - // // if (data.TeamId == (long)PlayerTeam.Red) - // // { - // // redPlayer.SetPlayerValue(data); - // // } - // // else - // // { - // // bluePlayer.SetPlayerValue(data); - // // } - // // DrawHome(data); - // //} - // foreach (var data in listOfBombedBullet) - // { - // DrawBombedBullet(data); - // } - // foreach (var data in listOfBullet) - // { - // DrawBullet(data); - // } - // foreach (var data in listOfResource) - // { - // DrawResource(data); - // } - // //foreach (var data in listOfShip) - // //{ - // // if (data.TeamId == (long)PlayerTeam.Red) - // // { - // // redPlayer.SetShipValue(data); - // // } - // // else - // // { - // // bluePlayer.SetShipValue(data); - // // } - // // // TODO: Dynamic change the ships' label - // // DrawShip(data); - // //} - // } - // } - // } - // finally - // { - - // } - // //counter++; - //} + private void DrawHome(MessageOfHome data) { From bf355646e422264226980587ca10c78a50642792 Mon Sep 17 00:00:00 2001 From: Grange <2634070476@qq.com> Date: Sat, 16 Mar 2024 19:10:51 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/Client/ViewModel/MapViewModel.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logic/Client/ViewModel/MapViewModel.cs b/logic/Client/ViewModel/MapViewModel.cs index e7a0ec2c..93ef47ce 100644 --- a/logic/Client/ViewModel/MapViewModel.cs +++ b/logic/Client/ViewModel/MapViewModel.cs @@ -330,7 +330,7 @@ private void PureDrawMap(int[,] Map) // } //} - + //private int FindIndexOfResource(MessageOfResource obj) //{ @@ -380,7 +380,7 @@ private void PureDrawMap(int[,] Map) // return -1; //} - + private void DrawHome(MessageOfHome data) { From fcac29de2ebfdd1cd253f187b9869c2d5ca09116 Mon Sep 17 00:00:00 2001 From: Grange <2634070476@qq.com> Date: Sat, 23 Mar 2024 10:51:38 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Modified=20sweeper=20?= =?UTF-8?q?names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/Client/Model/MapPatch.cs | 14 +- logic/Client/Model/Player.cs | 6 +- logic/Client/Model/Ship.cs | 22 +- logic/Client/Old/PlayerStatusBar.xaml | 18 +- logic/Client/Old/PlayerStatusBar.xaml.cs | 72 +++--- logic/Client/Properties/PlayerBarGrid_new.xml | 16 +- logic/Client/Util/UtilInfo.cs | 38 ++-- logic/Client/View/MainPage.xaml | 40 ++-- logic/Client/View/MainPage.xaml.cs | 210 +++++++++--------- logic/Client/ViewModel/GeneralViewModel.cs | 179 ++++++++------- logic/Client/ViewModel/MapViewModel.cs | 176 +++++++-------- 11 files changed, 395 insertions(+), 396 deletions(-) diff --git a/logic/Client/Model/MapPatch.cs b/logic/Client/Model/MapPatch.cs index 97c9730f..1a22f728 100644 --- a/logic/Client/Model/MapPatch.cs +++ b/logic/Client/Model/MapPatch.cs @@ -9,16 +9,16 @@ namespace Client.Model { public enum MapPatchType { - Space = 0, + Ground = 0, RedHome = 1, BlueHome = 2, Ruin = 3, - Shadow = 4, - Asteroid = 5, - Resource = 6, - Factory = 7, - Community = 8, - Fort = 9, + Grass = 4, + River = 5, + Garbage = 6, + RecycleBank = 7, + ChargeStation = 8, + SignalTower = 9, WormHole = 10, Null = 11 }; diff --git a/logic/Client/Model/Player.cs b/logic/Client/Model/Player.cs index 38d6446c..a6ac7fb5 100644 --- a/logic/Client/Model/Player.cs +++ b/logic/Client/Model/Player.cs @@ -51,12 +51,12 @@ public int Score } } - private ObservableCollection ships; - public ObservableCollection Ships + private ObservableCollection ships; + public ObservableCollection Sweepers { get { - return ships ?? (ships = new ObservableCollection()); + return ships ?? (ships = new ObservableCollection()); } set { diff --git a/logic/Client/Model/Ship.cs b/logic/Client/Model/Ship.cs index ef252220..ab208650 100644 --- a/logic/Client/Model/Ship.cs +++ b/logic/Client/Model/Ship.cs @@ -16,9 +16,9 @@ namespace Client.Model //public enum TypeEnum //{ - // CivilShip, - // WarShip, - // FlagShip + // CivilSweeper, + // WarSweeper, + // FlagSweeper //} //public enum StateEnum @@ -58,11 +58,11 @@ namespace Client.Model //} - public class Ship : BindableObject + public class Sweeper : BindableObject { private long teamID; - private ShipType type; - private ShipState state; + private SweeperType type; + private SweeperState state; private int Hp; private ProducerType producerModule; private ConstructorType constuctorModule; @@ -76,7 +76,7 @@ public class Ship : BindableObject private string armorModule_s; private string shieldModule_s; private string weaponModule_s; - + public long TeamID { @@ -87,7 +87,7 @@ public long TeamID OnPropertyChanged(); } } - public ShipType Type + public SweeperType Type { get => type; set @@ -96,7 +96,7 @@ public ShipType Type OnPropertyChanged(); } } - public ShipState State + public SweeperState State { get => state; set @@ -161,7 +161,7 @@ public WeaponType WeaponModule } public string Type_s { - get => UtilInfo.ShipTypeNameDict[Type]; + get => UtilInfo.SweeperTypeNameDict[Type]; set { type_s = value; @@ -170,7 +170,7 @@ public string Type_s } public string State_s { - get => UtilInfo.ShipStateNameDict[State]; + get => UtilInfo.SweeperStateNameDict[State]; set { state_s = value; diff --git a/logic/Client/Old/PlayerStatusBar.xaml b/logic/Client/Old/PlayerStatusBar.xaml index 2bd2daa2..51d8e08e 100644 --- a/logic/Client/Old/PlayerStatusBar.xaml +++ b/logic/Client/Old/PlayerStatusBar.xaml @@ -34,7 +34,7 @@