Overflown/gamedata/objects/combat/player.py
2021-11-17 14:34:57 +01:00

155 lines
5.7 KiB
Python

from gamedata.objects.combat.movable import Movable
class Player(Movable):
def __init__(self,game):
super().__init__(game,0,0)
spawnpoint = game.lib.choice(self.tileset.spawns)
self.rect.move_ip(spawnpoint.center)
self.sprite = game.sprite_lib["player/0.png"]
self.rect[2] = self.sprite.get_width()
self.rect[3] = self.sprite.get_height()
self.water = self.game.gameloop.findname("Water")[0]
self.speed = 200
self.controlled = True
self.playerid = 0
self.teamid = self.playerid
self.hitpose = False
self.hitposeduration = 0.01
self.hitposetimer = game.lib.Timer(self.hitposeduration)
self.damage = 0
self.horkb = 0
self.verkb = 0
self.reducekb = 50
self.multkb = 1
self.attackstate = None
self.gravity = 15
self.maxgravity = 30
self.jump = -5
self.verspd = self.maxgravity
self.candash = False
self.canfastfall = False
self.fastfall = False
# Small leap in order to get out of the water
self.leaptimer = 1
self.leapmaxtimer = 0.5
self.leapmargin = 35 # Vertical margin for the leap
self.gravityway = 1
def step(self):
if not self.hitpose:
if self.water and self.rect.y>self.water.rect.y: # Reverse gravity underwater
if self.gravityway == 1:
self.gravityway = -1
# Décellerer
self.canfastfall = False
self.verspd *= 0.9
else:
if self.gravityway == -1:
self.gravityway = 1
# Décellerer
self.canfastfall = False
self.verspd *= 0.9
if self.water and self.water.rect.y-self.leapmargin<=self.rect.y<=self.water.rect.y+self.leapmargin:
self.leaptimer-=self.game.dt # Inside the margin
else:
self.leaptimer = self.leapmaxtimer # Outside, reset the timer
if not self.game.globals["hitpose"]:
keys = self.game.inputs["keys"]
if self.controlled:
self.horspd=(keys["right"]["pressed"]-keys["left"]["pressed"])*self.game.dt*self.speed
if self.attackstate:
self.attackstate(self) # Si je suis sur le sol
self.onground = False
self.onceilling = False
if self.checkcollisions(0,self.gravityway):
self.onground = True
self.candash = True
self.canfastfall = True
self.fastfall = False
if self.gravityway > 0:
self.verspd=min(0,self.verspd)
else:
self.verspd=max(0,self.verspd)
else:
self.verspd+=self.gravity*self.game.dt*self.gravityway
if self.gravityway>0:
self.verspd= min(self.maxgravity,self.verspd)
else:
self.verspd = max(-self.maxgravity,self.verspd)
if self.checkcollisions(0,-self.gravityway):
self.onceilling = True
if self.gravityway>0:
self.verspd= max(0,self.verspd) # Se cogne au plafond BONK
else:
self.verspd = min(0,self.verspd)
# Adding knockback
self.horspd+=self.horkb
self.verspd+=self.verkb
self.jumped = False
if self.onground or self.leaptimer<0:
if 0<keys["up"]["timer"]<=3:
self.verspd= self.jump*self.gravityway
if self.leaptimer<0 and not self.onground:
self.verspd = -abs(self.verspd) # Small leap
self.jumped = True
super().step()
# Reducing the knockback
if self.horkb>0:
self.horkb = max(0,self.horkb-self.reducekb)*self.game.dt
if self.horkb<0:
self.horkb = min(0,self.horkb+self.reducekb)*self.game.dt
if self.verkb>0:
self.verkb = max(0,self.verkb-self.reducekb)*self.game.dt
if self.verkb<0:
self.verkb = min(0,self.verkb+self.reducekb)*self.game.dt
# Je passe en dessous des semi-plateformes
if keys["down"]["timer"]==1:
falled = False
if not self.checkcollisions(0,1,semi=False):
semi = self.checkcollisions(0,1,classic=False)
if semi!=self.checkcollisions(0,0,classic=False) and semi:
self.rect.bottom = semi.top+1
falled = True
if not falled: # Check for fastfall
if self.canfastfall and not self.onground:
self.verspd = self.maxgravity/5*self.gravityway
self.canfastfall = False
self.fastfall = True
else:
self.fastfall = False
else: # If I'm in hitpose
if self.hitposetimer.tick(self.game.dt):
self.hitpose = False
self.game.globals["camerax"] = self.rect.center[0]-self.game.globals["cameraw"]/2
self.game.globals["cameray"] = self.rect.center[1]-self.game.globals["camerah"]/2
def draw(self):
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])