import siege
from siege.core import *
from siege.graphics import *
from siege.input import *
from siege.physics import *

from siege.core.entity import *
from siege.graphics.font import *
from siege.graphics.sprite import *
from siege.physics.space import *
from siege.physics.body import *
from siege.physics.shape import *

class Box(Entity):
    def __init__(self, sprite, x, y, density, static = False):
        Entity.__init__(self, 0.0)
        
        self.physicsBody = Body(None, 0x4 if static else 0x2)
        self.sprite = sprite
        self.pos = x, y
        
        w, h = sprite.size
        
        verts = [-w / 2.0, -h / 2.0
                ,-w / 2.0,  h / 2.0
                , w / 2.0,  h / 2.0
                , w / 2.0, -h / 2.0]
        
        self.shape = PolyShape(self.physicsBody, 0.0, 0.0, verts)
        self.shape.friction = 0.75
        self.shape.restitution = 0.25
        
        self.physicsBody.mass = self.shape.getMass(density)
        self.physicsBody.moment = self.shape.getMomentDensity(density)

class WBox(Box):
    def __init__(self, x, y):
        Box.__init__(self, wbox, x, y, 1.0, False)
class MBox(Box):
    def __init__(self, x, y):
        Box.__init__(self, mbox, x, y, 2.5, False)
class Floor(Box):
    def __init__(self, sprite, x, y):
        Box.__init__(self, sprite, x, y, float('inf'), True)

class Circle(Entity):
    def __init__(self, size, x, y, density, static = False):
        Entity.__init__(self, 0.0)
        
        self.physicsBody = Body(None, 0x4 if static else 0x2)
        self.pos = x, y
        
        self.shape = CircleShape(self.physicsBody, 0.0, 0.0, 0.0, size)
        self.shape.friction = 0.75
        self.shape.restitution = 0.25
        
        self.physicsBody.mass = self.shape.getMass(density)
        self.physicsBody.moment = self.shape.getMomentDensity(density)
    def evDraw(self):
        if self.sprite is not None:
            Entity.evDraw(self)
        else:
            self.shape.drawDBG()
            draw.colorf(1.0, 1.0, 1.0, 1.0)

class Barrel(Circle):
    def __init__(self, x, y):
        Circle.__init__(self, 16, x, y, 1.5, False)

class Monitor(Entity):
    def __init__(self):
        Entity.__init__(self, 0.0)
    
    def evMouseButtonLeftPress(self):
        mx, my = mouse.getPos()
        items.append(WBox(mx, my))
    
    def evMouseButtonRightPress(self):
        mx, my = mouse.getPos()
        items.append(MBox(mx, my))
    
    def evMouseButtonMiddlePress(self):
        mx, my = mouse.getPos()
        items.append(Barrel(mx, my))

core.loadModules('SDL', 'OpenGL', 'DevIL', 'Freetype', 'Chipmunk')
core.init(640, 480, 32, 0)

Space.getDefault().damping = 0.75

pacman = Sprite('data/sprites/Pacman.png')
wbox = Sprite('data/sprites/CrateSmall.png')
mbox = Sprite('data/sprites/StrongboxSmall.png')

mplate = Sprite('data/sprites/FloorMetalPlate.png')
hwall = Sprite('data/sprites/HazardWall.png')
sbar = Sprite('data/sprites/SupportBar.png')

font = Font('data/fonts/DejaVuSans.ttf', 12)

items = []

monitor = Monitor()

for i in xrange(32, 640, 64):
    items.append(Floor(mplate, i, 448))
for i in xrange(224, 448, 64):
    items.append(Floor(sbar, i, 384))
    items.append(Floor(hwall, i, 320))

while core.loop()[0]:    
    window.swapBuffers()
    draw.clear()
    
    draw.colorf(1.0, 0.0, 0.0, 1.0)
    draw.rectangle(320-32, 240-32, 320+32, 240+32, False)
    
    draw.colorf(1.0, 1.0, 1.0, 1.0)
    draw.circle(320, 240, 32, False)
    
    pacman.draw(320, 240)
    
    font.printCentered(320, 240-48, 'Spam and cheese!')
    
    #for box in items:
    #    box.shape.drawDBG()
    draw.colorf(1.0, 1.0, 1.0, 1.0)
