The robot now works when using levers.
This commit is contained in:
parent
4eab2f55ed
commit
e288cc4e11
|
@ -30,6 +30,7 @@ import os
|
||||||
|
|
||||||
import worldobject
|
import worldobject
|
||||||
import boulder
|
import boulder
|
||||||
|
import lever
|
||||||
|
|
||||||
class Player(worldobject.WorldObject):
|
class Player(worldobject.WorldObject):
|
||||||
def __init__(self, level, x, y, z=1, movable=True):
|
def __init__(self, level, x, y, z=1, movable=True):
|
||||||
|
@ -42,6 +43,8 @@ class Player(worldobject.WorldObject):
|
||||||
self.frame = 0
|
self.frame = 0
|
||||||
self.anim_speed = 15
|
self.anim_speed = 15
|
||||||
|
|
||||||
|
self.working = False
|
||||||
|
|
||||||
self.ignore_list.append(boulder.Boulder)
|
self.ignore_list.append(boulder.Boulder)
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
@ -57,7 +60,32 @@ class Player(worldobject.WorldObject):
|
||||||
('carry_up', os.path.join('robot_carry', 'up')),
|
('carry_up', os.path.join('robot_carry', 'up')),
|
||||||
('carry_down', os.path.join('robot_carry', 'down')),
|
('carry_down', os.path.join('robot_carry', 'down')),
|
||||||
('carry_right', os.path.join('robot_carry', 'right')),
|
('carry_right', os.path.join('robot_carry', 'right')),
|
||||||
('carry_left', os.path.join('robot_carry', 'right'))]
|
('carry_left', os.path.join('robot_carry', 'right')),
|
||||||
|
|
||||||
|
# Lever
|
||||||
|
|
||||||
|
('lever_down_right', os.path.join('robot_lever', 'down_left')),
|
||||||
|
('lever_down_left', os.path.join('robot_lever', 'down_left')),
|
||||||
|
('lever_down_up', os.path.join('robot_lever', 'vertical_down')),
|
||||||
|
('lever_down_down', os.path.join('robot_lever', 'vertical_down')),
|
||||||
|
|
||||||
|
('lever_right_right', os.path.join('robot_lever',
|
||||||
|
'right_horizontal')),
|
||||||
|
('lever_right_left', os.path.join('robot_lever',
|
||||||
|
'right_horizontal')),
|
||||||
|
('lever_right_up', os.path.join('robot_lever', 'right_down')),
|
||||||
|
('lever_right_down', os.path.join('robot_lever', 'right_down')),
|
||||||
|
|
||||||
|
('lever_up_right', os.path.join('robot_lever', 'up_right')),
|
||||||
|
('lever_up_left', os.path.join('robot_lever', 'up_right')),
|
||||||
|
('lever_up_up', os.path.join('robot_lever', 'vertical_up')),
|
||||||
|
('lever_up_down', os.path.join('robot_lever', 'vertical_up')),
|
||||||
|
|
||||||
|
('lever_left_right', os.path.join('robot_lever', 'right_down')),
|
||||||
|
('lever_left_left', os.path.join('robot_lever', 'right_down')),
|
||||||
|
('lever_left_up', os.path.join('robot_lever', 'right_down')),
|
||||||
|
('lever_left_down', os.path.join('robot_lever', 'right_down'))
|
||||||
|
]
|
||||||
):
|
):
|
||||||
|
|
||||||
self.imgs[anim] = []
|
self.imgs[anim] = []
|
||||||
|
@ -75,13 +103,25 @@ class Player(worldobject.WorldObject):
|
||||||
img = pygame.image.load(f)
|
img = pygame.image.load(f)
|
||||||
|
|
||||||
# Special treatment:
|
# Special treatment:
|
||||||
if anim in ['idle_left', 'carry_left']:
|
if anim in ['idle_left', 'carry_left',
|
||||||
|
'lever_down_right', 'lever_left_down',
|
||||||
|
'lever_up_left']:
|
||||||
img = pygame.transform.flip(img, 1, 0)
|
img = pygame.transform.flip(img, 1, 0)
|
||||||
|
|
||||||
self.imgs[anim].append(img)
|
self.imgs[anim].append(img)
|
||||||
|
|
||||||
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
|
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
|
||||||
|
|
||||||
|
def direction_as_string(self):
|
||||||
|
if self.direction == (1, 0):
|
||||||
|
return "right"
|
||||||
|
if self.direction == (-1, 0):
|
||||||
|
return "left"
|
||||||
|
if self.direction == (0, -1):
|
||||||
|
return "up"
|
||||||
|
if self.direction == (0, 1):
|
||||||
|
return "down"
|
||||||
|
|
||||||
def touch(self, touch_x, touch_y):
|
def touch(self, touch_x, touch_y):
|
||||||
for obj in self.level.objects:
|
for obj in self.level.objects:
|
||||||
if (obj.x == self.x + touch_x * self.tile_x
|
if (obj.x == self.x + touch_x * self.tile_x
|
||||||
|
@ -89,43 +129,77 @@ class Player(worldobject.WorldObject):
|
||||||
and obj != self):
|
and obj != self):
|
||||||
obj.use(self)
|
obj.use(self)
|
||||||
|
|
||||||
def update(self, e, t, dt):
|
# Concerning levers
|
||||||
for event in e:
|
if type(obj) == lever.Lever:
|
||||||
if event.type == pygame.KEYDOWN:
|
self.working = True
|
||||||
if event.key == pygame.K_SPACE:
|
|
||||||
self.touch(*self.direction)
|
|
||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
# List all possible combinations ...
|
||||||
if keys[pygame.K_UP]:
|
lever_direction = (
|
||||||
if not self.holding and not self.is_moving:
|
'up' if obj.anim == 'lever_updown' and obj.setting
|
||||||
self.direction = (0, -1)
|
else
|
||||||
self.anim = 'up'
|
'right' if obj.anim == 'lever_leftright' and obj.setting
|
||||||
self.move(0, -1)
|
else
|
||||||
elif keys[pygame.K_DOWN]:
|
'down' if obj.anim == 'lever_updown' and not obj.setting
|
||||||
if not self.holding and not self.is_moving:
|
else
|
||||||
self.direction = (0, 1)
|
'left')
|
||||||
self.anim = 'down'
|
|
||||||
self.move(0, 1)
|
self.anim_root = 'lever'
|
||||||
elif keys[pygame.K_RIGHT]:
|
self.frame = 0
|
||||||
if not self.holding and not self.is_moving:
|
self.anim = (
|
||||||
self.direction = (1, 0)
|
self.direction_as_string() + '_' + lever_direction)
|
||||||
self.anim = 'right'
|
|
||||||
self.move(1, 0)
|
def update(self, e, t, dt):
|
||||||
elif keys[pygame.K_LEFT]:
|
if not self.working:
|
||||||
if not self.holding and not self.is_moving:
|
self.anim_root = 'carry' if self.holding else 'idle'
|
||||||
self.direction = (-1, 0)
|
|
||||||
self.anim = 'left'
|
for event in e:
|
||||||
self.move(-1, 0)
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_SPACE:
|
||||||
|
self.touch(*self.direction)
|
||||||
|
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
if keys[pygame.K_UP]:
|
||||||
|
if not self.holding and not self.is_moving:
|
||||||
|
self.direction = (0, -1)
|
||||||
|
self.anim = self.direction_as_string()
|
||||||
|
self.move(0, -1)
|
||||||
|
elif keys[pygame.K_DOWN]:
|
||||||
|
if not self.holding and not self.is_moving:
|
||||||
|
self.direction = (0, 1)
|
||||||
|
self.anim = self.direction_as_string()
|
||||||
|
self.move(0, 1)
|
||||||
|
elif keys[pygame.K_RIGHT]:
|
||||||
|
if not self.holding and not self.is_moving:
|
||||||
|
self.direction = (1, 0)
|
||||||
|
self.anim = self.direction_as_string()
|
||||||
|
self.move(1, 0)
|
||||||
|
elif keys[pygame.K_LEFT]:
|
||||||
|
if not self.holding and not self.is_moving:
|
||||||
|
self.direction = (-1, 0)
|
||||||
|
self.anim = self.direction_as_string()
|
||||||
|
self.move(-1, 0)
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.working = not (self.frame == len(
|
||||||
|
self.imgs[self.anim_root + '_' + self.anim]) - 1)
|
||||||
|
if not self.working:
|
||||||
|
self.anim = self.direction_as_string()
|
||||||
|
self.anim_root = 'carry' if self.holding else 'idle'
|
||||||
|
|
||||||
# Update the animation
|
# Update the animation
|
||||||
self.frame = ((self.frame + self.anim_speed * dt) %
|
if not self.working:
|
||||||
len(self.imgs[self.anim_root + '_' + self.anim]))
|
self.frame = ((self.frame + self.anim_speed * dt) %
|
||||||
|
len(self.imgs[self.anim_root + '_' + self.anim]))
|
||||||
|
else:
|
||||||
|
self.frame = min(
|
||||||
|
self.frame + self.anim_speed * dt,
|
||||||
|
len(self.imgs[self.anim_root + '_' + self.anim]) - 1)
|
||||||
|
|
||||||
|
|
||||||
worldobject.WorldObject.update(self, e, t, dt)
|
worldobject.WorldObject.update(self, e, t, dt)
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
self.anim_root = 'carry' if self.holding else 'idle'
|
print(self.anim_root + '_' + self.anim, int(self.frame))
|
||||||
|
|
||||||
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
|
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
|
||||||
self.img.set_alpha(128)
|
self.img.set_alpha(128)
|
||||||
window.blit(self.img, (self.x - 32 - self.level.camera_x,
|
window.blit(self.img, (self.x - 32 - self.level.camera_x,
|
||||||
|
|
Loading…
Reference in New Issue