changeset 126:0f205b1384db

Make science allocation sticky
author Neil Muller <drnlmuller@gmail.com>
date Thu, 10 May 2012 13:13:57 +0200
parents c224057befff
children 591a85a4edac
files gamelib/gamegui.py
diffstat 1 files changed, 33 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/gamegui.py	Thu May 10 12:19:29 2012 +0200
+++ b/gamelib/gamegui.py	Thu May 10 13:13:57 2012 +0200
@@ -105,20 +105,36 @@
         if (self.parent.game.get_available_points() > 0 and
                 self.science.can_spend(self.parent.game.lab, self.points + 1)):
             self.points += 1
+            self.parent.game.cur_allocation.append(self.science)
+            self.parent.update_labels()
+            self.set_text()
+
+    def set_text(self):
+        if self.points > 0:
             self.text = '%s: %d + %d' % (self.science.NAME,
                     self.science.points, self.points)
-            self._draw_text()
-            self.parent.game.cur_allocation.append(self.science)
-            self.parent.update_labels()
+        else:
+            self.text = '%s: %d' % (self.science.NAME, self.science.points)
+        self._draw_text()
 
     def reset(self):
         while self.points > 0:
             self.parent.game.cur_allocation.remove(self.science)
             self.points -= 1
-        self.text = '%s: %d' % (self.science.NAME, self.science.points)
-        self._draw_text()
+        self.set_text()
         self.parent.update_labels()
 
+    def restore_selection(self, old_selection):
+        points = old_selection.count(self.science)
+        # We skip things we cannot allocate enoigh points to (temp points
+        # boosts, etc.)
+        if (points != self.points and
+                points <= self.parent.game.get_available_points()):
+            self.points = points
+            self.parent.game.cur_allocation.extend([self.science] * points)
+            self.parent.update_labels()
+            self.set_text()
+
 
 class MissionWidget(BigButton):
 
@@ -499,6 +515,10 @@
         for widget in self._sciences:
             widget.reset()
 
+    def restore_selection(self, old_selection):
+        for widget in self._sciences:
+            widget.restore_selection(old_selection)
+
 
 class LabWindow(GameStateWindow):
     """Window for the research lab"""
@@ -543,8 +563,13 @@
     def update_widgets(self):
         self._make_science_widgets()
 
+    def restore_selection(self, old_selection):
+        for widget in self._sciences:
+            widget.restore_selection(old_selection)
+
     def end_turn(self):
         self.game.cur_missions = self.activity.get_mission_list()
+        old_allocation = self.game.cur_allocation
         messages = self.game.end_turn()
         results = ResultsWindow(self.screen, messages, self.game.turn)
         if results.is_game_over:
@@ -557,6 +582,9 @@
         self.update_widgets()
         self.develop.update_widgets()
         self.activity.update_widgets()
+        # restore previous allocation
+        self.restore_selection(old_allocation)
+        self.develop.restore_selection(old_allocation)
         AddWindow.post(results)
 
     def save_game(self):