Overflown/gamedata/objects/ingame/player.py

284 lines
12 KiB
Python
Raw Normal View History

2021-11-21 12:28:27 +01:00
from gamedata.objects.ingame.movable import Movable
2021-06-25 16:13:52 +02:00
2021-07-08 16:20:22 +02:00
class Player(Movable):
2021-06-25 16:13:52 +02:00
2021-07-03 00:26:02 +02:00
def __init__(self,game):
2021-07-08 16:20:22 +02:00
super().__init__(game,0,0)
2021-11-16 23:36:56 +01:00
2021-07-08 16:20:22 +02:00
spawnpoint = game.lib.choice(self.tileset.spawns)
self.rect.move_ip(spawnpoint.center)
2021-11-17 20:56:33 +01:00
self.sprites = game.getSpriteDir("player/walking/") # Walking animation
self.spritestill = game.sprite_lib["player/still.png"] # Standing still
self.spritejumping = game.sprite_lib["player/jumping.png"] # Ascending
self.spritefalling = game.sprite_lib["player/falling.png"] # Descending
self.spritehurt = game.sprite_lib["player/hurt.png"] # Being hurted
self.spritelanding = game.sprite_lib["player/landing.png"]
2021-11-22 09:56:55 +01:00
self.testsprites = self.game.getchars("Test")
2021-11-17 20:56:33 +01:00
self.landingtimer = 0
2021-11-18 08:26:30 +01:00
self.maxlandtime = 0.15
2021-11-17 20:56:33 +01:00
self.spriteindex = 0
self.flipx = False
self.animationspeed = 2.5
self.sprite = self.spritestill
2021-11-19 13:42:37 +01:00
collisionratio = 0.7
self.rect[2] = self.sprite.get_width()*collisionratio
2021-11-16 15:34:12 +01:00
self.rect[3] = self.sprite.get_height()
2021-11-19 13:42:37 +01:00
self.spriteoffset = self.sprite.get_width()*(1-collisionratio)/2
2021-11-16 23:36:56 +01:00
self.water = self.game.gameloop.findname("Water")[0]
2021-11-17 20:56:33 +01:00
2021-11-17 14:50:52 +01:00
self.hitrect = self.rect.copy()
2021-11-19 13:42:37 +01:00
self.hitrect[2] = round(self.hitrect[2]*1)
2021-11-17 14:50:52 +01:00
self.hitrect[3] = round(self.hitrect[3]*0.7)
self.canhit = False
2021-11-16 23:36:56 +01:00
2021-11-17 15:16:07 +01:00
self.dustparticles = game.getSpriteDir("particles/dust/")
2021-11-17 22:00:13 +01:00
self.splashparticles = game.getSpriteDir("particles/splash/")
2021-11-17 15:16:07 +01:00
2021-07-08 16:20:22 +02:00
self.speed = 200
2021-08-08 19:55:21 +02:00
self.playerid = 0
self.teamid = self.playerid
2021-07-08 16:20:22 +02:00
self.hitpose = False
2021-08-08 19:55:21 +02:00
self.hitposeduration = 0.01
2021-07-08 16:20:22 +02:00
self.hitposetimer = game.lib.Timer(self.hitposeduration)
2021-08-08 19:55:21 +02:00
self.damage = 0
self.horkb = 0
self.verkb = 0
2021-11-17 16:42:10 +01:00
self.reducekb = 15
2021-08-08 19:55:21 +02:00
self.multkb = 1
self.attackstate = None
2021-07-08 16:20:22 +02:00
self.gravity = 15
2021-11-16 11:32:31 +01:00
self.maxgravity = 30
2021-07-08 16:20:22 +02:00
self.jump = -5
self.verspd = self.maxgravity
2021-06-25 18:57:40 +02:00
2021-11-16 15:34:12 +01:00
self.candash = False
self.canfastfall = False
2021-11-18 08:26:30 +01:00
self.fastfall = 0
2021-11-16 15:34:12 +01:00
2021-11-16 16:03:53 +01:00
# Small leap in order to get out of the water
self.leaptimer = 1
2021-11-17 14:34:57 +01:00
self.leapmaxtimer = 0.5
2021-11-17 21:39:18 +01:00
self.leapmargin = 30 # Vertical margin for the leap
2021-11-16 11:32:31 +01:00
self.gravityway = 1
2021-11-18 09:12:09 +01:00
self.combo = 0
self.combosprites = self.game.getSpriteDir("player/combo/")
2021-11-19 13:42:37 +01:00
self.combosfxs = self.game.getSpriteDir("sfx/combo/",ext=".wav",assetdir="sound_lib")
self.combodefault = self.game.sound_lib["sfx/combo/default.wav"]
2021-11-18 09:28:47 +01:00
self.combotimer = 1
2021-11-18 09:12:09 +01:00
2021-11-19 13:42:37 +01:00
# SFXs
self.landsfx = self.game.sound_lib["sfx/land.wav"]
self.slamsfx = self.game.sound_lib["sfx/slam.wav"]
self.jumpsfx = self.game.sound_lib["sfx/jump.wav"]
2021-07-08 16:20:22 +02:00
def step(self):
2021-07-02 16:25:40 +02:00
2021-07-08 16:20:22 +02:00
if not self.hitpose:
2021-11-18 18:03:34 +01:00
if self.water and self.rect.center[1]>self.water.rect.y: # Reverse gravity underwater
2021-11-16 16:03:53 +01:00
if self.gravityway == 1:
self.gravityway = -1
# Décellerer
2021-11-18 18:03:34 +01:00
self.verspd *= 0.85
2021-11-17 22:00:13 +01:00
# Spawn splash particles
2021-11-18 08:26:30 +01:00
self.game.addParticle(self.splashparticles,self.rect.left-5,self.water.rect.top-5,fps=12)
self.game.addParticle(self.splashparticles,self.rect.right+5,self.water.rect.top-5,flipx=True,fps=12)
2021-11-16 16:03:53 +01:00
else:
if self.gravityway == -1:
self.gravityway = 1
# Décellerer
2021-11-18 18:03:34 +01:00
self.verspd *= 0.85
2021-11-18 08:26:30 +01:00
# Spawn splash particles
self.game.addParticle(self.splashparticles,self.rect.left-5,self.water.rect.top-5,fps=12)
self.game.addParticle(self.splashparticles,self.rect.right+5,self.water.rect.top-5,flipx=True,fps=12)
2021-11-16 16:03:53 +01:00
2021-11-16 23:36:56 +01:00
if self.water and self.water.rect.y-self.leapmargin<=self.rect.y<=self.water.rect.y+self.leapmargin:
2021-11-16 16:03:53 +01:00
self.leaptimer-=self.game.dt # Inside the margin
else:
self.leaptimer = self.leapmaxtimer # Outside, reset the timer
2021-11-18 08:26:30 +01:00
if self.leaptimer<0:
self.canfastfall = True
2021-11-16 16:03:53 +01:00
2021-08-08 19:55:21 +02:00
if not self.game.globals["hitpose"]:
2021-11-17 16:42:10 +01:00
canmove = (abs(self.verkb)+1)*(abs(self.horkb)+1)<2
2021-08-08 19:55:21 +02:00
keys = self.game.inputs["keys"]
2021-11-17 16:42:10 +01:00
self.horspd = 0
2021-11-17 20:56:33 +01:00
if canmove:
2021-08-08 19:55:21 +02:00
self.horspd=(keys["right"]["pressed"]-keys["left"]["pressed"])*self.game.dt*self.speed
2021-11-17 20:56:33 +01:00
if self.landingtimer<=0:
self.sprite = self.spritestill
else:
self.landingtimer-=self.game.dt
self.spriteindex = 0
if self.horspd != 0:
if self.landingtimer<=0:
self.spriteindex+=self.horspd*self.animationspeed*self.game.dt
self.sprite = self.sprites[int(self.spriteindex)%len(self.sprites)]
self.flipx = self.horspd<0
else:
# Beeing hurt
self.sprite = self.spritehurt
2021-08-08 19:55:21 +02:00
if self.attackstate:
2021-11-17 14:31:55 +01:00
self.attackstate(self) # Si je suis sur le sol
2021-08-08 19:55:21 +02:00
self.onground = False
self.onceilling = False
2021-11-18 09:28:47 +01:00
if self.combotimer>=0:
self.combotimer-=self.game.dt*5
2021-11-18 08:26:30 +01:00
# Cancel fastfall if not enough speed
if abs(self.verspd)<5:
self.fastfall = 0
2021-11-16 11:32:31 +01:00
if self.checkcollisions(0,self.gravityway):
2021-08-08 19:55:21 +02:00
self.onground = True
2021-11-18 09:12:09 +01:00
self.combo = 0
2021-11-17 21:39:18 +01:00
self.leaptimer = self.leapmaxtimer
2021-11-16 15:34:12 +01:00
self.candash = True
self.canfastfall = True
if self.verspd!=0 and abs(self.verspd)/self.verspd==abs(self.gravityway)/self.gravityway:
2021-11-17 20:56:33 +01:00
self.sprite=self.spritelanding
self.landingtimer = self.maxlandtime
2021-11-19 13:42:37 +01:00
if self.fastfall:
self.fastfall = 0
# Spawns dust
self.game.addParticle(self.dustparticles,self.rect.right+4,self.rect.center[1]+self.rect[3]/2*self.gravityway,fps=25)
self.game.addParticle(self.dustparticles,self.rect.left-4,self.rect.center[1]+self.rect[3]/2*self.gravityway,fps=25)
self.slamsfx.play()
else:
self.landsfx.play()
2021-11-16 11:32:31 +01:00
if self.gravityway > 0:
self.verspd=min(0,self.verspd)
else:
self.verspd=max(0,self.verspd)
2021-08-08 19:55:21 +02:00
else:
2021-11-16 11:32:31 +01:00
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):
2021-08-08 19:55:21 +02:00
self.onceilling = True
2021-11-16 11:32:31 +01:00
if self.gravityway>0:
self.verspd= max(0,self.verspd) # Se cogne au plafond BONK
else:
self.verspd = min(0,self.verspd)
2021-08-08 19:55:21 +02:00
2021-11-17 20:56:33 +01:00
# Jumping and falling sprites
if canmove and not self.onground:
verdir = self.verspd>0
gravdir = self.gravityway>0
2021-11-18 08:26:30 +01:00
if not self.fastfall:
if verdir != gravdir:
self.sprite=self.spritejumping
else:
self.sprite=self.spritefalling
2021-11-17 20:56:33 +01:00
else:
2021-11-18 08:26:30 +01:00
self.sprite = self.spritelanding
2021-11-17 20:56:33 +01:00
2021-08-08 19:55:21 +02:00
# Adding knockback
self.horspd+=self.horkb
self.verspd+=self.verkb
self.jumped = False
2021-11-16 16:03:53 +01:00
if self.onground or self.leaptimer<0:
2021-08-08 19:55:21 +02:00
if 0<keys["up"]["timer"]<=3:
2021-11-16 11:32:31 +01:00
self.verspd= self.jump*self.gravityway
2021-11-16 16:03:53 +01:00
if self.leaptimer<0 and not self.onground:
self.verspd = -abs(self.verspd) # Small leap
2021-11-19 13:42:37 +01:00
else:
self.jumpsfx.play()
2021-08-08 19:55:21 +02:00
self.jumped = True
2021-11-17 14:50:52 +01:00
super().step() # Actually move
# Updated hitrect
self.hitrect[0] = self.rect.center[0]-self.hitrect[2]/2
offset = 0
if self.verspd>0:
offset = 1
elif self.verspd<0:
offset = -1
self.canhit = abs(self.verspd)>2
self.hitrect[1] = self.rect.center[1]-self.hitrect[3]/2+10*offset
2021-08-08 19:55:21 +02:00
# Reducing the knockback
if self.horkb>0:
2021-11-17 16:42:10 +01:00
self.horkb -= self.reducekb*self.game.dt
self.horkb = max(0,self.horkb)
2021-08-08 19:55:21 +02:00
if self.horkb<0:
2021-11-17 16:42:10 +01:00
self.horkb += self.reducekb*self.game.dt
self.horkb = min(0,self.horkb)
2021-08-08 19:55:21 +02:00
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
2021-11-16 11:32:31 +01:00
if not falled: # Check for fastfall
2021-11-17 13:36:38 +01:00
if self.canfastfall and not self.onground:
2021-11-18 08:26:30 +01:00
self.verspd = self.maxgravity/5
if self.leaptimer>0:
self.verspd*=self.gravityway
2021-11-18 17:58:38 +01:00
else:
self.leaptimer = 0
2021-11-16 15:34:12 +01:00
self.canfastfall = False
2021-11-18 08:26:30 +01:00
self.fastfall = self.gravityway
2021-11-17 14:31:55 +01:00
else:
self.fastfall = False
2021-08-08 19:55:21 +02:00
else: # If I'm in hitpose
2021-07-08 16:20:22 +02:00
if self.hitposetimer.tick(self.game.dt):
self.hitpose = False
2021-07-01 13:54:56 +02:00
2021-07-02 21:06:34 +02:00
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
2021-11-18 09:12:09 +01:00
def upcombo(self):
2021-11-18 09:28:47 +01:00
if self.combotimer<=0:
# Up the combo and spawn particles
if self.combo>=1:
index = min(self.combo-1,len(self.combosprites)-1)
self.game.addParticle([self.combosprites[index]],self.rect.center[0],self.rect.top,fps=1)
2021-11-19 13:42:37 +01:00
self.combosfxs[index].play()
else:
self.combodefault.play()
2021-11-18 09:28:47 +01:00
self.combo+=1
self.combotimer = 1
2021-11-18 09:12:09 +01:00
2021-11-20 22:01:44 +01:00
def yeet(self,hor,ver,resetcombo=True):
2021-11-18 17:58:38 +01:00
self.verspd = 0
self.horkb = hor
self.verkb = ver
self.canfastfall = True
2021-11-20 22:01:44 +01:00
if resetcombo:
self.combo = 0
2021-11-18 17:58:38 +01:00
2021-07-02 21:06:34 +02:00
def draw(self):
2021-11-18 08:26:30 +01:00
flipy = (self.gravityway<0 and not self.leaptimer<0)
if self.fastfall:
flipy = self.fastfall<0
sprite = self.game.pygame.transform.flip(self.sprite,self.flipx,flipy)
2021-11-19 13:42:37 +01:00
self.game.window.blit(sprite,[self.rect[0]-self.game.globals["camerax"]-self.spriteoffset,self.rect[1]-self.game.globals["cameray"]])
2021-11-22 09:56:55 +01:00
for i,spr in enumerate(self.testsprites):
if spr:
self.game.window.blit(spr,[self.rect[0]+i*9-self.game.globals["camerax"]-self.spriteoffset,self.rect[1]-self.game.globals["cameray"]])