PinmikPanik/gamedata/objects/ingame/lemmings.py

199 lines
8.3 KiB
Python
Raw Normal View History

2021-09-10 21:33:24 +02:00
from gamedata.objects.base import BaseObject
import random,math
class Lemming(BaseObject):
2021-09-18 18:03:04 +02:00
def __init__(self,x,y,game,speedmargin=5,skin="base"):
2021-09-15 20:34:48 +02:00
super().__init__(x,y,game,w=70,h=70)
2021-09-10 21:33:24 +02:00
self.direction = random.randint(0,360)
2021-09-12 21:38:55 +02:00
self.holdrect = self.rect.copy()
2021-09-15 20:34:48 +02:00
self.holdradius = 80
2021-09-12 21:38:55 +02:00
self.holdrect = self.holdrect.inflate(self.holdradius,self.holdradius)
2021-09-15 21:09:15 +02:00
self.basespeed = max(20,40+random.randint(-speedmargin,speedmargin)) # Speed that he normally walks by
2021-09-13 07:41:32 +02:00
self.holdtimer = game.lib.Timer(2.5) # Max seconds of holding
2021-09-10 21:33:24 +02:00
self.normalspeed = self.basespeed # Speed "objective"
2021-09-11 15:21:31 +02:00
self.speed = 0 # Current speed, leaning towards objective speed
2021-09-10 21:33:24 +02:00
2021-09-19 20:24:49 +02:00
self.cooldown = False
self.cooldowntimer = game.lib.Timer(0.5)
2021-09-19 12:20:43 +02:00
self.handlepause = True
self.scoreratio = 0.3
2021-09-18 20:44:22 +02:00
if skin=="gold":
self.basespeed*=1.2
self.scoreratio*=3
2021-09-19 12:20:43 +02:00
if skin=="platinum":
2021-09-19 15:07:59 +02:00
self.basespeed*=1.4
2021-09-19 12:20:43 +02:00
self.scoreratio*=5
2021-09-19 15:07:59 +02:00
if skin=="monster":
2021-09-19 15:49:34 +02:00
self.scoreratio*=6
2021-09-18 20:44:22 +02:00
2021-09-18 18:03:04 +02:00
self.skin = skin
2021-09-19 15:07:59 +02:00
self.maxhealth = 2 # Pinmiks can be attacked by the monsters
self.health = self.maxhealth
2021-09-19 15:49:34 +02:00
self.regen = 1
2021-09-19 15:07:59 +02:00
2021-09-10 21:33:24 +02:00
self.selected = False # If beeing redirected
self.anglemargin = 25
2021-09-10 21:33:24 +02:00
# Used for movement
self.restx = 0
self.resty = 0
2021-09-15 19:22:38 +02:00
self.dusttimer = game.lib.Timer(0.7)
2021-09-11 13:29:02 +02:00
# Sprites
self.orientations = ["Right","Left","Left","Right"]
self.sprites = {}
for i in self.orientations:
2021-09-18 18:03:04 +02:00
self.sprites[i] = game.getSpriteDir("lemmings/"+skin+"/"+i+"/")
2021-09-11 13:29:02 +02:00
self.spriteindex = 0
self.animspeed = 0.2
2021-09-13 11:14:58 +02:00
self.tiles = game.gameloop.findname("Tiles")[0]
self.manager = game.gameloop.findname("Manager")[0]
2021-09-10 21:33:24 +02:00
def step(self):
2021-09-11 19:13:14 +02:00
# Depth updating based on position
self.depth = 1+self.rect[1]/100
2021-09-19 20:24:49 +02:00
# Cooldown to be relaunched
if self.cooldown:
if self.cooldowntimer.tick(self.game.dt):
self.cooldown = False
2021-09-10 21:33:24 +02:00
# 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)
2021-09-19 15:07:59 +02:00
self.health = min(self.maxhealth,self.health+self.regen*self.game.dt) # Regen live
# Attacking other pinmiks
if self.skin=="monster":
for pinmik in self.game.gameloop.findname("Lemming"):
if pinmik.skin!="monster":
if self.rect.collidepoint(pinmik.rect.center):
2021-09-19 15:49:34 +02:00
pinmik.health-=self.game.dt*3.5
2021-09-19 15:07:59 +02:00
if self.holdrect.collidepoint(pinmik.rect.center):
2021-09-19 15:49:34 +02:00
pinmik.health-=self.game.dt*2
# Lean towards middle
angle_to_mid = math.degrees(math.atan2(self.game.DISPLAY_HEIGHT/2-self.rect.center[1],self.game.DISPLAY_WIDTH/2-self.rect.center[0]))
if abs(angle_to_mid-self.direction-360)<abs(angle_to_mid-self.direction):
angle_to_mid-=360
self.direction += (angle_to_mid-self.direction)*self.game.dt*0.5
2021-09-19 15:07:59 +02:00
2021-09-15 19:22:38 +02:00
# 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])
2021-09-10 21:33:24 +02:00
# Mouse selection
mouse=self.game.inputs["mouse"]
if self.selected:
# Releasing it
2021-09-12 21:38:55 +02:00
if mouse["click"]==0 or not self.holdrect.collidepoint(mouse["campos"]):
2021-09-10 21:33:24 +02:00
self.launch()
2021-09-19 20:24:49 +02:00
self.cooldown = True
2021-09-13 07:41:32 +02:00
else:
if self.holdtimer.tick(self.game.dt):
self.selected = False
self.holdtimer.reset()
self.normalspeed = self.basespeed
2021-09-19 20:24:49 +02:00
if self.game.inputs["mouse"]["click"]==1 and not self.cooldown:
2021-09-12 21:38:55 +02:00
if self.rect.collidepoint(mouse["campos"]):
2021-09-10 21:33:24 +02:00
self.selected = True
2021-09-11 13:29:02 +02:00
# Animation
self.spriteindex+=self.game.dt*self.animspeed*self.speed
2021-09-13 11:14:58 +02:00
# 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
2021-09-19 15:07:59 +02:00
if dead or self.health<0:
if self.skin!="monster": # Monsters die without removing lives
self.manager.death()
2021-09-15 21:14:53 +02:00
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
2021-09-13 11:14:58 +02:00
self.game.gameloop.delid(self.id)
2021-09-15 19:22:38 +02:00
# 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)
2021-09-18 11:07:14 +02:00
# 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)
2021-09-10 21:33:24 +02:00
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)
2021-09-10 21:33:24 +02:00
2021-09-16 15:43:59 +02:00
self.speed = self.basespeed*4
self.cachedrel = []
2021-09-10 21:33:24 +02:00
self.selected = False
2021-09-15 20:40:29 +02:00
self.holdtimer.reset()
2021-09-10 21:33:24 +02:00
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)
2021-09-12 21:38:55 +02:00
self.holdrect[0]+=int(velx)
self.holdrect[1]+=int(vely)
2021-09-11 13:29:02 +02:00
def draw(self):
2021-09-19 15:49:34 +02:00
if self.skin=="monster":
# Red aura
self.game.lib.drawcenter(self.game,self.game.sprite_lib["lemmings/monster/aura.png"],self.rect.center[0],self.rect.center[1])
2021-09-19 15:07:59 +02:00
# Shaking based on life
offx,offy = 0,0
if self.health<self.maxhealth:
shake = (1-(self.health/self.maxhealth))*4
offx = random.random()*shake*2-shake
offy = random.random()*shake*2-shake
2021-09-11 13:29:02 +02:00
orientation = self.orientations[int(self.direction%360/361*4)]
sprites = self.sprites[orientation]
2021-09-19 15:07:59 +02:00
self.game.lib.drawcenter(self.game,self.game.sprite_lib["lemmings/shadow.png"],self.rect.center[0]-self.game.globals["camerax"]+offx,self.rect.center[1]-self.game.globals["cameray"]+offy)
self.game.lib.drawcenter(self.game,sprites[int(self.spriteindex)%len(sprites)],self.rect.center[0]-self.game.globals["camerax"]+offx,self.rect.center[1]-self.game.globals["cameray"]+offy)
2021-09-15 20:34:48 +02:00
if self.selected:
2021-09-19 15:07:59 +02:00
self.game.lib.drawcenter(self.game,self.game.sprite_lib["lemmings/selected.png"],self.rect.center[0]-self.game.globals["camerax"]+offx,self.rect.center[1]-self.game.globals["cameray"]+offy)
2021-09-12 21:38:55 +02:00
if self.game.globals["debug"]:
s = self.game.pygame.Surface(self.rect.size)
s.fill([255,0,0])
s.set_alpha(30)
2021-09-14 17:01:58 +02:00
self.game.window.blit(s,(self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]))
2021-09-12 21:38:55 +02:00
s = self.game.pygame.Surface(self.holdrect.size)
s.fill([0,255,0])
s.set_alpha(30)
2021-09-14 17:01:58 +02:00
self.game.window.blit(s,(self.holdrect[0]-self.game.globals["camerax"],self.holdrect[1]-self.game.globals["cameray"]))