source: trunk/PARPG/scripts/engine.py @ 122

Revision 122, 5.4 KB checked in by maximinus_parpg, 10 years ago (diff)

A few minor edits.
Added windmill gfx and to the map, but FIFE can't load the PNG's.
Left PARPG in a playable state.

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
18# there should be NO references to FIFE here!
19from agents.hero import Hero
20from agents.npc import NPC
21from objects import GameObject
22from objLoader import LocalXMLParser
23
24# design note:
25# there is a map file that FIFE reads. We use that file for half the map
26# format because the map editor in FIFE uses it, and secondly because it
27# save us writing a bunch of new code.
28# However, the objects and characters on a map are liable to change
29# whilst the game is being run, so when we change the map, we need to
30# to grab the objects and npc data EITHER from the engine state, or grab
31# from another file if in their initial state
32# This other file has the name AAA_objects.xml where AAA.xml is the name
33# of the original mapfile.
34
35class Engine:
36    """Engine holds the logic for the game
37       Since some data (object position and so forth) is held in the
38       fife, and would be pointless to replicate, we hold a instance of
39       the fife view here. This also prevents us from just having a
40       function heavy controller"""
41    def __init__(self, view):
42        # a World object
43        self.view = view
44        self.PC = None
45        self.npcs = []
46        self.objects = []
47
48    def loadObjects(self, filename):
49        """Load objects from the XML file
50           Returns True if it worked, False otherwise"""
51        try:
52            objects_file = open(filename, 'rt')
53        except(IOError):
54            sys.stderr.write("Error: Can't find objects file\n")
55            return False
56        # now open and read the XML file
57        cur_handler = LocalXMLParser()
58        parser = cur_handler.getParser()
59        parser.setContentHandler(cur_handler)
60        parser.parse(objects_file)
61        objects_file.close()
62        # must have at least 1 PC
63        if(cur_handler.pc == None):
64            sys.stderr.write("Error: No PC defined\n")
65            sys.exit(False)
66        # now add to the map and the engine
67        self.addPC(cur_handler.pc)
68        self.addNPCs(cur_handler.npcs)
69        self.addObjects(cur_handler.objects)
70        return True
71
72    def addPC(self,pc):
73        """Add the PC to the world"""
74        self.view.addObject(float(pc[0]), float(pc[1]),"PC","PC")
75        self.PC = Hero("PC", self.view.agent_layer)
76        # ensure the PC starts on a default action
77        self.PC.start()
78        self.view.addPC(self.PC.agent)
79
80    def addObjects(self,objects):
81        """Add all of the objects we found into the fife map
82           and into our class
83           An NPC is just an object to FIFE"""
84        for i in objects:
85            # is it visible?
86            if(i[0] == True):
87                self.view.addObject(float(i[1]), float(i[2]), i[3], i[4])
88            # now add it as an engine object
89            self.objects.append(GameObject(i))
90
91    def addNPCs(self,npcs):
92        """Add all of the NPCs we found into the fife map
93           and into this class"""
94        for i in npcs:
95            self.view.addObject(float(i[0]), float(i[1]), i[2], i[3])
96            # now add as engine data
97            self.npcs.append(NPC(i[4], str(i[3]), self.view.agent_layer))
98            self.npcs[-1].start()
99
100    def objectActive(self, ident):
101        """Given the objects ID, pass back the object if it is active,
102           False if it doesn't exist or not displayed"""
103        for i in self.objects:
104            if((i.display == True)and(i.id == ident)):
105                # we found a match
106                return i
107        # now try NPC's
108        for i in self.npcs:
109            # all NPC's are deemed active
110            if(i.id == ident):
111                return i
112        # no match
113        return False
114
115    def getItemActions(self, obj_id):
116        """Given the objects ID, return the text strings and callbacks"""
117        actions=[]
118        # note: ALWAYS check NPC's first!
119        # is it an NPC?
120        for i in self.npcs:
121            if(obj_id == i.id):
122                # keep it simple for now
123                actions.append(("Talk",None))
124                actions.append(("Attack",None))
125        # is it in our objects?
126        for i in self.objects:
127            if(obj_id == i.id):
128                actions.append(("Examine",None))
129                # is it a container?
130                if(i.container == True):
131                    actions.append(("Open",None))
132                # can you pick it up?
133                if(i.carry == True):
134                    actions.append(("Pick Up",None))
135                return actions
136        return actions
137
138    def loadMap(self,map_file):
139        """Load a new map
140           TODO: needs some error checking"""
141        # first we let FIFE load the rest of the map
142        self.view.load(map_file)
143        # then we update FIFE with the PC, NPC and object details
144        self.loadObjects(map_file[:-4]+"_objects.xml")
145
146    def handleMouseClick(self,position):
147        self.PC.run(position)
148
Note: See TracBrowser for help on using the repository browser.