
import util

from constants import *


class Thing:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.s_x = 0
        self.s_y = 0
        self.scale = (1,1)
        self.width = TEXSIZE
        self.height = TEXSIZE
        self.edge = 0 
        
        self.midX = self.x + self.width / 2.0
        self.midY = self.y + self.height / 2.0
        
        # let things spin
        self.rotation = 0
        self.spinSpeed = 0
	self.rotationCenter = None # by default, spin around the middle of the thing

        
    def set(self, x, y):
        self.x = x
        self.y = y
        
    def setv(self, tup):
        self.set(tup[0], tup[1])
    
    """def setTile(self, x, y):
        self.x = x*TEXSIZE
        self.y = y*TEXSIZE

    def setTilev(self, tup):
        self.setTile(tup[0], tup[1])"""
        
    def setSpeed(self, x = None, y = None):
        if x != None:
            self.s_x = x
        if y != None:
            self.s_y = y
        
    def setSpeedx(self, tup):
        self.setSpeed(tup[0], tup[1])
        
    
    def get(self):
        return [self.x, self.y]
         
    def getCenter(self):
        return [self.x + self.scale[0]/2.0 * TEXSIZE, self.y + self.scale[1]/2.0 * TEXSIZE]
    
    def update(self, delta):

        #Stop at edges
        if(self.s_x > 0):
          if(self.x >= SWIDTH - self.width):
            self.x = SWIDTH - self.width
            self.edge = 1
          else:
             self.x = self.x + self.s_x*delta
        elif(self.s_x < 0):
          if(self.x <= 0):
            self.x = 0
            self.edge = 1
          else:
             self.x = self.x + self.s_x*delta

        if(self.s_y > 0):
          if(self.y >= SHEIGHT - self.height):
            self.y = SHEIGHT - self.height
            self.edge = 1
          else:
             self.y = self.y + self.s_y*delta
        elif(self.s_y < 0):
          if(self.y <= 0):
            self.y = 0
            self.edge = 1
          else:
             self.y = self.y + self.s_y*delta

        # rotate
        if self.rotation > 360:
            self.rotation -= 360
        
        self.rotation += self.spinSpeed*delta
    