2021-11-17 15:01:25 +01:00
from gamedata . objects . combat . movable import Movable
class Ennemy ( Movable ) :
def __init__ ( self , x , y , game , nodes = None ) :
super ( ) . __init__ ( game , x , y )
self . player = None
2021-11-17 15:30:46 +01:00
self . respawnmargin = 30
self . respawn = True
self . dead = False
2021-11-17 16:14:29 +01:00
self . particleoffsetx = 0
self . particleoffsety = 0
2021-11-17 16:42:10 +01:00
self . canhit = True
2021-11-17 15:16:07 +01:00
self . dustparticles = game . getSpriteDir ( " particles/dust/ " )
2021-11-17 15:01:25 +01:00
def step ( self ) :
2021-11-17 15:30:46 +01:00
if not self . dead :
super ( ) . step ( )
# Check for collision with player
if not self . player :
self . player = next ( iter ( self . game . gameloop . findname ( " Player " ) ) , None )
else :
2021-11-17 16:42:10 +01:00
if self . canhit :
if self . rect . colliderect ( self . player ) :
# Knock it horizontally
if self . player . rect [ 0 ] > self . rect [ 0 ] :
hor = 1
else :
hor = - 1
self . player . horkb = hor * 6
self . player . verkb = self . player . gravityway * - 3
2021-11-17 15:30:46 +01:00
if self . player . canhit :
if self . rect . colliderect ( self . player . hitrect ) :
# Die
self . dead = True
# Add particles
2021-11-17 16:14:29 +01:00
self . game . addParticle ( self . dustparticles , self . rect . center [ 0 ] + self . particleoffsetx , self . rect . center [ 1 ] + self . particleoffsety ) # Dust
self . game . addParticle ( [ self . deadsprite ] , self . rect . center [ 0 ] + self . particleoffsetx , self . rect . center [ 1 ] + self . particleoffsety , fps = 0.6 , vely = - 1.5 , modvely = 0.15 ) # Die sprite
2021-11-17 15:30:46 +01:00
# Boost the player vertically
if not self . player . fastfall :
if self . player . rect [ 1 ] > self . rect [ 1 ] :
direction = 1
else :
direction = - 1
self . player . verspd = self . player . gravity / 3 * direction
elif self . respawn :
if self . rect . center [ 0 ] < self . game . globals [ " camerax " ] - self . respawnmargin :
self . dead = False # Respawn
if self . rect . center [ 0 ] > self . game . globals [ " camerax " ] + self . game . globals [ " cameraw " ] + self . respawnmargin :
self . dead = False # Respawn
2021-11-17 15:01:25 +01:00
else :
2021-11-17 15:30:46 +01:00
self . game . gameloop . delid ( self . id )