/// <summary> /// Get the list of weapons from the server, which helps the client list statistics /// about each weapons. Repeated calls on this method are allowed, because it's /// cached locally after the first call so as to not waste the server's time. /// </summary> /// <returns>Array list of weapon types.</returns> /* public VTankObject.Weapon[] GetWeaponList() * { * if (weaponList != null) * { * return weaponList; * } * * weaponList = session.GetWeaponList(); * * return weaponList; * }*/ /// <summary> /// Create a new tank synchronously. This method may throw an exception if some values /// are invalid. /// </summary> /// <param name="name">Name of the tank.</param> /// <param name="speedFactor">Speed factor of the tank.</param> /// <param name="armorFactor">Armor factor of the tank.</param> /// <param name="model">Model name of the tank.</param> /// <param name="weaponId">Weapon ID used with the tank.</param> /// <param name="color">Color of the tank.</param> /// <returns>True if the attempt succeeded, false if an error occurred.</returns> public bool CreateTank(string name, float speedFactor, float armorFactor, string model, string skin, int weaponId, VTankObject.VTankColor color) { VTankObject.TankAttributes tank = new VTankObject.TankAttributes( name, speedFactor, armorFactor, 0, 100, model, skin, weaponId, color); CheckTank(tank); try { if (session.CreateTank(tank)) { tankListNeedsRefresh = true; return(true); } } catch (Ice.Exception e) { logger.Error("CreateTank() Ice.Exception: {0}", e); throw new ConnectionLostException("You have lost connection with the server."); } catch (System.Exception e) { logger.Error("CreateTank() System.Exception: {0}", e); throw new UnknownException("An unknown error occurred."); } return(false); }
/// <summary> /// Update an existing tank with new information asynchronously. This method may throw an /// exception if the information is invalid, or if an unknown error occurs. /// </summary> /// <param name="name">Name of the tank.</param> /// <param name="speedFactor">Speed factor of the tank.</param> /// <param name="armorFactor">Armor factor of the tank.</param> /// <param name="model">Model name of the tank.</param> /// <param name="weaponId">Weapon ID used with the tank.</param> /// <param name="color">Color of the tank.</param> /// <param name="callback">Method to be called once it finishes.</param> public void UpdateTankAsync(string name, float speedFactor, float armorFactor, string model, string skin, int weaponId, VTankObject.VTankColor color, ActionFinished <bool> callback) { VTankObject.TankAttributes tank = new VTankObject.TankAttributes( name, speedFactor, armorFactor, 0, 100, model, skin, weaponId, color); CheckTank(tank); try { session.UpdateTank_async(new UpdateTank_Callback(callback), name, tank); } catch (Ice.Exception e) { logger.Error("UpdateTankAsync() Unexpected Ice.Exception: {0}", e); throw new UnknownException("Connection lost: An unknown error occurred."); } catch (System.Exception e) { logger.Error("UpdateTankAsync() Unexpected System.Exception: {0}", e); throw new UnknownException("An unknown error occurred."); } // Assume the best. tankListNeedsRefresh = true; }
/// <summary> /// Event handler for when the user selects a different tank /// </summary> void SelectionChanged(object sender, TomShane.Neoforce.Controls.EventArgs e) { if (tanks.Length > 0) { form.PlayButton.Enabled = true; form.EditButton.Enabled = true; form.DeleteButton.Enabled = true; selectedTank = tanks[form.SelectedIndex]; form.Armor = GameForms.Utils.ConvertFactorToProgressBar(selectedTank.armorFactor); form.Speed = GameForms.Utils.ConvertFactorToProgressBar(selectedTank.speedFactor); Scene scene = ServiceManager.Game.Renderer.ActiveScene; scene.ClearAll(); tankObj = new Renderer.SceneTools.Entities.Object3( ServiceManager.Resources.GetModel(@"tanks\" + selectedTank.model), Vector3.Zero); tankObj.MeshColor = Toolkit.GetColor(selectedTank.color); tank = scene.Add(tankObj, 0); turretObj = new Renderer.SceneTools.Entities.Object3( ServiceManager.Resources.GetModel(@"weapons\" + WeaponLoader.GetWeapon(selectedTank.weaponID).Model), Vector3.Zero); turretObj.MeshColor = Toolkit.GetColor(selectedTank.color); turret = scene.Add(turretObj, 0); turretObj.Attach(tankObj, Constants.TURRET_MOUNT); ApplySkin(); } }
/// <summary> /// Check a tank first before asking the server to accept it. /// Note that this method will throw an exception if it fails the test. /// </summary> /// <param name="tank">Tank to test.</param> private void CheckTank(VTankObject.TankAttributes tank) { if (string.IsNullOrEmpty(tank.name)) { throw new InvalidValueException("Tank names cannot be empty."); } Regex pattern = new Regex("[^A-Za-z0-9]"); if (pattern.IsMatch(tank.name)) { throw new InvalidValueException( "Tank names must contain only letters and numbers."); } if (tank.name.Contains(" ")) { throw new InvalidValueException( "Tank names are not allowed to have spaces."); } if (tank.speedFactor < 0.5f || tank.speedFactor > 1.5f || tank.armorFactor < 0.5f || tank.armorFactor > 1.5f) { throw new InvalidValueException( "The speed factor and armor factor must be between 50% and 150%."); } float difference_speed = (float)Math.Abs(1.0f - Math.Round(tank.speedFactor, 2)); float difference_armor = (float)Math.Abs(1.0f - Math.Round(tank.armorFactor, 2)); if (difference_speed - difference_armor > 0.001f) { throw new InvalidValueException( "The speed factor and armor factor values must have exactly " + "the same distance from 100%."); } if (String.IsNullOrEmpty(tank.model)) { throw new InvalidValueException( "You must select a tank model to use."); } if (tank.color.green < 0 || tank.color.green > 255 || tank.color.red < 0 || tank.color.red > 255 || tank.color.blue < 0 || tank.color.blue > 255) { throw new InvalidValueException( "The RGB value of the tank must be between 0 and 255."); } if (tank.weaponID < 0) { throw new InvalidValueException( "The weapon ID must not be negative."); } }
/// <summary> /// Constructor: Will automatically select the tank that is passed in if it exists. /// </summary> public TankListState(VTankObject.TankAttributes _selectedTank) { ServiceManager.Game.Renderer.ActiveScene.ClearAll(); form = new TankList(ServiceManager.Game.Manager); form.CreateButton.Click += new TomShane.Neoforce.Controls.EventHandler(CreateButton_Click); form.EditButton.Click += new TomShane.Neoforce.Controls.EventHandler(EditButton_Click); form.DeleteButton.Click += new TomShane.Neoforce.Controls.EventHandler(DeleteButton_Click); form.PlayButton.Click += new TomShane.Neoforce.Controls.EventHandler(PlayButton_Click); form.BackButton.Click += new TomShane.Neoforce.Controls.EventHandler(BackButton_Click); form.SelectionChanged += new TomShane.Neoforce.Controls.EventHandler(SelectionChanged); form.PlayButton.Enabled = false; form.EditButton.Enabled = false; form.DeleteButton.Enabled = false; selectedTank = _selectedTank; RefreshTankList(); ServiceManager.Game.Renderer.ActiveScene.SwitchCamera("Tank Display View"); GraphicOptions.graphics.GraphicsDevice.RenderState.DepthBufferEnable = true; Renderer.GraphicOptions.BackgroundColor = Color.Black; }
private void RefreshTankListForm() { // This action occurs in the GUI thread. if (tanks != null) { List <object> t = new List <object>(); foreach (VTankObject.TankAttributes attr in tanks) { t.Add((object)attr.name); } form.List = t; } if (tanks.Length > 0) { if (selectedTank != null) { if (form.List.Contains(selectedTank.name)) { form.SelectedIndex = form.List.IndexOf(selectedTank.name); selectedTank = tanks[form.SelectedIndex]; } else { form.SelectedIndex = 0; selectedTank = tanks[0]; } } else { form.SelectedIndex = 0; selectedTank = tanks[0]; } form.PlayButton.Enabled = true; form.EditButton.Enabled = true; form.DeleteButton.Enabled = true; } }
/// <summary> /// Event handler for the creation successful dialog /// </summary> void Confirm_Closed(object sender, TomShane.Neoforce.Controls.WindowClosedEventArgs e) { VTankObject.TankAttributes selTank = new VTankObject.TankAttributes(); selTank.name = form.TankName; ServiceManager.StateManager.ChangeState(new TankListState(selTank)); }
private void RefreshTankListForm() { // This action occurs in the GUI thread. if (tanks != null) { List<object> t = new List<object>(); foreach (VTankObject.TankAttributes attr in tanks) { t.Add((object)attr.name); } form.List = t; } if (tanks.Length > 0) { if (selectedTank != null) { if (form.List.Contains(selectedTank.name)) { form.SelectedIndex = form.List.IndexOf(selectedTank.name); selectedTank = tanks[form.SelectedIndex]; } else { form.SelectedIndex = 0; selectedTank = tanks[0]; } } else { form.SelectedIndex = 0; selectedTank = tanks[0]; } form.PlayButton.Enabled = true; form.EditButton.Enabled = true; form.DeleteButton.Enabled = true; } }
/// <summary> /// Constructor /// </summary> /// <param name="_selectedTank">The selected tank used for editing</param> public EditTankState(VTankObject.TankAttributes _selectedTank) { tank = _selectedTank; selectedTankModelName = tank.model; selectedTurretName = WeaponLoader.GetWeapon(tank.weaponID).Name; }