source: branches/active/character_customization/game/parpg/gamesceneview.py @ 766

Revision 736, 6.3 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites:

  • renamed scripts package to parpg
  • renamed parpg module to application
  • removed packaging and other related files (kept locally for reference, will reintroduce similar scripts to resolve bug #275
  • updated all import statements to respect changes above
  • Property svn:eol-style set to native
RevLine 
[584]1#!/usr/bin/env python
[26]2
[30]3#   This file is part of PARPG.
[26]4
[30]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.
[26]9
[30]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.
[26]14
[30]15#   You should have received a copy of the GNU General Public License
[35]16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
[26]17
[550]18from sounds import SoundEngine
[552]19from viewbase import ViewBase
[620]20from fife import fife
[26]21
[552]22class GameSceneView(ViewBase):
23    """GameSceneView is responsible for drawing the scene"""
24    def __init__(self, engine, model):
[648]25        """Constructor for GameSceneView
[552]26           @param engine: A fife.Engine instance
[141]27           @type engine: fife.Engine
[552]28           @param model: a script.GameModel instance
29           @type model: script.GameModel
30           """
31        super(GameSceneView, self).__init__(engine, model)
[361]32
33        # init the sound
34        self.sounds = SoundEngine(engine)
35
[550]36        self.hud = None       
[361]37
38        # The current highlighted object
39        self.highlight_obj = None
[549]40     
[412]41        # faded objects in top layer
42        self.faded_objects = set()
43
[603]44    def displayObjectText(self, obj_id, text, time=1000):
[141]45        """Display on screen the text of the object over the object.
[598]46           @type obj_id: id of fife.instance
47           @param obj: id of object to draw over
[340]48           @type text: String
[141]49           @param text: text to display over object
50           @return: None"""
[598]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:
[603]62            obj.say(str(text), time)
[77]63
[150]64    def onWalk(self, click):
[277]65        """Callback sample for the context menu."""
[270]66        self.hud.hideContainer()
[579]67        self.model.game_state.player_character.run(click)
[115]68
[549]69    def refreshTopLayerTransparencies(self):
[552]70        """Fade or unfade TopLayer instances if the PlayerCharacter
71        is under them."""
[563]72        if not self.model.active_map:
73            return
74
[552]75        # get the PlayerCharacter's screen coordinates
76        camera = self.model.active_map.cameras[self.model.active_map.my_cam_id]
[579]77        point = self.model.game_state.player_character.\
[552]78                                        behaviour.agent.getLocation()
[412]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,
[552]83                        self.model.active_map.top_layer)
[412]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)
[552]91                self.model.active_map.top_layer.getInstance(instance_id).\
[549]92                        get2dGfxVisual().setTransparency(128)
[412]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)
[552]98                self.model.active_map.top_layer.getInstance(instance_id).\
[412]99                        get2dGfxVisual().setTransparency(0)
100
[338]101
[598]102    #def removeHighlight(self):
103       
104   
[549]105    def highlightFrontObject(self, mouse_coords):
[552]106        """Highlights the object that is at the
107        current mouse coordinates"""       
[563]108        if not self.model.active_map:
109            return
[549]110        if mouse_coords:
[552]111            front_obj = self.model.getObjectAtCoords(mouse_coords)
[549]112            if front_obj != None:
113                if self.highlight_obj == None \
[598]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, "")
[552]118                    self.model.active_map.outline_renderer.removeAllOutlines()
[598]119                    self.highlight_obj = front_obj.getId()
[552]120                    self.model.active_map.outline_renderer.addOutlined(
[598]121                                                    front_obj, 
[549]122                                                    0,
123                                                    137, 255, 2)
124                    # get the text
[598]125                    item = self.model.objectActive(self.highlight_obj)
[549]126                    if item is not None:
127                        self.displayObjectText(self.highlight_obj, 
128                                                    item.name)
129            else:
[552]130                self.model.active_map.outline_renderer.removeAllOutlines()
[550]131                self.highlight_obj = None 
132           
[620]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           
Note: See TracBrowser for help on using the repository browser.