(no commit message)
[asgard.git] / battle.py
blobd23b6d35d8d45c76cec926fa4c5bfd112b5f3f38
1 ########################################################
2 #Copyright (c) 2006 Russ Adams, Sean Eubanks, Asgard Contributors
3 #This file is part of Asgard.
5 #Asgard 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 2 of the License, or
8 #(at your option) any later version.
10 #Asgard 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.
15 #You should have received a copy of the GNU General Public License
16 #along with Asgard; if not, write to the Free Software
17 #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 ########################################################
19 from os import listdir
20 from time import *
21 from fighter import *
22 from party import *
23 from event import *
25 class Battle:
26 """ Model: Battle Class """
28 __currentTime = 0
29 """ The number of iterations of the gameLoop since the battle began. """
31 __party1 = None
32 """ The hero party. """
34 __party2 = None
35 """ The villain party. """
37 __eventTypes = []
38 """ List of static EventType objects. """
40 __monsters = []
41 """ List of static monsters Fighter objects. """
43 view = None
44 """ Link to the viewer object. Currently BattleScreen. """
46 cont = None
47 """ Link to the cont object. Currently Asgard. """
49 def __init__(self):
50 """ Constructs a Battle object. Current Time set to 0, both
51 parties nullified and eventtype/monster static data defaults to
52 empty. """
54 self.__currentTime = 0
56 self.__party1 = None
57 self.__party2 = None
58 self.__eventTypes = []
59 self.__monsters = []
61 def isOver(self):
62 """ Is one of the parties dead? """
63 return not self.__party1.isAlive() or not self.__party2.isAlive()
65 def nextTurn(self):
66 """ Executes the next iteration of the game loop: After a pause
67 of some duration, increment the current time of the battle.
68 Then, execute all the events that are available for execution
69 this iteration. Also, calls Status.genIteration(). Returns true
70 if an event was executed. """
72 sleep(.1)
74 wereEvents = False
76 # execute all available events
77 while self.executeNextEvent():
78 # if there was at least one event
79 wereEvents = True
80 # generate per-iteration status transactions
81 for f in self.__party1.getFighters():
82 for s in f.getStatus():
83 s.genIteration()
84 for f in self.__party2.getFighters():
85 for s in f.getStatus():
86 s.genIteration()
87 continue
89 self.__currentTime += 1
91 return wereEvents
93 def executeNextEvent(self):
94 """ Query the two parties to see if there are any events that
95 can be generated this iteration of the game loop. If there are
96 events, execute the event from the party whose fighter has the
97 highest dexterity. Return whether or not the event was executed
98 or not. """
101 if self.isOver():
102 return False
104 # Try to get an event from party1
105 event = self.__party1.getNextEvent(self.__party2,self.__currentTime,self.cont)
107 # If there was one, execute and return true
108 if event != None:
109 event.execute()
110 #drawTransactions...
111 return True
113 # Try to get an event from party2
114 event = self.__party2.getNextEvent(self.__party1,self.__currentTime,self.cont)
116 # If there was one, execute and return true
117 if event != None:
118 event.execute()
119 #drawTransactions...
120 return True
122 # Otherwise, notify that there are no more events at this time.
123 return False
125 def getCurrentTime(self):
126 return self.__currentTime
128 def setCurrentTime(self,t):
129 self.__currentTime = t
131 def getParty1(self):
132 return self.__party1
134 def setParty1(self, p):
135 self.__party1 = p
137 def getParty2(self):
138 return self.__party2
140 def setParty2(self, p):
141 self.__party2 = p
144 def getEventTypeList(self):
145 return self.__eventTypes
147 def setEventTypeList(self,evts):
148 self.__eventTypes = evts
150 def getEventType(self,et_s):
151 for et in self.__eventTypes:
152 if et.getName() == et_s:
153 return et
155 def getMonsters(self):
156 return self.__monsters
158 def setMonsters(self,monsters):
159 self.__monsters = monsters