ia = {}

require "ia.*"

local gota_wait = 1

templates = {
    ALIVE = 0,
    DYING = 1,
    DEAD = 2,
    RESURRECTING = 3,
} local me = templates

function me.create(type, options)
    local sprite
    local key, value = type:match("^(%S+)%s*(.*)$")

    if key == "mummy" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=16 },
            bbo = { left=3, top=2, right=3, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/mummy.gif"),
            animation = "mummy_walk",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_mummy
        }
    elseif key == "bullet" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=4,h=3 },
            bbo = { left=0, top=0, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/morcus.gif"),
            animation = "bullet",
            ia = ia.update_bullet
        }
    elseif key == "bullet_particle" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            dir = options.dir,
            size = { w=8,h=8 },
            bbo = { left=2, top=3, right=3, bottom=1 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/morcus.gif"),
            animation = "particle",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_gota_esguit
        }
    elseif key == "coin" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=8,h=8 },
            bbo = { left=0, top=0, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/misc.gif"),
            animation = "coin",
            state = me.ALIVE,
            timer = 0,
            light = 15,
            light_ox = 4,
            light_oy = 4,
            ia = ia.update_coin
        }
    elseif key == "brick" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=8,h=8 },
            bbo = { left=0, top=0, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/misc.gif"),
            animation = "brick",
            state = me.ALIVE,
            timeout = 10,
            ia = ia.update_brick
        }
    elseif key == "torxa" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=8,h=16 },
            bbo = { left=0, top=0, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/torxa.gif"),
            animation = "torxa",
            state = me.ALIVE,
            light = 30,
            light_ox = 4,
            light_oy = 4,
            no_shadow = true,
            ia = ia.update_torxa
        }
    elseif key == "clau" then
        sprite = {
            type = key,
            color = value,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=8 },
            bbo = { left=0, top=0, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/"..value..".gif"),
            animation = "clau",
            state = me.ALIVE,
            light = 15,
            light_ox = 7,
            light_oy = 4,
            ia = ia.update_clau
        }
    elseif key == "porta" then
        sprite = {
            type = key,
            color = value,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=8,h=16 },
            bbo = { left=0, top=0, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/"..value..".gif"),
            animation = "porta",
            state = me.ALIVE,
            ia = ia.update_porta
        }
    elseif key == "sucubo" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=16 },
            bbo = { left=3, top=2, right=3, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/sucubo.gif"),
            animation = "sucubo_stand",
            state = me.ALIVE,
            timer = tonumber(value),
            enemy = true,
            room = options.room,
            ia = ia.update_sucubo
        }
    elseif key == "palo" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=6 },
            bbo = { left=0, top=0, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/misc.gif"),
            animation = "palo",
            dy = -4,
            ay = 1,
            ia = ia.update_palo
        }
    elseif key == "nemesio" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=16 },
            bbo = { left=3, top=2, right=3, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/nemesio.gif"),
            animation = "nemesio",
            state = me.ALIVE,
            substate = nemesio.STATIC,
            timer = 0,
            enemy = true,
            room = options.room,
            ia = ia.update_nemesio
        }
    elseif key == "rata" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=9,h=8 },
            bbo = { left=0, top=5, right=0, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/rata.gif"),
            animation = "rata",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_rata
        }
    elseif key == "gota" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=8,h=8 },
            bbo = { left=2, top=3, right=3, bottom=1 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/gota.gif"),
            animation = "gota",
            state = me.ALIVE,
            enemy = true,
            timer = gota_wait,
            room = options.room,
            ia = ia.update_gota
        }
        gota_wait = gota_wait + 10
        if gota_wait > 40 then gota_wait = 1 end
    elseif key == "gota_caiguent" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=8,h=8 },
            bbo = { left=2, top=3, right=3, bottom=1 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/gota.gif"),
            animation = "gota",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_gota_caiguent
        }
    elseif key == "gota_esguit" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            dir = options.dir,
            size = { w=8,h=8 },
            bbo = { left=2, top=3, right=3, bottom=1 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/gota.gif"),
            animation = "esguit",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_gota_esguit
        }
    elseif key == "berserk" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=16 },
            bbo = { left=3, top=2, right=3, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/berserk.gif"),
            animation = "mummy_walk",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_berserk
        }
    elseif key == "abad" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=18 },
            bbo = { left=3, top=2, right=3, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/abad.gif"),
            animation = "abad",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_coin
        }
    elseif key == "dimoni" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=16,h=17 },
            bbo = { left=3, top=2, right=3, bottom=0 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/dimoni.gif"),
            animation = "dimoni_walk",
            state = me.ALIVE,
            enemy = true,
            bulletproof = true,
            room = options.room,
            ia = ia.update_dimoni
        }
    elseif key == "ratpenat" then
        sprite = {
            type = key,
            pos = options.pos,--{ x=100, y=4*12*8+71 },
            size = { w=8,h=8 },
            bbo = { left=1, top=1, right=1, bottom=1 },
            current_frame = 1,
            current_wait = 1,
            flipped = options.flipped,
            surf = surf.load("gfx/ratpenat.gif"),
            animation = "ratpenat",
            state = me.ALIVE,
            enemy = true,
            room = options.room,
            ia = ia.update_ratpenat
        }
    else
        error("Template not recognized")
    end
    --sprite.room = rooms.current()
    print("creat sprite de tipus "..type)
    return sprite
end
