2012-10-12 13:50:02 +02:00
|
|
|
# This file is part of A Robot's Conundrum.
|
2012-08-07 19:59:52 +02:00
|
|
|
#
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum 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.
|
2012-08-07 19:59:52 +02:00
|
|
|
#
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum 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.
|
2012-08-07 19:59:52 +02:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum. If not, see <http://www.gnu.org/licenses/>.
|
2012-08-07 19:59:52 +02:00
|
|
|
#
|
|
|
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
|
|
|
#
|
|
|
|
# worldobject.py
|
|
|
|
# --------------------
|
|
|
|
# date created : Tue Aug 7 2012
|
|
|
|
# copyright : (C) 2012 Sakse Dalum
|
|
|
|
# maintained by : Sakse Dalum <don_s@hongabar.org>
|
|
|
|
|
|
|
|
"""
|
|
|
|
A generic world object.
|
|
|
|
"""
|
|
|
|
|
2012-08-08 14:52:08 +02:00
|
|
|
import pygame
|
|
|
|
import numpy
|
|
|
|
import copy
|
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
class WorldObject(object):
|
2012-08-21 18:36:51 +02:00
|
|
|
def __init__(self, level, x, y, z=0, z_px=0, direction=(1, 0), speed=4,
|
2012-08-07 23:21:44 +02:00
|
|
|
tile_x=64, tile_y=48,
|
2012-08-13 21:47:15 +02:00
|
|
|
movable=False, blocking=True, is_moving=False, visible=True,
|
|
|
|
blit_area=None):
|
2012-08-07 19:59:52 +02:00
|
|
|
self.__dict__.update(locals())
|
|
|
|
|
2012-08-08 22:40:27 +02:00
|
|
|
self.init_x = self.move_x = self.x = x - (x % self.tile_x)
|
|
|
|
self.init_y = self.move_y = self.y = y - (y % self.tile_y)
|
2012-08-09 14:04:50 +02:00
|
|
|
self.init_direction = self.move_direction = self.direction
|
2012-08-07 19:59:52 +02:00
|
|
|
|
2012-08-07 23:21:44 +02:00
|
|
|
self.holding = None
|
|
|
|
self.holder = None
|
|
|
|
|
2012-08-21 18:36:51 +02:00
|
|
|
self.ignore_list = [WithBackground]
|
2012-08-09 15:22:42 +02:00
|
|
|
|
2012-08-09 13:54:27 +02:00
|
|
|
self.is_currently_opaque = True
|
|
|
|
|
2012-08-08 14:52:08 +02:00
|
|
|
if hasattr(self, 'img'):
|
|
|
|
self.img = copy.copy(self.img)
|
|
|
|
|
2012-08-08 22:40:27 +02:00
|
|
|
def set_pos(self, x, y):
|
|
|
|
self.move_x = self.x = x - (x % self.tile_x)
|
|
|
|
self.move_y = self.y = y - (y % self.tile_y)
|
|
|
|
|
2012-08-08 13:50:48 +02:00
|
|
|
def set_init_pos(self):
|
|
|
|
self.init_x, self.init_y = self.x, self.y
|
2012-08-08 18:13:53 +02:00
|
|
|
self.init_direction = self.direction
|
2012-08-08 13:50:48 +02:00
|
|
|
|
|
|
|
def reset_pos(self):
|
2012-08-08 16:17:43 +02:00
|
|
|
self.x, self.y = self.move_x, self.move_y = self.init_x, self.init_y
|
2012-08-08 18:13:53 +02:00
|
|
|
self.direction = self.init_direction
|
|
|
|
|
|
|
|
def share_tile(self, obj_type):
|
|
|
|
for obj in self.level.objects:
|
|
|
|
if (obj.x - (obj.x % self.tile_x) == self.x - (self.x % self.tile_x)
|
|
|
|
and obj.y - (obj.y % self.tile_y) == self.y - (self.y
|
|
|
|
% self.tile_y)
|
|
|
|
and obj is not self and isinstance(obj, obj_type)):
|
|
|
|
return obj
|
|
|
|
return None
|
2012-08-08 13:50:48 +02:00
|
|
|
|
2012-08-07 23:21:44 +02:00
|
|
|
def check_move(self, move_x, move_y):
|
|
|
|
if self.move_x == self.x and self.move_y == self.y and self.movable:
|
|
|
|
for obj in self.level.objects:
|
|
|
|
if (obj.x == self.x + move_x * self.tile_x
|
|
|
|
and obj.y == self.y + move_y * self.tile_y
|
2012-08-08 17:41:19 +02:00
|
|
|
and obj is not self and obj is not self.holder
|
2012-08-09 15:22:42 +02:00
|
|
|
and obj is not self.holding and obj.blocking
|
|
|
|
and type(obj) not in self.ignore_list):
|
2012-08-07 23:21:44 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
def move(self, move_x, move_y):
|
2012-08-07 23:21:44 +02:00
|
|
|
if self.check_move(move_x, move_y):
|
|
|
|
|
|
|
|
if self.holding:
|
|
|
|
if not self.holding.check_move(move_x, move_y):
|
|
|
|
return False
|
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
self.move_x += move_x * self.tile_x
|
|
|
|
self.move_y += move_y * self.tile_y
|
|
|
|
|
2012-08-07 23:21:44 +02:00
|
|
|
if self.holding:
|
|
|
|
self.holding.move(move_x, move_y)
|
|
|
|
|
2012-08-09 14:04:50 +02:00
|
|
|
self.move_direction = self.move_x, self.move_y
|
|
|
|
|
2012-08-08 18:13:53 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2012-08-08 17:18:49 +02:00
|
|
|
def use(self, obj):
|
2012-08-07 23:21:44 +02:00
|
|
|
pass
|
|
|
|
|
2012-08-08 17:06:10 +02:00
|
|
|
def activate(self, setting):
|
|
|
|
pass
|
|
|
|
|
2012-08-09 13:54:27 +02:00
|
|
|
def update_alpha(self):
|
|
|
|
pass
|
|
|
|
|
2012-08-08 14:52:08 +02:00
|
|
|
def set_alpha(self, value):
|
|
|
|
"""
|
|
|
|
Set the relative translucency of the per-pixel-alpha image.
|
|
|
|
|
|
|
|
Value between 0 - 1, where 0 is completely transparent and 1 is
|
|
|
|
completely opaque.
|
|
|
|
"""
|
|
|
|
if hasattr(self, 'img') and hasattr(self, 'orig_alpha'):
|
|
|
|
alpha = pygame.surfarray.pixels_alpha(self.img)
|
|
|
|
alpha[:] = (self.orig_alpha * value).astype(numpy.uint8)
|
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
def update(self, e, t, dt):
|
|
|
|
if self.x > self.move_x:
|
|
|
|
self.x -= min(self.speed * dt * self.tile_x,
|
|
|
|
abs(self.x - self.move_x))
|
|
|
|
if self.x < self.move_x:
|
|
|
|
self.x += min(self.speed * dt * self.tile_x,
|
|
|
|
abs(self.x - self.move_x))
|
|
|
|
if self.y > self.move_y:
|
|
|
|
self.y -= min(self.speed * dt * self.tile_y,
|
|
|
|
abs(self.y - self.move_y))
|
|
|
|
if self.y < self.move_y:
|
|
|
|
self.y += min(self.speed * dt * self.tile_y,
|
|
|
|
abs(self.y - self.move_y))
|
|
|
|
|
2012-08-08 18:13:53 +02:00
|
|
|
self.is_moving = self.x != self.move_x or self.y != self.move_y
|
|
|
|
|
2012-08-07 19:59:52 +02:00
|
|
|
self.x, self.y = int(self.x), int(self.y)
|
2012-08-21 18:36:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WithBackground(WorldObject):
|
|
|
|
def __init__(self, level, img, x, y, z_px=0, blit_area=None):
|
|
|
|
self.__dict__.update(locals())
|
|
|
|
WorldObject.__init__(self, level, x, y, z_px=z_px, blit_area=blit_area)
|
|
|
|
|
|
|
|
def draw(self, window):
|
|
|
|
window.blit(self.img, (self.x - 32 - self.level.camera_x,
|
|
|
|
self.y - self.img.get_size()[1] + 24
|
|
|
|
- self.level.camera_y + self.z_px),
|
|
|
|
self.blit_area)
|