115 lines
3.6 KiB
Python
115 lines
3.6 KiB
Python
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"]])
|