Re: GUI design in simulation: or MVC pattern usefulness?
Daniel Pitts wrote:
So, I'm working on a project that I've done a few times before. A port
of an existing "game" called AT-Robots. The base description is a
simulation of a few virtual machines, along with the virtual robots that
they control. The robots can interact with the "Arena" by firing
missile, laying mines, moving, and scanning in various ways.
"A project that I've done a few times before." Sounds like something I
would say: I have two projects that I've started a few times but realize
in the middle that the entire plan is wrong and start over from scratch
again.
The view, on the other hand, would have to know how to render so many
different things (robots, missiles, scan representations, mines,
explosions, etc...). I would either have to make a visual peer for each
of these objects, or have the objects themselves know how to render
themselves. Of course, this gets even more complicated if I want to
support "remote" rendering (shared Arenas over a network).
My general preference for graphics is closest to a Visitor pattern (?):
display function:
....
map.display(this);
[ map class: ]
void display(Display d) {
// ... draw map ...
for (DisplayableObject o : objects)
o.display(d);
}
[ random displayable object: ]
void display(Display d) {
d.draw(x, y, z, theta, phi, this.getDrawingObject());
// or
d.draw(x, y, heading, this.getDrawingObject());
}
The drawing objects might be something like a 3D-model representing a
missile or a 2D-image that can be rotated depending on its heading.
The reason I like this model is because it is (relatively) easily to
extend to new types of objects, and it works well with my preference for
using data files to specify various objects as opposed to very large
class trees. In addition, it means you can use different types of
display: one display for drawing to a screen, another for sending data
across a network, and a third for having debugging console output.
[ Note: theta and phi represent the two needed angles for three
dimensions, if that wasn't made clear enough. ]
[OT for this thread:]
I just thought of how writing a game can be a good introduction to Java
for programmers: it's a good example of where inheritance trees can be
deep instead of broad; several of the collections are going to be used;
it would handle both network and file I/O; if properly planned out,
multithreading would be used; and all games need some sort of GUI worked
out. Hm, I should try recommending this as a course somewhere.
[/OT]
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth