source: trunk/PARPG/scripts/world.py @ 54

Revision 54, 5.5 KB checked in by maximinus_parpg, 11 years ago (diff)

Set Model / View data model for PARPG.
Code no longer looks like Rio demo.
Started to split map from objects.
Added crate and NPC character.

Line 
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
18import fife
19from scripts.common.eventlistenerbase import EventListenerBase
20from loaders import loadMapFile
21from agents.hero import Hero
22from agents.npc import NPC
23from settings import Setting
24
25TDS = Setting()
26
27class MapListener(fife.MapChangeListener):
28    """This class listens to changes happening on the map.
29       Since in theory we initiate these ourselves, do we need this class?"""
30    def __init__(self, map):
31        fife.MapChangeListener.__init__(self)
32
33    def onMapChanged(self, map, changedLayers):
34        pass
35
36    def onLayerCreate(self, map, layer):
37        pass
38
39    def onLayerDelete(self, map, layer):
40        pass
41
42class Map:
43    def __init__(self,fife_map):
44        self.listener=MapListener
45        self.map=fife_map
46   
47    def addPC(self,pc):
48        pass
49   
50    def addNPC(self,pc):
51        pass
52
53class World(EventListenerBase):
54    """World holds the data needed by fife to render the engine
55       The engine keeps a copy of this class"""
56    def __init__(self, engine):
57        super(World, self).__init__(engine, regMouse=True, regKeys=True)
58        self.engine = engine
59        self.eventmanager = engine.getEventManager()
60        self.model = engine.getModel()
61        self.view = self.engine.getView()
62        self.filename = ''
63        self.instance_to_agent = {}
64        self.transitions = []
65        self.PC=None
66        self.npcs=[]
67
68    def reset(self):
69        """Rest the map to default settings"""
70        self.map, self.agentlayer = None, None
71        self.cameras = {}
72        self.PC = None
73        self.cur_cam2_x,self.initial_cam2_x,self.cam2_scrolling_right=0,0,True
74        self.target_rotation = 0
75        self.instance_to_agent = {}
76
77    def load(self, filename):
78        """Load a map given the filename
79           TODO: a map should only contain static items and floor tiles
80           Everything else should be loaded from the engine, because it
81           is subject to change"""
82        self.filename = filename
83        self.reset()
84        self.map = loadMapFile(filename, self.engine)
85        self.maplistener = MapListener(self.map)
86
87        # there must be a PC object on the objects layer!
88        self.agentlayer = self.map.getLayer('ObjectLayer')
89        # it's possible there's no transition layer
90        size=len('TransitionLayer')
91        for layer in self.map.getLayers():
92            # could be many layers, but hopefully no more than 3
93            if(layer.getId()[:size]=='TransitionLayer'):
94                self.transitions.append(self.map.getLayer(layer.getId()))
95
96        # init the camera
97        for cam in self.view.getCameras():
98            self.cameras[cam.getId()] = cam
99        self.view.resetRenderers()
100        self.target_rotation = self.cameras['main'].getRotation()
101        self.cord_render=self.cameras['main'].getRenderer('CoordinateRenderer')
102
103    def addPC(self,xpos,ypos):
104        """Add the player character to the map
105           The id we use is always is always PC"""
106        # first we need to add the PC as an object on the map
107        self.PC = Hero(self.model,'PC',self.agentlayer)
108        self.instance_to_agent[self.PC.agent.getFifeId()] = self.PC
109        # ensure the PC starts on a default action
110        self.PC.start()
111        # attach the main camera to the PC
112        self.cameras['main'].attach(self.PC.agent)
113   
114    def addNPC(self):
115        """Add a non=player character to the map"""
116        pass
117   
118    def addObject(self):
119        """Add an object to the map"""
120        pass
121
122    # all key / mouse event handling routines go here
123    def keyPressed(self, evt):
124        """When a key is depressed, fife calls this routine."""
125        key=evt.getKey()
126        keyval = key.getValue()
127
128        if keyval == key.T:
129            self.toggle_renderer('GridRenderer')
130        if keyval == key.F5:
131            # logic would say we use similar code to above and toggle
132            # logic here does not work, my friend :-(
133            self.cord_render.setEnabled(not self.cord_render.isEnabled())
134
135    def mousePressed(self, evt):
136        """If a mouse button is pressed down, fife calss this routine
137           Currently we only check for a left click, and we assume this is on
138           the map"""
139        clickpoint = fife.ScreenPoint(evt.getX(), evt.getY())
140        if (evt.getButton()==fife.MouseEvent.LEFT):
141            target_mapcoord=self.cameras['main'].toMapCoordinates(clickpoint, False)
142            target_mapcoord.z = 0
143            l = fife.Location(self.agentlayer)
144            l.setMapCoordinates(target_mapcoord)
145            self.PC.run(l)
146           
147    def toggle_renderer (self,r_name):
148        """Enable or disable the renderer named `r_name`"""
149        renderer = self.cameras['main'].getRenderer('GridRenderer')
150        renderer.setEnabled(not renderer.isEnabled())
151
152    def pump(self):
153        """Routine called during each frame. Our main loop is in ./run.py
154           We ignore this main loop (but FIFE complains if it is missing)"""
155        pass
156
Note: See TracBrowser for help on using the repository browser.