source: trunk/game/scripts/gamesceneview.py @ 598

Revision 598, 5.3 KB checked in by beliar, 9 years ago (diff)

Patch by Beliar.

  • Fixed crash when picking up items
  • changed displayObjectText of GameSceneView?, it now stores the id of the instance instead of the instance.
  • getObjectById of GameState? can now be called without a map_id, the method will now use its getMapOfObject method if no map is given.
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18from sounds import SoundEngine
19from viewbase import ViewBase
20
21class GameSceneView(ViewBase):
22    """GameSceneView is responsible for drawing the scene"""
23    def __init__(self, engine, model):
24        """Constructor for engine
25           @param engine: A fife.Engine instance
26           @type engine: fife.Engine
27           @param model: a script.GameModel instance
28           @type model: script.GameModel
29           """
30        super(GameSceneView, self).__init__(engine, model)
31
32        self.action_number = 1
33
34        # init the sound
35        self.sounds = SoundEngine(engine)
36
37        self.hud = None       
38
39        # The current highlighted object
40        self.highlight_obj = None
41     
42        # faded objects in top layer
43        self.faded_objects = set()
44
45    def displayObjectText(self, obj_id, text):
46        """Display on screen the text of the object over the object.
47           @type obj_id: id of fife.instance
48           @param obj: id of object to draw over
49           @type text: String
50           @param text: text to display over object
51           @return: None"""
52        try:
53            if obj_id:
54                obj = self.model.active_map.agent_layer.getInstance(obj_id)
55            else:
56                obj = None
57        except RuntimeError as error:
58            if error.args[0].split(',')[0].strip() == "_[NotFound]_":
59                obj = None
60            else:
61                raise
62        if obj:
63            obj.say(str(text), 1000)
64
65    def onWalk(self, click):
66        """Callback sample for the context menu."""
67        self.hud.hideContainer()
68        self.model.game_state.player_character.run(click)
69
70    def refreshTopLayerTransparencies(self):
71        """Fade or unfade TopLayer instances if the PlayerCharacter
72        is under them."""
73        if not self.model.active_map:
74            return
75
76        # get the PlayerCharacter's screen coordinates
77        camera = self.model.active_map.cameras[self.model.active_map.my_cam_id]
78        point = self.model.game_state.player_character.\
79                                        behaviour.agent.getLocation()
80        scr_coords = camera.toScreenCoordinates(point.getMapCoordinates())
81
82        # find all instances on TopLayer that fall on those coordinates
83        instances = camera.getMatchingInstances(scr_coords,
84                        self.model.active_map.top_layer)
85        instance_ids = [ instance.getId() for instance in instances ]
86        faded_objects = self.faded_objects
87
88        # fade instances
89        for instance_id in instance_ids:
90            if instance_id not in faded_objects:
91                faded_objects.add(instance_id)
92                self.model.active_map.top_layer.getInstance(instance_id).\
93                        get2dGfxVisual().setTransparency(128)
94
95        # unfade previously faded instances
96        for instance_id in faded_objects.copy():
97            if instance_id not in instance_ids:
98                faded_objects.remove(instance_id)
99                self.model.active_map.top_layer.getInstance(instance_id).\
100                        get2dGfxVisual().setTransparency(0)
101
102
103    #def removeHighlight(self):
104       
105   
106    def highlightFrontObject(self, mouse_coords):
107        """Highlights the object that is at the
108        current mouse coordinates"""       
109        if not self.model.active_map:
110            return
111        if mouse_coords:
112            front_obj = self.model.getObjectAtCoords(mouse_coords)
113            if front_obj != None:
114                if self.highlight_obj == None \
115                                    or front_obj.getId() != \
116                                    self.highlight_obj:
117                    if self.model.game_state.hasObject(front_obj.getId()):
118                        self.displayObjectText(self.highlight_obj, "")
119                    self.model.active_map.outline_renderer.removeAllOutlines()
120                    self.highlight_obj = front_obj.getId()
121                    self.model.active_map.outline_renderer.addOutlined(
122                                                    front_obj, 
123                                                    0,
124                                                    137, 255, 2)
125                    # get the text
126                    item = self.model.objectActive(self.highlight_obj)
127                    if item is not None:
128                        self.displayObjectText(self.highlight_obj, 
129                                                    item.name)
130            else:
131                if self.highlight_obj:
132                    self.displayObjectText(self.highlight_obj,"")
133                self.model.active_map.outline_renderer.removeAllOutlines()
134                self.highlight_obj = None 
135           
Note: See TracBrowser for help on using the repository browser.