XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Where to store data/control

Last post 9/7/2009 8:32 PM by Lilith. 3 replies.
  • 9/5/2009 8:24 PM

    Where to store data/control

    I'm looking for opinions on the best approach for making certain classes work together.  This is a 2D maze type game.  My game component, aside from the various controls, has a grid object and a car object.  Ultimately multiple cars.  The grid holds a 2D array of intersections designed to show connections to other intersections and which controls where the roadways are.  The car needs to know which intersection it's heading toward and what direction changes can be made at what intersection.

    What I'm concerned about is the appropriate way of letting the car know about its relationship to the grid.  Do I maintain the information regarding the location on the grid and its objectives in the car object itself or should I place that in the game component that's calling the updates on both the grid and the car?  Should I bind a reference to the grid to the car and let the car access the information directly?

    I've never been completely comfortable with class design and I get afraid I'm making too many interdependencies that make me feel like I'm just patching to adjust to need rather than thoughtful design.
  • 9/6/2009 7:11 AM In reply to

    Re: Where to store data/control

    You could try using a Model-View-Controller (MVC) pattern.

    1. Model
    The classes involved in creating the actual maze-y traffic simulation grid, with the information about where the intersections and cars are. Including the Car, Grid, and Intersection types.

    2. View
    One or more classes capable of describing the model in a format suitable for interaction by a controller. Like a WindshieldView (the intersections ahead of a designated car's position). Another View could be a TrafficDispatcherView which detects heavily congested areas on the map.

    3. Controller
    A class that invokes changes to the model. For instance, the Driver who uses the WindshieldView class to respond to events (reaching an intersection) to make decisions (taking a turn). Another Controller could be a class that spawns new cars periodically or puts up detours temporarily.

    The MVC pattern is event-driven so you would have the model publish events (like OnCarReachedIntersection) for the controller Driver to subscribe to. When the model fires the event, it constructs a pertinent view and passes it along as an event argument. As another example, a driver with a radio can also get information from the TrafficDispatcher by subscribing to the OnRadioAnnouncement event, which tells it what areas of the grid are congested.
  • 9/6/2009 8:58 PM In reply to

    Re: Where to store data/control

    Don't over-engineer it. If your cars will only ever drive on grids, then give each car a Grid member as part of the constructor, say. The cars can then ask the Grid for information when they need it (say, in simulation).
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 9/7/2009 8:32 PM In reply to

    Re: Where to store data/control

    Thanks to you both.  I think what it comes down to is what works for the situation and the programming environment.  In that this isn't currently a team effort I think what works best for me is giving the car access to the intersection information by reference.  If I had other people working on this I'd probably need to adopt a cleaner model.

Page 1 of 1 (4 items) Previous Next