94 lines
2.8 KiB
Python
94 lines
2.8 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]
|
|
self.water = game.gameloop.findname("Water")[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.hrest = 0
|
|
self.vrest = 0
|
|
|
|
|
|
def step(self):
|
|
|
|
self.move(self.horspd,self.verspd)
|
|
|
|
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 rect!=self.rect and temprect.colliderect(rect):
|
|
result = rect
|
|
break
|
|
if not result and semi:
|
|
for rect in self.semirects:
|
|
if rect!=self.rect and 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"]])
|