Reputation: 5960
I often see very different implementations of the Model View Controller Pattern, and completely understand that you should adapt and use what fits your needs the best, but I was wondering what would be the advantages/disadvantages/best practice of keeping simple game logic in ether the controller or model?
In essence which is the correct way I should be doing this?
for this simple example the player receives damage and I have listed three possible ways of dealing with it:
1.
contoller:
_model.playerDamage - 15;
if (_model.playerDamage <= 0){
_model.playerLives --;
_model.restartLevel();
}
2.
controller:
_model.playerDamage = 15;
model:
function set playerDamage(value:int){
playerDamage = value;
updatePlayer();
}
function updatePlayer():void{
if (playerDamage<=0){
palyerLives --;
restartLevel();
}
}
3.
controller:
_model.playerDamage = 15;
_model.addEventListener('playerChange', checkPlayerStatus);
function checkPlayerStatus(e:Event):void{
if (_model.playerDamage<=0){
_model.playerLives --;
_model.restartLevel();
}
}
model:
function set playerDamage(value:int){
playerDamage = value;
dispatchEvent(new Event('playerChange'));
}
Upvotes: 4
Views: 503
Reputation: 4177
Ofcourse in Model because you may have multiple controllers (in future) which affect things in Model in similar or same way. Controllers are just a mechanism to translate UI events into business events. Model is the place that crunches the logic.
You may find following stackoverflow threads useful:
Though they are java specific but the ideas discussed here are platform independent.
Hope that helps.
Upvotes: 6