71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
|
from gamedata.objects.base import BaseObject
|
||
|
import random,math
|
||
|
|
||
|
class Lemming(BaseObject):
|
||
|
def __init__(self,x,y,game):
|
||
|
super().__init__(x,y,game)
|
||
|
self.direction = random.randint(0,360)
|
||
|
self.radius = 30 # Radius in which the mouse can click
|
||
|
self.basespeed = 80 # Speed that he normally walks by
|
||
|
self.normalspeed = self.basespeed # Speed "objective"
|
||
|
self.speed = self.normalspeed # Current speed, leaning towards objective speed
|
||
|
|
||
|
self.selected = False # If beeing redirected
|
||
|
|
||
|
self.cachedrel = [] # Storing relative movement of mouse
|
||
|
self.cachedrelsize = 10
|
||
|
self.mincachedsize = 5
|
||
|
|
||
|
# Used for movement
|
||
|
self.restx = 0
|
||
|
self.resty = 0
|
||
|
|
||
|
def step(self):
|
||
|
|
||
|
# 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)
|
||
|
|
||
|
# Mouse selection
|
||
|
mouse=self.game.inputs["mouse"]
|
||
|
distance = math.sqrt((mouse["pos"][0]-self.rect.center[0])**2+(mouse["pos"][1]-self.rect.center[1])**2)
|
||
|
if self.selected:
|
||
|
# Caching mouse relative movement
|
||
|
self.cachedrel.append(mouse["rel"])
|
||
|
if len(self.cachedrel)>self.cachedrelsize:
|
||
|
self.cachedrel.pop(0)
|
||
|
# Releasing it
|
||
|
if mouse["click"]==0 or distance>4*self.radius:
|
||
|
self.launch()
|
||
|
if self.game.inputs["mouse"]["click"]==1:
|
||
|
if distance<=self.radius:
|
||
|
self.selected = True
|
||
|
|
||
|
def launch(self):
|
||
|
# Launch itself in the mouse direction
|
||
|
if len(self.cachedrel)>self.mincachedsize:
|
||
|
averagerel = [sum([x[i] for x in self.cachedrel])/len(self.cachedrel) for i in range(2)]
|
||
|
self.direction = math.degrees(math.atan2(averagerel[1],averagerel[0]))
|
||
|
|
||
|
self.speed = self.basespeed*2
|
||
|
self.cachedrel = []
|
||
|
self.selected = False
|
||
|
|
||
|
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)
|