PinmikPanik/gamedata/objects/ingame/lemmings.py

148 lines
6.1 KiB
Python

from gamedata.objects.base import BaseObject
import random,math
class Lemming(BaseObject):
def __init__(self,x,y,game,speedmargin=5,skin="base"):
super().__init__(x,y,game,w=70,h=70)
self.direction = random.randint(0,360)
self.holdrect = self.rect.copy()
self.holdradius = 80
self.holdrect = self.holdrect.inflate(self.holdradius,self.holdradius)
self.basespeed = max(20,40+random.randint(-speedmargin,speedmargin)) # Speed that he normally walks by
self.holdtimer = game.lib.Timer(2.5) # Max seconds of holding
self.normalspeed = self.basespeed # Speed "objective"
self.speed = 0 # Current speed, leaning towards objective speed
self.skin = skin
self.selected = False # If beeing redirected
self.anglemargin = 25
# Used for movement
self.restx = 0
self.resty = 0
self.dusttimer = game.lib.Timer(0.7)
# Sprites
self.orientations = ["Right","Left","Left","Right"]
self.sprites = {}
for i in self.orientations:
self.sprites[i] = game.getSpriteDir("lemmings/"+skin+"/"+i+"/")
self.spriteindex = 0
self.animspeed = 0.2
self.tiles = game.gameloop.findname("Tiles")[0]
self.manager = game.gameloop.findname("Manager")[0]
def step(self):
# Depth updating based on position
self.depth = 1+self.rect[1]/100
# Lean towards the normal speed
if self.selected:
self.normalspeed = 0
else:
self.normalspeed = self.basespeed
self.speed += (self.normalspeed-self.speed)*self.game.dt*3
# Getting x and y velocity
diffx = math.cos(math.radians(self.direction))*self.speed
diffy = math.sin(math.radians(self.direction))*self.speed
self.move(diffx*self.game.dt,diffy*self.game.dt)
# Spawning dust particles if being launched
if self.speed > self.basespeed*1.5:
if self.dusttimer.tick(self.game.dt*(self.speed/self.basespeed)**2):
sprites = self.game.getSpriteDir("particles/dust/")
self.game.addParticle(sprites,self.rect.center[0],self.rect.center[1])
# Mouse selection
mouse=self.game.inputs["mouse"]
if self.selected:
# Releasing it
if mouse["click"]==0 or not self.holdrect.collidepoint(mouse["campos"]):
self.launch()
else:
if self.holdtimer.tick(self.game.dt):
self.selected = False
self.holdtimer.reset()
self.normalspeed = self.basespeed
if self.game.inputs["mouse"]["click"]==1:
if self.rect.collidepoint(mouse["campos"]):
self.selected = True
# Animation
self.spriteindex+=self.game.dt*self.animspeed*self.speed
# Check if still on ground
gridx = int((self.rect.center[0]-self.tiles.rect[0])/self.tiles.cellsize)
gridy = int((self.rect.center[1]-self.tiles.rect[1])/self.tiles.cellsize)
dead = True
if 0<=gridy<len(self.tiles.grid):
if 0<=gridx<len(self.tiles.grid[gridy]):
if self.tiles.grid[gridy][gridx]==1:
dead = False
if dead:
self.manager.death()
if self.game.globals["scamerax"]*self.game.globals["scameray"]>5: # Avoiding shaking to much
self.game.globals["scamerax"]+=1
self.game.globals["scamerax"]+=1
else:
self.game.globals["scamerax"]=5
self.game.globals["scameray"]=5
self.game.gameloop.delid(self.id)
# Spawn particles
sprites = self.game.getSpriteDir("particles/dust/")
for velx in range(-1,2):
for vely in range(-1,2):
self.game.addParticle(sprites,self.rect.center[0],self.rect.center[1],velx=velx/2,vely=vely/2)
# Spawn little ghost
sprites = self.game.getSpriteDir("particles/ghost/")
self.game.addParticle(sprites,self.rect.center[0],self.rect.center[1],fps=2,vely=-1)
def launch(self):
# Launch itself in the mouse direction
xdiff = self.game.inputs["mouse"]["pos"][0]-self.rect.center[0]
ydiff = self.game.inputs["mouse"]["pos"][1]-self.rect.center[1]
self.direction = math.degrees(math.atan2(ydiff,xdiff))+random.randint(-self.anglemargin,self.anglemargin)
self.speed = self.basespeed*4
self.cachedrel = []
self.selected = False
self.holdtimer.reset()
def move(self,x,y):
# Remember the digits, pygame rects only move with integers
velx = x+self.restx
vely = y+self.resty
self.restx = velx-int(velx)
self.resty = vely-int(vely)
self.rect[0]+=int(velx)
self.rect[1]+=int(vely)
self.holdrect[0]+=int(velx)
self.holdrect[1]+=int(vely)
def draw(self):
orientation = self.orientations[int(self.direction%360/361*4)]
sprites = self.sprites[orientation]
self.game.lib.drawcenter(self.game,self.game.sprite_lib["lemmings/shadow.png"],self.rect.center[0]-self.game.globals["camerax"],self.rect.center[1]-self.game.globals["cameray"])
self.game.lib.drawcenter(self.game,sprites[int(self.spriteindex)%len(sprites)],self.rect.center[0]-self.game.globals["camerax"],self.rect.center[1]-self.game.globals["cameray"])
if self.selected:
self.game.lib.drawcenter(self.game,self.game.sprite_lib["lemmings/selected.png"],self.rect.center[0]-self.game.globals["camerax"],self.rect.center[1]-self.game.globals["cameray"])
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[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]))
s = self.game.pygame.Surface(self.holdrect.size)
s.fill([0,255,0])
s.set_alpha(30)
self.game.window.blit(s,(self.holdrect[0]-self.game.globals["camerax"],self.holdrect[1]-self.game.globals["cameray"]))