模型视图控制器 - 在哪里保留简单逻辑
我经常看到模型视图控制器模式的非常不同的实现,并且完全理解您应该适应和使用最适合您需求的东西,但我想知道将简单的游戏逻辑保留在以太中的优点/缺点/最佳实践是什么控制器或型号?
从本质上讲,我应该这样做的正确方法是什么?
对于这个简单的例子,玩家受到伤害,我列出了三种可能的处理方法:
1.
控制器:
_model.playerDamage - 15;
if (_model.playerDamage <= 0){
_model.playerLives --;
_model.restartLevel();
}
2.
控制器:
_model.playerDamage = 15;
型号:
function set playerDamage(value:int){
playerDamage = value;
updatePlayer();
}
function updatePlayer():void{
if (playerDamage<=0){
palyerLives --;
restartLevel();
}
}
3.
控制器:
_model.playerDamage = 15;
_model.addEventListener('playerChange', checkPlayerStatus);
function checkPlayerStatus(e:Event):void{
if (_model.playerDamage<=0){
_model.playerLives --;
_model.restartLevel();
}
}
型号:
function set playerDamage(value:int){
playerDamage = value;
dispatchEvent(new Event('playerChange'));
}
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'));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然在模型中,因为您可能有多个控制器(将来),它们以类似或相同的方式影响模型中的事物。控制器只是将 UI 事件转换为业务事件的机制。模型是处理逻辑的地方。
您可能会发现以下 stackoverflow 线程很有用:
虽然它们是特定于 Java 的,但这里讨论的想法是独立于平台的。
希望有帮助。
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.