Ignore:
Timestamp:
06/19/10 22:35:21 (9 years ago)
Author:
beliar
Message:

This concludes the cleanup and refactoring for now. I hope the changes will make it easier to add functionality to the game.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/code-cleanup-and-refactoring/game/scripts/gamesceneview.py

    r550 r552  
    1616#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>. 
    1717 
    18 from map import Map 
    19 from fife import fife 
    2018from sounds import SoundEngine 
     19from viewbase import ViewBase 
    2120 
    22 # this file should be the meta-file for all FIFE-related code 
    23 # engine.py handles is our fife_model fife_model, whilst this is our view 
    24 # in order to not replicate fife_model, some of our fife_model fife_model will naturally 
    25 # reside on this side of the fence (PC xpos and ypos, for example). 
    26 # we should aim to never replicate any fife_model as this leads to maintenance 
    27 # issues (and just looks plain bad). 
    28 # however, any logic needed to resolve this should sit in engine.py 
    29  
    30 class GameSceneView(object): 
    31     """GameSceneView holds the fife_model needed by fife to render the engine 
    32        The engine keeps a copy of this class""" 
    33     def __init__(self, engine): 
     21class GameSceneView(ViewBase): 
     22    """GameSceneView is responsible for drawing the scene""" 
     23    def __init__(self, engine, model): 
    3424        """Constructor for engine 
     25           @param engine: A fife.Engine instance 
    3526           @type engine: fife.Engine 
    36            @param engine: A fife.Engine instance 
    37            @return: None""" 
    38         # self.engine is a fife.Engine object, not an Engine object 
    39         self.engine = engine 
    40  
    41         # self.fife_model is an gamemodel.GameModel object, but is set in run.py 
    42         self.fife_model = None 
    43  
    44         # self.map is a Map object, set to none here 
    45         self.active_map = None 
    46         self.maps = {} 
     27           @param model: a script.GameModel instance 
     28           @type model: script.GameModel  
     29           """ 
     30        super(GameSceneView, self).__init__(engine, model) 
    4731 
    4832        self.action_number = 1 
     
    5943        self.faded_objects = set() 
    6044 
    61     def loadMap(self, map_name, filename): 
    62         """Loads a map and stores it under the given name in the maps list. 
    63            @type map_name: String 
    64            @param map_name: The name of the map to load 
    65            @type filename: String 
    66            @param filename: File which contains the map to be loaded 
    67            @return: None""" 
    68         if not map_name in self.maps: 
    69             new_map = Map(self.engine, self.fife_model) 
    70             self.maps[map_name] = new_map 
    71             new_map.load(filename) 
    72         else: 
    73             self.setActiveMap(map_name) 
    74  
    75     def setActiveMap(self, map_name): 
    76         """Sets the active map that is to be rendered. 
    77            @type map_name: String 
    78            @param map_name: The name of the map to load 
    79            @return: None""" 
    80         # Turn off the camera on the old map before we turn on the camera 
    81         # on the new map. 
    82         self.active_map.cameras[self.active_map.my_cam_id].setEnabled(False) 
    83         # Make the new map active. 
    84         self.active_map = self.maps[map_name] 
    85         self.active_map.makeActive() 
    86  
    8745    def displayObjectText(self, obj, text): 
    8846        """Display on screen the text of the object over the object. 
     
    9755        """Callback sample for the context menu.""" 
    9856        self.hud.hideContainer() 
    99         self.fife_model.game_state.PC.run(click) 
     57        self.engine.getModel().game_state.PlayerCharacter.run(click) 
    10058 
    10159    def refreshTopLayerTransparencies(self): 
    102         """Fade or unfade TopLayer instances if the PC is under them.""" 
    103         # get the PC's screen coordinates 
    104         camera = self.active_map.cameras[self.active_map.my_cam_id] 
    105         point = self.fife_model.game_state.PC.behaviour.agent.getLocation() 
     60        """Fade or unfade TopLayer instances if the PlayerCharacter  
     61        is under them.""" 
     62        # get the PlayerCharacter's screen coordinates 
     63        camera = self.model.active_map.cameras[self.model.active_map.my_cam_id] 
     64        point = self.model.game_state.PlayerCharacter.\ 
     65                                        behaviour.agent.getLocation() 
    10666        scr_coords = camera.toScreenCoordinates(point.getMapCoordinates()) 
    10767 
    10868        # find all instances on TopLayer that fall on those coordinates 
    10969        instances = camera.getMatchingInstances(scr_coords, 
    110                         self.active_map.top_layer) 
     70                        self.model.active_map.top_layer) 
    11171        instance_ids = [ instance.getId() for instance in instances ] 
    11272        faded_objects = self.faded_objects 
     
    11676            if instance_id not in faded_objects: 
    11777                faded_objects.add(instance_id) 
    118                 self.active_map.top_layer.getInstance(instance_id).\ 
     78                self.model.active_map.top_layer.getInstance(instance_id).\ 
    11979                        get2dGfxVisual().setTransparency(128) 
    12080 
     
    12383            if instance_id not in instance_ids: 
    12484                faded_objects.remove(instance_id) 
    125                 self.active_map.top_layer.getInstance(instance_id).\ 
     85                self.model.active_map.top_layer.getInstance(instance_id).\ 
    12686                        get2dGfxVisual().setTransparency(0) 
    12787 
    128     def teleport(self, position): 
    129         """Called when a door is used that moves a player to a new 
    130            location on the same map. the setting of position may want 
    131            to be created as its own method down the road. 
    132            @type position: String Tuple 
    133            @param position: X,Y coordinates passed from engine.changeMap 
    134            @return: fife.Location""" 
    135         print position 
    136         coord = fife.DoublePoint3D(float(position[0]), float(position[1]), 0) 
    137         location = fife.Location(self.active_map.agent_layer) 
    138         location.setMapCoordinates(coord) 
    139         self.fife_model.game_state.PC.teleport(location) 
    140                  
    141     def getObjectAtCoords(self, coords): 
    142         """Get the object which is at the given coords 
    143             @type coords: fife.Screenpoint 
    144             @param coords: Coordinates where to check for an object 
    145             @rtype: fife.Object 
    146             @return: An object or None""" 
    147         instances = self.active_map.cameras[self.active_map.my_cam_id].\ 
    148             getMatchingInstances(coords, self.active_map.agent_layer) 
    149         # no object returns an empty tuple 
    150         if(instances != ()): 
    151             front_y = 0 
    152              
    153  
    154             for obj in instances: 
    155                 # check to see if this in our list at all 
    156                 if(self.fife_model.objectActive(obj.getId())): 
    157                     # check if the object is on the foreground 
    158                     obj_map_coords = \ 
    159                                       obj.getLocation().getMapCoordinates() 
    160                     obj_screen_coords = self.active_map.\ 
    161                         cameras[self.active_map.my_cam_id]\ 
    162                         .toScreenCoordinates(obj_map_coords) 
    163  
    164                     if obj_screen_coords.y > front_y: 
    165                         #Object on the foreground 
    166                         front_y = obj_screen_coords.y 
    167                         return obj 
    168                     else: 
    169                         return None 
    170         else: 
    171             return None 
    172  
    173     def getCoords(self, click): 
    174         """Get the map location x, y coordinates from the screen coordinates 
    175            @type click: fife.ScreenPoint 
    176            @param click: Screen coordinates 
    177            @rtype: fife.Location 
    178            @return: The map coordinates""" 
    179         coord = self.active_map.cameras[self.active_map.my_cam_id].\ 
    180                     toMapCoordinates(click, False) 
    181         coord.z = 0 
    182         location = fife.Location(self.active_map.agent_layer) 
    183         location.setMapCoordinates(coord) 
    184         return location 
    185  
    186     def togglePause(self): 
    187         """ Pause/Unpause the game. 
    188             @return: nothing""" 
    189         self.active_map.togglePause() 
    190      
    191     def deleteMaps(self): 
    192         """Clear all currently loaded maps from FIFE as well as clear our 
    193             local map cache 
    194             @return: nothing""" 
    195         self.engine.getModel().deleteMaps() 
    196         self.engine.getModel().deleteObjects() 
    197         self.maps = {} 
    19888 
    19989    def highlightFrontObject(self, mouse_coords): 
    200         """Highlights the object that is at the current mouse coordinates"""         
     90        """Highlights the object that is at the  
     91        current mouse coordinates"""         
    20192        if mouse_coords: 
    202             front_obj = self.getObjectAtCoords(mouse_coords) 
     93            front_obj = self.model.getObjectAtCoords(mouse_coords) 
    20394            if front_obj != None: 
    20495                if self.highlight_obj == None \ 
     
    20798                    if self.highlight_obj: 
    20899                        self.displayObjectText(self.highlight_obj,"") 
    209                     self.active_map.outline_renderer.removeAllOutlines() 
     100                    self.model.active_map.outline_renderer.removeAllOutlines() 
    210101                    self.highlight_obj = front_obj 
    211                     self.active_map.outline_renderer.addOutlined( 
     102                    self.model.active_map.outline_renderer.addOutlined( 
    212103                                                    self.highlight_obj,  
    213104                                                    0, 
    214105                                                    137, 255, 2) 
    215106                    # get the text 
    216                     item = self.fife_model.objectActive( 
     107                    item = self.model.objectActive( 
    217108                                                    self.highlight_obj.getId()) 
    218109                    if item is not None: 
     
    222113                if self.highlight_obj: 
    223114                    self.displayObjectText(self.highlight_obj,"") 
    224                 self.active_map.outline_renderer.removeAllOutlines() 
     115                self.model.active_map.outline_renderer.removeAllOutlines() 
    225116                self.highlight_obj = None   
    226117            
Note: See TracChangeset for help on using the changeset viewer.