
dofile("testmap.lua")

--[[for i = 1, 50 do
    quad.new("test.png", 15):scale(0.2, 0.2):position(math.random(-100, 900), math.random(-100, 700)):alpha(0.2)
end]]

--[[
local e = entity.new()
--e:setCircleCollider(150)
e:texture("test3.png"):position(400, 300)
--e:setAABBCollider()
e:setLineCollider(0, 0, 100, 200)
e:rotate(90, 5, -1, true, true)

function e:update(dt)
    --local x, y = self:getPosition()
    --self:position(x + math.random(-2, 2), y + math.random(-2, 2))
end

local q = quad.new("debug/pixels.png", 28):scale(4, 4)
local q2 = quad.new("debug/pixels.png", 28):scale(4, 4):rotate(360, 0.6, -1)

local w = entity.new()
w:setCircleCollider(30)
--w:setAABBCollider(-20, -20, 20, 20)
--w:setLineCollider(40, -50, -30, 20)
--w:rotate(360, 5, -1)

local vq = quad.new("debug/vector.png", 28):alpha(0)

function w:update(dt)
    self:position(getMouseWorldPos())
    
    local c, x, y = self:collideWith(e)
    if c then
        q:position(x, y)
        q:alpha(1)
        --camera.shake(30, 0.5)
    else
        q:alpha(0)
    end
    
    local c, x, y = self:collideGrid()
    if c then
        q2:position(x, y)
        q2:alpha(1)
        --camera.shake(10, 0.5)
    else
        q2:alpha(0)
    end
    
    if isMouseButton(1) then
        local mx, my = getMouseWorldPos()
        local wx, wy = getWallNormal(mx, my, 30, 5)
        if wx == 0 and wy == 0 then
            vq:alpha(0)
        else
            vq:alpha(1)
            vq:position(mx, my)
            vq:rotate(vector.angle(wx, wy))
        end
    end
end
]]
local ball = entity.new():texture("debug/boulder.png"):setCircleCollider(12)
ball.fly = false
local ballv = quad.new("debug/vector.png", 29)

function ball:update(dt)
    
    local vx, vy = self:getVelocity()
    self:rotate(self:getRotation() + dt*vx*2)
    
    local va = vector.angle(vx, vy)
    local s = vector.len(vx, vy) / 300
    ballv:rotate(va)
    ballv:scale(s, s)
    ballv:position(self:getPosition())
    
    if isMouseButton(3) then
        self:position(getMouseWorldPos())
        self:velocity(0, 0)
        --self:gravity(0, 400)
        self.fly = true
        self.cx = 0
        self.cy = 0
        return
    end
    
    if self.fly then
    
        --local vx, vy = self:getVelocity()
        --vx = vx + 100 * dt
        
        if vy < 5 and self:checkWall(0, 10) then
            self:velocity(vx, 0)
        else
            vy = vy + 400 * dt
            self:velocity(vx, vy)
        end
        
        --local px, py = self:getPosition()   
    
        local c, x, y = self:collideGrid()
        if c then
            self:position(self.cx, self.cy)
            --quad.new("debug/pixels.png", 28):scale(4, 4):position(x, y):alpha(0, 1):delete(1)
            print("ERROR")
            --self:unstuck()
        else
            self.cx, self.cy = self:getPosition()
        end
        
        self:updatePhysics(dt)
        
        --c, x, y = self:collideGrid()
        if c then
            self:position(self.cx, self.cy)
            --self:unstuck()
            --quad.new(self:getTexture(), 29):position(self:getPosition()):alpha(0.8):alpha(0, 1):delete(1)

            local nx, ny = getWallNormal(x, y)
            quad.new("debug/vector.png", 28):scale(0.3, 0.3):position(x, y):alpha(0, 1):rotate(vector.angle(nx, ny)):delete(1)
            local vx, vy = self:getVelocity()
            vx, vy = vector.reflection(vx, vy, nx, ny)
            vx = vx * 0.99
            vy = vy * 0.75
            --if vector.len(vx, vy) < 20 then vx, vy = vector.setLen(vx, vy, 20) end
            --print(dt, vx, vy)
            self:velocity(vx, 0)
            self:updatePhysics(dt)
            self:velocity(0, vy)
            self:updatePhysics(dt)
            self:velocity(vx, vy)
            --self.fly = false
        else
            --self.cx, self.cy = self:getPosition()
        end
        
        
    end
        
end



--local s = sound.new("klaxonloop.ogg")
--local s = sound.new("alarm.ogg")
--s:play()
--sound.new("klaxonloop.ogg"):play(2)

--music.play("green.spc")


--f:createText("Hello world!", 30, 30, empty, 2)


--local ff = quad.new("insanity.png", 10):blend(BLEND_ADD)

--q:addChild(ff)




--[[local p = player.new()
p:position(400, 300)
p:texture("sprites/olaf1.anim")
camera.follow(p)]]

--local b = button.new(300, 150, "Blarg"):position(400, 400)