From e2d9bd69b3e6e21c432be2321e3ce81f11b0e987 Mon Sep 17 00:00:00 2001 From: adrianna Date: Thu, 15 Sep 2016 00:45:55 +0200 Subject: [PATCH] first stab at automatic variable light pricing --- TODO.txt | 6 +++--- data/stations/01-tutorial.json | 1 - data/stations/02-tunnels.json | 1 - data/stations/03-lighthouse.json | 1 - data/stations/04-multicolor.json | 1 - data/stations/05-spiral.json | 1 - data/stations/06-station-alpha.json | 3 --- tabakrolletjie/lights.py | 32 +++++++++++++++++++++++++++++ tabakrolletjie/scenes/day.py | 18 +++++++++------- 9 files changed, 46 insertions(+), 18 deletions(-) diff --git a/TODO.txt b/TODO.txt index 0043060..a38aa58 100644 --- 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.? diff --git a/data/stations/01-tutorial.json b/data/stations/01-tutorial.json index 3c2ec59..d7fb07f 100644 --- a/data/stations/01-tutorial.json +++ b/data/stations/01-tutorial.json @@ -13,7 +13,6 @@ "available_lights": [ { "type": "lamp", - "cost": 3, "available_colours": ["blue", "red", "blue/red"], "intensity": 0.5 } diff --git a/data/stations/02-tunnels.json b/data/stations/02-tunnels.json index 14cac71..bd0dfa1 100644 --- a/data/stations/02-tunnels.json +++ b/data/stations/02-tunnels.json @@ -13,7 +13,6 @@ "available_lights": [ { "type": "spotlight", - "cost": 3, "available_colours": ["blue", "red"], "intensity": 0.6, "spread": 35, diff --git a/data/stations/03-lighthouse.json b/data/stations/03-lighthouse.json index 212d4ac..8320fd6 100644 --- a/data/stations/03-lighthouse.json +++ b/data/stations/03-lighthouse.json @@ -13,7 +13,6 @@ "available_lights": [ { "type": "spotlight", - "cost": 3, "available_colours": ["yellow", "magenta"], "intensity": 0.5, "radius_limits": [20, 400], diff --git a/data/stations/04-multicolor.json b/data/stations/04-multicolor.json index 831d60a..384688f 100644 --- a/data/stations/04-multicolor.json +++ b/data/stations/04-multicolor.json @@ -13,7 +13,6 @@ "available_lights": [ { "type": "lamp", - "cost": 3, "available_colours": ["blue", "red"], "intensity": 0.8 } diff --git a/data/stations/05-spiral.json b/data/stations/05-spiral.json index 718856f..4c33b86 100644 --- a/data/stations/05-spiral.json +++ b/data/stations/05-spiral.json @@ -13,7 +13,6 @@ "available_lights": [ { "type": "pulsatinglamp", - "cost": 3, "available_colours": ["red/blue", "blue/green"], "intensity": 0.5, "radius_limits": [20, 400] diff --git a/data/stations/06-station-alpha.json b/data/stations/06-station-alpha.json index 854b8b3..ecc54b9 100644 --- a/data/stations/06-station-alpha.json +++ b/data/stations/06-station-alpha.json @@ -13,7 +13,6 @@ "available_lights": [ { "type": "spotlight", - "cost": 5, "available_colours": ["red"], "radius_limits": [20, 400], "direction": 135, @@ -23,13 +22,11 @@ }, { "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 } diff --git a/tabakrolletjie/lights.py b/tabakrolletjie/lights.py index 318aefc..44c9387 100644 --- a/tabakrolletjie/lights.py +++ b/tabakrolletjie/lights.py @@ -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 diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index bf97f95..8dd9c7b 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -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) -- 2.34.1