[Video] OOP design issue

Started by
1 comment, last by blackbird04217 14 years, 2 months ago
Hi, I seem to have hit a roadblock with something that I could use assistance on, primarily with regards to best practices when it comes to object-oriented design. Here is my current situation: I am working on an RTS game and plan to implement the selecting of units based on collision with a selection box (if you have EVER played an RTS, you will know what I am talking about.) I had the SelectionBox class working beautifully until I tried to implement unit selection. In my main game loop, I take the current events and pass it off to SelectionBox, to allow the class to traffic-control the events itself without cluttering the main loop.

def eventHandler(self, event):
     if event.type == MOUSEBUTTONDOWN:
          if event.button == 1:
	  self.setActive(event.pos)
		
     if event.type == MOUSEMOTION:
          if self.active:
               self.update(event.pos)
				
     if event.type == MOUSEBUTTONUP:
          if event.button == 1:
               if self.active:
               self.setInactive()



This works great, until I noticed I won't be able to check for collisions between units and the box during the MOUSEBUTTONUP event. In order to be able to create and call a checkSelectedUnits method, I would have to also pass in all allied units into the eventHandler() call, which wouldn't lend itself well to being allowed to take this class and use it in any project. -- Actually, while I'm typing this, I'm realizing this isn't some static class I can go and use without creating an object of first, so I guess my issue here isn't one at all. I'm going to assume you are all going to tell me to just add the parameter for passing in all the units and call it a day, but I'll wait and see. Best other new programmers read this anyways and realize my mistake :) [Edited by - Kada on February 18, 2010 1:38:49 AM]
Advertisement
Here is the result of my solution:



Well typically there is some world, or level type object that is easily accessed by things, and this object knows of all the units. Derive a class from your current, working SelectionBox class and overload the update method.

In the new UnitSelectionBox class the update will get the area/volume that is being selected, and query the World/Level Object to gather all the units. The underlying SelectionBox never needs to know about the highlighting of units, but the UnitSelectionBox will, and although it doesn't need to know everything about the Units it will need to know how to query for them within the volume, or have some way that the Level/World object will select the units based on the volume - which changes with mouse movement.

-However you may need to states within the Unit. Selected and Highlighted. Highlighted is turned on when the selection box is over a unit, but selected only gets turned on during the MouseButtonUp event.

This remains quite clean and flexible, while keeping your current working SelectionBox ready for use.

Hope that helps - happy coding.

This topic is closed to new replies.

Advertisement