Virtual Earth 3D Flight Simulator – part 4

You now have to create the main functionality of the plug-in. You will want to create some global Airport objects like so:

private Airport lasVegas = new Airport(36.07639, -115.12571, 0, -14, 89.42);
private Airport torontoPearson = new Airport(43.6738, -79.66459, 0, -14, -47.13);
private Airport newyorkNewark = new Airport(40.67988, -74.17279, 0, -14, -24.56);
private Airport seattleTacoma = new Airport(47.43102, -122.30807, 0, -14, -2);

You will want to create a method to hook into the JavaScript from the client side. This can be done like so:

private void RegisterInterfaces()
{
    this.UnregisterInterfaces();
    if (!this.interfacesRegistered)
    {
        this.Host.CommunicationManager.AttachToEvent(this.Guid, "FlyToAirport", "FlyToAirportHandler", FlyToAirport);
        this.Host.CommunicationManager.AttachToEvent(this.Guid, "ChangeCockpit", "ChangeCockpitHandler", ChangeCockpit);
        this.interfacesRegistered = true;
    }
}

For good practice you should also create a method to unreferenced these hooks. This can be done like so:

private void UnregisterInterfaces()
{
    if (this.interfacesRegistered)
    {
        this.Host.CommunicationManager.DetachFromEvent(this.Guid, "FlyToAirport", "FlyToAirportHandler");
        this.Host.CommunicationManager.DetachFromEvent(this.Guid, "ChangeCockpit", "ChangeCockpitHandler");
        this.interfacesRegistered = false;
    }
}

In order to fly between airports you will need to create a method called FlyToAirport which will determine which airport you want to fly to and will navigate the camera to the runway at the selected airport. This method should like this:

private void FlyToAirport(string functionName, object airport)
{
    switch (airport.ToString())
    {
        case "LasVegas":
            lasVegas.FlyToHere(this.Host);
            break;
        case "TorontoPearson":
            torontoPearson.FlyToHere(this.Host);
            break;
        case "newyorkNewark":
            newyorkNewark.FlyToHere(this.Host);
            break;
        case "seattleTacoma":
            seattleTacoma.FlyToHere(this.Host);
            break;
        default:
            break;
    }
}

In order to change the cockpit overlay you will need to create a method called ChangeCockpit. This method will determine which cockpit the user has selected and will call the UpdateCockpit method of the airplane. This method should like this:

private void ChangeCockpit(string functionName, object cockpit)
{
    switch (cockpit.ToString())
    {
        case "cockpit1":
            airplane.UpdateCockpit(this.Host, "SimpleFlightSimulator.cockpit.png", new Point(0, 0), new Size(1024, 768), Color.White, new Point(755, 375));
            break;
        case "cockpit2":
            airplane.UpdateCockpit(this.Host, "SimpleFlightSimulator.cockpit2.png", new Point(0, 268), new Size(1024, 500), Color.GreenYellow, new Point(430, 375));
            break;
        case "cockpit3":
            airplane.UpdateCockpit(this.Host, "SimpleFlightSimulator.cockpit3.png", new Point(0, 0), new Size(1024, 768), Color.DarkBlue, new Point(560, 235));
            break;
        default:
            break;
    }
}

In the Activate method you will want to create and Airplane object and load it. You can also want to create a timer object that will call a method calls the UpdateGaage method of your airplane object. You will also want to call the RegisterInerfaces method. This method should now look like this:

public override void Activate(object activationObject)
{
    base.Activate(activationObject);

    //create the airplane cockpit overlay
    airplane = new Aiplane(this.Host);
    airplane.Load("SimpleFlightSimulator.cockpit.png", new Point(0, 0), new Size(1024, 768), Color.White, new Point(775, 375));

    //registers a javascript interface
    RegisterInterfaces();

    //use a timer to update the gauges every 250ms
    timer = new Timer(new TimerCallback(airplane.UpdateGauges), null, 0, 250);

    //start off at the Toronto Pearson airport
    FlyToAirport(null,"TorontoPearson");
}

The Deactivate method should also be updated to call the UnregisterInterfaces method and setting the timer timeout to infinite. Setting a timers timeout to infinite essentially disables it. This method should now look like this:

public override void Deactivate()
{
    base.Deactivate();

    //stop the timer
    timer.Change(Timeout.Infinite, 25);

    //unregister the javascript interface
    UnregisterInterfaces();
}

 

In order to load the cockpit images into the plug-in you will have to make them embedded resources. This can be done by right clicking on the images and going to the properties. Set the Build Action to Embedded Resource.

The final step is to strongly name the plug-in. This is required to give the plug-in partial trust to your system. To create a strongly name key for this plug-in:

  1. Open a Visual Studio Command Prompt.
  2. At the command prompt, type cd C:\Samples\Test (or the directory where you created your application) and press ENTER.
  3. At the command prompt, type sn -k TestPlugin.snk and press ENTER.
  4. At the command prompt, type exit, and press ENTER.
  5. In Visual Studio, right click your project and select Properties.
  6. Click the Signing tab and check Sign the assembly.
  7. In the Choose a strong name key file drop down list, select <Browse…>
  8. Select the SimpleFlightSimulatorPlugin.snk file and click Open. The strong name file is automatically added to your project.
  9. Save the properties window and then close it.

You can now build your project to compile the plug-in DLL. You can then open the HTML in a browser to view your flight simulator. To increase the sensation of flying around Virtual Earth with your own flight simulator try using an Xbox controller. The built in navigation controls of Virtual Earth recognizes the Xbox controller. The controls are similar to Halo.

The complete source code for this application can be downloaded here:

http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Sample%20code/SimpleFlightSimulator.zip


Part 1 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!756.entry
Part 2 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!752.entry
Part 3 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!751.entry

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s