Community Lounge

Reply

Level 3

Level 3
GibFather
Posts: 57
Registered: ‎23-12-2011
Message 1 of 8 (248 Views)

MVC Design Question

Hey guys, I thought I'd post a question here since there seems to be some knowledgeable and experienced programmers here.

 

I'm trying to use the MVC(Model-View-Controller) design pattern for an engine.  I had a quick question for the model portion of the code.  Can my model class include an update function or is that strictly handled by the controller?

 

Thanks!

www.kahunastudios.com
Please use plain text.

Re: MVC Design Question

[ Edited ]

I have a little experience with the MVVM pattern which is very similar to MVC and I would think people would prefer update code in the C or VM. The model wouldn't have a sense of time or interactions with the room, other actors, etc so updating would be limited.

 

I've been toying with this idea too but my impression is that implementing it would entail forgoing use of GameEngine2d completely. I'm waiting to see if later versions of PSS included collision detection as part of GE2d before I make up my mind.

 

Edit: Here's a series of articles about Coco2d and MVC.

Please use plain text.

Level 3

Level 3
GibFather
Posts: 57
Registered: ‎23-12-2011
Message 3 of 8 (239 Views)

Re: MVC Design Question

Thanks for the quick reply!  So this what I have so far.  I've defined my model, view, and controller classes.  My model class contains all the raw data the game needs, my views reads from the model in order to get whatever information it needs to display, and my controller controls the updates and render calls to all the views as well as send events to the model to change states.

 

However, I also have an update method inside my model class which the controller also calls within its own update method.  So according to what you said, it should be best if the model didn't have to worry about updating the game world and is just a container for world data correct?

www.kahunastudios.com
Please use plain text.

Re: MVC Design Question

As I see it, the model would not have an update method. The controller or viewmodel would have the update method.

 

Keep in mind that my experience is with MVVM and not MVC. The components in MVVM have slightly different resposibilities than in MVC.

 

But here is what I think PSS would look like in psuedocode as MVVM. As I imagine it, the view would actually be a class that derives from some Node class, such as SpriteUV. If it worked like this then I could understand using the Director more.

 

public class BadGuyView : SpriteUV
{
  public BadGuyView(BadGuyViewModel vm)
  {
    OnCollision += HandleCollision;
    BindingSource = vm;
    Position = BindTo(BindingSource.Position);
  }

  public override void HandleCollision(Object sender, CollisionEventArgs e)
  {
    Message.Send(this, MsgToken.CollisionDetected, sender, e);
  }
}

public class BadGuyViewModel : ViewModelBase
{
  public Game _Game;
  public BadGuyModel _Model;
  public BadGuyView _View;

  public BadGuyViewModel(Game game)
  {
    _Game = game;
    _Model = new BadGuyModel();
    _View = new BadGuyModel(this);

    _Game.Director.AddToScene(_View);

    //Message.Listen(_View, MsgToken.GamePadInput, OnGamePadInputOccured);
    Message.Listen(_View, MsgToken.CollisionDetected, OnCollisionOccured);
  }

  //Called every frame by Game.Director
  public override void Update(Int32 ticksSinceLastUpdate)
  {
    Position += 20 * ticksSinceLastUpdate;
  }

  public void OnCollisionOccured(MessageArgs args)
  {
    Position = CalcNewPosition(args.ObjectHit, args.Angle);
  }

  public Vector2 Position
  {
    { get { return _Model.Position; }
    { set { _Model.Position = value; }
  }
}

public class BadGuyModel : ModelBase
{
  public Vector2 Position;
}

 

Please use plain text.

Re: MVC Design Question

I'm curious how you plan to use MVC in PSS. Are you doing a GameEngine2d game or low level 2d or 3d?

Please use plain text.

Re: MVC Design Question

PS- I was reading up on MVC and it seems like your idea of an Update method in the model is sound. MVVM moves all of that type of code from the model into the viewmodel.

Please use plain text.

Level 3

Level 3
GibFather
Posts: 57
Registered: ‎23-12-2011
Message 7 of 8 (223 Views)

Re: MVC Design Question

[ Edited ]

Hey there..sorry for the late reply!  I just got back home.  But to answer your question, I'm not doing MVC with PSS but rather taking a take home test that I need to do in order to move forward in the job interview process. 

 

I think I have everything set up correctly where my views only display game data, my controller has handles to all the active views and calls the various update and render calls for each view.  The controller also handles all events from the views and passes it onto the model.  My model contains all the raw data that the game would need to function.  I was just curious if it would be okay to have an update method within the model and just make sure the controller was the one calling it rather than the model handling its own update itself.

 

I just wanted to make sure this was okay before I submitted my code samples to the company I'm applying for.  Right now, I'm doing everything in Actionscript 3.  But looking at it some more, the MVC pattern addresses many dependency issues I had in the past.  I haven't looked into how I can utilize this with PSS but now that I know a little more, it could be worth investigating.

 

Thanks again for the help and the time you spent replying!  :smileyhappy:

www.kahunastudios.com
Please use plain text.

Level 3

Level 3
GibFather
Posts: 57
Registered: ‎23-12-2011
Message 8 of 8 (219 Views)

Re: MVC Design Question

Thanks again for that link!  I read the second part of his article and he did state that the model has its own update method.  So with that, I aim to wrap things up!  Many thanks again for the help and the info!  Good stuff.  :smileyhappy:

www.kahunastudios.com
Please use plain text.
Announcements

Welcome to the PlayStation Mobile Developer Forums


This is a community for the discussion of technical topics with other developers and SCE engineers. Posting ideas/requests are also appreciated. Join the discussion!

PlayStation®Mobile開発者フォーラムでは世界中の開発者の皆様と一緒に、議論や情報交換が可能です。SCEも議論に参加し、皆様の開発をサポートします。アイデアやリクエストも大歓迎です。ぜひご参加ください。

PSM Developer Registration (for free) on PSM DevPortal is required to post on the forum.
Please sign out then sign in again to the forum and PSM DevPortal after you have completed the registration.

フォーラムへ投稿をするにはPSM DevPortalへの登録(無料)が必要です。
登録後はフォーラムと PSM DevPortalを一度ログアウトし、再度ログインしてください。