On this episode, we will be fixing our Floaters so that they have more functionality. Of course, this will come at a price: the floaters have to have their own structure, and all important members of this structure will have to be saved to and loaded from file when we save them. Now, a Floater that moves in a circle is fine, but it would be nice if it moved in other shapes as well. This, of course, requires us to make more periodic functions and, since we cannot store a function into a variable, we'll have to have a number to represent which function to use. This screams "Enumeration", so we'll use an Enumeration to determine which function to use. Of course, with all periodic functions, we need to determine an origin, a radius, and a frequency. The origin is just the initial position of the floater, which can be represented as the initial rectangle values, so the origin can be omitted. Our periodic functions are defining how much the floater moves during each frame, so we'll just call the radius a radispeed (or whatever). Since there will be a periodic function for horizontal and another for vertical movement (right now we're using Cos for horizontal and Sin for vertical), we need to have a pair of enumeration variables, a pair of radispeed, and of frequency. In order to make frequency values be stored in an integer, we will be dividing the frequency by a number like 64 or 128, so that we can have a value that represents a frequency of 1, and lower numbers will represent a frequency that is less than 1. Side Note: Since our periodic functions are defining how much the floater moves each frame, the actual radius (let's call it r) is multiplied by the frequency of the periodic function (let's say f) to give us the new coefficient (radispeed) of the periodic function (s, let's say). In our last example, the coefficient which multiplied the sine and cosine was 5 and the frequency is 1/8, so the actual radius of the floater here is s/f = r. So 5 divided by one-eighth (not divided by eight) gives us 40. Now, let's get to declaring the floater function enumeration and structure. First, the enumeration:

public enum desgperiodic { stable = 0, cosinefxn, sinefxn } //Designates periodic functions that will be used to move a floater. |

public struct levfloater { private Rectangle rect; private desgperiodic hfxn; private desgperiodic vfxn; private int xrveloc; private int yrveloc; private int xfreq; private int yfreq; } |

public Rectangle Loc { set { rect = value; } get { return rect; } } public desgperiodic HorizFxn { get { return hfxn; } set { hfxn = value; } } public desgperiodic VertFxn { get { return vfxn; } set { vfxn = value; } } public int radispeedX { get { return xrveloc; } set { xrveloc = value; } } public int radispeedY { get { return yrveloc; } set { yrveloc = value; } } public int XFrequency { get { return xfreq; } set { xfreq = value; } } public int YFrequency { get { return yfreq; } set { yfreq = value; } } |

public levfloater(Rectangle Location, desgperiodic HFxn, desgperiodic VFxn, int XRadispeed, int YRadispeed, int XFrequency, int YFrequency) { rect = Location; hfxn = HFxn; vfxn = VFxn; xrveloc = XRadispeed; yrveloc = YRadispeed; xfreq = XFrequency; yfreq = YFrequency; } |

public void Offset(int dx, int dy) { // Moves the underlying location rectangle. rect.Offset(dx, dy); } |

levfloater floater; // This holds the location and information of the floater. |

const int thefrequencydivider = 128; // This number will divide the integer frequencies of the floater to hopeful fractions. |

floater = new levfloater(new Rectangle(480, 280, thefloatersize, thefloatersize), 5, 5, 16, 16, desgperiodic.cosinefxn, desgperiodic.sinefxn ); // Set up floater information. |

gfx.DrawImage(floatbmp, floater.Loc); // Draw the floater. |

if (playerloc.IntersectsWith(floater.Loc )) { // Touched the floater. isgoner = true; playerveloc = PFMain.thesbarinitialvelocity; } |

int dx, dy; //Describes the amount by which a floater will move. |

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. |