Rewriting lasers in progress.

This commit is contained in:
Niels Serup 2012-08-12 20:43:14 +02:00
parent 5fe049f585
commit 483da974d0
3 changed files with 63 additions and 28 deletions

View File

@ -29,9 +29,9 @@ import pygame
import worldobject import worldobject
class Laser(worldobject.WorldObject): class Laser(worldobject.WorldObject):
def __init__(self, level, p0, p1): def __init__(self, level, p0, p1, laser_direction):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, p0[0], p0[1]) worldobject.WorldObject.__init__(self, level, p0[0], max(p0[1], p1[1]) + 48)
def update(self, e, t, dt): def update(self, e, t, dt):

View File

@ -76,8 +76,8 @@ class Level(object):
window.blit(self.background, (0 - self.camera_x, 0 - self.camera_y)) window.blit(self.background, (0 - self.camera_x, 0 - self.camera_y))
def _sorted_objs(self, objs=None): def _sorted_objs(self, objs=None):
return sorted(self.objects if objs is None else objs, objs = self.objects if objs is None else objs
key=lambda obj: (obj.y + obj.z)) return sorted(objs, key=lambda obj: (obj.y + obj.z))
def draw(self, window): def draw(self, window):
self._blit_background(window) self._blit_background(window)

View File

@ -115,25 +115,64 @@ class Level4(level.Level):
for ((x0, y0), (x1, y1)), direc in self.lasers_orig: for ((x0, y0), (x1, y1)), direc in self.lasers_orig:
self.lasers.append(laser.Laser( self.lasers.append(laser.Laser(
self, (x0 * 64, y0 * 48), self, (x0 * 64, y0 * 48),
(x1 * 64, y1 * 48))) (x1 * 64, y1 * 48), direc))
def draw(self, window): def draw(self, window):
self._blit_background(window) self._blit_background(window)
objs = self._sorted_objs() objs = self._sorted_objs(self.objects + self.lasers)
self._after_sort(itertools.groupby(objs, lambda obj: obj.y + obj.z))
for obj in objs: for obj in objs:
obj.draw(window) obj.draw(window)
for obj in self.lasers: self.darkness.draw(window)
obj.draw(window)
for obj in objs: def _after_sort(self, objss):
x0, y0 = obj.x / 64, obj.y / 48 - 1 for objs in objss:
if isinstance(obj, mirror.Mirror): self._after_sort_line(objs)
for (p0, p1), laser_direc in self.lasers_orig:
if p0 == (x0, y0): def _after_sort_line(self, objs):
mirror_direc = Left if obj.left_up else Right is_special = lambda obj: isinstance(obj, mirror.Mirror) or isinstance(obj, laser.Laser)
do_redraw = { specials, nonspecials = filter(is_special, objs), filter(lambda obj: not is_special(obj), objs)
return nonspecials + self._sort_line_specials(specials)
def _sort_line_specials(self, objs):
print(objs)
return objs
# def _compare(a, b):
# types = map(type, (a, b))
# if a.y + a.z == b.y + b.z:
# if isinstance(a, mirror.Mirror) and isinstance(b, laser.Laser):
# return 1
# if isinstance(b, mirror.Mirror) and isinstance(a, laser.Laser):
# return -1
# # if mirror.Mirror in types and laser.Laser in types:
# # if not isinstance(a, laser.Laser):
# # a, b, swapped = b, a, True
# # else:
# # swapped = False
# # laser_direc = a.laser_direction
# # if a.p0[1] < a.p1[1]:
# # laser_direc = Down # instead of Down
# # if a.p0[1] > a.p1[1]:
# # laser_direc = Up # instead of Up
# # mirror_direc = Left if b.left_up else Right
# # result = 1 if _is_laser_behind_mirror(mirror_direc, laser_direc) \
# # else -1
# # result = -1
# # return result if not swapped else -1 * result
# if a is laser.Laser and b is not laser.Laser:
# return 1
# if b is laser.Laser and a is not laser.Laser:
# return -1
# return 0
# return -1 if a.y + a.z < b.y + b.z \
# else 1 if a.y + a.z > b.y + b.z else 0
def _is_laser_behind_mirror(mirror_direc, laser_direc):
return {
(Left, Up): True, (Left, Up): True,
(Left, Left): True, (Left, Left): True,
(Left, Down): False, (Left, Down): False,
@ -143,7 +182,3 @@ class Level4(level.Level):
(Right, Down): False, (Right, Down): False,
(Right, Left): False (Right, Left): False
}[(mirror_direc, laser_direc)] }[(mirror_direc, laser_direc)]
if do_redraw:
obj.draw(window)
self.darkness.draw(window)