comparison gamesceneview.py @ 0:7a89ea5404b1

Initial commit of parpg-core.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 14 May 2011 01:12:35 -0700
parents
children 98f26f7636d8
comparison
equal deleted inserted replaced
-1:000000000000 0:7a89ea5404b1
1 # This file is part of PARPG.
2
3 # PARPG is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7
8 # PARPG is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
15
16 from sounds import SoundEngine
17 from viewbase import ViewBase
18 from fife import fife
19
20 class GameSceneView(ViewBase):
21 """GameSceneView is responsible for drawing the scene"""
22 def __init__(self, engine, model):
23 """Constructor for GameSceneView
24 @param engine: A fife.Engine instance
25 @type engine: fife.Engine
26 @param model: a script.GameModel instance
27 @type model: script.GameModel
28 """
29 super(GameSceneView, self).__init__(engine, model)
30
31 # init the sound
32 self.sounds = SoundEngine(engine)
33
34 self.hud = None
35
36 # The current highlighted object
37 self.highlight_obj = None
38
39 # faded objects in top layer
40 self.faded_objects = set()
41
42 def displayObjectText(self, obj_id, text, time=1000):
43 """Display on screen the text of the object over the object.
44 @type obj_id: id of fife.instance
45 @param obj: id of object to draw over
46 @type text: String
47 @param text: text to display over object
48 @return: None"""
49 try:
50 if obj_id:
51 obj = self.model.active_map.agent_layer.getInstance(obj_id)
52 else:
53 obj = None
54 except RuntimeError as error:
55 if error.args[0].split(',')[0].strip() == "_[NotFound]_":
56 obj = None
57 else:
58 raise
59 if obj:
60 obj.say(str(text), time)
61
62 def onWalk(self, click):
63 """Callback sample for the context menu."""
64 self.hud.hideContainer()
65 self.model.game_state.player_character.run(click)
66
67 def refreshTopLayerTransparencies(self):
68 """Fade or unfade TopLayer instances if the PlayerCharacter
69 is under them."""
70 if not self.model.active_map:
71 return
72
73 # get the PlayerCharacter's screen coordinates
74 camera = self.model.active_map.cameras[self.model.active_map.my_cam_id]
75 point = self.model.game_state.player_character.\
76 behaviour.agent.getLocation()
77 scr_coords = camera.toScreenCoordinates(point.getMapCoordinates())
78
79 # find all instances on TopLayer that fall on those coordinates
80 instances = camera.getMatchingInstances(scr_coords,
81 self.model.active_map.top_layer)
82 instance_ids = [ instance.getId() for instance in instances ]
83 faded_objects = self.faded_objects
84
85 # fade instances
86 for instance_id in instance_ids:
87 if instance_id not in faded_objects:
88 faded_objects.add(instance_id)
89 self.model.active_map.top_layer.getInstance(instance_id).\
90 get2dGfxVisual().setTransparency(128)
91
92 # unfade previously faded instances
93 for instance_id in faded_objects.copy():
94 if instance_id not in instance_ids:
95 faded_objects.remove(instance_id)
96 self.model.active_map.top_layer.getInstance(instance_id).\
97 get2dGfxVisual().setTransparency(0)
98
99
100 #def removeHighlight(self):
101
102
103 def highlightFrontObject(self, mouse_coords):
104 """Highlights the object that is at the
105 current mouse coordinates"""
106 if not self.model.active_map:
107 return
108 if mouse_coords:
109 front_obj = self.model.getObjectAtCoords(mouse_coords)
110 if front_obj != None:
111 if self.highlight_obj == None \
112 or front_obj.getId() != \
113 self.highlight_obj:
114 if self.model.game_state.hasObject(front_obj.getId()):
115 self.displayObjectText(self.highlight_obj, "")
116 self.model.active_map.outline_renderer.removeAllOutlines()
117 self.highlight_obj = front_obj.getId()
118 self.model.active_map.outline_renderer.addOutlined(
119 front_obj,
120 0,
121 137, 255, 2)
122 # get the text
123 item = self.model.objectActive(self.highlight_obj)
124 if item is not None:
125 self.displayObjectText(self.highlight_obj,
126 item.name)
127 else:
128 self.model.active_map.outline_renderer.removeAllOutlines()
129 self.highlight_obj = None
130
131
132 def moveCamera(self, direction):
133 """Move the camera in the given direction.
134 @type direction: list of two integers
135 @param direction: the two integers can be 1, -1, or 0
136 @return: None """
137
138 if 'cameras' in dir(self.model.active_map):
139 cam = self.model.active_map.cameras[self.model.active_map.my_cam_id]
140 location = cam.getLocation()
141 position = location.getMapCoordinates()
142
143 #how many pixls to move by each call
144 move_by = 1
145 #create a new DoublePoint3D and add it to position DoublePoint3D
146 new_x, new_y = move_by * direction[0], move_by * direction[1]
147
148 position_offset = fife.DoublePoint3D(int(new_x), int(new_y))
149 position += position_offset
150
151 #give location the new position
152 location.setMapCoordinates(position)
153
154 #detach the camera from any objects
155 cam.detach()
156 #move the camera to the new location
157 cam.setLocation(location)
158
159