时间延迟和模型视图控制器
我正在实现一款回合制游戏,有两侧,每侧都有多个单位,在每个特定时刻只有一个单位可以在棋盘上移动。
由于一次只能移动一个单位,在我弄清楚它应该去哪里之后,就模拟而言,它可以立即传送到那里,但玩游戏时你会希望看到单位移动,以便你意识到谁搬家以及他去了哪里。
问题是,您是否会将移动算法(例如,在 N 秒内的 2 点之间进行插值)放入模型中,然后让视图显示处于插值位置的单位,甚至不知道它正在移动,或者传送该单位并通知认为它应该显示单位按其想要的最佳方式移动。
如果您采用第二种方法,如何防止模拟运行在视图前面太远,您是否会让视图在运动结束后恢复模拟?
预先感谢,Xtapodi。
I am implementing a turn based game, there are two sides and each side has several units, at each specific moment only one unit can move across the board.
Since only one unit can move at a time, after i figure out where it should go, as far as the simulation is concerned it Can instantly be teleported there, but playing the game you would want to see the unit moving so that you realise who moved and where he went.
The question is, would you put the movement algorithm (eg interpolating between 2 points in N seconds) in the model and then have the view show the unit in the interpolated position without even knowing that it is moving, or teleport the unit and notify the view that it should show the unit moving as best as it wants.
If you would take the second approach, how would you keep the simulation from running too far ahead of the view, would you put the view in command of resuming the simulation after the movement ended?
Thanks in advance, Xtapodi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,又一个例子提醒我们,MVC 最初并不是为实时图形设计的。 ;)
我将在模型中存储当前位置和先前位置。当对象移动时,当前位置被复制到以前的位置,新位置被复制到当前位置,并向视图发送模型已更改的通知。然后视图可以相应地在旧位置和新位置之间进行插值。它可以完全根据特定的视图设置加速、减慢甚至删除插值,而不需要在模型中存储任何额外的数据。
您可以只存储每个单位的最后一步,而不是存储当前位置和前一个位置,并且该移动本身包含前一个位置。如果您需要存储有关移动的额外信息,这可能更通用。
Ah, yet another example that reminds us that MVC was never originally designed for real-time graphics. ;)
I would store the current position and the previous position in the model. When the object moves, the current position is copied into the previous position, the new position is copied into the current position, and a notification is sent to the view that the model has changed. The view can then interpolate between the old and the new position accordingly. It can speed up, slow down, or even remove the interpolation entirely based on the specific view settings, without requiring any extra data to be stored within the model.
Rather than storing the current position and the previous position, you could instead just store the last move with each unit, and the move itself contains the previous position. This is probably more versatile if you ever need to store extra information about a move.
您可能想要的是让单位图像移动每一帧。每帧移动图像的距离与插值类似。
unitsPerSecond =totalUnits/(framesPerSecond *totalSeconds)
因此,如果我想在 2 秒内将图像从位置 0 移动到位置 60,并且我的帧率为 30,那么我需要在 60 帧中移动 60 个单位,因此我的速度为 1。帧时,我将图像移动 1 个单位,如果移动该单位会使我超出目的地,只需将我的位置设置为目的地即可。
What you probably want is to have the unit image move each frame. How far to move the image each frame is similar to your interpolation.
unitsPerSecond = totalUnits / (framesPerSecond * totalSeconds)
So if I want to move an image from position 0 to position 60 in 2 seconds and my framerate is 30, I need to move 60 units in 60 frames, therefore my speed is 1. So each frame, I move the image 1 unit, and if moving the unit will take me beyond my destination, simply set my location to my destination.