Changeset 114


Ignore:
Timestamp:
06/10/09 16:35:54 (10 years ago)
Author:
maximinus_parpg
Message:

Added meggies code to move the NPC
Good work!

Location:
trunk/PARPG/scripts
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/PARPG/scripts/agents/npc.py

    r70 r114  
    1616#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>. 
    1717 
    18 class NPC: 
     18import fife 
     19from settings import Setting 
     20from random import randint 
     21 
     22TDS = Setting() 
     23_STATE_NONE, _STATE_IDLE, _STATE_WANDER = xrange(3) 
     24 
     25class NPC(fife.InstanceActionListener): 
    1926    """This is the class we use for all NPCs""" 
    20     def __init__(self, xpos, ypos, ident, text): 
    21         self.xpos = xpos 
    22         self.ypos = ypos 
    23         self.id = ident 
     27    def __init__(self, text, agentName, layer): 
     28        """Init function. 
     29           @param text: a string of text that will be output to screen when 
     30               character is right clicked 
     31           @param agentName: the 'id' of the NPC in the map_object.xml file 
     32           @param layer: a fife.Instance object, (engine.view.agent_layer)""" 
     33        fife.InstanceActionListener.__init__(self) 
     34        self.text = text 
     35        self.agentName = agentName 
     36        # a fife.Instance 
     37        self.agent = layer.getInstance(agentName) 
     38        self.agent.addActionListener(self) 
     39        self.state = _STATE_NONE 
     40        self.speed = float(TDS.readSetting("PCSpeed"))-1 
     41        self.target_loc = self.getTargetLocation()  
    2442        self.text = text 
    2543 
     44    def getX(self): 
     45        """@return: the x coordinate of the NPC's location as an int""" 
     46        return self.agent.getLocation().getLayerCoordinates().x 
     47 
     48    def getY(self): 
     49        """@return: the y coordinate of the NPC's location as an int""" 
     50        return self.agent.getLocation().getLayerCoordinates().y 
     51 
     52    def getTargetLocation(self): 
     53        """@return: a fife.Location object based off of the NPC's 
     54           position and current state""" 
     55        x = self.getX() 
     56        y = self.getY() 
     57        if self.state == _STATE_WANDER: 
     58            """ Random Walk """ 
     59            rl = randint(-1, 1) 
     60            ud = randint(-1, 1) 
     61            x += rl 
     62            y += ud 
     63        l = fife.Location(self.agent.getLocation()) 
     64        l.setLayerCoordinates(fife.ModelCoordinate(*tuple([x, y]))) 
     65        return l 
     66     
     67    def onInstanceActionFinished(self, instance, action): 
     68        """What the NPC does when it has finished an action. 
     69           Called somewhere else (TODO: where?)""" 
     70        if self.state == _STATE_WANDER: 
     71            self.target_loc = self.getTargetLocation() 
     72        self.idle() 
     73 
     74    def start(self): 
     75        self.idle() 
     76 
     77    def idle(self): 
     78        """Controls the NPC when it is idling. Different actions 
     79           based on the NPC's state""" 
     80        if self.state == _STATE_NONE: 
     81            self.state = _STATE_IDLE 
     82            self.agent.act('stand', self.agent.getFacingLocation()) 
     83        elif self.state == _STATE_IDLE: 
     84            self.target_loc = self.getTargetLocation() 
     85            self.state = _STATE_WANDER 
     86            self.agent.act('stand', self.agent.getFacingLocation()) 
     87        elif self.state == _STATE_WANDER: 
     88            self.wander(self.target_loc) 
     89 
     90    def wander(self, location): 
     91        """Nice slow movement for random walking. 
     92           @param location: a fife.Location object, where the NPC 
     93           will walk to""" 
     94        self.agent.move('walk', location, self.speed-1) 
     95 
     96    def run(self, location): 
     97        """Faster movement than walk. 
     98           @param location: a fife.Location object, where the NPC will run to""" 
     99        self.agent.move('run', location, self.speed+1) 
     100 
  • trunk/PARPG/scripts/engine.py

    r111 r114  
    4040       function heavy controller""" 
    4141    def __init__(self, view): 
     42        # a World object 
    4243        self.view = view 
    4344        self.PC = None 
     
    9495            self.view.addObject(float(i[0]), float(i[1]), i[2], i[3]) 
    9596            # now add as engine data 
    96             self.npcs.append(NPC(int(float(i[0])), int(float(i[1])), 
    97                                  i[3], i[4])) 
     97            self.npcs.append(NPC(i[4], str(i[3]), self.view.agent_layer)) 
     98            self.npcs[-1].start() 
    9899 
    99100    def objectActive(self, ident): 
     
    107108        for i in self.npcs: 
    108109            # all NPC's are deemed active 
    109             if(i.id == ident): 
     110            if(i.agentName == ident): 
    110111                return i 
    111112        # no match 
Note: See TracChangeset for help on using the changeset viewer.