comparison charactercreationcontroller.py @ 113:c76f03c290f6

Removed imports of the old objects.
author KarstenBock@gmx.net
date Sat, 01 Oct 2011 14:39:27 +0200
parents 7b31de1dc964
children 8bb84164e6a2
comparison
equal deleted inserted replaced
112:c40d04cf6ed1 113:c76f03c290f6
19 import characterstatistics as char_stats 19 import characterstatistics as char_stats
20 from serializers import XmlSerializer 20 from serializers import XmlSerializer
21 from controllerbase import ControllerBase 21 from controllerbase import ControllerBase
22 from gamescenecontroller import GameSceneController 22 from gamescenecontroller import GameSceneController
23 from gamesceneview import GameSceneView 23 from gamesceneview import GameSceneView
24 from parpg.inventory import Inventory 24 from parpg.world import World
25 from parpg.entities import General
25 26
26 DEFAULT_STAT_VALUE = 50 27 DEFAULT_STAT_VALUE = 50
27 28
28 29
29 def getStatCost(offset): 30 def getStatCost(offset):
50 return 8 51 return 8
51 elif offset < 41: 52 elif offset < 41:
52 return 9 53 return 9
53 else: 54 else:
54 return 10 55 return 10
55
56 #TODO: Should be replaced with the real character class once its possible
57 class SimpleCharacter(object):
58 """This is a simple class that is used to store the data during the
59 character creation"""
60
61 def __init__(self, name, gender, origin, age, picture, traits,
62 primary_stats, secondary_stats, inventory):
63 self.name = name
64 self.gender = gender
65 self.origin = origin
66 self.age = age
67 self.picture = picture
68 self.traits = traits
69 self.statistics = {}
70 for primary_stat in primary_stats:
71 short_name = primary_stat.short_name
72 self.statistics[short_name] = char_stats.PrimaryStatisticValue(
73 primary_stat,
74 self,
75 DEFAULT_STAT_VALUE)
76 long_name = primary_stat.long_name
77 self.statistics[long_name] = char_stats.PrimaryStatisticValue(
78 primary_stat,
79 self,
80 DEFAULT_STAT_VALUE)
81 for secondary_stat in secondary_stats:
82 name = secondary_stat.name
83 self.statistics[name] = char_stats.SecondaryStatisticValue(
84 secondary_stat,
85 self)
86 self.inventory = inventory
87 56
88 class CharacterCreationController(ControllerBase): 57 class CharacterCreationController(ControllerBase, World):
89 """Controller defining the behaviour of the character creation screen.""" 58 """Controller defining the behaviour of the character creation screen."""
90 59
91 #TODO: Change to actual values 60 #TODO: Change to actual values
92 MAX_TRAITS = 3 61 MAX_TRAITS = 3
93 MIN_AGE = 16 62 MIN_AGE = 16
94 MAX_AGE = 40 63 MAX_AGE = 40
95 ORIGINS = {"None": None,} 64 ORIGINS = {"None": None,}
96 GENDERS = ["Male", "Female",] 65 GENDERS = ["Male", "Female",]
97 PICTURES = {"Male": ["None",], "Female": ["None",],} 66 PICTURES = {"Male": ["None",], "Female": ["None",],}
98 TRAITS = {} 67 TRAITS = {}
68 MAX_BULK = 100
69 INV_SLOTS = 20
70
99 def __init__(self, engine, view, model, application): 71 def __init__(self, engine, view, model, application):
100 """Construct a new L{CharacterCreationController} instance. 72 """Construct a new L{CharacterCreationController} instance.
101 @param engine: Rendering engine used to display the associated view. 73 @param engine: Rendering engine used to display the associated view.
102 @type engine: L{fife.Engine} 74 @type engine: L{fife.Engine}
103 @param view: View used to display the character creation screen. 75 @param view: View used to display the character creation screen.
107 @param application: Application used to glue the various MVC 79 @param application: Application used to glue the various MVC
108 components together. 80 components together.
109 @type application: 81 @type application:
110 L{fife.extensions.basicapplication.ApplicationBase}""" 82 L{fife.extensions.basicapplication.ApplicationBase}"""
111 ControllerBase.__init__(self, engine, view, model, application) 83 ControllerBase.__init__(self, engine, view, model, application)
84 World.__init__(self)
112 self.settings = self.model.settings 85 self.settings = self.model.settings
113 self.view.start_new_game_callback = self.startNewGame 86 self.view.start_new_game_callback = self.startNewGame
114 self.view.cancel_new_game_callback = self.cancelNewGame 87 self.view.cancel_new_game_callback = self.cancelNewGame
115 self.view.show()
116 # FIXME M. George Hansen 2011-06-06: character stats scripts aren't 88 # FIXME M. George Hansen 2011-06-06: character stats scripts aren't
117 # finished, unfortunately. 89 # finished, unfortunately.
118 # primary_stats_file = \ 90 # primary_stats_file = \
119 # vfs.VFS.open('character_scripts/primary_stats.xml') 91 # vfs.VFS.open('character_scripts/primary_stats.xml')
120 # primary_stats = XmlSerializer.deserialize(primary_stats_file) 92 # primary_stats = XmlSerializer.deserialize(primary_stats_file)
121 # secondary_stats_file = \ 93 # secondary_stats_file = \
122 # vfs.VFS.open('character_scripts/secondary_stats.xml') 94 # vfs.VFS.open('character_scripts/secondary_stats.xml')
123 # secondary_stats = XmlSerializer.deserialize(secondary_stats_file) 95 # secondary_stats = XmlSerializer.deserialize(secondary_stats_file)
96
97 def reset_character(self):
124 primary_stats = [] 98 primary_stats = []
125 secondary_stats = [] 99 secondary_stats = []
126 self.char_data = SimpleCharacter( 100 inventory = []
127 "", 101 for x in xrange(self.INV_SLOTS):
128 self.GENDERS[0], 102 inventory.append(None)
129 self.ORIGINS.keys()[0], 103 self.char_data = General(self, "PlayerCharacter")
130 20, 104 self.char_data.description.view_name = "Player"
131 self.PICTURES[self.GENDERS[0]][0], 105 self.char_data.description.real_name = "Enter name here"
132 [], 106 self.char_data.characterstats.gender = self.GENDERS[0]
133 primary_stats, 107 self.char_data.characterstats.origin = self.ORIGINS.keys()[0]
134 secondary_stats, 108 self.char_data.characterstats.age = 20
135 Inventory() 109 self.char_data.characterstats.picture = (
110 self.PICTURES[self.GENDERS[0]][0]
136 ) 111 )
112 for primary_stat in primary_stats:
113 short_name = primary_stat.short_name
114 self.char_data.characterstats.primary_stats[short_name] = (
115 char_stats.PrimaryStatisticValue(
116 primary_stat, self, DEFAULT_STAT_VALUE)
117 )
118 for secondary_stat in secondary_stats:
119 name = secondary_stat.name
120 self.char_data.characterstats.secondary_stats[name] = (
121 char_stats.SecondaryStatisticValue(secondary_stat, self)
122 )
123 self.char_data.container.max_bulk = self.MAX_BULK
124 self.char_data.container.children = inventory
137 self._stat_points = 200 125 self._stat_points = 200
138 126
139 127
140 def startNewGame(self): 128 def startNewGame(self):
141 """Create the new character and start a new game. 129 """Create the new character and start a new game.
159 controller = MainMenuController(self.engine, view, self.model, 147 controller = MainMenuController(self.engine, view, self.model,
160 self.application) 148 self.application)
161 self.application.view = view 149 self.application.view = view
162 self.application.manager.activate_mode(controller) 150 self.application.manager.activate_mode(controller)
163 151
152 def on_activate(self):
153 self.reset_character()
154 self.view.show()
155
164 def on_deactivate(self): 156 def on_deactivate(self):
165 """Called when the controller is removed from the list. 157 """Called when the controller is removed from the list.
166 @return: None""" 158 @return: None"""
167 self.view.hide() 159 self.view.hide()
168 160
169 @property 161 @property
170 def name(self): 162 def name(self):
171 """Returns the name of the character. 163 """Returns the name of the character.
172 @return: Name of the character""" 164 @return: Name of the character"""
173 return self.char_data.name 165 return self.char_data.description.real_name
174 166
175 @property 167 @property
176 def age(self): 168 def age(self):
177 """Returns the age of the character. 169 """Returns the age of the character.
178 @return: Age of the character""" 170 @return: Age of the character"""
179 return self.char_data.age 171 return self.char_data.characterstats.age
180 172
181 @property 173 @property
182 def gender(self): 174 def gender(self):
183 """Returns the gender of the character. 175 """Returns the gender of the character.
184 @return: Gender of the character""" 176 @return: Gender of the character"""
185 return self.char_data.gender 177 return self.char_data.characterstats.gender
186 178
187 @property 179 @property
188 def origin(self): 180 def origin(self):
189 """Returns the origin of the character. 181 """Returns the origin of the character.
190 @return: Origin of the character""" 182 @return: Origin of the character"""
191 return self.char_data.origin 183 return self.char_data.characterstats.origin
192 184
193 @property 185 @property
194 def picture(self): 186 def picture(self):
195 """Returns the ID of the current picture of the character.""" 187 """Returns the ID of the current picture of the character."""
196 return self.char_data.picture 188 return self.char_data.characterstats.picture
197 189
198 def getStatPoints(self): 190 def getStatPoints(self):
199 """Returns the remaining statistic points that can be distributed""" 191 """Returns the remaining statistic points that can be distributed"""
200 return self._stat_points 192 return self._stat_points
201 193
204 @param statistic: Name of the statistic to increase 196 @param statistic: Name of the statistic to increase
205 @type statistic: string""" 197 @type statistic: string"""
206 if self.canIncreaseStatistic(statistic): 198 if self.canIncreaseStatistic(statistic):
207 cost = self.getStatisticIncreaseCost(statistic) 199 cost = self.getStatisticIncreaseCost(statistic)
208 if cost <= self._stat_points: 200 if cost <= self._stat_points:
209 self.char_data.statistics[statistic].value += 1 201 (self.char_data.characterstats.
202 primary_stats[statistic].value) += 1
210 self._stat_points -= cost 203 self._stat_points -= cost
211 204
212 def getStatisticIncreaseCost(self, statistic): 205 def getStatisticIncreaseCost(self, statistic):
213 """Calculate and return the cost to increase the statistic 206 """Calculate and return the cost to increase the statistic
214 @param statistic: Name of the statistic to increase 207 @param statistic: Name of the statistic to increase
215 @type statistic: string 208 @type statistic: string
216 @return cost to increase the statistic""" 209 @return cost to increase the statistic"""
217 cur_value = self.char_data.statistics[statistic].value 210 cur_value = (self.char_data.characterstats.
211 primary_stats[statistic].value)
218 new_value = cur_value + 1 212 new_value = cur_value + 1
219 offset = new_value - DEFAULT_STAT_VALUE 213 offset = new_value - DEFAULT_STAT_VALUE
220 return getStatCost(offset) 214 return getStatCost(offset)
221 215
222 def canIncreaseStatistic(self, statistic): 216 def canIncreaseStatistic(self, statistic):
223 """Checks whether the given statistic can be increased or not. 217 """Checks whether the given statistic can be increased or not.
224 @param statistic: Name of the statistic to check 218 @param statistic: Name of the statistic to check
225 @type statistic: string 219 @type statistic: string
226 @return: True if the statistic can be increased, False if not.""" 220 @return: True if the statistic can be increased, False if not."""
227 stat = self.char_data.statistics[statistic].value 221 stat = self.char_data.characterstats.primary_stats[statistic].value
228 return stat < stat.statistic_type.maximum 222 return stat < stat.statistic_type.maximum
229 223
230 def decreaseStatistic(self, statistic): 224 def decreaseStatistic(self, statistic):
231 """Decreases the given statistic by one. 225 """Decreases the given statistic by one.
232 @param statistic: Name of the statistic to decrease 226 @param statistic: Name of the statistic to decrease
233 @type statistic: string""" 227 @type statistic: string"""
234 if self.canDecreaseStatistic(statistic): 228 if self.canDecreaseStatistic(statistic):
235 gain = self.getStatisticDecreaseGain(statistic) 229 gain = self.getStatisticDecreaseGain(statistic)
236 self.char_data.statistics[statistic].value -= 1 230 self.char_data.characterstats.primary_stats[statistic].value -= 1
237 self._stat_points += gain 231 self._stat_points += gain
238 232
239 def getStatisticDecreaseGain(self, statistic): 233 def getStatisticDecreaseGain(self, statistic):
240 """Calculate and return the gain of decreasing the statistic 234 """Calculate and return the gain of decreasing the statistic
241 @param statistic: Name of the statistic to decrease 235 @param statistic: Name of the statistic to decrease
242 @type statistic: string 236 @type statistic: string
243 @return cost to decrease the statistic""" 237 @return cost to decrease the statistic"""
244 cur_value = self.char_data.statistics[statistic].value 238 cur_value = (self.char_data.characterstats.
239 primary_stats[statistic].value)
245 new_value = cur_value - 1 240 new_value = cur_value - 1
246 offset = new_value - DEFAULT_STAT_VALUE 241 offset = new_value - DEFAULT_STAT_VALUE
247 return getStatCost(offset) 242 return getStatCost(offset)
248 243
249 def canDecreaseStatistic(self, statistic): 244 def canDecreaseStatistic(self, statistic):
250 """Checks whether the given statistic can be decreased or not. 245 """Checks whether the given statistic can be decreased or not.
251 @param statistic: Name of the statistic to check 246 @param statistic: Name of the statistic to check
252 @type statistic: string 247 @type statistic: string
253 @return: True if the statistic can be decreased, False if not.""" 248 @return: True if the statistic can be decreased, False if not."""
254 stat = self.char_data.statistics[statistic].value 249 stat = self.char_data.characterstats.primary_stats[statistic].value
255 return stat > stat.statistic_type.minimum 250 return stat > stat.statistic_type.minimum
256 251
257 def getStatisticValue(self, statistic): 252 def getStatisticValue(self, statistic):
258 """Returns the value of the given statistic. 253 """Returns the value of the given statistic.
259 @param statistic: Name of the primary or secondary statistic 254 @param statistic: Name of the primary or secondary statistic
260 @type statistic: string 255 @type statistic: string
261 @return: Value of the given statistic""" 256 @return: Value of the given statistic"""
262 return self.char_data.statistics[statistic] 257 if self.char_data.characterstats.primary_stats.has_key:
258 return self.char_data.characterstats.primary_stats[statistic]
259 else:
260 return self.char_data.characterstats.secondary_stats[statistic]
263 261
264 def areAllStatisticsValid(self): 262 def areAllStatisticsValid(self):
265 """Checks if all statistics are inside the minimum/maximum values 263 """Checks if all statistics are inside the minimum/maximum values
266 @return True if all statistics are valid False if not""" 264 @return True if all statistics are valid False if not"""
267 for stat in self.char_data.statistics.items(): 265 all_stats = self.char_data.characterstats.primary_stats.items()
266 all_stats.extend(self.char_data.characterstats.secondary_stats.items())
267 for stat in all_stats:
268 if not (stat.value > stat.statistic_type.minumum and\ 268 if not (stat.value > stat.statistic_type.minumum and\
269 stat.value < stat.statistic_type.maximum): 269 stat.value < stat.statistic_type.maximum):
270 return False 270 return False
271 return True 271 return True
272 272
273 def setName(self, name): 273 def setName(self, name):
274 """Sets the name of the character to the given value. 274 """Sets the name of the character to the given value.
275 @param name: New name 275 @param name: New name
276 @type name: string""" 276 @type name: string"""
277 self.char_data.name = name 277 self.char_data.description.real_name = name
278 278
279 def isNameValid(self, name): 279 def isNameValid(self, name):
280 """Checks whether the name is valid. 280 """Checks whether the name is valid.
281 @param name: Name to check 281 @param name: Name to check
282 @type name: string 282 @type name: string
288 def changeOrigin(self, origin): 288 def changeOrigin(self, origin):
289 """Changes the origin of the character to the given value. 289 """Changes the origin of the character to the given value.
290 @param origin: New origin 290 @param origin: New origin
291 @type origin: string""" 291 @type origin: string"""
292 if self.isOriginValid(origin): 292 if self.isOriginValid(origin):
293 self.char_data.origin = origin 293 self.char_data.characterstats.origin = origin
294 #TODO: Make changes according to origin 294 #TODO: Make changes according to origin
295 295
296 def isOriginValid(self, origin): 296 def isOriginValid(self, origin):
297 """Checks whether the origin is valid. 297 """Checks whether the origin is valid.
298 @param origin: Origin to check 298 @param origin: Origin to check
303 def changeGender(self, gender): 303 def changeGender(self, gender):
304 """Changes the gender of the character to the given value. 304 """Changes the gender of the character to the given value.
305 @param gender: New gender 305 @param gender: New gender
306 @param gender: string""" 306 @param gender: string"""
307 if self.isGenderValid(gender): 307 if self.isGenderValid(gender):
308 self.char_data.gender = gender 308 self.char_data.characterstats.gender = gender
309 309
310 def isGenderValid(self, gender): 310 def isGenderValid(self, gender):
311 """Checks whether the gender is valid. 311 """Checks whether the gender is valid.
312 @param gender: Gender to check 312 @param gender: Gender to check
313 @type gender: string? 313 @type gender: string?
318 """Sets the age of the character to the given value. 318 """Sets the age of the character to the given value.
319 @param age: New age 319 @param age: New age
320 @type age: integer 320 @type age: integer
321 """ 321 """
322 if self.isAgeValid(age): 322 if self.isAgeValid(age):
323 self.char_data.age = age 323 self.char_data.characterstats.age = age
324 324
325 def isAgeValid(self, age): 325 def isAgeValid(self, age):
326 """Checks whether the age is valid. 326 """Checks whether the age is valid.
327 @param age: Age to check 327 @param age: Age to check
328 @type age: integer 328 @type age: integer
332 def setPicture(self, picture): 332 def setPicture(self, picture):
333 """Set picture of the character. 333 """Set picture of the character.
334 @param picture: ID of the new picture 334 @param picture: ID of the new picture
335 @type picture: string""" 335 @type picture: string"""
336 if self.isPictureValid(picture): 336 if self.isPictureValid(picture):
337 self.char_data.picture = picture 337 self.char_data.characterstats.picture = picture
338 338
339 def isPictureValid(self, picture): 339 def isPictureValid(self, picture):
340 """Checks whether the picture is valid. 340 """Checks whether the picture is valid.
341 @param picture: ID of the picture to check 341 @param picture: ID of the picture to check
342 @type picture: string 342 @type picture: string
347 """Adds a trait to the character. 347 """Adds a trait to the character.
348 @param trait: ID of the trait to add 348 @param trait: ID of the trait to add
349 @type trait: string""" 349 @type trait: string"""
350 if self.canAddAnotherTrait() and self.isTraitValid(trait)\ 350 if self.canAddAnotherTrait() and self.isTraitValid(trait)\
351 and not self.hasTrait(trait): 351 and not self.hasTrait(trait):
352 self.char_data.traits.append(trait) 352 self.char_data.characterstats.traits.append(trait)
353 353
354 def canAddAnotherTrait(self): 354 def canAddAnotherTrait(self):
355 """Checks whether another trait can be added. 355 """Checks whether another trait can be added.
356 @return: True if another trait can be added, False if not""" 356 @return: True if another trait can be added, False if not"""
357 return len(self.char_data.traits) < self.MAX_TRAITS 357 return len(self.char_data.characterstats.traits) < self.MAX_TRAITS
358 358
359 def removeTrait(self, trait): 359 def removeTrait(self, trait):
360 """Remove trait from character. 360 """Remove trait from character.
361 @param trait: ID of the trait to remove 361 @param trait: ID of the trait to remove
362 @type trait: string""" 362 @type trait: string"""
363 if self.hasTrait(trait): 363 if self.hasTrait(trait):
364 self.char_data.traits.remove(trait) 364 self.char_data.characterstats.traits.remove(trait)
365 365
366 def hasTrait(self, trait): 366 def hasTrait(self, trait):
367 """Checks whether the character has the trait. 367 """Checks whether the character has the trait.
368 @param trait: ID of the trait to check 368 @param trait: ID of the trait to check
369 @type trait: string 369 @type trait: string
370 @return: True if the character has the trait, False if not""" 370 @return: True if the character has the trait, False if not"""
371 return trait in self.char_data.traits 371 return trait in self.char_data.characterstats.traits
372 372
373 def isTraitValid(self, trait): 373 def isTraitValid(self, trait):
374 """Checks whether the trait is valid. 374 """Checks whether the trait is valid.
375 @param trait: ID of the trait to check 375 @param trait: ID of the trait to check
376 @type trait: string 376 @type trait: string
378 return trait in self.TRAITS 378 return trait in self.TRAITS
379 379
380 def areCurrentTraitsValid(self): 380 def areCurrentTraitsValid(self):
381 """Checks whether the characters traits are valid. 381 """Checks whether the characters traits are valid.
382 @return: True if the traits are valid, False if not""" 382 @return: True if the traits are valid, False if not"""
383 if len(self.char_data.traits) > self.MAX_TRAITS: 383 if len(self.char_data.characterstats.traits) > self.MAX_TRAITS:
384 return False 384 return False
385 for trait in self.char_data.traits: 385 for trait in self.char_data.characterstats.traits:
386 if not self.isTraitValid(trait): 386 if not self.isTraitValid(trait):
387 return False 387 return False
388 return True 388 return True
389 389
390 def isCharacterValid(self): 390 def isCharacterValid(self):