import pygame
import globals


class Anim:
	def __init__(self, position, dir, prefix, num_frames):
		self.position = position
		self.dir = dir
		self.prefix = prefix

		self.frames = []
		for i in range(0, num_frames):
			self.frames.append(pygame.image.load("%s%s%03d.png" % (self.dir, self.prefix, i)))

		self.current_frame = 0

	def play(self):
		globals.screen.blit(self.frames[self.current_frame], self.position)

		if self.current_frame != len(self.frames) - 1:
			self.current_frame = self.current_frame + 1
		else:
			self.current_frame = 0

# This is purely an interpolator class 
# - updates a counter that can loop or halt

class Interp:
    
    def __init__(self, mc, ms, loop):
        self.maxcount = mc
        self.count = 0
        self.ms = ms
        self.current = 0
        self.stopped = 1
        self.loop = loop
    
    def Stop(self):
        self.stopped = 1
    
    def Start(self):
        self.stopped = 0
        self.current = 0
    
    def Update(self, ms):
        if(self.stopped == 1):
            return
        
        if(self.current + ms > self.ms):
            self.current -= self.ms
            self.count += 1
            if((self.count > self.maxcount) and (self.loop == 1)):
                self.count = 0
            if((self.count > self.maxcount) and (self.loop == 0)):
                self.count = self.maxcount
        else:        
            self.current += ms
        
    def Reset(self):
        self.current = 0
        self.count = 0
    