Profil de StefaneXtreme's PlacePhotosBlogListesPlus Outils Aide

Blog


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)

Veuillez patienter...
Le commentaire entré est trop long. Raccourcissez-le.
Vous n'avez rien entré. Réessayez.
Il est actuellement impossible d'ajouter votre commentaire. Réessayez plus tard.
Pour ajouter un commentaire, tu dois avoir l'autorisation de tes parents. Demander l'autorisation
Tes parents ont désactivé les commentaires.
Il est actuellement impossible de supprimer votre commentaire. Réessayez plus tard.
Vous avez dépassé le nombre maximal de commentaires qu'il est possible d'envoyer le même jour. Réessayez dans 24 heures.
Votre compte a pu laisser les commentaires désactivés parce que nos systèmes indiquent que vous risquez d'arroser d'autres utilisateurs de messages. Si vous pensez que votre compte a été désactivé par erreur, contactez l'assistance en ligne de Windows Live.
Effectuez la vérification de sécurité ci-dessous pour finaliser l'envoi de votre commentaire.
Les caractères entrés pour la vérification de sécurité doivent correspondre à ceux de l'image ou du fichier audio.

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

17 Nov.
Aucun noma écrit :
労働問題 収益物件不動産売却などにはマンション査定土地売買1戸建て売却が含まれる賃貸 住宅不動産 賃貸賃貸マンション新築マンションもしっかりカバーしてありすごく充実したさいとでもちろん投資を目的の方やリフォームをしたい人もすごく参考になるだう。ところで今,SEO対策などいまはやっているがホームページ制作会社にいらいしてもうまくはいかないようだ。最近私は、資産運用にこっていて税金対策にインテリアを集めている。もちろんファッションにこだわりブランド品や下着,ランジェリーにはこだわりがある。 化粧品ダイエット用品高価なものがよく家具も最高級しか買わない、先日海外旅行にいってきてお土産に外車結婚指輪と高級時計をかったが、日本でしらべたら通販ですごく安く売っていた。 物件探しは広島 不動産 岡山 不動産 松山市 不動産 香川県 不動産 徳島 不動産 高知 不動産 高松 不動産をフルカバーしてます大手で 和歌山 富山 滋賀 石川 山梨 新潟 沖縄 大分 鹿児島 宮崎 熊本 高知
17 Nov.
4 Nov.
4 Nov.
18 Fév.

Rétroliens

L'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
  • Aucune