Overflown/gamedata/objects/ingame/ennemies/robot.py

65 lines
2.0 KiB
Python
Raw Normal View History

2021-11-21 12:28:27 +01:00
from gamedata.objects.ingame.ennemies.ennemy import Ennemy
2021-11-16 23:36:56 +01:00
2021-11-17 15:01:25 +01:00
class Robot(Ennemy):
2021-11-16 23:36:56 +01:00
2021-11-18 17:58:38 +01:00
def __init__(self,*args):
2021-11-16 23:36:56 +01:00
2021-11-18 17:58:38 +01:00
super().__init__(*args)
2021-11-16 23:36:56 +01:00
2021-11-18 17:58:38 +01:00
self.sprites = self.game.getSpriteDir("robot/")
2021-11-16 23:36:56 +01:00
self.sprite = self.sprites[0]
2021-11-18 17:58:38 +01:00
self.deadsprite = self.game.sprite_lib["robot/death.png"]
2021-11-16 23:36:56 +01:00
self.rect[2],self.rect[3] = self.deadsprite.get_size()
self.speed = 30
self.falling = 20
self.spriteindex = 0
2021-11-17 15:30:46 +01:00
2021-11-16 23:36:56 +01:00
self.gravityway = 1 # Vertical flip
self.walkingway = 1 # Horizontal flip
# Offset it correctly
2021-11-18 17:58:38 +01:00
self.rect[0] += 8-self.rect[2]/2
2021-11-16 23:36:56 +01:00
# Movement boundaries
2021-11-18 17:58:38 +01:00
if self.nodes:
self.endpoints = [x["x"] for x in self.nodes[:2]] # Get only x positions
2021-11-16 23:36:56 +01:00
self.endpoints.sort()
else:
self.endpoints = None
def step(self):
2021-11-17 15:30:46 +01:00
if not self.dead:
# Flipping vertically
if self.water and self.rect.center[1]>self.water.rect[1]:
self.gravityway = -1
else:
self.gravityway = 1
# Flipping horizontally
if self.endpoints:
if self.rect.center[0]>self.endpoints[1]:
self.walkingway = -1
if self.rect.center[0]<self.endpoints[0]:
self.walkingway = 1
2021-11-16 23:36:56 +01:00
2021-11-17 15:30:46 +01:00
speedmult = [1,0.5,0][int(self.spriteindex)%3]
2021-11-16 23:36:56 +01:00
2021-11-17 15:30:46 +01:00
self.horspd = self.walkingway*self.speed*self.game.dt*speedmult
self.verspd = self.gravityway*self.falling*self.game.dt
2021-11-16 23:36:56 +01:00
2021-11-17 15:30:46 +01:00
self.spriteindex += self.speed*self.game.dt*0.15
2021-11-16 23:36:56 +01:00
super().step() # Actually move
def draw(self):
2021-11-17 15:30:46 +01:00
if not self.dead:
self.sprite = self.sprites[int(self.spriteindex)%len(self.sprites)]
drawnsprite = self.game.pygame.transform.flip(self.sprite,self.walkingway==-1,self.gravityway==-1)
self.game.window.blit(drawnsprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])