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 | |
---|
18 | from sounds import SoundEngine |
---|
19 | from viewbase import ViewBase |
---|
20 | from fife import fife |
---|
21 | |
---|
22 | class GameSceneView(ViewBase): |
---|
23 | """GameSceneView is responsible for drawing the scene""" |
---|
24 | def __init__(self, engine, model): |
---|
25 | """Constructor for GameSceneView |
---|
26 | @param engine: A fife.Engine instance |
---|
27 | @type engine: fife.Engine |
---|
28 | @param model: a script.GameModel instance |
---|
29 | @type model: script.GameModel |
---|
30 | """ |
---|
31 | super(GameSceneView, self).__init__(engine, model) |
---|
32 | |
---|
33 | # init the sound |
---|
34 | self.sounds = SoundEngine(engine) |
---|
35 | |
---|
36 | self.hud = None |
---|
37 | |
---|
38 | # The current highlighted object |
---|
39 | self.highlight_obj = None |
---|
40 | |
---|
41 | # faded objects in top layer |
---|
42 | self.faded_objects = set() |
---|
43 | |
---|
44 | def displayObjectText(self, obj_id, text, time=1000): |
---|
45 | """Display on screen the text of the object over the object. |
---|
46 | @type obj_id: id of fife.instance |
---|
47 | @param obj: id of object to draw over |
---|
48 | @type text: String |
---|
49 | @param text: text to display over object |
---|
50 | @return: None""" |
---|
51 | try: |
---|
52 | if obj_id: |
---|
53 | obj = self.model.active_map.agent_layer.getInstance(obj_id) |
---|
54 | else: |
---|
55 | obj = None |
---|
56 | except RuntimeError as error: |
---|
57 | if error.args[0].split(',')[0].strip() == "_[NotFound]_": |
---|
58 | obj = None |
---|
59 | else: |
---|
60 | raise |
---|
61 | if obj: |
---|
62 | obj.say(str(text), time) |
---|
63 | |
---|
64 | def onWalk(self, click): |
---|
65 | """Callback sample for the context menu.""" |
---|
66 | self.hud.hideContainer() |
---|
67 | self.model.game_state.player_character.run(click) |
---|
68 | |
---|
69 | def refreshTopLayerTransparencies(self): |
---|
70 | """Fade or unfade TopLayer instances if the PlayerCharacter |
---|
71 | is under them.""" |
---|
72 | if not self.model.active_map: |
---|
73 | return |
---|
74 | |
---|
75 | # get the PlayerCharacter's screen coordinates |
---|
76 | camera = self.model.active_map.cameras[self.model.active_map.my_cam_id] |
---|
77 | point = self.model.game_state.player_character.\ |
---|
78 | behaviour.agent.getLocation() |
---|
79 | scr_coords = camera.toScreenCoordinates(point.getMapCoordinates()) |
---|
80 | |
---|
81 | # find all instances on TopLayer that fall on those coordinates |
---|
82 | instances = camera.getMatchingInstances(scr_coords, |
---|
83 | self.model.active_map.top_layer) |
---|
84 | instance_ids = [ instance.getId() for instance in instances ] |
---|
85 | faded_objects = self.faded_objects |
---|
86 | |
---|
87 | # fade instances |
---|
88 | for instance_id in instance_ids: |
---|
89 | if instance_id not in faded_objects: |
---|
90 | faded_objects.add(instance_id) |
---|
91 | self.model.active_map.top_layer.getInstance(instance_id).\ |
---|
92 | get2dGfxVisual().setTransparency(128) |
---|
93 | |
---|
94 | # unfade previously faded instances |
---|
95 | for instance_id in faded_objects.copy(): |
---|
96 | if instance_id not in instance_ids: |
---|
97 | faded_objects.remove(instance_id) |
---|
98 | self.model.active_map.top_layer.getInstance(instance_id).\ |
---|
99 | get2dGfxVisual().setTransparency(0) |
---|
100 | |
---|
101 | |
---|
102 | #def removeHighlight(self): |
---|
103 | |
---|
104 | |
---|
105 | def highlightFrontObject(self, mouse_coords): |
---|
106 | """Highlights the object that is at the |
---|
107 | current mouse coordinates""" |
---|
108 | if not self.model.active_map: |
---|
109 | return |
---|
110 | if mouse_coords: |
---|
111 | front_obj = self.model.getObjectAtCoords(mouse_coords) |
---|
112 | if front_obj != None: |
---|
113 | if self.highlight_obj == None \ |
---|
114 | or front_obj.getId() != \ |
---|
115 | self.highlight_obj: |
---|
116 | if self.model.game_state.hasObject(front_obj.getId()): |
---|
117 | self.displayObjectText(self.highlight_obj, "") |
---|
118 | self.model.active_map.outline_renderer.removeAllOutlines() |
---|
119 | self.highlight_obj = front_obj.getId() |
---|
120 | self.model.active_map.outline_renderer.addOutlined( |
---|
121 | front_obj, |
---|
122 | 0, |
---|
123 | 137, 255, 2) |
---|
124 | # get the text |
---|
125 | item = self.model.objectActive(self.highlight_obj) |
---|
126 | if item is not None: |
---|
127 | self.displayObjectText(self.highlight_obj, |
---|
128 | item.name) |
---|
129 | else: |
---|
130 | self.model.active_map.outline_renderer.removeAllOutlines() |
---|
131 | self.highlight_obj = None |
---|
132 | |
---|
133 | |
---|
134 | def moveCamera(self, direction): |
---|
135 | """Move the camera in the given direction. |
---|
136 | @type direction: list of two integers |
---|
137 | @param direction: the two integers can be 1, -1, or 0 |
---|
138 | @return: None """ |
---|
139 | |
---|
140 | if 'cameras' in dir(self.model.active_map): |
---|
141 | cam = self.model.active_map.cameras[self.model.active_map.my_cam_id] |
---|
142 | location = cam.getLocation() |
---|
143 | position = location.getMapCoordinates() |
---|
144 | |
---|
145 | #how many pixls to move by each call |
---|
146 | move_by = 1 |
---|
147 | #create a new DoublePoint3D and add it to position DoublePoint3D |
---|
148 | new_x, new_y = move_by * direction[0], move_by * direction[1] |
---|
149 | |
---|
150 | position_offset = fife.DoublePoint3D(int(new_x), int(new_y)) |
---|
151 | position += position_offset |
---|
152 | |
---|
153 | #give location the new position |
---|
154 | location.setMapCoordinates(position) |
---|
155 | |
---|
156 | #detach the camera from any objects |
---|
157 | cam.detach() |
---|
158 | #move the camera to the new location |
---|
159 | cam.setLocation(location) |
---|
160 | |
---|
161 | |
---|