Package fortuneengine :: Module GameEngineElement
[hide private]
[frames] | no frames]

Source Code for Module fortuneengine.GameEngineElement

  1  #    FortuneEngine is free software: you can redistribute it and/or modify 
  2  #    it under the terms of the GNU General Public License as published by 
  3  #    the Free Software Foundation, either version 3 of the License, or 
  4  #    (at your option) any later version. 
  5  # 
  6  #    FortuneEngine is distributed in the hope that it will be useful, 
  7  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  #    GNU General Public License for more details. 
 10  # 
 11  #    You should have received a copy of the GNU General Public License 
 12  #    along with the FortuneEngine.  If not, see <http://www.gnu.org/licenses/>. 
 13  # 
 14  #    Author: Justin Lewis  <jlew.blackout@gmail.com> 
 15   
 16  from fortuneengine.GameEngine import GameEngine 
 17  #from fortuneengine.DrawableFontObject import DrawableFontObject 
 18  #from fortuneengine.DrawableObject import DrawableObject 
 19  #from fortuneengine.DynamicDrawableObject import DynamicDrawableObject 
 20   
 21   
22 -class GameEngineElement(object):
23 """ 24 The GameEngineElement is a helper object that can be extended by 25 other classes. When the class is extended, it will automatically 26 register its self for the event and draw loops with the game engine. 27 """ 28
29 - def __init__(self, has_draw=True, has_event=True):
30 """ 31 Default constructor for GameEngineElement 32 33 @param has_draw: boolean to signify if element should be drawn 34 @param has_event: boolean to signify whether the element should be 35 given events from the queue 36 """ 37 self.__has_draw = has_draw 38 self.__has_event = has_event 39 self.__in_engine = False 40 self.game_engine = GameEngine.instance 41 self.__ddo_list = []
42
43 - def is_in_engine(self):
44 """ 45 Returns true if object has been registered with the game engine. 46 """ 47 return self.__in_engine
48
49 - def add_to_scene(self, objects):
50 """ 51 Adds some objects to the DynamicDrawableObject list and the 52 game engine's scene. 53 54 @param objects: A list of DynamicDrawableObjects 55 """ 56 57 self.game_engine.get_scene().addObjects(objects) 58 self.__ddo_list += objects
59
60 - def add_to_engine(self):
61 """ 62 Registers the object with the game engine. Registers draw and event 63 call backs separately if they were set to true in the constructor. 64 """ 65 if not self.__in_engine: 66 self.__in_engine = True 67 68 if self.__has_draw: 69 self.game_engine.add_draw_callback(self.draw) 70 71 if self.__has_event: 72 self.game_engine.add_event_callback(self.event_handler)
73
74 - def remove_from_engine(self):
75 """ 76 Removes the object from the correct queues in the engine 77 """ 78 if self.__in_engine: 79 self.__in_engine = False 80 81 if self.__has_draw: 82 self.game_engine.remove_draw_callback(self.draw) 83 84 if self.__has_event: 85 self.game_engine.remove_event_callback(self.event_handler) 86 87 if not (self.__ddo_list == []): 88 for object in self.__ddo_list: 89 self.game_engine.get_scene().removeObject(object)
90 91
92 - def event_handler(self, event):
93 """ 94 This method should be overridden by the user-specified class that 95 extends this GameEngineElement class. This method specifies how that 96 class will handle events given to it by the engine. 97 98 @return: true if the user wants to prevent the event from 99 continuing down the queue 100 """ 101 pass
102
103 - def draw(self, screen):
104 """ 105 This method should be overridden by the user-specified class that 106 extends this GameEngineElement class. This method specifies how the 107 class will be drawn. 108 """ 109 pass
110