185 lines
6.6 KiB
Python
185 lines
6.6 KiB
Python
# This file is part of ROBOTGAME
|
|
#
|
|
# ROBOTGAME is free software: you can redistribute it and/or modify it under the
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
# version.
|
|
#
|
|
# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License along with
|
|
# ROBOTGAME. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
|
#
|
|
# level1.py
|
|
# --------------------
|
|
# date created : Fri Aug 10 2012
|
|
# copyright : (C) 2012 Niels G. W. Serup
|
|
# maintained by : Niels G. W. Serup <ns@metanohi.name>
|
|
|
|
"""
|
|
The fourth level.
|
|
"""
|
|
|
|
import os
|
|
import pygame
|
|
import random
|
|
import re
|
|
import itertools
|
|
|
|
import level
|
|
import player
|
|
import tile
|
|
import block
|
|
import lever
|
|
import mirror
|
|
import laser
|
|
import misc
|
|
import worldobject
|
|
|
|
import logic.lasermirror as lm
|
|
from logic.direction import *
|
|
|
|
class Level4(level.Level):
|
|
def __init__(self, game, graphics_dir, paused=False):
|
|
level.Level.__init__(self, game, graphics_dir,
|
|
size=(64 * 16, 48 * 16), paused=paused)
|
|
|
|
self.dimensions = 16, 16
|
|
|
|
for i in range(self.dimensions[0]):
|
|
for j in range(self.dimensions[1]):
|
|
self.tiles.append(
|
|
tile.Tile(self, i*64, (j+1)*48,
|
|
self.imgs['indoor%d' % random.randint(1, 6)]))
|
|
|
|
self.draw_background()
|
|
|
|
self.playfield = lm.generate_simple_playfield(16)
|
|
|
|
for (x, y), t in self.playfield.items():
|
|
x1, y1 = 64 * x, 48 * (y + 1)
|
|
if isinstance(t, lm.Source):
|
|
self.objects.append(block.Block(self, x1, y1,
|
|
self.imgs['block3'],
|
|
movable=False))
|
|
continue
|
|
def mir(b, x1, y1):
|
|
def f(x, y):
|
|
def g(setting):
|
|
self.playfield[(x, y)] = lm.MirrorLeft \
|
|
if self.playfield[(x, y)] is lm.MirrorRight \
|
|
else lm.MirrorRight
|
|
self.generate_lasers()
|
|
return g
|
|
return mirror.Mirror(self, x1, y1, b, links=[f(x, y)])
|
|
self.objects.append({
|
|
lm.MirrorLeft: mir(True, x1, y1),
|
|
lm.MirrorRight: mir(False, x1, y1),
|
|
lm.Lever: lever.Lever(
|
|
self, x1, y1, [lambda setting: self.generate_lasers],
|
|
toggling=True,
|
|
anim='lever_leftright' if x in (0, 15)
|
|
else 'lever_updown'),
|
|
lm.Target: block.Block(self, x1, y1, self.imgs['block3'],
|
|
movable=False),
|
|
lm.Blocker: block.Block(self, x1, y1, self.imgs['block1'],
|
|
movable=False)
|
|
}[t])
|
|
mirrors = list(filter(lambda obj: isinstance(obj, mirror.Mirror),
|
|
self.objects))
|
|
levers = list(filter(lambda obj: isinstance(obj, lever.Lever),
|
|
self.objects))
|
|
random.shuffle(levers)
|
|
for l in levers:
|
|
m = min(mirrors, key=lambda m: misc.manhattan_dist(
|
|
(m.x, m.y), (l.x, l.y)))
|
|
mirrors.remove(m)
|
|
l.links.insert(0, (lambda m: lambda setting: m.rotate())(m))
|
|
|
|
self.generate_lasers()
|
|
|
|
self.player.set_pos(64 * 7, 48 * 2)
|
|
self.player.set_init_pos()
|
|
|
|
def restart(self):
|
|
for obj in self.objects:
|
|
obj.reset_pos()
|
|
|
|
def generate_lasers(self):
|
|
self.lasers_orig = lm.generate_lasers(self.playfield)
|
|
self.lasers = []
|
|
for ((x0, y0), (x1, y1)), direc in self.lasers_orig:
|
|
self.lasers.append(laser.Laser(
|
|
self, (x0 * 64, y0 * 48),
|
|
(x1 * 64, y1 * 48), direc))
|
|
|
|
def draw(self, window):
|
|
self._blit_background(window)
|
|
|
|
objs = self._sorted_objs(self.objects + self.lasers)
|
|
self._after_sort(itertools.groupby(objs, lambda obj: obj.y + obj.z))
|
|
for obj in objs:
|
|
obj.draw(window)
|
|
|
|
self.darkness.draw(window)
|
|
|
|
def _after_sort(self, objss):
|
|
for objs in objss:
|
|
self._after_sort_line(objs)
|
|
|
|
def _after_sort_line(self, objs):
|
|
is_special = lambda obj: isinstance(obj, mirror.Mirror) or isinstance(obj, laser.Laser)
|
|
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, Left): True,
|
|
(Left, Down): False,
|
|
(Left, Right): False,
|
|
(Right, Up): True,
|
|
(Right, Right): True,
|
|
(Right, Down): False,
|
|
(Right, Left): False
|
|
}[(mirror_direc, laser_direc)]
|