Changeset 68


Ignore:
Timestamp:
05/13/09 16:21:58 (10 years ago)
Author:
maximinus_parpg
Message:

Cleaned up NPC class (a lot)
Right-clicking an object now shows info, albeit on the terminal.
Added information to /maps/map_objects.xml

Location:
trunk/PARPG
Files:
1 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/PARPG/maps/map_objects.xml

    r58 r68  
    77    <PC xpos="0.0" ypos="0.0"></PC> 
    88    <!-- There can be any number of NPC characters --> 
    9     <NPC gfx="npc-woman" xpos="-4.0" ypos="-7.0"></NPC> 
     9    <NPC gfx="npc-woman" xpos="-4.0" ypos="-7.0" id="women01" 
     10         text="A friendly woman"></NPC> 
    1011    <!-- There can be any number of objects --> 
    1112    <!-- Object blocking is set in the objects own xml file --> 
    12     <object gfx="crate" xpos="-3.0" ypos="-4.0"></object> 
    13     <object gfx="crate" xpos="-4.0" ypos="-6.0"></object> 
     13    <object gfx="crate" xpos="-3.0" ypos="-4.0" id="crate01"  
     14            text="Just a crate"></object> 
     15    <object gfx="crate" xpos="-4.0" ypos="-6.0" id="crate02" 
     16            text="A dirty old crate"></object> 
    1417</objects> 
    1518 
  • trunk/PARPG/run.py

    r66 r68  
    6767        self.world = world.World(self.engine) 
    6868        self.model = engine.Engine(self.world) 
     69        self.world.data = self.model 
    6970        self.listener = ApplicationListener(self.engine,self.world) 
    7071        self.world.quitFunction = self.listener.quitGame 
  • trunk/PARPG/scripts/agents/hero.py

    r67 r68  
    1 from agent import Agent 
     1#!/usr/bin/python 
     2 
     3#   This program 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#   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. 
     15 
     16import fife 
    217from settings import Setting 
    318 
     
    520_STATE_NONE, _STATE_IDLE, _STATE_RUN = xrange(3) 
    621 
    7 class Hero(Agent): 
     22class Hero(fife.InstanceActionListener): 
    823    """This is the class we use for the PC character""" 
    924    def __init__(self, agentName, layer): 
    10         super(Hero, self).__init__(agentName, layer) 
     25        # add this class for callbacks from fife itself 
     26        fife.InstanceActionListener.__init__(self) 
     27        self.agentName = agentName 
     28        self.agent = layer.getInstance(agentName) 
     29        self.agent.addActionListener(self) 
    1130        self.state = _STATE_NONE 
    1231        self.idlecounter = 1 
  • trunk/PARPG/scripts/agents/npc.py

    r54 r68  
    1717 
    1818from agent import Agent 
    19 from settings import Setting 
    2019 
    21 TDS = Setting() 
    22 _STATE_NONE, _STATE_IDLE, _STATE_RUN = xrange(3) 
     20class NPC: 
     21    """This is the class we use for all NPCs""" 
     22    def __init__(self, xpos, ypos, ident, text): 
     23        self.xpos = xpos 
     24        self.ypos = ypos 
     25        self.id = ident 
     26        self.text = text 
    2327 
    24 class NPC(Agent): 
    25     """This is the class we use for all NPCs""" 
    26     def __init__(self, model, agentName, layer, uniqInMap=True): 
    27         super(NPC, self).__init__(model, agentName, layer, uniqInMap) 
    28         self.state = _STATE_NONE 
    29         self.idlecounter = 1 
    30         #self.speed=float(TDS.readSetting("PCSpeed")) 
    31  
    32     def onInstanceActionFinished(self, instance, action): 
    33         self.idle() 
    34         if action.getId() != 'stand': 
    35             self.idlecounter = 1 
    36         else: 
    37             self.idlecounter += 1 
    38  
    39     def start(self): 
    40         self.idle() 
    41  
    42     def idle(self): 
    43         self.state = _STATE_IDLE 
    44         self.agent.act('stand', self.agent.getFacingLocation()) 
    45  
    46     def run(self, location): 
    47         self.state = _STATE_RUN 
    48         self.agent.move('run',location,self.speed) 
    49  
  • trunk/PARPG/scripts/engine.py

    r66 r68  
    2121from agents.hero import Hero 
    2222from agents.npc import NPC 
     23from objects import GameObject 
    2324 
    2425# design note: 
     
    6566                ypos = attrs.getValue("ypos") 
    6667                gfx = attrs.getValue("gfx") 
     68                ident = attrs.getValue("id") 
     69                text = attrs.getValue("text") 
    6770            except(KeyError): 
    6871                sys.stderr.write("Error: Data missing in NPC definition\n") 
    6972                sys.exit(False) 
    7073            # now we have the data, save it for later 
    71             self.npcs.append([xpos,ypos,gfx]) 
     74            self.npcs.append([xpos, ypos, gfx, ident, text]) 
    7275        elif(name == "object"): 
    7376            # same old same old 
     
    7679                ypos = attrs.getValue("ypos") 
    7780                gfx = attrs.getValue("gfx") 
     81                ident = attrs.getValue("id") 
     82                text = attrs.getValue("text") 
    7883            except(KeyError): 
    7984                sys.stderr.write("Error: Data missing in object definition\n") 
    8085                sys.exit(False) 
    8186            # now we have the data, save it for later 
    82             self.objects.append([xpos, ypos, gfx]) 
     87            self.objects.append([xpos, ypos, gfx, ident, text]) 
    8388 
    8489class Engine: 
     
    132137        for i in objects: 
    133138            self.view.addObject(float(i[0]), float(i[1]), i[2]) 
     139            # now add it as an engine object 
     140            self.objects.append(GameObject(int(float(i[0])), 
     141                                           int(float(i[1])), i[3], i[4])) 
    134142 
    135143    def addNPCs(self,npcs): 
     
    137145           and into this class""" 
    138146        for i in npcs: 
    139             self.view.addObject(float(i[0]), float(i[1]),i[2]) 
     147            self.view.addObject(float(i[0]), float(i[1]), i[2]) 
     148            # now add as engine data 
     149            self.npcs.append(NPC(int(float(i[0])), int(float(i[1])), 
     150                                 i[3], i[4])) 
     151 
     152    def getObjectString(self, xpos, ypos): 
     153        """Get the objects description of itself""" 
     154        # cycle through all of the objects; do we have something there? 
     155        for i in self.objects: 
     156            if((xpos == i.xpos)and(ypos == i.ypos)): 
     157                # yes, we have a match, so return the text 
     158                return i.text 
     159        for i in self.npcs: 
     160            if((xpos == i.xpos)and(ypos == i.ypos)): 
     161                # yes, we have a match, so return the text 
     162                return i.text 
     163        # nothing, but return the empty string in case we print it 
     164        return "" 
    140165 
    141166    def loadMap(self,map_file): 
     
    146171        # then we update FIFE with the PC, NPC and object details 
    147172        self.loadObjects(map_file[:-4]+"_objects.xml") 
    148         # add a callbacks routine to handle mouse clicks 
    149         # TODO: not totally happy about this code 
    150         self.view.mouseCallback=self.handleMouseClick 
    151173 
    152174    def handleMouseClick(self,position): 
  • trunk/PARPG/scripts/world.py

    r66 r68  
    6767        self.inventoryShown = False 
    6868        self.firstInventory = True 
     69        self.data = None 
    6970        self.mouseCallback = None 
    7071 
     
    121122        fife.InstanceVisual.create(obj) 
    122123 
     124    def displayInventory(self): 
     125        """Pause the game and enter the inventory screen""" 
     126 
    123127    # all key / mouse event handling routines go here 
    124128    def keyPressed(self, evt): 
    125         """When a key is depressed, fife calls this routine.""" 
     129        """When a key is pressed, fife calls this routine.""" 
    126130        key = evt.getKey() 
    127131        keyval = key.getValue() 
     
    159163                self.inventoryShown = True 
    160164 
     165    def getCoords(self, click): 
     166        """Get the map location x, y cords that have been clicked""" 
     167        coord = self.cameras["main"].toMapCoordinates(click, False) 
     168        coord.z = 0 
     169        location = fife.Location(self.agent_layer) 
     170        location.setMapCoordinates(coord) 
     171        return location 
     172 
    161173    def mousePressed(self, evt): 
    162174        """If a mouse button is pressed down, fife cals this routine 
    163175           Currently we only check for a left click, and we assume this is on 
    164176           the map""" 
    165         clickpoint = fife.ScreenPoint(evt.getX(), evt.getY()) 
    166         if (evt.getButton() == fife.MouseEvent.LEFT): 
    167             target_mapcoord = self.cameras['main'].toMapCoordinates(clickpoint, 
    168                                                                     False) 
    169             target_mapcoord.z = 0 
    170             l = fife.Location(self.agent_layer) 
    171             l.setMapCoordinates(target_mapcoord) 
    172             self.mouseCallback(l) 
    173              
     177        click = fife.ScreenPoint(evt.getX(), evt.getY()) 
     178        if(evt.getButton() == fife.MouseEvent.LEFT): 
     179            self.data.handleMouseClick(self.getCoords(click)) 
     180        elif(evt.getButton() == fife.MouseEvent.RIGHT): 
     181            # there's no need to query fife about what things are where, 
     182            # the engine code should know.... 
     183            coords = self.getCoords(click).getLayerCoordinates() 
     184            obj = self.data.getObjectString(coords.x, coords.y) 
     185            if(obj != ""): 
     186                print obj 
     187 
    174188    def toggle_renderer (self, r_name): 
    175189        """Enable or disable the renderer named `r_name`""" 
Note: See TracChangeset for help on using the changeset viewer.