Fastfall
This commit is contained in:
parent
5f4d490f07
commit
b8285755ae
@ -97,6 +97,8 @@ class Timer(): # Mannière de gerer les timers pour des espaces de temps flottan
|
||||
return self.maxcount
|
||||
def getratio(self):
|
||||
return max(min(1,self.timer/self.maxcount),0)
|
||||
def reset(self):
|
||||
self.timer = self.maxcount
|
||||
def add(self,amount):
|
||||
self.timer+=amount
|
||||
self.maxcount+=amount
|
||||
|
@ -139,36 +139,43 @@ class Game():
|
||||
self.inputs["keys"] = {
|
||||
"escape":{
|
||||
"timer" : 0, # Timer de la touche
|
||||
"realtime" : 0, # Temps réel préssé
|
||||
"pressed" : False,
|
||||
"keycode" : pygame.K_ESCAPE # Code pygame de la touche en question
|
||||
},
|
||||
"backspace":{
|
||||
"timer" : 0, # Timer de la touche
|
||||
"realtime" : 0, # Temps réel préssé
|
||||
"pressed" : False,
|
||||
"keycode" : pygame.K_BACKSPACE # Code pygame de la touche en question
|
||||
},
|
||||
"enter":{
|
||||
"timer" : 0, # Timer de la touche
|
||||
"realtime" : 0, # Temps réel préssé
|
||||
"pressed" : False,
|
||||
"keycode" : pygame.K_RETURN # Code pygame de la touche en question
|
||||
},
|
||||
"left":{
|
||||
"timer" : 0, # Timer de la touche
|
||||
"realtime" : 0, # Temps réel préssé
|
||||
"pressed" : False,
|
||||
"keycode" : pygame.K_q # Code pygame de la touche en question
|
||||
},
|
||||
"right":{
|
||||
"timer" : 0, # Timer de la touche
|
||||
"realtime" : 0, # Temps réel préssé
|
||||
"pressed" : False,
|
||||
"keycode" : pygame.K_d # Code pygame de la touche en question
|
||||
},
|
||||
"up":{
|
||||
"timer" : 0, # Timer de la touche
|
||||
"realtime" : 0, # Temps réel préssé
|
||||
"pressed" : False,
|
||||
"keycode" : pygame.K_z # Code pygame de la touche en question
|
||||
},
|
||||
"down":{
|
||||
"timer" : 0, # Timer de la touche
|
||||
"realtime" : 0, # Temps réel préssé
|
||||
"pressed" : False,
|
||||
"keycode" : pygame.K_s # Code pygame de la touche en question
|
||||
}
|
||||
@ -202,8 +209,10 @@ class Game():
|
||||
for i in self.inputs["keys"].keys():
|
||||
if self.inputs["keys"][i]["pressed"]:
|
||||
self.inputs["keys"][i]["timer"]+=1
|
||||
self.inputs["keys"][i]["realtime"]+=self.dt
|
||||
else:
|
||||
self.inputs["keys"][i]["timer"]=0
|
||||
self.inputs["keys"][i]["realtime"]=0
|
||||
# Timer du click
|
||||
boutons = pygame.mouse.get_pressed()
|
||||
if boutons[0]: # Si click gauche
|
||||
|
114
gamedata/objects/combat/movable.py
Normal file
114
gamedata/objects/combat/movable.py
Normal file
@ -0,0 +1,114 @@
|
||||
from gamedata.objects.base import BaseObject
|
||||
|
||||
class Movable(BaseObject):
|
||||
|
||||
def __init__(self,game,x,y):
|
||||
|
||||
self.sprite = game.sprite_lib["icon.png"]
|
||||
|
||||
self.tileset = game.gameloop.findname("TilesetRenderer")[0]
|
||||
|
||||
super().__init__(x,y,game,self.sprite.get_width(),self.sprite.get_height())
|
||||
|
||||
self.collisionrects = self.tileset.rects
|
||||
self.semirects = self.tileset.semirects
|
||||
self.tilew = self.tileset.solidtilew
|
||||
self.tileh = self.tileset.solidtileh
|
||||
|
||||
self.horspd = 0
|
||||
self.verspd = 0
|
||||
self.currentspdh = 0
|
||||
self.currentspdv = 0
|
||||
|
||||
self.hrest = 0
|
||||
self.vrest = 0
|
||||
|
||||
self.dechorentropy = 10
|
||||
self.acchorentropy = 15
|
||||
self.decverentropy = 100
|
||||
self.accverentropy = 25
|
||||
|
||||
def step(self):
|
||||
|
||||
self.entropy()
|
||||
self.move(self.currentspdh,self.currentspdv)
|
||||
|
||||
def entropy(self):
|
||||
|
||||
# Horizontal
|
||||
if abs(self.currentspdh)>abs(self.horspd): # Décelleration
|
||||
self.currentspdh += (self.horspd - self.currentspdh)*self.game.dt*self.dechorentropy
|
||||
else:
|
||||
self.currentspdh += (self.horspd - self.currentspdh)*self.game.dt*self.acchorentropy
|
||||
|
||||
# Vertical
|
||||
if abs(self.currentspdv)>abs(self.verspd): # Décelleration
|
||||
self.currentspdv += (self.verspd - self.currentspdv)*self.game.dt*self.decverentropy
|
||||
else: # Acceleration
|
||||
self.currentspdv += (self.verspd - self.currentspdv)*self.game.dt*self.accverentropy
|
||||
|
||||
def move(self,movex,movey):
|
||||
hstoped = False
|
||||
hor = int(movex+self.hrest)
|
||||
self.hrest = movex+self.hrest-hor
|
||||
while abs(hor)>0:
|
||||
if hor>=self.tilew:
|
||||
diffx = self.tilew
|
||||
elif hor<=-self.tilew:
|
||||
diffx = -self.tilew
|
||||
else:
|
||||
diffx = hor
|
||||
rect = self.checkcollisions(diffx,0)
|
||||
if rect:
|
||||
hor = 0
|
||||
self.hrest = 0
|
||||
hstopped = True
|
||||
if diffx>0:
|
||||
self.rect.right = rect.left
|
||||
else:
|
||||
self.rect.left = rect.right
|
||||
else:
|
||||
hor-=diffx
|
||||
self.rect[0]+=diffx
|
||||
|
||||
vstoped = False
|
||||
ver = int(movey+self.vrest)
|
||||
self.vrest = movey+self.vrest-ver
|
||||
while abs(ver)>0:
|
||||
if abs(ver)>self.tileh:
|
||||
diffy = self.tileh*abs(ver)/ver
|
||||
else:
|
||||
diffy = ver
|
||||
rect = self.checkcollisions(0,diffy)
|
||||
if rect:
|
||||
vstopped = True
|
||||
ver = 0
|
||||
self.vrest = 0
|
||||
if diffy>0:
|
||||
self.rect.bottom = rect.top
|
||||
else:
|
||||
self.rect.top = rect.bottom
|
||||
else:
|
||||
ver-=diffy
|
||||
self.rect[1]+=diffy
|
||||
|
||||
def checkcollisions(self,offx,offy,classic=True,semi=True):
|
||||
temprect = self.rect.copy()
|
||||
temprect[0]+=offx
|
||||
temprect[1]+=offy
|
||||
result = False
|
||||
if classic:
|
||||
for rect in self.collisionrects:
|
||||
if temprect.colliderect(rect):
|
||||
result = rect
|
||||
break
|
||||
if not result and semi:
|
||||
for rect in self.semirects:
|
||||
if self.rect.bottom<=rect.top:
|
||||
if temprect.colliderect(rect):
|
||||
result = rect
|
||||
break
|
||||
return result
|
||||
|
||||
def draw(self):
|
||||
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])
|
@ -1,136 +1,74 @@
|
||||
from gamedata.objects.base import BaseObject
|
||||
from gamedata.objects.combat.movable import Movable
|
||||
|
||||
class Player(BaseObject):
|
||||
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["icon.png"]
|
||||
|
||||
tileset = game.gameloop.findname("TilesetRenderer")[0]
|
||||
self.speed = 200
|
||||
|
||||
spawnpoint = game.lib.choice(tileset.spawns)
|
||||
x,y = spawnpoint.center
|
||||
self.fastfallmargin = 2
|
||||
|
||||
super().__init__(x,y,game,self.sprite.get_width(),self.sprite.get_height())
|
||||
|
||||
self.collisionrects = tileset.rects
|
||||
self.semirects = tileset.semirects
|
||||
self.tilew = tileset.solidtilew
|
||||
self.tileh = tileset.solidtileh
|
||||
|
||||
self.speed = 300
|
||||
self.hitpose = False
|
||||
self.hitposeduration = 0.5
|
||||
self.hitposetimer = game.lib.Timer(self.hitposeduration)
|
||||
|
||||
self.stepsize = 20
|
||||
|
||||
self.gravity = 1500
|
||||
self.maxgravity = 800
|
||||
self.jump = -600
|
||||
self.vertical = self.maxgravity
|
||||
|
||||
self.hrest = 0
|
||||
self.vrest = 0
|
||||
self.gravity = 15
|
||||
self.maxgravity = 80
|
||||
self.jump = -5
|
||||
self.verspd = self.maxgravity
|
||||
|
||||
def step(self):
|
||||
keys = self.game.inputs["keys"]
|
||||
hor=keys["right"]["pressed"]-keys["left"]["pressed"]
|
||||
|
||||
# Si je suis sur le sol
|
||||
self.onground = False
|
||||
if self.checkcollisions(0,1):
|
||||
self.onground = True
|
||||
self.vertical=min(0,self.vertical)
|
||||
if not self.hitpose:
|
||||
keys = self.game.inputs["keys"]
|
||||
self.horspd=(keys["right"]["pressed"]-keys["left"]["pressed"])*self.game.dt*self.speed
|
||||
|
||||
# Si je suis sur le sol
|
||||
self.onground = False
|
||||
if self.checkcollisions(0,1):
|
||||
self.onground = True
|
||||
self.verspd=min(0,self.verspd)
|
||||
else:
|
||||
self.verspd+=self.gravity*self.game.dt
|
||||
self.verspd= min(self.maxgravity,self.verspd)
|
||||
if self.checkcollisions(0,-1):
|
||||
self.verspd= max(0,self.verspd) # Se cogne au plafond BONK
|
||||
|
||||
self.jumped = False
|
||||
if self.onground:
|
||||
if 0<keys["up"]["timer"]<=3:
|
||||
self.verspd= self.jump
|
||||
self.jumped = True
|
||||
if self.verspd==0:
|
||||
self.move(0,-self.stepsize)
|
||||
super().step()
|
||||
self.move(0,self.stepsize)
|
||||
else:
|
||||
super().step()
|
||||
|
||||
# Je passe en dessous des semi-plateformes
|
||||
if keys["down"]["timer"]==1:
|
||||
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
|
||||
if 0<self.verspd <= self.fastfallmargin or (abs(self.verspd)<abs(self.currentspdv) and abs(self.verspd)<=self.fastfallmargin/1.8):
|
||||
self.verspd = self.maxgravity/4
|
||||
|
||||
else:
|
||||
self.vertical+=self.gravity*self.game.dt
|
||||
self.vertical = min(self.maxgravity,self.vertical)
|
||||
if self.checkcollisions(0,-1):
|
||||
self.vertical = max(0,self.vertical) # Se cogne au plafond
|
||||
|
||||
self.jumped = False
|
||||
if self.onground:
|
||||
if 0<keys["up"]["timer"]<=3:
|
||||
self.vertical = self.jump
|
||||
self.jumped = True
|
||||
|
||||
if self.vertical==0:
|
||||
self.move(0,-self.stepsize)
|
||||
self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt)
|
||||
self.move(0,self.stepsize)
|
||||
else:
|
||||
self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt)
|
||||
|
||||
# Je passe en dessous des semi-plateformes
|
||||
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:
|
||||
if keys["down"]["timer"]==1:
|
||||
self.rect.bottom = semi.top+1
|
||||
|
||||
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 move(self,movex,movey):
|
||||
hstoped = False
|
||||
hor = int(movex+self.hrest)
|
||||
self.hrest = movex+self.hrest-hor
|
||||
while abs(hor)>0:
|
||||
if hor>=self.tilew:
|
||||
diffx = self.tilew
|
||||
elif hor<=-self.tilew:
|
||||
diffx = -self.tilew
|
||||
else:
|
||||
diffx = hor
|
||||
rect = self.checkcollisions(diffx,0)
|
||||
if rect:
|
||||
hor = 0
|
||||
self.hrest = 0
|
||||
hstopped = True
|
||||
if diffx>0:
|
||||
self.rect.right = rect.left
|
||||
else:
|
||||
self.rect.left = rect.right
|
||||
else:
|
||||
hor-=diffx
|
||||
self.rect[0]+=diffx
|
||||
|
||||
vstoped = False
|
||||
ver = int(movey+self.vrest)
|
||||
self.vrest = movey+self.vrest-ver
|
||||
while abs(ver)>0:
|
||||
if abs(ver)>self.tileh:
|
||||
diffy = self.tileh*abs(ver)/ver
|
||||
else:
|
||||
diffy = ver
|
||||
rect = self.checkcollisions(0,diffy)
|
||||
if rect:
|
||||
vstopped = True
|
||||
ver = 0
|
||||
self.vrest = 0
|
||||
if diffy>0:
|
||||
self.rect.bottom = rect.top
|
||||
else:
|
||||
self.rect.top = rect.bottom
|
||||
else:
|
||||
ver-=diffy
|
||||
self.rect[1]+=diffy
|
||||
|
||||
def checkcollisions(self,offx,offy,classic=True,semi=True):
|
||||
temprect = self.rect.copy()
|
||||
temprect[0]+=offx
|
||||
temprect[1]+=offy
|
||||
result = False
|
||||
if classic:
|
||||
for rect in self.collisionrects:
|
||||
if temprect.colliderect(rect):
|
||||
result = rect
|
||||
break
|
||||
if not result and semi:
|
||||
for rect in self.semirects:
|
||||
if self.rect.bottom<=rect.top:
|
||||
if temprect.colliderect(rect):
|
||||
result = rect
|
||||
break
|
||||
return result
|
||||
|
||||
def draw(self):
|
||||
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])
|
||||
|
Loading…
Reference in New Issue
Block a user