In this episode, we continue with the floaters. Among some strange new things that we'll have to deal with concerning the floaters is how to set the properties of the floater using the editor. Other things like using the floater arraylist and file I/O should be common knowledge by now. First, we will only be concerning ourselves with getting multiple floaters. This should be simple by now, but I'll show it again (as always). Declaration:
ArrayList floaters; /* This holds all of the floaters that are in the level */ |
floater = new levfloater(new Rectangle(480, 280, thefloatersize, thefloatersize), 5, 5, 16, 16, desgperiodic.cosinefxn, desgperiodic.sinefxn ); // Set up floater information. |
floaters = new ArrayList(10); floaters.Add(new levfloater(new Rectangle(480, 280, thefloatersize, thefloatersize), 5, 5, 16, 16, desgperiodic.cosinefxn, desgperiodic.sinefxn )); floaters.Add(new levfloater(new Rectangle(100, 100, thefloatersize, thefloatersize), 5, 5, 16, 16, desgperiodic.cosinefxn, desgperiodic.stable)); // Put some floaters' information into the floater list. |
foreach (levfloater floater in floaters) { gfx.DrawImage(floatbmp, floater.Loc); // Draw the floater. } |
for (lv = 0; lv < floaters.Count; lv++) { //Get a floater from the arraylist and move it. floater = (levfloater)floaters[lv]; switch (floater.HorizFxn) { case desgperiodic.cosinefxn: dx = (int)(floater.radispeedX * Math.Cos((double)floater.XFrequency * floattick / thefrequencydivider)); break; case desgperiodic.sinefxn: dx = (int)(floater.radispeedX * Math.Sin((double)floater.XFrequency * floattick / thefrequencydivider)); break; default: dx = 0; break; } //Calculates how much to move horizontally. switch (floater.VertFxn) { case desgperiodic.cosinefxn: dy = (int)(floater.radispeedY * Math.Cos((double)floater.YFrequency * floattick / thefrequencydivider)); break; case desgperiodic.sinefxn: dy = (int)(floater.radispeedY * Math.Sin((double)floater.YFrequency * floattick / thefrequencydivider)); break; default: dy = 0; break; } //Calculates how much to move vertically. floater.Offset(dx, dy); //And then the floater is moved. floaters[lv] = floater; //Must set the floater back into the array, otherwise changes above are discarded. } floattick += 1; |
foreach (levfloater floater in floaters) { if (playerloc.IntersectsWith(floater.Loc )) { // Touched the floater. isgoner = true; playerveloc = PFMain.thesbarinitialvelocity; } } |
private void FloaterEd_Load(object sender, System.EventArgs e) { comboBox1.Items.AddRange(System.Enum.GetNames(desgperiodic.stable.GetType())); comboBox2.Items.AddRange(System.Enum.GetNames(desgperiodic.stable.GetType())); //lists all of the periodic functions in the combobox. } |
private levfloater flot; // Floater data that will be passed onto the game form. public levfloater Floater { get { return flot; } } |
private void button1_Click(object sender, System.EventArgs e) { desgperiodic hf = (desgperiodic)comboBox1.SelectedIndex; desgperiodic vf = (desgperiodic)comboBox2.SelectedIndex; //Extract functions from enumeration by way of the index. int xrspd = (int)this.numericUpDown1.Value; int yrspd = (int)this.numericUpDown2.Value; int xfreq = (int)this.numericUpDown3.Value; int yfreq = (int)this.numericUpDown4.Value; //extract floater properties from nud controls. //At this point, we know everything about the floater except where it is... //this will be determined by the editor. flot = new levfloater(Rectangle.Empty, xrspd, yrspd, xfreq, yfreq, hf, vf); //The floater is ready... we close the form. this.Close(); } |
else if (e.KeyCode == Keys.F) { if (!ismousedown) { modobject = 'F'; } } |
case 'F': FloaterEd F = new FloaterEd(); //Create the new prompt form. levfloater ftr; clock.Stop(); //Stops the timer so that it doesn't run in excess while we are putting in floater info. F.ShowDialog(); //Show dialog. Execution continues when it closes. ftr = F.Floater; //Get data from form to our floater. ftr.Loc = new Rectangle(initialpt.X, initialpt.Y, thefloatersize, thefloatersize); //Set the location property that we know now. floaters.Insert(0, ftr); //Insert into list of floaters. //Note that after we add the floater to the form, its current movement is not indicative of its movement //immediately after we load from the file. clock.Start(); break; |
bw.Write(floaters.Count); //Write floaters to the file. foreach (levfloater floater in floaters) { bw.Write(floater.Loc.Left); bw.Write(floater.Loc.Top); bw.Write(floater.radispeedX); bw.Write(floater.radispeedY); bw.Write(floater.XFrequency); bw.Write(floater.YFrequency); bw.Write(floater.HorizFxn); bw.Write(floater.VertFxn); } |
floaters = new ArrayList(br.ReadInt32()); // Floater count. for (lv = 0; lv < floaters.Capacity; lv++) { floaters.Add(new levfloater(new Rectangle(br.ReadInt32(), br.ReadInt32(), thefloatersize, thefloatersize), br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), (desgperiodic)br.ReadInt32(), (desgperiodic)br.ReadInt32())); //Put all of this information. It's important that the integers are written to the file in order of their parameters. //Otherwise, we'd need temporary variables to reroute the data coming from file. } |
floaters = new ArrayList(10); floaters.Add(new levfloater(new Rectangle(480, 280, thefloatersize, thefloatersize), 5, 5, 16, 16, desgperiodic.cosinefxn, desgperiodic.sinefxn )); floaters.Add(new levfloater(new Rectangle(100, 100, thefloatersize, thefloatersize), 5, 5, 16, 16, desgperiodic.cosinefxn, desgperiodic.stable)); // Put some floaters' information into the floater list. |