diff options
| author | Aaron Perley <aaron.perley@gmail.com> | 2016-10-16 23:48:20 -0400 |
|---|---|---|
| committer | Aaron Perley <aaron.perley@gmail.com> | 2016-10-16 23:48:20 -0400 |
| commit | c1153ae74b6f023b7277cd1337537f0710b6d124 (patch) | |
| tree | 6878f7dfc2a2f22bc65babb29bb682dd65760390 /fish_show.py | |
| parent | Basic twinkle (diff) | |
| parent | Shark (diff) | |
| download | bridge-c1153ae74b6f023b7277cd1337537f0710b6d124.tar.zst | |
Merge branch 'master' into twinkle
Conflicts:
ShowRunner.py
Diffstat (limited to 'fish_show.py')
| -rw-r--r-- | fish_show.py | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/fish_show.py b/fish_show.py index cb80d55..6abf3bc 100644 --- a/fish_show.py +++ b/fish_show.py @@ -11,16 +11,18 @@ from fish_types import Fish class Light(object): """Represent a bridge light and its state.""" + __slots__=('light', 'color', 'transitioning_to', 'x', 'y', 'step', 'result_color') def __init__(self, x, y, light): """Store all facts about a light.""" self.light = light self.color = color.black + self.result_color = self.color self.transitioning_to = self.color self.step = 0 self.x = x self.y = y - def update(self, depth, fishes): + def update_base(self, depth): """Change color depending on where the fish is.""" if abs(self.transitioning_to - self.color) <= self.step: self.transitioning_to = pick_HSV(depth).to_RGB() @@ -31,15 +33,20 @@ class Light(object): ) self.color = new_background - new_color = new_background - for fish in fishes: - coverage = compute_coverage(fish, self.x, self.y) - fish_color = fish.color_at(self.x, self.y) - new_color = coverage * fish_color + (1 - coverage) * new_color + self.result_color = new_background + + def update_fish(self, fish): + coverage = compute_coverage(fish, self.x, self.y) + fish_color = fish.color_at(self.x, self.y) + self.result_color = coverage * fish_color + (1 - coverage) * self.result_color + + def blit(self): self.light.setRGBRaw( - *map(lambda x: clamp(0, x, 1), new_color.components) + *map(lambda x: clamp(0, x, 1), self.result_color.components) ) + + def compute_coverage(fish, x, y): """Calculate the coverage of a fish over a box.""" middle_of_fish = fish.x + fish.width / 2 @@ -91,8 +98,8 @@ class FishShow(Show): MAX_DEPTH = 6 def init(self): - self.lights = [Light(x, y, self.bridge.get_light(x, y)) - for y in xrange(self.bridge.HEIGHT) + self.lights = [[Light(x, y, self.bridge.get_light(x, y)) + for y in xrange(self.bridge.HEIGHT)] for x in xrange(self.bridge.WIDTH)] self.fishes = set() self.tick_count = 0 @@ -102,13 +109,26 @@ class FishShow(Show): event = self.recv_event() if event and event[0] == 'spawn': new_fish = Fish.by_type(event[1]['type']) - self.fishes.add(new_fish) + if new_fish: + self.fishes.add(new_fish) + + for col in self.lights: + for light in col: + light.update_base(self.depth) for fish in list(self.fishes): if not fish.update(): self.fishes.remove(fish) - for light in self.lights: - light.update(self.depth, self.fishes) + + for x in xrange(int(fish.x), int(fish.x+fish.width+1)): + if x < len(self.lights): + for light in self.lights[x]: + light.update_fish(fish) + + for col in self.lights: + for light in col: + light.blit() + self.tick_count += 1 if self.tick_count % self.TICKS_PER_DEPTH == 0: |
