function init(self)
	go.set_position(vmath.vector3(950,700,0.2))
	self.velocity = vmath.vector3(0, 0, 0)
	self.gravity = -5000 -- gravity force
	self.grounded = false
end

function final(self)
	-- Add finalization code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function update(self, dt)
	local pos = go.get_position()
	pos = pos + self.velocity * dt
	
	local centre = vmath.vector3(pos.x, pos.y, 0)
	local platform = pos +  vmath.vector3(0, -45, 0)
	
--	msg.post("@render:", "draw_line", { start_point = centre, end_point = platform, color = vmath.vector4(1,0,0,1) })
	
	local platform_result = physics.raycast(centre, platform, { hash("platform") })
	if platform_result then
		local point = platform_result.position - pos
		--msg.post("@render:", "draw_debug_text", { text = "Platform", position = vmath.vector3(20, 400, 0), color = vmath.vector4(1, 0, 0, 1) } )
		--msg.post("@render:", "draw_debug_text", { text = "X : " .. tostring(point.x), position = vmath.vector3(20, 380, 0), color = vmath.vector4(1, 0, 0, 1) } )
		--msg.post("@render:", "draw_debug_text", { text = "Y : " .. tostring(point.y), position = vmath.vector3(20, 360, 0), color = vmath.vector4(1, 0, 0, 1) } )
		self.grounded = true
		self.velocity.y = 0
		if point.y > -45 then
			pos.y = pos.y + (point.y + 45)
		end
	else
		self.grounded = false
	end
	if not self.grounded then
		self.velocity.y = self.velocity.y + self.gravity * dt
	end
	go.set_position(pos)
end	

function fixed_update(self, dt)
	-- This function is called if 'Fixed Update Frequency' is enabled in the Engine section of game.project
	-- Can be coupled with fixed updates of the physics simulation if 'Use Fixed Timestep' is enabled in
	-- Physics section of game.project
	-- Add update code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function on_message(self, message_id, message, sender)
	if message_id == hash("explode") then
		sprite.play_flipbook("#still", hash("explosion"))
		--msg.post("mummy", "disable")
	end
end

function on_input(self, action_id, action)
	-- Add input-handling code here. The game object this script is attached to
	-- must have acquired input focus:
	--
	--    msg.post(".", "acquire_input_focus")
	--
	-- All mapped input bindings will be received. Mouse and touch input will
	-- be received regardless of where on the screen it happened.
	-- Learn more: https://defold.com/manuals/input/
	-- Remove this function if not needed
end

function on_reload(self)
	-- Add reload-handling code here
	-- Learn more: https://defold.com/manuals/hot-reload/
	-- Remove this function if not needed
end
