first stab at automatic variable light pricing
authoradrianna <adrianna.pinska@gmail.com>
Wed, 14 Sep 2016 22:45:55 +0000 (00:45 +0200)
committeradrianna <adrianna.pinska@gmail.com>
Wed, 14 Sep 2016 22:45:55 +0000 (00:45 +0200)
TODO.txt
data/stations/01-tutorial.json
data/stations/02-tunnels.json
data/stations/03-lighthouse.json
data/stations/04-multicolor.json
data/stations/05-spiral.json
data/stations/06-station-alpha.json
tabakrolletjie/lights.py
tabakrolletjie/scenes/day.py

index 004306010c3423c1cd3dc9fac94f038e04370b2e..a38aa5886435b4517403f9dbef006e5c9c7fb766 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
@@ -10,9 +10,6 @@ Features
 * Maybe configure lights in separate json file, to allow reuse of consistent models in multiple levels?
 * Allow going down to zero seeds by buying lights if a turnip is planted
 * Maybe make the mould weaker at the start
-* Different prices for different lights
-* Different power usage for different lights
-* Calculate both from # of colours, speed, beam width, etc.?
 * Display power usage when lights are bought? Also other properties.
 * Maybe also display information next to existing lights (on click?)
 * Generally balance the levels better and add more levels
@@ -49,3 +46,6 @@ Done
 * Bug: Crash when removing a rotating light
 * Turnip / light spacing bug: a light can be placed right below a turnip, but must be much further away if above the turnip. This happens whether the turnip or the light is placed first. I assume it has to do with how the turnip image is positioned relative to its body.
 * Allow denser turnip spacing
