Changed hitbox system

This commit is contained in:
theo@manjaro 2021-09-12 21:38:55 +02:00
parent 8f2d2b20f1
commit d9d51042e3
2 changed files with 19 additions and 9 deletions

View File

@ -48,8 +48,7 @@ class Game():
self.globals["cameray"] = 0 self.globals["cameray"] = 0
self.globals["scamerax"] = 3 self.globals["scamerax"] = 3
self.globals["scameray"] = 0 self.globals["scameray"] = 0
self.globals["players"] = [] self.globals["debug"] = True
self.globals["hitpose"] = False
self.scaleCamera() self.scaleCamera()
settings = {"sfx":1,"bgm":1} settings = {"sfx":1,"bgm":1}

View File

@ -3,10 +3,12 @@ import random,math
class Lemming(BaseObject): class Lemming(BaseObject):
def __init__(self,x,y,game,speedmargin=5): def __init__(self,x,y,game,speedmargin=5):
super().__init__(x,y,game) super().__init__(x,y,game,w=45,h=45)
self.direction = random.randint(0,360) self.direction = random.randint(0,360)
self.radius = 30 # Radius in which the mouse can click self.holdrect = self.rect.copy()
self.basespeed = 50+random.randint(-speedmargin,speedmargin) # Speed that he normally walks by self.holdradius = 60
self.holdrect = self.holdrect.inflate(self.holdradius,self.holdradius)
self.basespeed = 40+random.randint(-speedmargin,speedmargin) # Speed that he normally walks by
self.normalspeed = self.basespeed # Speed "objective" self.normalspeed = self.basespeed # Speed "objective"
self.speed = 0 # Current speed, leaning towards objective speed self.speed = 0 # Current speed, leaning towards objective speed
@ -47,22 +49,20 @@ class Lemming(BaseObject):
# Mouse selection # Mouse selection
mouse=self.game.inputs["mouse"] mouse=self.game.inputs["mouse"]
distance = math.sqrt((mouse["campos"][0]-self.rect.center[0])**2+(mouse["campos"][1]-self.rect.center[1])**2)
if self.selected: if self.selected:
# Caching mouse relative movement # Caching mouse relative movement
self.cachedrel.append(mouse["rel"]) self.cachedrel.append(mouse["rel"])
if len(self.cachedrel)>self.cachedrelsize: if len(self.cachedrel)>self.cachedrelsize:
self.cachedrel.pop(0) self.cachedrel.pop(0)
# Releasing it # Releasing it
if mouse["click"]==0 or distance>4*self.radius: if mouse["click"]==0 or not self.holdrect.collidepoint(mouse["campos"]):
self.launch() self.launch()
if self.game.inputs["mouse"]["click"]==1: if self.game.inputs["mouse"]["click"]==1:
if distance<=self.radius: if self.rect.collidepoint(mouse["campos"]):
self.selected = True self.selected = True
# Animation # Animation
self.spriteindex+=self.game.dt*self.animspeed*self.speed self.spriteindex+=self.game.dt*self.animspeed*self.speed
def launch(self): def launch(self):
# Launch itself in the mouse direction # Launch itself in the mouse direction
if len(self.cachedrel)>self.mincachedsize: if len(self.cachedrel)>self.mincachedsize:
@ -81,9 +81,20 @@ class Lemming(BaseObject):
self.resty = vely-int(vely) self.resty = vely-int(vely)
self.rect[0]+=int(velx) self.rect[0]+=int(velx)
self.rect[1]+=int(vely) self.rect[1]+=int(vely)
self.holdrect[0]+=int(velx)
self.holdrect[1]+=int(vely)
def draw(self): def draw(self):
orientation = self.orientations[int(self.direction%360/361*4)] orientation = self.orientations[int(self.direction%360/361*4)]
sprites = self.sprites[orientation] sprites = self.sprites[orientation]
self.game.lib.drawcenter(self.game,self.game.sprite_lib["lemmings/shadow.png"],self.rect.center[0],self.rect.center[1]) self.game.lib.drawcenter(self.game,self.game.sprite_lib["lemmings/shadow.png"],self.rect.center[0],self.rect.center[1])
self.game.lib.drawcenter(self.game,sprites[int(self.spriteindex)%len(sprites)],self.rect.center[0],self.rect.center[1]) self.game.lib.drawcenter(self.game,sprites[int(self.spriteindex)%len(sprites)],self.rect.center[0],self.rect.center[1])
if self.game.globals["debug"]:
s = self.game.pygame.Surface(self.rect.size)
s.fill([255,0,0])
s.set_alpha(30)
self.game.window.blit(s,self.rect[:2])
s = self.game.pygame.Surface(self.holdrect.size)
s.fill([0,255,0])
s.set_alpha(30)
self.game.window.blit(s,self.holdrect[:2])