Profil de StefaneXtreme's PlacePhotosBlogListesPlus ![]() | Aide |
|
21 avril How to implement GameService?If you're working on a game that is made of several GameComponents, and you find out that "Skybox" component needs access of Matrix world, view and projection founded inside "Camera" component, then you're pretty much in trouble if you don't know how GameService works! Trying to make a instance of the Camera component (in this case) in Skybox, is simply impossible - because Camera component requires us to pass a game reference to it..Trying few other ways will fail as well.. That's where GameServices comes into play.A game service is nothing more than a simply interface, which you can use for passing fields, methods..Now there another problem occurs - wait we can't use fields in Interfaces right? Well that's not really a problem when you have the field you want into component itself.. Below is the example of using Player's (component) view Matrix inside Skybox (component): // Player Component public interface IPlayerShipService { Matrix View { get; set; } } public class Player : DrawableGameComponent, IPlayerShipService { private Matrix view; public Matrix View { get { return view; } set { view = value; } } public Player(Game game) : base(game) { game.Services.AddService(typeof(IPlayerShipService), this); } } // Skybox public partial class Skybox : DrawableGameComponent { Matrix view; Matrix proj; public override void Update(GameTime gameTime) { IPlayerShipService playerService = (IPlayerShipService)Game.Services.GetService(typeof(IPlayerShipService)); view = playerService.View; proj = playerService.Proj; } } So this is working example, as you can see later inside Skybox/Update(), we can simply assign view and proj to the Player's view and proj.The reason why I'm doing this inside Update() is so they will be updated. The reason why I'm inheriting interface IPlayerShipService from Player GameComponent is because we need to assign interface to that component.We finish that off by writing line of code in constructor..Later we simply use GetService in the Skybox Update() method to get all the things we need from the interface IPlayerShipService.. So that will wrap up this article - GameService and how they work! I hope you've enjoyed reading it and learned something from it, because this is really, I mean, really important if you're dealing with GameComponent.. If you have any question just drop a comment and I'll respond as soon as I can! Cheers Commentaires (5)Pour ajouter un commentaire, connectez-vous avec votre identifiant Windows Live ID (si vous utilisez Messenger ou Xbox LIVE, vous avez un identifiant Windows Live ID). Connectez-vous Vous n'avez pas d'identifiant Windows Live ID ? Inscrivez-vous
RétroliensL'URL de rétrolien de ce billet est : http://yostefan.spaces.live.com/blog/cns!B0538EFE99343A0C!162.trak Blogs Web qui font référence à ce billet
|
|
|