+* Different power usage for different lights (this was already done)
+* Different prices for different lights
+* Calculate both from # of colours, speed, beam width, etc.?
index 3c2ec596c7aa866face3cbcb8f1f9d51a305abb4..d7fb07f880138bf5536d07e5d5d8541e5841e652 100644 (file)
@@ -13,7 +13,6 @@
   "available_lights": [
     {
       "type": "lamp",
-      "cost": 3,
       "available_colours": ["blue", "red", "blue/red"],
       "intensity": 0.5
     }
index 14cac71e1447b4b5957641f61f4e0dfd18ad4a0d..bd0dfa1353ba2f0a72ad4d967a9f848afb03beee 100644 (file)
@@ -13,7 +13,6 @@
   "available_lights": [
     {
       "type": "spotlight",
-      "cost": 3,
       "available_colours": ["blue", "red"],
       "intensity": 0.6,
       "spread": 35,
index 212d4ac3ecd19699a0360f3277a7a3115b1bef45..8320fd6bcc4155ed94b7c794d752ccd1a9f7be9e 100644 (file)
@@ -13,7 +13,6 @@
   "available_lights": [
     {
       "type": "spotlight",
-      "cost": 3,
       "available_colours": ["yellow", "magenta"],
       "intensity": 0.5,
       "radius_limits": [20, 400],
index 831d60a1a54b3185d44e80cbad4a0d8ed505d03a..384688f66d01e03924191de849c563e3d9199739 100644 (file)
@@ -13,7 +13,6 @@
   "available_lights": [
     {
       "type": "lamp",
-      "cost": 3,
       "available_colours": ["blue", "red"],
       "intensity": 0.8
     }
index 718856f8caea520ccf28536a00ad50e8b5e96602..4c33b8614e1c68f54da188e5126812db76e55025 100644 (file)
@@ -13,7 +13,6 @@
   "available_lights": [
     {
       "type": "pulsatinglamp",
-      "cost": 3,
       "available_colours": ["red/blue", "blue/green"],
       "intensity": 0.5,
       "radius_limits": [20, 400]
index 854b8b35238a888c7e93ba66c2b5e3a50133af14..ecc54b9ff43a1c0da3c66d24f5b58984d9659573 100644 (file)
@@ -13,7 +13,6 @@
   "available_lights": [
     {
       "type": "spotlight",
-      "cost": 5,
       "available_colours": ["red"],
       "radius_limits": [20, 400],
       "direction": 135,
     },
     {
       "type": "lamp",
-      "cost": 3,
       "available_colours": ["blue", "yellow/red/green", "cyan/magenta"],
       "intensity": 0.5
     },
     {
       "type": "pulsatinglamp",
-      "cost": 3,
       "available_colours": ["cyan", "blue/green"],
       "intensity": 0.5
     }
index 318aefca3ca2579a4c36cc0b373944c2a91b455a..44c93871c805b34b5fc70761e4be500f3b461957 100644 (file)
@@ -137,6 +137,12 @@ def light_fitting_by_type(light_type):
     return BaseLight.find_cls(light_type).FITTING_IMG
 
 
+def seed_cost(light_config, num_colours):
+    """Calculate a seed cost for a light from its configuration. """
+    cls = BaseLight.find_cls(light_config["type"])
+    return cls.BASE_COST + int(cls.find_cost(light_config) / 10) + num_colours
+
+
 class BaseLight(object):
     """ Common light functionality. """
 
@@ -144,6 +150,7 @@ class BaseLight(object):
     RAY_MANAGER = RayPolyManager
     FITTING_IMG = None
     FITTING_RADIUS = 24.0
+    BASE_COST = 0
 
     # cached surfaces
     _surface_cache = {}
@@ -204,6 +211,11 @@ class BaseLight(object):
             if c.__name__.lower() == light_type]
         return light_class
 
+    @classmethod
+    def find_cost(cls, config):
+        cost = 5 * config["intensity"]
+        return cost
+
     def add(self, space):
         if self.body.space is not None:
             space.remove(self.body, *self.body.shapes)
@@ -313,6 +325,7 @@ class BaseLight(object):
 class Lamp(BaseLight):
 
     FITTING_IMG = "lamp.png"
+    BASE_COST = 1
 
 
 class PulsatingLamp(BaseLight):
@@ -322,6 +335,7 @@ class PulsatingLamp(BaseLight):
     DEFAULT_PULSE_VELOCITY = 2
     DEFAULT_INTENSITY_RANGE = (0.0, 0.9)
     DEFAULT_INTENSITY_VELOCITY = 0.1
+    BASE_COST = 3
 
     def __init__(self, **kw):
         self.pulse_range = kw.pop("pulse_range", self.DEFAULT_PULSE_RANGE)
@@ -357,10 +371,22 @@ class PulsatingLamp(BaseLight):
         self.intensity, self.intensity_velocity = self._update_range(
             self.intensity, self.intensity_velocity, self.intensity_range)
 
+    @classmethod
+    def find_cost(cls, config):
+        cost = super(PulsatingLamp, cls).find_cost(config)
+        cost += config.get("pulse_velocity", cls.DEFAULT_PULSE_VELOCITY)
+        pr = config.get("pulse_range", cls.DEFAULT_PULSE_RANGE)
+        cost += (pr[1] - pr[0]) / 10
+        cost += 5 * config.get("intensity_velocity", cls.DEFAULT_INTENSITY_VELOCITY)
+        ir = config.get("intensity_range", cls.DEFAULT_INTENSITY_RANGE)
+        cost += 5 * (ir[1] - ir[0])
+        return cost
+
 
 class SpotLight(BaseLight):
 
     FITTING_IMG = "spotlight.png"
+    BASE_COST = 5
 
     def __init__(self, **kw):
         self.angular_velocity = kw.pop("angular_velocity", None)
@@ -386,3 +412,9 @@ class SpotLight(BaseLight):
         if self.angular_velocity:
             self.ray_manager.direction -= self.angular_velocity
             self.ray_manager.update_shapes()
+
+    @classmethod
+    def find_cost(cls, config):
+        cost = super(SpotLight, cls).find_cost(config)
+        cost += config.get("angular_velocity", 0)
+        return cost
index bf97f95bfc95133c6bb8bc7e49bc9beefc09203c..8dd9c7b84add10af9ffbb1187ef1baa8d22f98a1 100644 (file)
@@ -11,7 +11,7 @@ import pymunk.pygame_util
 
 from .base import BaseScene
 from ..battery import BatteryManager
-from ..lights import LightManager, light_fitting_by_type, check_space_for_light
+from ..lights import LightManager, light_fitting_by_type, check_space_for_light, seed_cost
 from ..infobar import InfoBar
 from ..obstacles import ObstacleManager
 from ..events import SceneChangeEvent
@@ -103,9 +103,6 @@ class DayScene(BaseScene):
             tool = ImageButton(
                 '32', '%s.png' % light_config["type"], name='light',
                 pos=(x, y))
-            font = loader.load_font(FONTS["sans"], size=12)
-            tool_cost = font.render("%d" % light_config["cost"], True, (0, 0, 0))
-            tool._img.blit(tool_cost, (16, 12), None)
             tool.light_config = light_config
             tools.append(tool)
             x += step
@@ -173,11 +170,18 @@ class DayScene(BaseScene):
         colour_combos = light_config["available_colours"]
         for combo in colour_combos:
             colours = combo.split("/")
+            cost = seed_cost(light_config, len(colours))
+            
             light_fitting = light_fitting_by_type(light_config["type"])
             light_tool = ImageButton(
                 "32", light_fitting, transform=ColourWedges(colours=colours),
                 pos=(x, height), name=combo)
+            font = loader.load_font(FONTS["sans"], size=12)
+            tool_cost = font.render("%d" % cost, True, (0, 0, 0))
+            light_tool._img.blit(tool_cost, (16, 12), None)
+            
             light_tool.colours = colours
+            light_tool.cost = cost
             self._light_toolbar.append(light_tool)
             x += 40
 
@@ -214,9 +218,8 @@ class DayScene(BaseScene):
                 light_cfg["direction"] = math.degrees(angle)
                 break
 
-    def _place_light(self, gamestate, cfg, colours, ev):
+    def _place_light(self, gamestate, cfg, cost, colours, ev):
         cfg = cfg.copy()
-        cost = cfg.pop("cost")
         cfg.pop("available_colours")
         if gamestate.seeds > cost:
             pos = pymunk.pygame_util.from_pygame(
@@ -294,6 +297,7 @@ class DayScene(BaseScene):
                             transform=ColourWedges(colours=light_tool.colours))
                         # colour=COLOURS[0] + (172,)))
                         self._light_colors = light_tool.colours
+                        self._light_cost = light_tool.cost
                         return
                 if self._tool:
                     if self._tool.name == "seed":
@@ -303,7 +307,7 @@ class DayScene(BaseScene):
                     elif self._tool.name == "light" and self._light_colors:
                         self._place_light(
                             gamestate, self._tool.light_config,
-                            self._light_colors, ev)
+                            self._light_cost, self._light_colors, ev)
                 else:
                     # Not tool, so check lights
                     self._lights.toggle_nearest(ev.pos, surfpos=True)