[GS 2.0 Beta]
One of the things I want to feature in Star Maze are ship upgrades, allowing you to attach different thrusters & weapons to your ship.
Given the parent/child system, we know that we can "attach" one sprite to another, like what we did in Tutorial 7. And since our game objects are nothing more than "sprites with a brain", we can actually attach one game object to another. So after incorporating the psuedo-physics parameters & processing to the sprite class, I created a game component meant soley to be a child of another; a thruster module which can be attached to anything to make it move. This is the base thruster class from which you derive to create a custom thruster object:
With the base thruster class you can create a rotational thruster for spinning around or a linear thruster for vector movement. A keyboard key parameter will be monitored by the thruster to turn itself on and off, and there's a parameter to hold how much force the thruster applies to it's parent (whatever it's attached to).
When it Updates, it calls the ApplyForce or ApplyRotationalForce methods of it's parent, and the system moves accordingly.
A simple interface defines two routines, StopThrusterAnim & StartThrusterAnim, which provides the implementor with a place & time to perform graphical feedback as it relates to the thruster entering an On or Off state.
Using this base thruster class I created 4 different game components; custom thrusters which I can attach to the player's ship. Two different aft thrusters (linear), a port thruster (clockwise rotational), and a starboard thruster (counterclockwise rotational).
The player's ship object can now be defined very simply, by creating instances of these thrusters and attaching whichever ones we want accordingly:
Here's a quick lo-fi vid of this in action:
(direct link to youtube)
In the video I start out using one of two aft thruster classes which derive from base thruster (the green one, it produces 500 pixels per second (pps) of linear force). When the app stops and I go into the IDE, I swap out the green 500pps thruster for a different thruster class, a red one that produces 250 pps of linear force. You my also be able to see the tiny little port and starboard thrusters applying the counter- and clockwise rotational forces as well.
As you can see from the Ship class code above, the ship itself has no inherit way to move itself; it's just a hull that doesn't handle keyboard input or anything of the sort. To make it move, we simply attach thruster components. The neat part about this system is that you can attach any thruster you want to any game object you want. You can put thrusters on the player's ship, on enemy AI ships, you can stick one on a bowling ball if you want. The thruster component is a somewhat generic way to apply force, to any game object.
My laptop was lagging for unrelated reasons when I recorded this, so the fps is kinda low, but in an otherwise normal environment the code runs quite nicely.
2 comments:
The code looks pretty slick :D I would like to see the video however but youtube says it is no longer available :(
Ziggy
Ziggyware XNA News and Tutorials
Thanks :) I have changed it somewhat by adding a speed cap, so thrusters understand to max out once the parent velocity matches their force value. The current code just keeps getting faster and faster...
I dunno what youtube's problem is, I'll just add an additional link beneath the embeded video and hope visiting youtube directly does the trick. I see the vid ok, but that may not count since I own it.
Post a Comment