Overflown/gamedata/objects/ingame/movable.py

94 lines
2.8 KiB
Python
Raw Normal View History

2021-07-08 16:20:22 +02:00
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]
2021-11-16 11:32:31 +01:00
self.water = game.gameloop.findname("Water")[0]
2021-07-08 16:20:22 +02:00
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):
2021-11-16 16:03:53 +01:00
self.move(self.horspd,self.verspd)
2021-07-08 16:20:22 +02:00
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:
2021-11-18 09:12:09 +01:00
if rect!=self.rect and temprect.colliderect(rect):
2021-07-08 16:20:22 +02:00
result = rect
break
if not result and semi:
for rect in self.semirects:
2021-11-18 09:12:09 +01:00
if rect!=self.rect and self.rect.bottom<=rect.top:
2021-07-08 16:20:22 +02:00
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"]])