1 | #!/usr/bin/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 | |
---|
18 | from sounds import SoundEngine |
---|
19 | from viewbase import ViewBase |
---|
20 | |
---|
21 | class 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, text): |
---|
46 | """Display on screen the text of the object over the object. |
---|
47 | @type obj: fife.instance |
---|
48 | @param obj: object to draw over |
---|
49 | @type text: String |
---|
50 | @param text: text to display over object |
---|
51 | @return: None""" |
---|
52 | obj.say(str(text), 1000) |
---|
53 | |
---|
54 | def onWalk(self, click): |
---|
55 | """Callback sample for the context menu.""" |
---|
56 | self.hud.hideContainer() |
---|
57 | self.engine.getModel().game_state.PlayerCharacter.run(click) |
---|
58 | |
---|
59 | def refreshTopLayerTransparencies(self): |
---|
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() |
---|
66 | scr_coords = camera.toScreenCoordinates(point.getMapCoordinates()) |
---|
67 | |
---|
68 | # find all instances on TopLayer that fall on those coordinates |
---|
69 | instances = camera.getMatchingInstances(scr_coords, |
---|
70 | self.model.active_map.top_layer) |
---|
71 | instance_ids = [ instance.getId() for instance in instances ] |
---|
72 | faded_objects = self.faded_objects |
---|
73 | |
---|
74 | # fade instances |
---|
75 | for instance_id in instance_ids: |
---|
76 | if instance_id not in faded_objects: |
---|
77 | faded_objects.add(instance_id) |
---|
78 | self.model.active_map.top_layer.getInstance(instance_id).\ |
---|
79 | get2dGfxVisual().setTransparency(128) |
---|
80 | |
---|
81 | # unfade previously faded instances |
---|
82 | for instance_id in faded_objects.copy(): |
---|
83 | if instance_id not in instance_ids: |
---|
84 | faded_objects.remove(instance_id) |
---|
85 | self.model.active_map.top_layer.getInstance(instance_id).\ |
---|
86 | get2dGfxVisual().setTransparency(0) |
---|
87 | |
---|
88 | |
---|
89 | def highlightFrontObject(self, mouse_coords): |
---|
90 | """Highlights the object that is at the |
---|
91 | current mouse coordinates""" |
---|
92 | if mouse_coords: |
---|
93 | front_obj = self.model.getObjectAtCoords(mouse_coords) |
---|
94 | if front_obj != None: |
---|
95 | if self.highlight_obj == None \ |
---|
96 | or front_obj.getId() != \ |
---|
97 | self.highlight_obj.getId(): |
---|
98 | if self.highlight_obj: |
---|
99 | self.displayObjectText(self.highlight_obj,"") |
---|
100 | self.model.active_map.outline_renderer.removeAllOutlines() |
---|
101 | self.highlight_obj = front_obj |
---|
102 | self.model.active_map.outline_renderer.addOutlined( |
---|
103 | self.highlight_obj, |
---|
104 | 0, |
---|
105 | 137, 255, 2) |
---|
106 | # get the text |
---|
107 | item = self.model.objectActive( |
---|
108 | self.highlight_obj.getId()) |
---|
109 | if item is not None: |
---|
110 | self.displayObjectText(self.highlight_obj, |
---|
111 | item.name) |
---|
112 | else: |
---|
113 | if self.highlight_obj: |
---|
114 | self.displayObjectText(self.highlight_obj,"") |
---|
115 | self.model.active_map.outline_renderer.removeAllOutlines() |
---|
116 | self.highlight_obj = None |
---|
117 | |
---|